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

import "core:log"

import "fw"

Object_Spawner :: proc(Object_Resource)
Room_Change_Callback :: proc(Room_Id)

current_room: Room_Position_Resource

object_spawners := [Object_Type]Object_Spawner{
  .Player_Spawn = object_spawner_player_spawn,
  .Collision = object_spawner_collision,
  .Upgrade = object_spawner_upgrade,
  .Prop = object_spawner_prop,
  .Rope = object_spawner_rope,
}

room_open_cb := []Room_Change_Callback{
  on_room_open_platforms,
  on_room_open_bullets,
  on_room_open_props,
  on_room_open_rope,
}

room_close_cb := []Room_Change_Callback{
}

@(private = "file")
_spawn_objects :: proc(room_id: Room_Id) {
  room := get_room(room_id)

  for obj in room.objects {
    spawner := object_spawners[obj.type]
    if spawner != nil {
      spawner(obj)
    }
  }
}

@(private = "file")
_open_room :: proc(room_pos: Room_Position_Resource) {
  for cb in room_close_cb {
    cb(current_room.id)
  }

  current_room = room_pos

  log.infof("Opened room %v", current_room.id)

  for cb in room_open_cb {
    cb(current_room.id)
  }

  _spawn_objects(current_room.id)
}

point_inside_room :: proc(pos: Vec2) -> bool {
  width := f32(current_room.width)
  height := f32(current_room.height)

  return pos.x >= 0 &&
     pos.x <= width &&
     pos.y >= 0 &&
     pos.y <= height
}

open_room_at :: proc(pos: [2]i32) -> bool {
  pos := pos
  pos += {current_room.x, current_room.y}

  for room_pos in world {
    if room_pos.id == current_room.id {
      continue
    }

    if pos.x >= room_pos.x &&
       pos.x <= room_pos.x + room_pos.width &&
       pos.y >= room_pos.y &&
       pos.y <= room_pos.y + room_pos.height {
      _open_room(room_pos)
      return true
    }
  }
  return false
}

open_room :: proc(id: Room_Id) -> bool {
  for room_pos in world {
    if room_pos.id == id {
      _open_room(room_pos)
      return true
    }
  }

  return false
}

draw_room :: proc(id: Room_Id) {
  draw_tile :: proc(x: i32, y: i32, tile_id: u32) {
    tile := tiles[tile_id]
    fw.draw_tile(
      get_image(tile.image),
      Vec2{f32(x), f32(y)},
      Rect{
        {f32(tile.rect.start.x), f32(tile.rect.start.y)},
        {f32(tile.rect.size.x), f32(tile.rect.size.y)},
      },
    )
  }

  iterate_room_tiles(id, draw_tile)
}

Room_Iterate_Callback :: proc(_: i32, _: i32, _: u32)

iterate_room_tiles :: proc(id: Room_Id, callback: Room_Iterate_Callback) {
  room := get_room(id)

  for layer in room.layers {
    x: i32 = 0
    y: i32 = 0

    for cell, i in layer {
      if cell > 0 {
        callback(x, y, cell)
      }

      x += room.tile_width
      if x > (room.width - 1) * room.tile_width {
        x = 0
        y += room.tile_height
      }
    }
  }
}