-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
330 lines (227 loc) · 7.76 KB
/
main.lua
File metadata and controls
330 lines (227 loc) · 7.76 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
local animation = require "animation"
local control = require "control"
local tiles = require "tiles"
local cameras = require "cameras"
local collision = require "collision"
local minimap = require "minimap"
local levels = require "levels"
local animators = {}
local controllers = {}
local players = {}
local mobs = {}
local mobCount = 0
local bullets = {}
local slashes = {}
local screen = {}
local map = nil
local camera
local mini = nil
local bulletSpriteSheet
local drawSlashes = false
debug_data = {}
local function create_player(map, x, y, keys)
local player = {}
local zero_spritesheet = dofile("resources/characters/zerox3.lua")
local animator = animation.animator(zero_spritesheet, player, screen)
animators[player] = animator
player.x = x
player.y = y
player.dx = 30
player.dy = 45
player.orientation = 1
player.state = "idle"
player.subState = {}
controllers[player] = control.player(player, map, keys)
return player
end
local FPSCAP = 60 -- change if you want higher/lower max fps
local lastframe
local function sleepIfPossible(dt)
if lastframe then
local slack = 1 / FPSCAP - (love.timer.getTime() - lastframe)
if slack > 0 then
love.timer.sleep(slack)
end
local now = love.timer.getTime()
local diff = now - lastframe
end
lastframe = love.timer.getTime()
end
local function createEntity(origin, brain)
local mob = brain.newInstance(origin)
local character = brain.character
if (character ~= nil) then
local mobAnimator = animation.animator(dofile(character), mob, screen)
animators[mob] = mobAnimator
end
local mobController = brain.controller(mob, players[1], map, screen)
controllers[mob] = mobController
return mob
end
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest", 1)
if arg[#arg] == "-debug" then require("mobdebug").start()
end
if arg[#arg] == "-ideadebug" then
package.path = [[/home/mofleury/.IdeaIC2017.2/config/plugins/Lua/mobdebug/?.lua;]] .. package.path
require("mobdebug").start()
end
love.window.setMode(1600, 1200, { highdpi = true })
screen.x = 0
screen.y = 0
screen.dx = love.graphics.getWidth() / 2
screen.dy = love.graphics.getHeight() / 2
local minimap_location = { x = 6 * screen.dx / 8, y = 6 * screen.dy / 8, dx = screen.dx / 8, dy = screen.dy / 8 }
local camera_window_width = 100
local camera_window = { x = screen.dx / 2 - camera_window_width / 2, y = screen.dy / 2 - 100, dx = camera_window_width, dy = 200 }
local skeleton = levels.generate_skeleton(1, 4, 10, 5, 5)
local cellBank = levels.resolveCellBank("cells_index")
local tileMap = levels.buildTileMap(skeleton, "resources/levels/cell", cellBank)
levels.print_skeleton(skeleton)
levels.print_tilemap(tileMap)
map = tiles.tilemapDirect(tileMap, "resources/levels", screen)
--map = tiles.tilemap("resources/levels/sandbox", "resources/levels", screen)
table.insert(players, create_player(map, screen.dx / 4, 100, { left = 'left', right = 'right', jump = 'a', dash = 's', shoot = 'd', slash = 'c' }))
camera = cameras.windowCamera(camera_window, screen, players[1])
mini = minimap.minimap(screen, map, players, minimap_location, 20, 140)
-- table.insert(players, create_player(map, screen.dx / 2 + 50, 400, { left = 'k', right = 'l', jump = 'q', dash = 'w' }))
end
local function newBullet(playerShotEvent)
local player = playerShotEvent.from
local ox, oy = control.actionOrigin(player)
local origin = { x = ox - 2, y = oy, orientation = playerShotEvent.orientation }
local bullet = createEntity(origin, require("controllers/bullet"))
bullets[bullet] = true
end
local function newSlash(playerSlashEvent)
local source = playerSlashEvent.from
local origin = { x = source.x, y = source.y, dx = 0, dy = 0 }
local slash = createEntity(origin, require("controllers/slash"))
slashes[slash] = true
end
local function endSlash(event)
local slash = event.from
controllers[slash] = nil
slashes[slash] = nil
end
local function destroyBullet(bullet)
controllers[bullet] = nil
animators[bullet] = nil
bullets[bullet] = nil
end
local function bulletLost(bulletLostEvent)
destroyBullet(bulletLostEvent.from)
end
local function destroyMob(mob)
controllers[mob] = nil
animators[mob] = nil
mobs[mob] = nil
mobCount = mobCount - 1
end
function love.update(dt)
-- debug_data.players = players
-- debug_data.screen = screen
-- debug_data.controllers = controllers
-- debug_data.animators = animators
if (mobCount < 2 and love.keyboard.isDown('f')) then
local player = players[1]
if (mobCount == 0) then
local mob = createEntity({ x = player.x + 100, y = player.y, orientation = player.orientation }, require("controllers/walker"))
mobs[mob] = true
mobCount = mobCount + 1
else
local mob = createEntity({ x = player.x + 100, y = player.y, orientation = player.orientation }, require("controllers/rabbit"))
mobs[mob] = true
mobCount = mobCount + 1
end
end
local allEvents = {}
debug_data.colliding = {}
for i, c in pairs(controllers) do
local events = c.update(dt)
if events ~= nil then
table.insert(allEvents, events)
end
end
for i, a in pairs(animators) do
a.update(dt)
end
for i, events in ipairs(allEvents) do
if events.playerShot then
newBullet(events.playerShot)
end
if events.bulletLost then
bulletLost(events.bulletLost)
end
if events.playerSlash then
newSlash(events.playerSlash)
end
if events.slashComplete then
endSlash(events.slashComplete)
end
end
debug_data.things = { events, mobs, bullets, slashes }
for b, i in pairs(bullets) do
for m, j in pairs(mobs) do
if collision.overlap(b, m) then
destroyBullet(b)
destroyMob(m)
end
end
end
for b, i in pairs(slashes) do
for m, j in pairs(mobs) do
if collision.overlap(b, m) then
destroyMob(m)
end
end
end
camera.update(dt)
sleepIfPossible(dt)
end
local function drawBox(b)
-- love.graphics.setColor(200, 200, 200)
love.graphics.rectangle('line', b.x - screen.x, screen.dy - (b.y + b.dy - screen.y), b.dx, b.dy)
end
local function deepPrint(t)
local function deepPrintWithGap(t, gap, buffer)
for key, value in pairs(t) do
if (type(value) == "table") then
table.insert(buffer, gap .. tostring(key))
deepPrintWithGap(value, gap .. " ", buffer)
else
table.insert(buffer, gap .. tostring(key) .. " = " .. tostring(value))
end
end
end
local b = {}
deepPrintWithGap(t, "", b)
love.graphics.print(table.concat(b, "\n"))
end
function love.draw()
-- love.graphics.translate(0, 500)
-- love.graphics.scale(0.5, 0.5)
love.graphics.scale(2, 2)
-- drawBox(camera.windowBox())
map.draw()
for i, a in pairs(animators) do
a.draw()
end
for i, a in pairs(debug_data.colliding) do
drawBox(a)
end
if (debug_data.around ~= nil) then
for i, a in pairs(debug_data.around) do
drawBox(a)
end
end
if drawSlashes then
for i, a in pairs(slashes) do
drawBox(i)
end
end
mini.draw()
deepPrint(debug_data)
debug_data = {}
love.graphics.print("FPS : " .. love.timer.getFPS(), screen.dx - 100, 20)
end