128 lines
2.9 KiB
GDScript
128 lines
2.9 KiB
GDScript
extends CharacterBody3D
|
|
|
|
|
|
const SPEED:= 300.0
|
|
const JUMP_VELOCITY:= 10.0
|
|
var Executed = false
|
|
|
|
@onready var animator = get_node("gobot_new/AnimationPlayer")
|
|
@onready var hud_container: HBoxContainer = $hud/Hud_container
|
|
|
|
@export var view : Node3D
|
|
@export var health := 3
|
|
@export var foot_damage:= 1
|
|
@onready var immunity_cooldown = 1.5
|
|
@onready var immunity_time = $immunity
|
|
|
|
var gravity = 0
|
|
var moviment_velocity : Vector3
|
|
var rotation_direction : float
|
|
var knockbacked := false
|
|
var coins := 0
|
|
var is_dead := false
|
|
|
|
|
|
func _physics_process(delta):
|
|
sncy()
|
|
handle_input(delta)
|
|
# Add the gravity.
|
|
apply_gravity(delta)
|
|
jump(delta)
|
|
handle_animations()
|
|
|
|
|
|
var applied_velocity : Vector3
|
|
applied_velocity = velocity.lerp(moviment_velocity, delta * 10)
|
|
applied_velocity.y = -gravity
|
|
|
|
velocity = applied_velocity
|
|
|
|
|
|
move_and_slide()
|
|
|
|
func sncy():
|
|
hud_container.update_life(health)
|
|
func handle_input(delta):
|
|
|
|
if not is_dead:
|
|
if Input.is_action_just_pressed("node_pause"):
|
|
get_parent().get_node("Pause").visible = true
|
|
get_tree().paused = true
|
|
|
|
if !knockbacked:
|
|
var input := Vector3.ZERO
|
|
input.x = Input.get_axis("move_left", "move_right")
|
|
input.z = Input.get_axis("move_forward", "move_backward")
|
|
|
|
|
|
velocity = input * SPEED * delta
|
|
|
|
if Vector2(velocity.z, velocity.x).length() > 0:
|
|
rotation_direction = Vector2(velocity.z, velocity.x).angle()
|
|
|
|
rotation.y = lerp_angle(rotation.y, rotation_direction, delta * 10)
|
|
func handle_animations():
|
|
if not is_dead:
|
|
if is_on_floor():
|
|
if abs(velocity.x) > 1 or abs(velocity.z) > 1:
|
|
animator.play("Run", 0.3)
|
|
else:
|
|
animator.play("Idle", 0.2)
|
|
|
|
if !is_on_floor() and gravity > 2:
|
|
animator.play("Fall", 0.3)
|
|
else:
|
|
animator.play("Dead", 0.3)
|
|
await animator.animation_finished
|
|
get_tree().paused = true
|
|
get_parent().get_node("Game_over").visible = true
|
|
|
|
func apply_gravity(delta):
|
|
if not is_on_floor():
|
|
gravity += 25 * delta
|
|
|
|
func jump(_delta):
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
gravity = -JUMP_VELOCITY
|
|
if gravity > 0 and is_on_floor():
|
|
gravity = 0
|
|
|
|
@warning_ignore("unused_parameter")
|
|
func knockback(impact_point: Vector3, force: Vector3) -> void:
|
|
force.y = abs(force.y)
|
|
velocity = force.limit_length(9.0)
|
|
|
|
func _on_hurtbox_body_entered(body):
|
|
|
|
|
|
var body_collision = (body.global_position - global_position)
|
|
var force = -body_collision
|
|
force *= 10.0
|
|
gravity = -5.0
|
|
knockback(body_collision, force)
|
|
knockbacked = true
|
|
await get_tree().create_timer(0.3).timeout
|
|
knockbacked = false
|
|
|
|
func collect_coin():
|
|
coins += 1
|
|
hud_container.update_coin(coins)
|
|
|
|
func damage(amount: int):
|
|
if not Executed:
|
|
health -= amount
|
|
verify_life()
|
|
Executed = true
|
|
await get_tree().create_timer(1.0).timeout
|
|
Executed = false
|
|
|
|
func verify_life():
|
|
if health == 0:
|
|
is_dead = true
|
|
|
|
|
|
|
|
func _on_foot_body_entered(body: Node3D) -> void: #foot
|
|
|
|
print(body.name)
|