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

import os "core:os/os2"
import "core:log"
import "core:encoding/json"
import "core:path/filepath"
import "core:strings"

World_Room :: struct {
  file_name: string `json:"fileName"`, 
  x: i32,
  y: i32,
  width: i32,
  height: i32,
}

res_dir: string
world: []World_Room
current_room: struct {
  tmap: Map,
  using room: World_Room,
}

load_world :: proc(path: string) -> bool {
  world_text, read_err := os.read_entire_file(path, context.temp_allocator)
  if read_err != nil {
    log.errorf("Failed to read file %v (%v)", path, read_err)
    return false
  }

  jworld: struct {
    maps: []World_Room,
  }
  unmarshal_err := json.unmarshal(
    world_text,
    &jworld,
  )
  if unmarshal_err != nil {
    log.errorf("Failed to unmarshal file %v (%v)", path, unmarshal_err)
    return false
  }

  world = jworld.maps
  res_dir = filepath.dir(path)

  return true
}

delete_world :: proc() {
  for room in world {
    delete(room.file_name)
  }

  delete_map(current_room.tmap)
  delete(world)
  delete(res_dir)
}

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

  log.debug("trying to change room...", pos)
  for room in world {
    if strings.compare(room.file_name, current_room.room.file_name) == 0 {
      continue
    }

    if pos.x >= room.x && pos.x <= room.x + room.width \
    && pos.y >= room.y && pos.y <= room.y + room.height {
      delete_map(current_room.tmap)

      path := strings.concatenate(
        {res_dir, "/", room.file_name},
        allocator = context.temp_allocator,
      )
      new_map, err := load_map(path)
      if err != .NONE {
        log.error("could not load new room")
        return false
      }
      current_room.tmap = new_map
      current_room.room = room
      return true
    }
  }
  return false
}