implement basic player animations
This commit is contained in:
parent
ed854ba96b
commit
1c1be19cfa
1 changed files with 30 additions and 5 deletions
|
|
@ -1,18 +1,43 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
const MAX_SPEED: float = 200
|
const MAX_SPEED: float = 150
|
||||||
|
|
||||||
|
var was_moving: bool
|
||||||
|
var is_moving: bool
|
||||||
|
var current_dir: String
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
pass
|
was_moving = true
|
||||||
|
is_moving = false
|
||||||
|
current_dir = 'down'
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
if not is_moving and was_moving:
|
||||||
|
$AnimatedSprite2D.play('down')
|
||||||
|
$AnimatedSprite2D.frame = 1
|
||||||
|
$AnimatedSprite2D.pause()
|
||||||
|
|
||||||
|
if is_moving:
|
||||||
|
$AnimatedSprite2D.play(current_dir)
|
||||||
|
|
||||||
|
func _physics_process(delta: float) -> void:
|
||||||
var movement_vector: Vector2 = get_movement_vector()
|
var movement_vector: Vector2 = get_movement_vector()
|
||||||
var direction: Vector2 = movement_vector.normalized()
|
var direction: Vector2 = movement_vector.normalized()
|
||||||
velocity = direction * MAX_SPEED
|
velocity = direction * MAX_SPEED
|
||||||
|
|
||||||
|
is_moving = velocity.length() > 10
|
||||||
|
|
||||||
|
if is_moving:
|
||||||
|
if abs(velocity.x) > abs(velocity.y):
|
||||||
|
current_dir = 'right' if velocity.x > 0 else 'left'
|
||||||
|
else:
|
||||||
|
current_dir = 'down' if velocity.y > 0 else 'up'
|
||||||
|
|
||||||
|
was_moving = true
|
||||||
|
|
||||||
move_and_slide()
|
move_and_slide()
|
||||||
|
|
||||||
func get_movement_vector() -> Vector2:
|
func get_movement_vector() -> Vector2:
|
||||||
var x_mov: float = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
|
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
||||||
var y_mov: float = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
||||||
return Vector2(x_mov, y_mov)
|
return Vector2(x_mov, y_mov)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue