106 lines
2.9 KiB
GDScript3
106 lines
2.9 KiB
GDScript3
|
extends KinematicBody
|
||
|
|
||
|
var dir = Vector3()
|
||
|
const GRAVITY = -24.8
|
||
|
var vel = Vector3()
|
||
|
const MAX_SPEED = 20
|
||
|
const ACCEL= 4.5
|
||
|
|
||
|
const DEACCEL= 16
|
||
|
const MAX_SLOPE_ANGLE = 40
|
||
|
|
||
|
var camera_rotation
|
||
|
var camera
|
||
|
|
||
|
var MOUSE_SENSITIVITY = 0.05
|
||
|
|
||
|
|
||
|
# class member variables go here, for example:
|
||
|
# var a = 2
|
||
|
# var b = "textvar"
|
||
|
|
||
|
func _ready():
|
||
|
camera_rotation = $Camera_rotation_helper
|
||
|
camera = $Camera_rotation_helper/Camera
|
||
|
|
||
|
func _process(delta):
|
||
|
process_input(delta)
|
||
|
process_movement(delta)
|
||
|
|
||
|
|
||
|
func process_input(delta):
|
||
|
# ----------------------------------
|
||
|
# Walking
|
||
|
dir = Vector3()
|
||
|
var cam_xform = camera.get_global_transform()
|
||
|
|
||
|
var input_movement_vector = Vector2()
|
||
|
var cam_scroll = 0.0
|
||
|
|
||
|
if Input.is_action_pressed("move_up"):
|
||
|
input_movement_vector.y += 1
|
||
|
if Input.is_action_pressed("move_down"):
|
||
|
input_movement_vector.y -= 1
|
||
|
if Input.is_action_pressed("move_left"):
|
||
|
input_movement_vector.x -= 1
|
||
|
if Input.is_action_pressed("move_right"):
|
||
|
input_movement_vector.x = 1
|
||
|
|
||
|
input_movement_vector = input_movement_vector.normalized()
|
||
|
|
||
|
dir += -cam_xform.basis.z.normalized() * input_movement_vector.y
|
||
|
dir += cam_xform.basis.x.normalized() * input_movement_vector.x
|
||
|
|
||
|
|
||
|
|
||
|
func process_movement(delta):
|
||
|
dir.y = 0
|
||
|
dir = dir.normalized()
|
||
|
|
||
|
vel.y += delta*GRAVITY
|
||
|
|
||
|
var hvel = vel
|
||
|
hvel.y = 0
|
||
|
|
||
|
var target = dir
|
||
|
target *= MAX_SPEED
|
||
|
|
||
|
var accel
|
||
|
if dir.dot(hvel) > 0:
|
||
|
accel = ACCEL
|
||
|
else:
|
||
|
accel = DEACCEL
|
||
|
|
||
|
hvel = hvel.linear_interpolate(target, accel*delta)
|
||
|
vel.x = hvel.x
|
||
|
vel.z = hvel.z
|
||
|
vel = move_and_slide(vel,Vector3(0,1,0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
|
||
|
|
||
|
func _input(event):
|
||
|
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||
|
camera_rotation.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
|
||
|
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
|
||
|
|
||
|
var camera_rot = camera_rotation.rotation_degrees
|
||
|
camera_rot.x = clamp(camera_rot.x, -30, 30)
|
||
|
camera_rotation.rotation_degrees = camera_rot
|
||
|
|
||
|
|
||
|
if event is InputEventMouseButton and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||
|
|
||
|
# to prevent the cam sliding effect when clamp limit reached.
|
||
|
var old_x_translation = camera.translation.x
|
||
|
|
||
|
var cam_scroll = Vector3( 0.0, 0.0, 0.0 )
|
||
|
if event.button_index == BUTTON_WHEEL_UP:
|
||
|
cam_scroll.z = -1.0 * MOUSE_SENSITIVITY
|
||
|
if event.button_index == BUTTON_WHEEL_DOWN:
|
||
|
cam_scroll.z = 1.0 * MOUSE_SENSITIVITY
|
||
|
|
||
|
camera.translate( cam_scroll )
|
||
|
|
||
|
camera.translation.x = old_x_translation
|
||
|
camera.translation.z = clamp(camera.translation.z, -5, 0)
|
||
|
|
||
|
if event.is_action_pressed( "game_flashlight" ) and not event.is_echo():
|
||
|
camera.get_node( "Flashlight" ).visible = not camera.get_node( "Flashlight" ).visible
|