68 lines
1.8 KiB
GDScript
68 lines
1.8 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var jump_sound: AudioStreamPlayer2D = $JumpSound
|
|
@onready var death_sound: AudioStreamPlayer2D = $DeathSound
|
|
@onready var respawn_timer: Timer = $RespawnTimer
|
|
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
|
|
|
|
const SPEED = 320.0
|
|
const JUMP_VELOCITY = -850.0
|
|
var alive = true
|
|
var can_move = true
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if !alive:
|
|
return
|
|
# Add animation
|
|
if velocity.x > 1 or velocity.x < -1:
|
|
animated_sprite_2d.animation = "running"
|
|
else:
|
|
animated_sprite_2d.animation = "idle"
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
animated_sprite_2d.animation = "jumping"
|
|
|
|
if can_move:
|
|
# Handle jump.
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
jump_sound.play()
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
var direction := Input.get_axis("left", "right")
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
move_and_slide()
|
|
|
|
if direction == 1:
|
|
animated_sprite_2d.flip_h = false
|
|
elif direction == -1:
|
|
animated_sprite_2d.flip_h = true
|
|
|
|
func _die() -> void:
|
|
alive = false
|
|
call_deferred("_disable_collision")
|
|
print(collision_shape_2d)
|
|
animated_sprite_2d.animation = "dying"
|
|
death_sound.play()
|
|
respawn_timer.start()
|
|
|
|
func _disable_collision():
|
|
collision_shape_2d.disabled = true
|
|
|
|
func _enable_collision():
|
|
collision_shape_2d.disabled = false
|
|
|
|
func _on_respawn_timer_timeout() -> void:
|
|
call_deferred("_enable_collision")
|
|
position = Vector2(50, 150)
|
|
alive = true
|
|
|