HeartBeast/script/player/Player.gd

79 lines
2.1 KiB
GDScript3
Raw Normal View History

2021-04-03 23:27:13 +02:00
extends KinematicBody2D
var velocity = Vector2.ZERO
var roll_vector = Vector2.ZERO
2021-04-03 23:27:13 +02:00
const MAX_SPEED = 80
const ROLL_SPEED = MAX_SPEED*1.5
2021-04-03 23:27:13 +02:00
const ACCELERATION = 500
const FRICTION = 500
enum states{
MOVE,
ROLL,
ATTACK
}
var state = states.MOVE
2021-04-03 23:27:13 +02:00
onready var animationPlayer = $AnimationPlayer
2021-04-03 23:51:22 +02:00
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get('parameters/playback')
onready var swordHitbox = $HitBoxPivot/SwordHitbox
2021-04-03 23:27:13 +02:00
func _ready():
animationTree.active = true
swordHitbox.knockbackvector = roll_vector
2021-04-03 23:27:13 +02:00
func _physics_process(delta):
match state:
states.MOVE:
do_move(delta)
states.ATTACK:
do_attack(delta)
states.ROLL:
do_roll(delta)
2021-04-03 23:27:13 +02:00
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):
2021-04-03 23:27:13 +02:00
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")
2021-04-03 23:27:13 +02:00
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
roll_vector = input_vector
swordHitbox.knockbackvector = roll_vector
set_facing_direction(input_vector)
2021-04-03 23:51:22 +02:00
animationState.travel("Walk")
2021-04-03 23:27:13 +02:00
velocity = velocity.move_toward(input_vector*MAX_SPEED,ACCELERATION*delta)
else:
2021-04-03 23:51:22 +02:00
animationState.travel("Idle")
2021-04-03 23:27:13 +02:00
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