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
|
package demonchime
import rl "vendor:raylib"
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,
}
room_open_cb := []Room_Change_Callback{
on_room_open_platforms,
}
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
for cb in room_open_cb {
cb(current_room.id)
}
spawn_objects(current_room.id)
}
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]
tileset := tilesets[tile.tileset]
rl.DrawTexturePro(
get_image(tileset.image),
rl.Rectangle {
x = f32(tile.rect.start.x),
y = f32(tile.rect.start.y),
width = f32(tile.rect.size.x),
height = f32(tile.rect.size.y),
},
rl.Rectangle {
x = f32(x),
y = f32(y),
width = f32(tile.rect.size.x),
height = f32(tile.rect.size.y),
},
{0, 0},
0,
rl.WHITE,
)
}
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 - 1)
}
x += room.tile_width
if x > (room.width - 1) * room.tile_width {
x = 0
y += room.tile_height
}
}
}
}
|