aboutsummaryrefslogtreecommitdiff
path: root/src/bullet.odin
blob: b26cc14b1a9dd0f9577a4a8b83b74789e69dc4cf (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
package demonchime

import "core:math"
import "core:log"

import "phys"

Bullet :: struct {
  handle: Entity_Handle,
  body_handle: phys.Body_Handle,
  sprite: Sprite,
  lifetime: f32,
}

make_bullet :: proc(
  pos: Vec2,
  vel: Vec2,
  lifetime: f32 = 5,
  image_id := Image_Id.BULLET,
  layers: bit_set[phys.Layer;u16] = {.PLAYER_PROJECTILE},
  mask: bit_set[phys.Layer;u16] = {.DEFAULT},
) -> (Entity_Handle, ^Bullet) {
  sprite: Sprite
  init_sprite(&sprite, image_id)

  sprite.offset = {
    math.round(f32(sprite.width) * 0.5),
    math.round(f32(sprite.height) * 0.5),
  }
  sprite.rotation = math.atan2(vel.y, vel.x) * math.DEG_PER_RAD
  sprite.pos = pos

  body_rect := phys.Rect{
    start = {-3, -3},
    size = {6, 6},
  }
  body_handle, body := phys.make_body(body_rect, layers, mask)

  body.vel = vel

  phys.set_body_position(body_handle, pos)

  return make_entity(&state.bullet_list, Bullet{
    body_handle = body_handle,
    sprite = sprite,
    lifetime = lifetime,
  })
}

delete_bullet :: proc(bullet_handle: Entity_Handle) {
  bullet := get_entity(state.bullet_list, bullet_handle)

  phys.remove_body(bullet.body_handle)
  delete_entity(&state.platform_list, bullet.handle)
}

update_bullets :: proc(dt: f32) {
  iter := iter_entity_list(state.bullet_list)
  for bullet in entity_list_iter(&iter) {
    bullet.lifetime -= dt

    phys.update_body(bullet.body_handle)
    body := phys.get_body(bullet.body_handle)

    if body.collisions != {} || bullet.lifetime < 0 {
      delete_entity(&state.bullet_list, bullet.handle)
    }

    bullet.sprite.pos = body.pos
  }
}

draw_bullets :: proc() {
  iter := iter_entity_list(state.bullet_list)
  for bullet in entity_list_iter(&iter) {
    draw_sprite(bullet.sprite)

    body := phys.get_body(bullet.body_handle)
    rect := body.rect
    rect.start += body.pos
    draw_rect(transmute(Rect)rect)
  }
}

clear_bullets :: proc() {
  iter := iter_entity_list(state.bullet_list)
  for bullet in entity_list_iter(&iter) {
    delete_bullet(bullet.handle)
  }
}

on_room_open_bullets :: proc(room_id: Room_Id) {
  clear_bullets()
}