-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.lua
executable file
·162 lines (133 loc) · 3.71 KB
/
global.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
local global = {}
local HC = require "hardoncollider"
local HCShapes = require "hardoncollider.shapes"
local Grid = require "grid"
local function onCollision(dt, a, b, dx, dy)
a.entity:onCollision(b.entity, dx, dy)
b.entity:onCollision(a.entity, -dx, -dy)
end
function global.addHitbox(obj, x, y, w, h)
local hitbox = global.collider:addRectangle(x, y, w, h)
hitbox.entity = obj
return hitbox
end
function global.removeHitbox(hitbox)
global.collider:remove(hitbox)
end
function global.setGhost(hitbox)
global.collider:setGhost(hitbox)
end
function global.setSolid(hitbox)
global.collider:setSolid(hitbox)
end
function global.drawHitboxes()
for h, _ in pairs(global.entities) do
if h.hitbox ~= nil then
h.hitbox:draw("line")
end
end
end
function global.addEntity(obj)
global.entities[obj] = obj
local class = obj.class.name:lower() .. "s"
if global[class] ~= nil then
global[class][obj] = obj
end
end
function global.removeEntity(obj)
global.entities[obj] = nil
local class = obj.class.name:lower() .. "s"
if global[class] ~= nil then
global[class][obj] = nil
end
end
function global.addDrawable(obj)
global.addEntity(obj)
table.insert(global.drawables, obj)
end
function global.removeDrawable(obj)
global.removeEntity(obj)
global.destroyedDrawables = global.destroyedDrawables or {}
global.destroyedDrawables[obj] = obj
end
function global.correctDrawables()
if global.destroyedDrawables then
local newDrawables = {}
for _, d in ipairs(global.drawables) do
if not global.destroyedDrawables[d] then
table.insert(newDrawables, d)
end
end
global.drawables = newDrawables
end
global.destroyedDrawables = nil
table.sort(global.drawables,
function (d1, d2) return d1.layer < d2.layer end)
end
function global.collidablesAt(x, y)
local collidables = {}
for _, shape in pairs(global.collider:shapesAt(x, y)) do
collidables[shape.entity] = shape.entity
end
return collidables
end
function global.collidablesUnder(x0, y0, x1, y1)
local collidables = {}
if x0 == x1 or y0 == y1 then
return collidables
end
local rect = HCShapes.newPolygonShape(x0, y0, x1, y0, x1, y1, x0, y1)
if x0 > x1 then
x0, x1 = x1, x0
end
if y0 > y1 then
y0, y1 = y1, y0
end
for _, shape in pairs(global.collider:shapesInRange(x0, y0, x1, y1)) do
if shape:collidesWith(rect) then
collidables[shape.entity] = shape.entity
end
end
return collidables
end
function global.countSelected()
local selcount = 0
for _, h in pairs(humans) do
if h.selected then
selcount = selcount + 1
end
end
return selcount
end
function global.log(str)
if global.messagenum == global.maxmessages then
local msgs = {}
for i = 1, global.messagenum - 1 do
msgs[i] = global.messages[i + 1]
end
global.messages = msgs
global.messagenum = global.messagenum - 1
end
global.messagenum = global.messagenum + 1
global.messages[global.messagenum] = str
end
function global.reset()
global.entities = {}
global.drawables = {}
global.destroyedDrawables = nil
global.humans = {}
global.zombies = {}
global.killedZombies = 0
global.grid = Grid(86, 48, 15)
global.debug = false
global.showHelp = true
global.endGame = false
global.collider = HC(100, onCollision)
global.messages = {}
global.messagenum = 0
global.maxmessages = 10
global.zSpawnRate = 0.25
global.maxZRate = 7
global.totalTime = 0
end
return global