-
Notifications
You must be signed in to change notification settings - Fork 2
/
WorldData.lua
135 lines (117 loc) · 3.39 KB
/
WorldData.lua
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
local Room = require("Room")
local Entity = require("Entity")
local WorldData = class("WorldData")
function WorldData:initialize()
local chunk = love.filesystem.load("data/maps/map.lua")
local data = chunk()
self._rooms = {}
self._spawns = {}
self._doors = {}
self._entities = {}
self._fgtiles = {}
self._bgtiles = {}
self._xtiles, self._ytiles = 0, 0
self._hourglasses = 0
Entity.static:resetIds()
for i,v in ipairs(data.layers) do
if v.name == "FG" then
self._xtiles = v.width
self._ytiles = v.height
self._fgtiles = v.data
elseif v.name == "BG" then
self._bgtiles = v.data
elseif v.name == "OBJ" then
for j, o in ipairs(v.objects) do
if o.type == "room" then
local r = Room(o.x, o.y, o.width, o.height, Entity.static:getId())
table.insert(self._rooms, r)
elseif o.type == "door" then
local d = {x = o.x+o.width/2, y = o.y+o.height/2, id = Entity.static:getId(), black=false}
table.insert(self._doors, d)
elseif o.type == "blackdoor" then
local d = {x = o.x+o.width/2, y = o.y+o.height/2, id = Entity.static:getId(), black=true}
table.insert(self._doors, d)
elseif o.type == "spawn" then
local s = {x = o.x+o.width/2, y = o.y+o.height/2, id = o.name}
table.insert(self._spawns, s)
else
o.id = Entity.static:getId()
o.properties.id = o.id
table.insert(self._entities, o)
if o.type == "hourglass" then
self._hourglasses = self._hourglasses + 1
end
end
end
end
end
-- Build doors
for i, door in ipairs(self._doors) do
for j, room in ipairs(self._rooms) do
if door.y >= room.y and door.y < room.y+room.h then
if door.x == room.x then
door.right = room.id
elseif door.x == room.x+room.w then
door.left = room.id
end
end
end
end
-- Build rooms
for i, room in ipairs(self._rooms) do
-- Setup tiles
local cx, cy = room.x/TILEW, room.y/TILEW
local cw, ch = room.w/TILEW, room.h/TILEW
for iy=cy, cy+ch-1 do
for ix=cx, cx+cw-1 do
local fg, bg = self:getTile(ix, iy)
table.insert(room.fgtiles, fg)
table.insert(room.bgtiles, bg)
end
end
-- Find doors and entities in room
for j, door in ipairs(self._doors) do
if door.left == room.id or door.right == room.id then
local d = { x = door.x-room.x, y = door.y-room.y, left = door.left, right = door.right, id = door.id, black = door.black }
table.insert(room.doors, d)
end
end
for j, e in ipairs(self._entities) do
if e.x >= room.x and e.x <= room.x+room.w
and e.y >= room.y and e.y <= room.y+room.h then
local newe = { x = e.x-room.x, y = e.y-room.y, type = e.type, id = e.id, prop = e.properties }
table.insert(room.entities, newe)
end
end
for j, spawn in ipairs(self._spawns) do
if spawn.room == nil
and spawn.x >= room.x and spawn.x <= room.x+room.w
and spawn.y >= room.y and spawn.y <= room.y+room.h then
spawn.x = spawn.x - room.x
spawn.y = spawn.y - room.y
spawn.room = room.id
end
end
end
end
function WorldData:getRooms()
return self._rooms
end
function WorldData:getSpawns()
return self._spawns
end
function WorldData:getTile(x, y)
return self._fgtiles[x + y*self._xtiles + 1],
self._bgtiles[x + y*self._xtiles + 1]
end
function WorldData:getRoom(id)
for i,v in ipairs(self._rooms) do
if v.id == id then
return v
end
end
end
function WorldData:getHourglassCount()
return self._hourglasses
end
return WorldData