-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
513 lines (391 loc) · 11.4 KB
/
main.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
--[[
split code up into the smallest parts
export properties:
axes:
x > forward
z > up
globals
player info
level info
graphics state info
probs
billboards not scaling! why!
damn stil...
mod
non-clearing canvas
non depth testing
scramble verts in memory
wireframe mode
draw each mesh with only one color
use spritebatch to make it all
glitter
could use a shader to make atlas animations
thoughts
globals are good when used sparingly
i am going in the right direction with the input system.
it would be cool if I could systemetize it more
it would be cool if tiny ecs had a way to implement a system that could get called when-needed,
rather than every frame. I guess that's just the observer pattern
todo:
split pixel code and vertex code
related:
use system indexes to set order
pick test
have a "blocking" color for picktesting
use blendmode clear
use sorting for draw order
the clicktest might be mushy I should still try and figure out special canvases
structure
types are self - reminders
make shit make sense or else u are doomed
"stuff?" are you serious my brotha
ok something to think about: what thing is a thing part of.
for serious
have a strong organization
file for entities?
save data component
have a draw order component -
either on the systems or on entities
separate drawIndex and updateIndex
I like how the camera system basically extends the camera
]]
-- local models = [[
-- -- code here
-- return {rat, globe, car, walls, fish, burger}
-- ]]
-- models = loadstring(models)()
-- world:add(unpack(models))
------------------------------------------------
-- top
------------------------------------------------
local world
local lg = love.graphics
local g3d = require("lib/g3d")
local inspect = require("lib.inspect")
cpml = require("lib.cpml")
tiny = require("lib.tiny")
-------------------------------------------------
-- local helpers
-------------------------------------------------
local utils = require("stuff.utils")
local types = require("stuff.types")
local loadPositions = utils.loadPositions
local newBillboard = types.newBillboard
local newSB = types.newSB
local newArrow = types.newArrow
local newTween = types.newTween
local _ts = require("stuff.test_shaders")
local _bb = require("stuff.bb_shader")
local shaders = {
defaultShader = g3d.shader,
defaultbbShader = _bb,
solidColorShader = _ts.model,
solidColorbbShader = _ts.billboard,
}
_ts = nil
_bb = nil
-- generic callbacks
local callbacks = {
printName = function(self)
local name = self.name or "no name, "..tostring(self)
print("self = "..name)
end
}
local function sortSystems(before, after)
if after.index < before.index then
world:setSystemIndex(after, before.index+1)
-- world:setSystemIndex(before, after.index)
end
end
---------------------------------------
-- globals
---------------------------------------
gamestates = {PLAY = 1, PAUSED = 2}
gamestate = 1
function gamestates:switch()
if gamestate == gamestates.PAUSED then
gamestate = gamestates.PLAY
return
end
if gamestate == gamestates.PLAY then
gamestate = gamestates.PAUSED
return
end
end
levelPos = {} -- table
playerPos = -1 -- table index
graphicsState = {
-- active state, always have value
shader = nil,
bbShader = nil,
camera = nil,
-- does not always have to have a value
canvas = lg.newCanvas(),
-- flags
updateCanvasFlag = false,
}
do
local gs = graphicsState
gs.camera = g3d.camera
gs.camera.r = 0
shaders.defaultShader:send("projectionMatrix", gs.camera.projectionMatrix)
shaders.defaultbbShader:send("projectionMatrix", gs.camera.projectionMatrix)
shaders.solidColorShader:send("projectionMatrix", gs.camera.projectionMatrix)
shaders.solidColorbbShader:send("projectionMatrix", gs.camera.projectionMatrix)
gs.shader = shaders.defaultShader
gs.bbShader = shaders.defaultbbShader
end
-----------------------------------------
-- local systems
-----------------------------------------
local spin = tiny.processingSystem()
spin.updateSystem = true
spin.processPlaying = true
spin.filter = tiny.requireAll("spinning")
local a = 0
local twoPI = 2*math.pi
function spin:process(e, dt)
a = (a + dt/2) % twoPI
e:setRotation(0, 0, a)
end
local updateTweens = tiny.processingSystem()
updateTweens.updateSystem = true
updateTweens.processPlaying = true
updateTweens.filter = tiny.requireAll("tween")
function updateTweens:process(e, dt)
local tw = e.tween
if not tw.finished and tw.t < tw.endTime then
tw.t = tw.t + dt
e:setQuaternionRotation(tw.initial:lerp(tw.final, tw.t))
else
tw.finished = true
if type(tw.initial) == "cdata" then
-- e:setQuaternionRotation(tw.final)
end
end
end
local tweenRotation = tiny.processingSystem()
tweenRotation.updateSystem = true
tweenRotation.processPlaying = true
tweenRotation.filter = tiny.requireAll("tween", "mesh", "quat")
function tweenRotation:process(e, dt)
-- print(e)
-- if not e.tween.finished then
-- end
end
local updateArrows = tiny.system({
input = "",
arrows = {},
arrowHeight = 0.3
})
updateArrows.updateSystem = true
updateArrows.processPlaying = true
function updateArrows:onAddToWorld(world)
self.arrows[1] = newArrow()
self.arrows[2] = newArrow()
world:add(unpack(self.arrows))
end
function updateArrows:update(dt)
if self.input then
if self.input == "o" then
local current = levelPos[playerPos]
print(current)
local c = cpml.vec3(current)
-- 2 targets for 2 arrows
for i = 1, 2 do
local target = levelPos[current.cons[i]]
target = cpml.vec3(target)
local v = (target - c):normalize()
local f = c + (v * 0.1)
self.arrows[i]:lookAtFrom({f.x, f.y, self.arrowHeight}, target)
-- local v = (target - cam):normalize()
-- local f = cam + (v * 0.1)
-- self.arrows[i]:lookAtFrom({f.x, f.y, arrowHeight}, target)
-- self.arrows[i]:lookAtFrom({cam.x, cam.y, arrowHeight}, target)
-- update callback
self.arrows[i].point_i = current.cons[i]
end
end
self.input = nil
end
end
local drawMenu = require("systems.drawMenu")
local getClickable = require("systems.getClickable")
local drawWrap = require("systems.drawWrap")
local drawModels = require("systems.drawModels")
local drawModelsID = require("systems.drawModelsID")
-----------------------------------------
-- driver
-----------------------------------------
function love.load()
graphicsState.camera.r = 4.6
local rat = g3d.newModel("assets/rat.obj", "assets/Tex_Rat.png", {0,0,0,1})
rat.clickable = callbacks.printName
rat.name = "rat"
local globe = g3d.newModel("assets/sphere.obj", "assets/earth.png", {-1.3, -2.3, 0}, nil, 0.5):compress()
globe.spinning = true
globe.clickable = callbacks.printName
globe.name = "globe"
local car = g3d.newModel("assets/car.obj", "assets/1377 car.png", {2,-1,0}, {0,0.5,0}, 0.05):compress()
car.clickable = callbacks.printName
car.name = "car"
-- local walls = g3d.newModel("assets/level.obj", "assets/alexander ross.png"):compress()
local walls = g3d.newModel("assets/level2.obj", "assets/alexander ross.png"):compress()
local fish = g3d.newModel("assets/Goldfish.obj", "assets/1377 car.png", {-0.1, -0.6, 0.2}, nil, 0.5):compress()
fish.tween = newTween(2, cpml.quat.new(), cpml.quat.from_angle_axis(2, 0,0,1))
fish.clickable = callbacks.printName
fish.name = "fish"
local burger = newSB("assets/burger.png", {0.5,0,2})
-- -3.5, -4.8, 0.3
local campfire = g3d.newModel("assets/campfire.obj", "assets/campfire.png", {-3.5, -4.8, 0.1}, nil, 0.4):compress()
campfire.clickable = callbacks.printName
campfire.name = "campfire"
local door = g3d.newModel("assets/door.obj", "assets/campfire.png", {-1.1, -5.5, 0})
local tracks = {
font = lg.newFont("assets/owl-font.otf", 22)
}
local pauseUI = {
menu = {
{
label = "resume",
fn = function() gamestates:switch() end
},
{
label = "quit",
fn = love.event.quit
},
{
label = "wireframe",
fn = function() lg.setWireframe() end
},
},
font = lg.newFont(20),
x = 500,
y = 500
}
world = tiny.world(
pauseUI, tracks,
door, rat, globe, car, walls, fish, burger, campfire,
spin,
updateTweens,
updateArrows
)
for _, path in pairs(love.filesystem.getDirectoryItems("systems")) do
path = path:gsub("%.lua$", "")
world:addSystem(require("systems."..path))
end
world:refresh()
-- gets reference
local draw2D = require("systems.draw2D")
local drawMenu = drawMenu
sortSystems(draw2D, drawMenu)
-- path locations
local nodes = loadPositions("assets/path.obj")
-- spawnPoint index
local si = -1
-- billboard image
local bi = lg.newImage("assets/1377 car.png")
for i, v in ipairs(nodes) do
world:addEntity(newBillboard(bi, v))
if v.isSpawnPoint then
si = i
end
end
-- local sp = nodes[si]
-- for _, i in ipairs(sp.cons) do
-- world:addEntity(newBillboard(bi, nodes[i]))
-- end
graphicsState.camera.position = nodes[si]
levelPos = nodes
playerPos = si
end
local playingFilter = tiny.requireAll("updateSystem", "processPlaying")
local pausedFilter = tiny.requireAll("updatesytem", "processPaused")
local drawPlayingFilter = tiny.requireAll("drawSystem", tiny.rejectAny("processPaused"))
local drawPausedFilter = tiny.requireAll("drawSystem")
function love.update(dt)
if gamestate == gamestates.PLAY then
world:update(dt, playingFilter)
elseif gamestate == gamestates.PAUSED then
world:update(dt, pausedFilter)
end
end
function love.draw()
if gamestate == gamestates.PLAY then
world:update(nil, drawPlayingFilter)
elseif gamestate == gamestates.PAUSED then
world:update(nil, drawPausedFilter)
end
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
gamestates.switch()
elseif key == "q" then
love.event.quit()
end
drawMenu.input = key
updateArrows.input = key
if key == "p" then
local cam = graphicsState.camera.position
print(inspect(cam))
-- fish:lookAt(cam.position, nil)
end
end
local function pickTest(x, y)
assert(getClickable)
getClickable:update()
local len = #getClickable.entities
for i, e in pairs(getClickable.entities) do
e.ID = i / len
end
graphicsState.updateCanvasFlag = true
drawModels.active = false
drawModelsID.active = true
graphicsState.shader = shaders.solidColorShader
graphicsState.bbShader = shaders.solidColorbbShader
coroutine.yield()
drawModels.active = true
drawModelsID.active = false
graphicsState.shader = shaders.defaultShader
graphicsState.bbShader = shaders.defaultbbShader
print(assert(graphicsState.canvas))
local data = graphicsState.canvas:newImageData()
-- quietly failing here?
print(data)
local r, g, b, a = data:getPixel(x, y)
if r == 0 then
return
end
local min = math.huge
local en_i = -1
for i, e in ipairs(getClickable.entities) do
local d = math.abs(e.ID - r)
if d < min then
min = d
en_i = i
end
-- clear ID
e.ID = nil
end
local entity = getClickable.entities[en_i]
-- do callback
assert(type(entity.clickable) == "function")
entity:clickable()
end
function love.mousepressed(x, y, button, istouch, presses)
-- create a coroutine
local pt = coroutine.create(pickTest)
coroutine.resume(pt, x, y)
-- resume the coroutine after all draw operations
drawWrap.coroutine = pt
end
function love.resize(w, h)
local gs = graphicsState
gs.camera.aspectRatio = w/h
gs.camera.updateProjectionMatrix()
gs.shader:send("projectionMatrix", gs.camera.projectionMatrix)
gs.bbShader:send("projectionMatrix", gs.camera.projectionMatrix)
end
function love.quit()
print("quitting")
end