Initial commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
# Movement speed in pixels per second
|
||||
@export var speed: float = 600.0
|
||||
var last_direction = Vector2.DOWN
|
||||
|
||||
var animation_playing =false;
|
||||
var animation_start_time = 0;
|
||||
var animation_duration = 0;
|
||||
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if animation_playing:
|
||||
var elapsed = (Time.get_ticks_msec() - animation_start_time)
|
||||
if elapsed > animation_duration:
|
||||
animation_playing = false
|
||||
reset_to_stand()
|
||||
|
||||
func _input(event):
|
||||
if animation_playing==true:
|
||||
return
|
||||
|
||||
if Input.is_action_just_pressed("move_left"):
|
||||
$Sprite.play_animation("walk_east", true)
|
||||
last_direction = Vector2.LEFT
|
||||
elif Input.is_action_just_pressed("move_right"):
|
||||
$Sprite.play_animation("walk_east", false)
|
||||
last_direction = Vector2.RIGHT
|
||||
elif Input.is_action_just_pressed("move_up"):
|
||||
$Sprite.play_animation("walk_north", false)
|
||||
last_direction = Vector2.UP
|
||||
elif Input.is_action_just_pressed("move_down"):
|
||||
$Sprite.play_animation("walk_south", false)
|
||||
last_direction = Vector2.DOWN
|
||||
|
||||
|
||||
if not Input.is_action_pressed("move_left") and not Input.is_action_pressed("move_right") and not Input.is_action_pressed("move_up") and not Input.is_action_pressed("move_down"):
|
||||
reset_to_stand()
|
||||
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
if animation_playing==true:
|
||||
return
|
||||
|
||||
|
||||
# 1. Get the direction vector based on our Input Map actions
|
||||
var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
||||
|
||||
# 2. Apply direction to velocity multiplied by our speed
|
||||
if direction != Vector2.ZERO:
|
||||
velocity = direction * speed
|
||||
else:
|
||||
# Smoothly slow down to a stop if no keys are pressed
|
||||
velocity = velocity.move_toward(Vector2.ZERO, speed)
|
||||
|
||||
# 3. Move the character body and handle physical collisions automatically
|
||||
move_and_slide()
|
||||
|
||||
func play_animation(name:StringName, duration):
|
||||
animation_playing = true
|
||||
animation_duration = duration
|
||||
$Sprite.play_animation(name, false)
|
||||
animation_start_time = Time.get_ticks_msec()
|
||||
|
||||
func reset_to_stand():
|
||||
if last_direction==Vector2.UP:
|
||||
$Sprite.play_animation("stand_north", false)
|
||||
elif last_direction==Vector2.DOWN:
|
||||
$Sprite.play_animation("stand_south", false)
|
||||
elif last_direction==Vector2.LEFT:
|
||||
$Sprite.play_animation("stand_east", true)
|
||||
elif last_direction==Vector2.RIGHT:
|
||||
$Sprite.play_animation("stand_east", false)
|
||||
@@ -0,0 +1 @@
|
||||
uid://syr14v0u22s7
|
||||
Reference in New Issue
Block a user