extends KinematicBody # export variables export(NodePath) var body_nodepath export(NodePath) var lookingAt_nodepath export(int) var keycard_lvl export(Array) var key_chain # const const GRAVITY = -24.8 const JUMP_SPEED = 8 const MOVE_SPEED = 20 const SPRINT_SPEED = 40 const ACCEL = 4.5 const MAX_SLOPE_ANGLE = 40 const MOUSE_SENSITIVITY = 0.05 const INTERACT_DISTANCE = 4 # private members var _body: Spatial var _camera: Camera var _lookCast: RayCast var _dir = Vector3(); var _vel = Vector3(); var _is_sprinting; func _ready(): _body = get_node(body_nodepath) as Spatial # = $Body assert(null != _body) _camera = $Body/Camera assert(null != _camera) Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) _lookCast = get_node(lookingAt_nodepath) as RayCast _lookCast.cast_to = Vector3(0, 0, INTERACT_DISTANCE) func _physics_process(delta): process_input() process_movement(delta) func process_input(): # Walking var input_movement_vector = Vector2() if Input.is_action_pressed("move_fwrd"): input_movement_vector.y += 1 if Input.is_action_pressed("move_back"): 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 # look around with mouse _dir = Vector3() var camera_transform = _camera.get_global_transform() _dir += -camera_transform.basis.z * input_movement_vector.y _dir += camera_transform.basis.x * input_movement_vector.x # jumping if Input.is_action_just_pressed("move_jump") and is_on_floor(): _vel.y = JUMP_SPEED # sprinting _is_sprinting = Input.is_action_pressed("move_sprint") # Taking a pill # TODO: Should be moved to a different component which only handles pills - this script should only do movement # Only here for testing purposes! if Input.is_action_just_pressed("take_pill"): Pills.take_pill() Logger.info("Player took a pill; new level: %s" % [Pills.get_round_level()]) func process_movement(delta): _vel.y += delta * GRAVITY # set movement speed var target = _dir if _is_sprinting: target *= SPRINT_SPEED else: target *= MOVE_SPEED var hvel = _vel 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 check_interact(): if _lookCast.is_colliding(): var collider = _lookCast.get_collider() if collider.is_in_group("Touchables") : collider.do_interact(self) func _input(event): # capture mouse movement if event is InputEventMouseMotion: _body.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1)) self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1)) var camera_rot = _body.rotation_degrees camera_rot.x = clamp(camera_rot.x, -70, 70) _body.rotation_degrees = camera_rot if Input.is_action_just_pressed("interact"): check_interact() # interact with object player is looking at