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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
package demonchime
import "core:math"
import "phys"
PLAYER_SPEED :: 100
PLAYER_ACCEL :: 10
PLAYER_JUMP_FORCE :: 350
PLAYER_DOUBLE_JUMP_FORCE :: 250
PLAYER_JUMP_BUFFERING :: 0.07
PLAYER_COYOTE_TIME :: 0.06
PLAYER_JUMP_RELEASE_CUT :: -100
PLAYER_DASH_SPEED :: 500
PLAYER_DASH_TIME :: 0.15
PLAYER_DASH_COOLDOWN :: 0.3
Player_State :: enum {
DEFAULT,
DASH,
}
// there will only ever be one player, so just make it a global :)
player: struct {
body_handle: phys.Body_Handle,
sprite: Sprite,
jump_buffer: f32,
coyote_time: f32,
dash_cooldown: f32,
dash_timer: f32,
state: Player_State,
outside_room: bool,
has_double_jumped: bool,
}
init_player :: proc() {
handle, body := phys.make_body(phys.Rect{{-4, 17}, {8, 16}})
player.body_handle = handle
phys.set_body_position(handle, Vec2{50, 100})
init_sprite(&player.sprite, .PLAYER)
player.sprite.offset = Vec2 {
math.floor(f32(player.sprite.width / 2)),
-f32(player.sprite.height),
}
}
delete_player :: proc() {
phys.remove_body(player.body_handle)
destroy_sprite(player.sprite)
}
@(private = "file")
_get_input_dir :: proc() -> f32 {
input: f32
if is_keybind_down(actions.move_left) {
input -= 1
}
if is_keybind_down(actions.move_right) {
input += 1
}
return input
}
@(private = "file")
_default_state :: proc(dt: f32) {
input := _get_input_dir()
if input != 0 {
set_sprite_active_tag(&player.sprite, "run")
player.sprite.scale.x = math.sign(input)
} else {
set_sprite_active_tag(&player.sprite, "idle")
}
if is_keybind_just_down(actions.jump) {
player.jump_buffer = PLAYER_JUMP_BUFFERING
}
if is_keybind_just_down(actions.dash) && player.dash_cooldown <= 0 {
_enter_dash()
return
}
body := phys.get_body(player.body_handle)
if .DOWN in body.collisions {
player.coyote_time = PLAYER_COYOTE_TIME
player.has_double_jumped = false
} else {
switch body.vel.y {
case -math.INF_F32..<-50:
set_sprite_active_tag(&player.sprite, "jump_up")
case 50..<math.INF_F32:
set_sprite_active_tag(&player.sprite, "jump_down")
case:
set_sprite_active_tag(&player.sprite, "jump_trans")
}
}
if player.jump_buffer > 0 {
if player.coyote_time > 0 {
body.vel.y = -PLAYER_JUMP_FORCE
player.jump_buffer = 0
player.coyote_time = 0
} else if !player.has_double_jumped {
body.vel.y = -PLAYER_DOUBLE_JUMP_FORCE
player.has_double_jumped = true
player.jump_buffer = 0
}
}
if .DOWN not_in body.collisions &&
!is_keybind_down(actions.jump) &&
body.vel.y < PLAYER_JUMP_RELEASE_CUT {
body.vel.y = PLAYER_JUMP_RELEASE_CUT
}
body.vel.x = math.lerp(
body.vel.x,
input * PLAYER_SPEED,
math.pow(0.5, dt * PLAYER_ACCEL),
)
body.vel.y = math.min(body.vel.y + GRAVITY * dt, TERMINAL_VELOCITY)
phys.update_body(player.body_handle)
}
@(private = "file")
_enter_dash :: proc() {
body := phys.get_body(player.body_handle)
// the sprite x scale is the direction the player is facing :)
body.vel = {math.sign(player.sprite.scale.x) * PLAYER_DASH_SPEED, 0}
player.dash_timer = PLAYER_DASH_TIME
player.state = .DASH
}
@(private = "file")
_dash_state :: proc(dt: f32) {
phys.update_body(player.body_handle)
player.dash_timer -= dt
body := phys.get_body(player.body_handle)
if player.dash_timer <= 0 || body.collisions != {} {
_exit_dash()
}
}
@(private = "file")
_exit_dash :: proc() {
player.state = .DEFAULT
player.dash_cooldown = PLAYER_DASH_COOLDOWN
body := phys.get_body(player.body_handle)
body.vel /= 2
}
@(private = "file")
_change_rooms :: proc() {
body := phys.get_body(player.body_handle)
width := f32(current_room.width)
height := f32(current_room.height)
if body.pos.x < 0 ||
body.pos.x > width ||
body.pos.y < 0 ||
body.pos.y > height {
prev_room_pos := Vec2{f32(current_room.x), f32(current_room.y)}
changed := open_room_at({i32(body.pos.x), i32(body.pos.y)})
if changed {
new_room_pos := Vec2{f32(current_room.x), f32(current_room.y)}
diff := prev_room_pos - new_room_pos
new_pos := body.pos + diff - {0, 0}
phys.set_body_position(player.body_handle, new_pos)
}
}
}
update_player :: proc(dt: f32) {
switch player.state {
case .DEFAULT: _default_state(dt)
case .DASH: _dash_state(dt)
}
body := phys.get_body(player.body_handle)
player.sprite.pos = body.pos
update_sprite(&player.sprite, dt)
player.dash_cooldown -= dt
player.jump_buffer -= dt
player.coyote_time -= dt
_change_rooms()
}
draw_player :: proc() {
draw_sprite(player.sprite)
}
@(private = "file")
already_spawned_player := false
object_spawner_player_spawn :: proc(obj: Object_Resource) {
if already_spawned_player {
return
}
already_spawned_player = true
phys.set_body_position(player.body_handle, obj.pos)
}
|