HeartBeast/script/player/Player.gd

79 lines
2.1 KiB
GDScript

extends KinematicBody2D
var velocity = Vector2.ZERO
var roll_vector = Vector2.ZERO
const MAX_SPEED = 80
const ROLL_SPEED = MAX_SPEED*1.5
const ACCELERATION = 500
const FRICTION = 500
enum states{
MOVE,
ROLL,
ATTACK
}
var state = states.MOVE
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get('parameters/playback')
onready var swordHitbox = $HitBoxPivot/SwordHitbox
func _ready():
animationTree.active = true
swordHitbox.knockbackvector = roll_vector
func _physics_process(delta):
match state:
states.MOVE:
do_move(delta)
states.ATTACK:
do_attack(delta)
states.ROLL:
do_roll(delta)
func do_roll(delta):
animationState.travel('Roll')
velocity = roll_vector * ROLL_SPEED
move_sprite()
pass
func do_attack(_delta):
animationState.travel('Attack')
velocity = Vector2.ZERO
func set_facing_direction(input_vector):
animationTree.set("parameters/Walk/blend_position",input_vector)
animationTree.set("parameters/Idle/blend_position",input_vector)
animationTree.set("parameters/Attack/blend_position",input_vector)
animationTree.set("parameters/Roll/blend_position",input_vector)
func do_move(delta):
var input_vector=Vector2.ZERO
input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
roll_vector = input_vector
swordHitbox.knockbackvector = roll_vector
set_facing_direction(input_vector)
animationState.travel("Walk")
velocity = velocity.move_toward(input_vector*MAX_SPEED,ACCELERATION*delta)
else:
animationState.travel("Idle")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION*delta)
move_sprite()
if Input.is_action_just_pressed("attack"):
state = states.ATTACK
if Input.is_action_just_pressed('roll'):
state = states.ROLL
func move_sprite():
velocity = move_and_slide(velocity)
func attack_animation_finished():
state = states.MOVE
func roll_animation_finished():
velocity = velocity / 2
state = states.MOVE