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

import rl "vendor:raylib"

Object_Spawner :: proc(Object_Resource)

current_room: Room_Position_Resource

object_spawners := [Object_Type]Object_Spawner{
  .PLAYER_SPAWN = player_spawn_object_spawner,
}

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

  for obj in room.objects {
    object_spawners[obj.type](obj)
  }
}

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 {
      current_room = room_pos
      spawn_objects(current_room.id)
      return true
    }
  }
  return false
}

open_room :: proc(id: Room_Id) -> bool {
  for room_pos in world {
    if room_pos.id == id {
      current_room = room_pos
      spawn_objects(current_room.id)
      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)
}

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

iterate_room_tiles :: proc(id: Room_Id, callback: Map_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
      }
    }
  }
}