-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.lua
176 lines (132 loc) · 4.22 KB
/
world.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
if not Entity then require("entity") end
local bump = require('vendor/bump/bump')
json = json or require('vendor/dkjson')
local World = {}
World.__index = World
--Private Methods
local function addObstacle(self, x, y, w, h, z)
local obstacle = Entity(x, y, w, h, z)
obstacle.set('isObstacle', true)
self.obstacles[#self.obstacles+1] = obstacle
self.bump:add(obstacle, x, y, w, h)
end
local function drawBox(box, r,g,b)
local _r, _g, _b, _a = love.graphics.getColor()
local x, y, w, h = box.getBoundingBox()
love.graphics.setColor(r,g,b,70)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r,g,b)
love.graphics.rectangle("line", x+0.5, y+0.5, w-1, h-1)
love.graphics.setColor(_r,_g,_b,_a)
end
local function zOrderSort (a, b)
return a.z > b.z
end
--Public Methods
function World.new()
local self = {}
setmetatable(self, World)
local contents, size = love.filesystem.read("assets/arena_highway.json")
self.data = json.decode(contents)
self.death_line = global.screen_height
self.background_image = love.graphics.newImage("assets/arena_highway_bg.png")
self.foreground_image = love.graphics.newImage("assets/arena_highway_fg.png")
self.timer = 0
self.tic_duration = 1
return self
end
-- broke init out of new so that we can pass references to world in lua.load
-- but initialize it when the game starts
function World:init()
self.entities = {}
self.drawables = {}
self.obstacles = {}
self.bump = bump.newWorld(32)
for i, v in pairs(self.data["layers"][2]["objects"]) do
addObstacle(self, v.x, v.y, v.width, v.height, global.z_orders.high_obstacle)
end
return self
end
function World:register(entity)
self.entities[entity.get("id")] = entity
-- store the entity ids so that we can use the
-- drawable table to look into the entities
-- table without making a new reference
table.insert(self.drawables, { id = entity.get("id"), z = entity.getZOrder() })
table.sort(self.drawables, zOrderSort)
if entity.onRegister then
entity.onRegister(self)
else
self.bump:add(entity, entity.getBoundingBox())
end
entity._unregister = function ()
world:unregister(entity)
end
end
function World:unregister(entity)
self.entities[entity.get("id")] = nil
-- we never remove from the drawables table
-- because we have no efficient way of finding
entity.cleanup()
if world.bump:hasItem(entity) then
self.bump:remove(entity)
end
end
function World:tic(dt)
self.timer = self.timer + dt
if self.timer > self.tic_duration then
for i, entity in pairs(self.entities) do
entity.tic()
end
self.timer = 0
end
end
function World:keypressed(key)
for i, entity in pairs(self.entities) do
if entity.keypressed then entity.keypressed(key) end
end
end
function World:keyreleased(key)
for i, entity in pairs(self.entities) do
if entity.keyreleased then entity.keyreleased(key) end
end
end
function World:update(dt)
self:tic(dt)
-- iterate over the entities
-- each of them that has queued a movement for this dt
-- should try to move
-- then resolve any collisions
for i, entity in pairs(self.entities) do
entity.update(dt, self)
end
end
function World:draw(dt)
local removes = { }
love.graphics.draw(self.background_image)
for i = 1, #(self.drawables) do
local drawable_id = self.drawables[i].id
if self.entities[drawable_id] then
self.entities[drawable_id].draw(dt)
else
-- oops! remember to remove it from the drawables table
table.insert(removes, i)
end
end
-- if we found anything old we'll remove and re-sort
if #removes > 0 then
for i = 1, #(removes) do
table.remove(self.drawables, removes[i])
end
table.sort(self.drawables, zOrderSort)
end
love.graphics.draw(self.foreground_image)
end
function World:serialize()
local data = {}
for i, entity in pairs(self.entities) do
table.insert(data, { entity.get("id"), entity.getX(), entity.getY() })
end
return data
end
return World