-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.gd
87 lines (71 loc) · 1.96 KB
/
Player.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
extends KinematicBody2D
#-----------------------------------------------------
# Numeric constants
const BASE_SPEED = 500
const JUMP_FORCE = -1100
const GRAVITY = 9.8
const WEIGHT = 8.16
const EPSILON = 0.01
#-----------------------------------------------------
# String constants
# Animation states
const RUNNING = "Running"
const IDLE = "Idle"
const ATTACK = "OverheadAttack"
# Input keys
const LEFT = "left"
const RIGHT = "right"
const JUMP = "jump"
const SELECT = "select"
# Player State
const STATE_NO_ATTACK = "NO ATTACK"
const STATE_ATTACK = "ATTACK"
#-----------------------------------------------------
# Member Variables
var dir = 1
var velocity
var state = STATE_NO_ATTACK
#-----------------------------------------------------
# Member methods
func flip(new_dir):
self.scale.x *= -1 if new_dir != dir else 1
dir = new_dir
func set_running():
$AnimationPlayer.play(RUNNING)
func attack():
state = STATE_ATTACK
$AnimationPlayer.play(ATTACK)
func calcGravity():
return WEIGHT * GRAVITY
#-----------------------------------------------------
# Internal methods
func _ready():
velocity = Vector2()
state = STATE_NO_ATTACK
dir = RIGHT if self.scale.x > 0 else LEFT
$AnimationPlayer.play(IDLE)
func _physics_process(_delta):
if state == STATE_NO_ATTACK:
if Input.is_action_pressed(RIGHT):
flip(RIGHT)
velocity.x = BASE_SPEED
set_running()
elif Input.is_action_pressed(LEFT):
flip(LEFT)
velocity.x = -BASE_SPEED
set_running()
if Input.is_action_just_pressed(JUMP) and is_on_floor():
velocity.y = JUMP_FORCE
elif Input.is_action_just_pressed(SELECT):
attack()
velocity.y += calcGravity()
if abs(velocity.x) <= EPSILON and not state == STATE_ATTACK:
velocity.x = 0
$AnimationPlayer.play(IDLE)
velocity = move_and_slide(velocity, Vector2.UP)
if is_on_floor():
velocity.x = lerp(velocity.x, 0, .6)
func _on_animation_player_finished(animation_name):
if animation_name == ATTACK:
state = STATE_NO_ATTACK
$AnimationPlayer.play(IDLE)