-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
141 lines (118 loc) · 3.44 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
extends KinematicBody2D
const UP = Vector2(0,-1)
var motion = Vector2(0,0)
export var gravity = 20
export var y_speed = 700
export var x_speed = 500
export var back_size = 4000
var is_dead = false
signal color_changed
signal is_dead
enum {RED, GREEN, BLUE, YELLOW}
enum {RUN, IDLE, JUMP, FALL, DIE}
var current_color = RED
var current_state = IDLE
onready var sfx = {
"jump": $SFX/Jump,
"change_color": $SFX/ChangeColor,
"change_level": $SFX/ChangeLevel,
"lose": $SFX/Lose
}
func _ready():
$Sprite.texture.size = Vector2(back_size, back_size)
func _physics_process(delta):
if !is_dead:
check_controls()
set_animation()
apply_gravity()
motion = move_and_slide(motion, UP)
pass
func check_controls():
# Horizontal input
if is_on_floor(): current_state = IDLE
if (Input.is_action_pressed("ui_right")):
motion.x += x_speed/3
$AnimatedSprite.flip_h = false
if is_on_floor(): current_state = RUN
elif (Input.is_action_pressed("ui_left")):
motion.x -= x_speed/3
$AnimatedSprite.flip_h = true
if is_on_floor(): current_state = RUN
if motion.x != 0 && !(Input.is_action_pressed("ui_right") || Input.is_action_pressed("ui_left")):
if motion.x > 0: motion.x -= motion.x/2
elif motion.x < 0: motion.x -= motion.x/2
motion.x = clamp(motion.x, -x_speed, x_speed)
# Vertical input
if (Input.is_action_pressed("ui_up") && is_on_floor()):
jump()
play_sound("jump")
if (Input.is_action_just_released("ui_up")):
stop_jump()
# Color change
if (Input.is_action_just_pressed("ui_select")):
current_color = (current_color+1)%4
play_sound("change_color")
emit_signal("color_changed")
# States and tweaks
if motion.y > 100:
current_state = FALL
if Input.is_action_just_released("ui_left") || Input.is_action_just_released("ui_right"):
current_state = IDLE
func jump():
current_state = JUMP
motion.y = -y_speed
func stop_jump():
if motion.y < 0:
motion.y += -0.5*motion.y
current_state = FALL
func apply_gravity():
motion.y += gravity
func die():
current_state = DIE
is_dead = true
motion = Vector2(0,0)
play_sound("lose")
$RestartTimer.start()
func play_sound(sound):
sfx[sound].play()
func restart_level():
emit_signal("is_dead")
func set_animation():
if current_state == DIE:
$AnimatedSprite.animation = "die"
elif current_color == RED:
if current_state == IDLE:
$AnimatedSprite.animation = "r_idle"
elif current_state == RUN:
$AnimatedSprite.animation = "r_run"
elif current_state == JUMP:
$AnimatedSprite.animation = "r_jump"
elif current_state == FALL:
$AnimatedSprite.animation = "r_fall"
elif current_color == GREEN:
if current_state == IDLE:
$AnimatedSprite.animation = "g_idle"
elif current_state == RUN:
$AnimatedSprite.animation = "g_run"
elif current_state == JUMP:
$AnimatedSprite.animation = "g_jump"
elif current_state == FALL:
$AnimatedSprite.animation = "g_fall"
elif current_color == BLUE:
if current_state == IDLE:
$AnimatedSprite.animation = "b_idle"
elif current_state == RUN:
$AnimatedSprite.animation = "b_run"
elif current_state == JUMP:
$AnimatedSprite.animation = "b_jump"
elif current_state == FALL:
$AnimatedSprite.animation = "b_fall"
elif current_color == YELLOW:
if current_state == IDLE:
$AnimatedSprite.animation = "y_idle"
elif current_state == RUN:
$AnimatedSprite.animation = "y_run"
elif current_state == JUMP:
$AnimatedSprite.animation = "y_jump"
elif current_state == FALL:
$AnimatedSprite.animation = "y_fall"