character is now running on screen
This commit is contained in:
37
scripts/characters/player.gd
Normal file
37
scripts/characters/player.gd
Normal file
@@ -0,0 +1,37 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
var last_direction = Vector2.DOWN
|
||||
|
||||
func get_input():
|
||||
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
||||
velocity = input_direction * SPEED
|
||||
return input_direction
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
print(get_input())
|
||||
move_and_slide()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if not $AnimatedSprite2D:
|
||||
return
|
||||
|
||||
var input_direction = get_input()
|
||||
var animation_name = ""
|
||||
|
||||
if input_direction != Vector2.ZERO:
|
||||
last_direction = input_direction
|
||||
# Determine dominant direction and build animation name
|
||||
if abs(input_direction.x) > abs(input_direction.y):
|
||||
animation_name = "Run" + ("Right" if input_direction.x > 0 else "Left")
|
||||
else:
|
||||
animation_name = "Run" + ("Down" if input_direction.y > 0 else "Up")
|
||||
else:
|
||||
# Use last direction for idle
|
||||
if abs(last_direction.x) > abs(last_direction.y):
|
||||
animation_name = "Idle" + ("Right" if last_direction.x > 0 else "Left")
|
||||
else:
|
||||
animation_name = "Idle" + ("Down" if last_direction.y > 0 else "Up")
|
||||
|
||||
$AnimatedSprite2D.play(animation_name)
|
||||
Reference in New Issue
Block a user