godot-ex1/Player/player.gd

96 lines
2.3 KiB
GDScript

extends KinematicBody2D
const FRICTION = 800
const ACCL = 300
const MAX_SPEED = 100
const ROLL_SPEED = MAX_SPEED * 1.2
enum {
MOVE,
ROLL,
ATTACK,
}
var state = MOVE
var velocity = Vector2.ZERO
# At the Player scense, player has to be faced to the right for this
# vector to be correct
var roll_vector = Vector2.RIGHT
onready var animation_tree = $AnimationTree
onready var animation_state = animation_tree.get("parameters/playback")
onready var sword_hitbox = $HitBoxPosition/SwordHitBox
func move():
velocity = move_and_slide(velocity)
func _ready():
animation_tree.active = true
sword_hitbox.knockback_vector = roll_vector
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
ROLL:
roll_state()
ATTACK:
attack_state()
func roll_state():
velocity = roll_vector * ROLL_SPEED
move()
animation_state.travel("Roll")
func attack_state():
# don't slide and attack
velocity = Vector2.ZERO
animation_state.travel("Attack")
# I call this call back in a track on each roll animation
func roll_finished():
velocity = velocity * 0.8
state = MOVE
# I call this call back in a track on each attack animation
func attack_finished():
state = MOVE
func move_state(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:
# We want to roll in direction of our movement and rememebr which direction was it
roll_vector = input_velocity
# Saving the direction of hits
sword_hitbox.knockback_vector = input_velocity
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_tree.set("parameters/Roll/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)
move()
if Input.is_action_just_pressed("ui_attack"):
state = ATTACK
if Input.is_action_just_pressed("ui_roll"):
state = ROLL