140 lines
3.6 KiB
GDScript3
Raw Normal View History

2019-11-23 13:01:03 +01:00
extends KinematicBody
# export variables
export(NodePath) var body_nodepath
export(NodePath) var camera_nodepath
2019-11-23 13:01:03 +01:00
export(NodePath) var animation_nodepath
export(NodePath) var ui_nodepath
export(NodePath) var interact_area_nodepath
2019-11-23 13:01:03 +01:00
# 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
2019-11-23 19:35:04 +01:00
const SPRINT_DEC = 0.015;
const SPRINT_ACC = 0.0015;
2019-11-23 13:01:03 +01:00
# private members
var _body: Spatial
var _camera: Camera
var _animation: AnimationPlayer
var _interface: Control
2019-11-23 19:35:04 +01:00
var _sprintometer: ProgressBar
var _interact_area: Area
2019-11-23 19:35:04 +01:00
var _dir = Vector3()
var _vel = Vector3()
var _is_sprinting
var _sprintVal: float = 100
2019-11-23 13:01:03 +01:00
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)
2019-11-23 19:35:04 +01:00
_sprintometer = _interface.get_node("ProgressBar")
assert(null != _sprintometer)
_interact_area = get_node(interact_area_nodepath) as Area
assert(null != _interact_area)
_interact_area.connect("area_entered", self, "_on_interact_entered")
# Setup mouse look
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
2019-11-23 13:01:03 +01:00
func _physics_process(delta):
2019-11-23 14:23:09 +01:00
_process_input()
_process_movement(delta)
2019-11-23 19:35:04 +01:00
#Logger.info("sprintVal: " + String(_sprintVal))
2019-11-23 13:01:03 +01:00
2019-11-23 19:35:04 +01:00
func _process(delta):
2019-11-23 14:23:09 +01:00
_process_animations()
2019-11-23 19:35:04 +01:00
if _is_sprinting and _sprintVal > 0:
_sprintVal -= SPRINT_DEC / delta
elif _sprintVal < 100:
_sprintVal += SPRINT_ACC / delta
2019-11-23 13:01:03 +01:00
2019-11-23 14:23:09 +01:00
func _process_input():
2019-11-23 17:07:00 +01:00
#exit
if Input.is_action_pressed("ui_cancel"):
_interface.gameOver()
2019-11-23 13:01:03 +01:00
# 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
2019-11-23 17:07:00 +01:00
2019-11-23 13:01:03 +01:00
# 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
2019-11-23 19:35:04 +01:00
_is_sprinting = Input.is_action_pressed("move_sprint") and _sprintVal > SPRINT_DEC
2019-11-23 13:01:03 +01:00
2019-11-23 14:23:09 +01:00
func _process_movement(delta):
2019-11-23 13:01:03 +01:00
_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))
2019-11-23 14:23:09 +01:00
func _process_animations():
2019-11-23 13:01:03 +01:00
_animation.playback_speed = _vel.length() / MOVE_SPEED
2019-11-23 19:35:04 +01:00
_sprintometer.value = _sprintVal
2019-11-23 13:01:03 +01:00
func _on_interact_entered(area: Area):
if area.is_in_group("Crystal"):
area.get_parent().queue_free() # Assuming crystal area is always parent of crystal root
_interface.increaseScore()
elif area.is_in_group("Dino"):
_interface.gameOver()
2019-11-23 13:01:03 +01:00
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)