126 lines
3.1 KiB
GDScript

extends KinematicBody
# export variables
export(NodePath) var body_nodepath
export(NodePath) var camera_nodepath
export(NodePath) var animation_nodepath
export(NodePath) var ui_nodepath
# const
const GRAVITY = -24.8
const JUMP_SPEED = 8
const MOVE_SPEED = 6
const SPRINT_SPEED = 10
const ACCEL = 15.0
const MAX_SLOPE_ANGLE = 40
const MOUSE_SENSITIVITY = 0.05
const INTERACT_DISTANCE = 4
# private members
var _body: Spatial
var _camera: Camera
var _animation: AnimationPlayer
var _interface: Control
var _dir = Vector3();
var _vel = Vector3();
var _is_sprinting;
func _ready():
# Assign child node variables
_body = get_node(body_nodepath) as Spatial
assert(null != _body)
_camera = get_node(camera_nodepath) as Camera
assert(null != _camera)
_animation = get_node(animation_nodepath) as AnimationPlayer
assert(null != _animation)
_interface = get_node(ui_nodepath) as Control
assert(null != _interface)
# Setup mouse look
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
_process_input()
_process_movement(delta)
_process_collision()
func _process(_delta):
_process_animations()
func _process_input():
#exit
if Input.is_action_pressed("ui_cancel"):
_interface.gameOver()
# 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")
func _process_movement(delta):
_vel.y += delta * GRAVITY
# set movement speed
var target = _dir * (SPRINT_SPEED if _is_sprinting else 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 _process_animations():
_animation.playback_speed = _vel.length() / MOVE_SPEED
func _process_collision():
var collCount = get_slide_count()
if collCount > 0:
#Logger.debug("Collide count: ", collCount)
for i in collCount:
var collision = get_slide_collision(i)
#Logger.debug("Collided with: ", collision.collider.name)
#TODO: replace with groups
if collision.collider.name == "CrystalBody":
#Logger.debug("removing crystal...")
collision.collider.queue_free()
_interface.increaseScore()
func _input(event):
# capture mouse movement
if event is InputEventMouseMotion:
_camera.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
# Prevent player from doing a purzelbaum
_camera.rotation_degrees.x = clamp(_camera.rotation_degrees.x, -70, 70)