godot-ex1/Player/player.gd

67 lines
1.5 KiB
GDScript

extends KinematicBody2D
const FRICTION = 800
const ACCL = 300
const MAX_SPEED = 100
enum {
MOVE,
ROLL,
ATTACK,
}
var state = MOVE
var velocity = Vector2.ZERO
onready var animation_tree = $AnimationTree
onready var animation_state = animation_tree.get("parameters/playback")
func _ready():
animation_tree.active = true
func _physics_process(delta):
match state:
MOVE:
move(delta)
ROLL:
roll()
ATTACK:
attack()
func roll():
pass
func attack():
# don't slide and attack
velocity = Vector2.ZERO
animation_state.travel("Attack")
# I call this call back in a track on each attack animation
func attack_finished():
state = MOVE
func move(delta):
var input_velocity = Vector2.ZERO
input_velocity.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_velocity.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_velocity = input_velocity.normalized()
if input_velocity != Vector2.ZERO:
animation_tree.set("parameters/Idle/blend_position", input_velocity)
animation_tree.set("parameters/Run/blend_position", input_velocity)
animation_tree.set("parameters/Attack/blend_position", input_velocity)
animation_state.travel("Run")
velocity = velocity.move_toward(input_velocity * MAX_SPEED, ACCL * delta)
else:
animation_state.travel("Idle")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
velocity = move_and_slide(velocity)
if Input.is_action_just_pressed("ui_attack"):
state = ATTACK