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

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

import "phys"

Bullet :: struct {
  handle: Entity_Handle,
  body: phys.Body_Handle,
  sprite: Sprite,
  vel: Vec2,
  lifetime: f32,
  lifetime_max: 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 = {
    0,
    // 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 := phys.make_body(body_rect, layers, mask)

  phys.set_velocity(body, vel)
  phys.set_position(body, pos)

  return make_entity(&state.bullet_list, Bullet{
    body = body,
    sprite = sprite,
    vel = vel,
    lifetime = lifetime,
    lifetime_max = lifetime,
  })
}

delete_bullet :: proc(h: Entity_Handle) {
  b := get_entity(state.bullet_list, h)
  phys.remove_body(b.body)
  delete_entity(&state.bullet_list, h)
}

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

    phys.update_body(b.body)

    if phys.get_collisions(b.body) != {} || b.lifetime < 0 {
      delete_bullet(b.handle)
      continue
    }

    phys.set_velocity(
      b.body,
      linalg.lerp(b.vel, Vec2{0, 0}, 1 - b.lifetime / b.lifetime_max),
    )

    b.sprite.pos = phys.get_position(b.body)

    b.sprite.scale.x = math.max(b.lifetime / b.lifetime_max, 0.1)
  }
}

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

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

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