2019-12-14 21:05:09 +01:00

202 lines
5.6 KiB
GDScript

extends KinematicBody
# export variables
export(NodePath) var body_nodepath
export(NodePath) var lookingAt_nodepath
export(NodePath) var animationWalk_nodepath
export(NodePath) var animationFadeOut_nodepath
export(NodePath) var ui_interact_nodepath
export(NodePath) var camera_nodepath
export(int) var keycard_lvl
export(Array) var key_chain
# 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 _lookCast: RayCast
var _animationWalk: AnimationPlayer
var _animationFadeOut: AnimationPlayer
var _labelInteract: Label
var _dir = Vector3();
var _vel = Vector3();
var _is_sprinting : bool;
var _prev_look;
# TODO: move to global
var _inventory: Control
func _ready():
# Assign child node variables
_body = get_node(body_nodepath) as Spatial # = $Body
assert(null != _body)
_camera = get_node(camera_nodepath) #as Camera
assert(null != _camera)
_animationWalk = get_node(animationWalk_nodepath) as AnimationPlayer
assert(null != _animationWalk)
_animationFadeOut = get_node(animationFadeOut_nodepath) as AnimationPlayer
assert(null != _animationFadeOut)
_labelInteract = get_node(ui_interact_nodepath) as Label
assert(null != _labelInteract)
# Setup mouse look
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
_lookCast = get_node(lookingAt_nodepath) as RayCast
_lookCast.cast_to = Vector3(0, 0, INTERACT_DISTANCE)
_inventory = get_node("HUD")
# setup collisions with Meldewesen
var area = get_node("InteractArea") as Area
assert(null != area)
area.connect("area_entered", self, "_on_interact_entered")
func _physics_process(delta):
process_input()
process_movement(delta)
process_collision_layers()
func _process(_delta):
check_interact()
process_animations()
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")
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():
_animationWalk.playback_speed = _vel.length() / MOVE_SPEED
func process_collision_layers():
var new_layer: int = 6
if Pills.get_level() >= Pills.LEVELS.VERY_LOW:
# If the masked view is almost invisible, collision with masked objects is disabled
new_layer -= 1 # See collision layer names in editor
collision_layer = new_layer
collision_mask = new_layer
func check_interact():
if _lookCast.is_colliding():
var collider = _lookCast.get_collider()
if null != collider and collider.is_in_group("Touchables"):
#show interact tooltip
_labelInteract.show()
#enable outline of seen object
collider.outline.show()
if _prev_look != null and is_instance_valid(_prev_look):
if _prev_look != collider:
_prev_look.outline.hide()
_prev_look = collider
#do interaction
if Input.is_action_just_pressed("interact"):
collider.do_interact(self)
if collider.is_in_group("Collectibles"):
_inventory.add_item(collider.name)
else:
#stop showing interact tooltip and disable outline
_labelInteract.hide()
if _prev_look != null and is_instance_valid(_prev_look):
_prev_look.outline.hide()
elif _prev_look != null and is_instance_valid(_prev_look):
#disable outline of last seen object
_prev_look.outline.hide()
_prev_look = null
#stop showing interact tooltip
_labelInteract.hide()
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)
func _on_interact_entered (area: Area):
if area.is_in_group("Enemy"):
Logger.info("ermahgerd they touched :-o !")
if Pills.get_level() < Pills.get_max() * 0.5:
_reset_scene() # TODO: call per event -> also rais if Meldewesen is looking at player and pill level reaches treshold!
else:
get_node("AudioStreamPlayer3D").play()
# TODO: check why this does not work as intended:
#var player = AudioStreamPlayer3D.new()
#var audio_file = "res://Resources/Audio/dino-eat.wav"
#player.stream = load("res://Resources/Audio/dino-eat.wav")
#add_child(player)
#Logger.info("should play sound again?")
#player.play()
#remove_child(player)
#player. queue_free()
func _reset_scene ():
# fade to black and restart scene
_animationFadeOut.play("FadeOut")
yield(_animationFadeOut, "animation_finished")
# reset values
_animationFadeOut.seek(0, true)
get_tree().reload_current_scene()
Pills._set_level(Pills.get_max())