aboutsummaryrefslogtreecommitdiff
path: root/src/player.odin
blob: 2f82e2f1421abe4185a52a3994a402a2e3cc8d9a (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package demonchime

import "core:math"
import "core:math/linalg"
import "core:log"

import "phys"
import "fw"

Player_State :: enum {
  Default,
  Dash,
}

// there will only ever be one player, so just make it a global :)
player: struct {
  body: phys.Body_Handle,
  sprite: Sprite,
  gun: struct {
    sprite: Sprite,
    kickback: f32,
  },
  scarf: Tail(7),
  jump_buffer: f32,
  coyote_time: f32,
  dash_cooldown: f32,
  dash_timer: f32,
  state: Player_State,
  outside_room: bool,
  has_double_jumped: bool,

  owns_double_jump: bool,
  owns_dash: bool,
}

init_player :: proc() {
  body := phys.make_body(
    phys.Rect{{-4, -8}, {8, 16}},
    layers = {.Player},
    mask = {.Hard}
  )
  player.body = body

  init_sprite(&player.sprite, .Player)
  set_sprite_offset_percentage(&player.sprite, {0.5, 0.5})

  init_sprite(&player.gun.sprite, .Pistol)

  player.gun.sprite.offset = Vec2 {
    -PLAYER_GUN_DIST,
    math.round(f32(player.gun.sprite.height) * 0.5),
  }

  player.scarf = {
    point_max_dist = 2,
    point_rad = 2,
    color_start = Color {0.18, 0.13, 0.18, 1} * 0.5,
    color_end = {0.18, 0.13, 0.18, 1},
  }
}

deinit_player :: proc() {
  phys.remove_body(player.body)
}

@(private = "file")
_get_input_dir :: proc() -> f32 {
  if !point_inside_room(phys.get_position(player.body)) {
    return 0
  }

  input: f32

  if fw.is_keybind_down(actions.move_left) {
    input -= 1
  }
  if fw.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")
  } else {
    set_sprite_active_tag(&player.sprite, "idle")
  }

  if fw.is_keybind_just_down(actions.jump) {
    player.jump_buffer = PLAYER_JUMP_BUFFERING
  }

  if player.owns_dash &&
     fw.is_keybind_just_down(actions.dash) && 
     player.dash_cooldown <= 0 {
    _enter_dash()
    return
  }

  // body := phys.get_body(player.body_handle)
  pos := phys.get_position(player.body)
  vel := phys.get_velocity(player.body)
  collisions := phys.get_collisions(player.body)

  if .Down in collisions {
    player.coyote_time = PLAYER_COYOTE_TIME
    player.has_double_jumped = false
  } else {
    switch 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")
    }
  }

  rel_mouse_pos := fw.get_mouse_pos() - pos

  if fw.is_keybind_just_down(actions.shoot) {
    mouse_dir := linalg.normalize0(rel_mouse_pos + {0, PLAYER_GUN_HEIGHT})
    
    bullet_pos := pos
    bullet_pos.y -= PLAYER_GUN_HEIGHT
    bullet_pos += mouse_dir * PLAYER_GUN_DIST

    player.gun.kickback = PLAYER_GUN_KICKBACK

    make_bullet(
      pos = bullet_pos,
      vel = mouse_dir * 800,
      lifetime = 0.25,
    )
  }

  player.sprite.scale.x = math.sign(rel_mouse_pos.x)

  if player.jump_buffer > 0 {
    if player.coyote_time > 0 {
      vel.y = -PLAYER_JUMP_FORCE

      player.jump_buffer = 0
      player.coyote_time = 0
    } else if player.owns_double_jump && !player.has_double_jumped {
      vel.y = -PLAYER_DOUBLE_JUMP_FORCE
      player.has_double_jumped = true

      player.jump_buffer = 0
    }
  }

  if .Down not_in collisions &&
     !fw.is_keybind_down(actions.jump) &&
     vel.y < PLAYER_JUMP_RELEASE_CUT {
    vel.y = PLAYER_JUMP_RELEASE_CUT
  }

  vel.x = dt_lerp(
    vel.x,
    input * PLAYER_SPEED,
    PLAYER_ACCEL,
  )
  vel.y = math.min(vel.y + GRAVITY * dt, TERMINAL_VELOCITY)

  phys.set_velocity(player.body, vel)

  phys.update_body(player.body)

  // cam_pos := phys.get_position(player.body) - SCREEN_SIZE * 0.5
  // fw.set_camera_pos(cam_pos)
}

@(private = "file")
_enter_dash :: proc() {
  input := _get_input_dir()
  if input == 0 {
    // the sprite x scale is the direction the player is facing :)
    input = math.sign(player.sprite.scale.x)
  }
  phys.set_velocity(
    player.body,
    {input * 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)

  player.dash_timer -= dt

  if player.dash_timer <= 0 || phys.get_collisions(player.body) != nil {
    _exit_dash()
  }
}

@(private = "file")
_exit_dash :: proc() {
  player.state = .Default

  player.dash_cooldown = PLAYER_DASH_COOLDOWN

  vel := phys.get_velocity(player.body)
  phys.set_velocity(player.body, vel / 2)
}

@(private = "file")
_get_camera_target_pos :: proc() -> Vec2 {
  pos := phys.get_position(player.body) - SCREEN_SIZE * 0.5
  room_size := Vec2{
    f32(current_room.width),
    f32(current_room.height),
  }

  camera_bounds_min := Vec2{0, 0}
  camera_bounds_max := room_size - SCREEN_SIZE

  pos.x = math.clamp(pos.x, camera_bounds_min.x, camera_bounds_max.x)
  pos.y = math.clamp(pos.y, camera_bounds_min.y, camera_bounds_max.y)

  return pos
}

@(private = "file")
_change_rooms :: proc() {
  pos := phys.get_position(player.body)

  if point_inside_room(pos) {
    return
  }

  prev_room_pos := Vec2{f32(current_room.x), f32(current_room.y)}
  changed := open_room_at({i32(pos.x), i32(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 := pos + diff
    phys.set_position(player.body, new_pos)

    if diff.y > 0 {
      vel := phys.get_velocity(player.body)
      vel.y = -PLAYER_JUMP_FORCE
      phys.set_velocity(player.body, vel.y)
    } else if diff.y > 0 {
      vel := phys.get_velocity(player.body)
      vel.y = 0
      phys.set_velocity(player.body, vel.y)
    }

    new_cam_pos := _get_camera_target_pos()
    state.camera_pos += diff
    state.camera_target += diff
  }
}

update_player :: proc(dt: f32) {
  switch player.state {
  case .Default: _default_state(dt)
  case .Dash:    _dash_state(dt)
  }

  pos := phys.get_position(player.body)

  player.sprite.pos = pos
  update_sprite(&player.sprite, dt)

  mouse_dir := linalg.normalize0(fw.get_mouse_pos() - player.gun.sprite.pos)
  player.gun.sprite.rotation = math.atan2(mouse_dir.y, mouse_dir.x)

  player.gun.sprite.pos = pos - Vec2{0, PLAYER_GUN_HEIGHT}
  player.gun.sprite.pos += -mouse_dir * player.gun.kickback
  player.gun.kickback = dt_lerp(player.gun.kickback, 0, 5)

  player.gun.sprite.scale.y = -1 if mouse_dir.x < 0 else 1

  player.dash_cooldown -= dt
  player.jump_buffer -= dt
  player.coyote_time -= dt

  state.camera_target = _get_camera_target_pos()

  scarf_dist: f32 = PLAYER_SCARF_DIST
  if _get_input_dir() != 0 {
    scarf_dist -= 1
  }
  scarf_pos := linalg.round(
    pos + Vec2{-scarf_dist * player.sprite.scale.x, 0},
  )
  set_tail_position(&player.scarf, scarf_pos, dt)

  _change_rooms()
}

draw_player :: proc() {
  draw_tail(player.scarf)
  draw_sprite(player.sprite)
  if player.state != .Dash {
    draw_sprite(player.gun.sprite)
  }
}

draw_player_fg :: proc() {
  rc_start := phys.get_position(player.body)
  dir := Vec2{math.cos(player.gun.sprite.rotation), math.sin(player.gun.sprite.rotation)}
  bodies := phys.get_colliding_bodies(phys.make_raycast(rc_start, dir), allocator = context.temp_allocator)
  is_colliding := len(bodies) > 0

  color := fw.GREEN if is_colliding else fw.RED
  fw.draw_line(rc_start, rc_start + dir * 500, color = color)

  for body in bodies {
    rect := phys.get_rect(body)
    fw.draw_rect(rect.start, rect.size, color = fw.RED)
  }
}

@(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_position(player.body, obj.pos)
}