extends KinematicBody class_name Player var attackRate : float = 0.3 var lastAttackTime : int = 0 # physics var airborneSpeed : float = 3.0 var walkSpeed : float = 5.0 var runSpeed : float = 10.0 var jumpForce : float = 10.0 var gravity : float = 15.0 var vel := Vector3() # components onready var camera = get_node("CameraOrbit") onready var interactCast = get_node("InteractRayCast") onready var anim = get_node("Casual2_Female/AnimationPlayer") # internal var interactables := [] var pushed_object func _process(delta): # attack input if Controller.player_input_enabled and is_on_floor(): if Input.is_action_just_pressed("interact"): try_interact_pressed() if Input.is_action_just_released("interact"): try_interact_released() for area in $TouchableArea.get_overlapping_areas(): try_touch(area) # called every physics step (60 times a second) func _physics_process (delta): var input := Vector3() # gravity vel.y -= gravity * delta if Controller.player_input_enabled : # movement inputs if Input.is_action_pressed("move_forward"): input.z += 1 if Input.is_action_pressed("move_backward"): input.z -= 1 if Input.is_action_pressed("move_left"): input.x += 1 if Input.is_action_pressed("move_right"): input.x -= 1 input = input.normalized() # get the relative direction var dir := (transform.basis.z * input.z + transform.basis.x * input.x) # apply the direction to our velocity if not is_on_floor() and dir.length_squared() == 0: vel.x = lerp(vel.x, 0, 0.02) vel.z = lerp(vel.z, 0, 0.02) elif Input.is_action_pressed("run"): vel.x = dir.x * runSpeed vel.z = dir.z * runSpeed else: vel.x = dir.x * walkSpeed vel.z = dir.z * walkSpeed # jump input if can_jump(): vel.y = jumpForce else: vel.x = 0 vel.z = 0 if is_on_floor(): if anim.current_animation != "Punch": if input.x != 0 || input.z != 0: if Controller.player_input_enabled and Input.is_action_pressed("run"): anim.play("Run") else: anim.play("Walk") else: anim.play("Idle") else: anim.play("RecieveHit") var previous_vel = vel # move pushed object first if there is one if pushed_object: var object_vel = pushed_object.move_and_slide(vel, Vector3.UP, true, 1, deg2rad(30)) vel.x = object_vel.x vel.z = object_vel.z # move along the current velocity vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30)) "debug for water" if translation.y <= -5.75: translation.y = -5.75 vel.y = 0 if is_on_wall(): vel.y = min(previous_vel.y, vel.y) # called when we collect a coin func give_gold (amount): var gold = Controller.player_infos.gold gold += amount Controller.player_infos.gold = gold # called when an enemy deals damage to us func take_damage (damageToTake): Controller.player_infos.curHp -= damageToTake # if our health is 0, die if Controller.player_infos.curHp <= 0: die() # called when our health reaches 0 func die (): # reload the scene get_tree().reload_current_scene() func try_touch(area): if area.has_method("on_touch"): area.on_touch(self) # called when we press the interact button func try_interact_pressed(): # play the animation anim.stop() anim.play("Idle") for area in $InteractorArea.get_overlapping_areas(): if area.has_method("on_interact_pressed"): interactables.push_back(area) area.on_interact_pressed(self) # called when we release the interact button func try_interact_released(): # play the animation anim.stop() anim.play("Idle") for interactable in interactables: if interactable.has_method("on_interact_released"): interactable.on_interact_released(self) interactables.clear() func start_pushing(body): pushed_object = body func stop_pushing(body): pushed_object = null func can_jump(): return pushed_object == null and Input.is_action_pressed("jump") and is_on_floor()