aboutsummaryrefslogtreecommitdiff
path: root/src/bullet.odin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bullet.odin')
-rw-r--r--src/bullet.odin94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/bullet.odin b/src/bullet.odin
new file mode 100644
index 0000000..b26cc14
--- /dev/null
+++ b/src/bullet.odin
@@ -0,0 +1,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()
+}