-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
136 lines (110 loc) · 3.3 KB
/
Copy pathmain.lua
File metadata and controls
136 lines (110 loc) · 3.3 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
--[[
main.lua — Composition Root
This is the single place where all modules are wired together.
No other module imports from here; all dependencies flow downward.
]]
require("src.Core.utils")
require("src.Core.constants")
local push = require("lib.push")
local Timer = require("lib.timer")
local AssetManager = require("src.Core.AssetManager")
local EventBus = require("src.Core.EventBus")
local Game = require("src.Controller.game")
local StateStack = require("src.Controller.stateStack")
local Actions = require("src.Controller.playerAction")
local GameView = require("src.View.states.gameView")
local EndGameView = require("src.View.states.endGameView")
local TransitionView = require("src.View.states.transitionView")
local assets
local eventBus
local game
local stateStack
local gameView
local function mouseProvider()
return push:toGame(love.mouse.getPosition())
end
local function gameStateQuery()
return game.gameState
end
local function startGame()
game:start()
gameView = GameView:new()
gameView:init(game.gameState, assets, eventBus, mouseProvider, gameStateQuery)
stateStack = StateStack:new()
stateStack:push(gameView)
end
local function startTransition(onMidpoint, onComplete)
stateStack:push(TransitionView:new(onMidpoint, onComplete))
end
local function restartGame()
startTransition(
function()
if gameView then
gameView:destroy()
end
game:start()
gameView = GameView:new()
gameView:init(game.gameState, assets, eventBus, mouseProvider, gameStateQuery)
stateStack.stack[1] = gameView
end,
function()
stateStack:pop()
end
)
end
local function showEndGame(didWin)
startTransition(
function()
-- Pass restartGame as callback to EndGameView
stateStack.stack[1] = EndGameView:new(didWin, restartGame, assets)
end,
function()
stateStack:pop()
end
)
end
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest")
love.window.setTitle("Scoundrel")
math.randomseed(os.time())
assets = AssetManager:new()
assets:load()
eventBus = EventBus:new()
game = Game:new(eventBus)
eventBus:subscribe("endGame", showEndGame)
startGame()
push:setupScreen(assets.WIDTH, assets.HEIGHT, assets.WIDTH * assets.SCALE, assets.HEIGHT * assets.SCALE, {
vsync = true,
fullscreen = false,
resizable = false
})
end
function love.update(dt)
assets:updateShaderTime()
Timer.update(dt)
stateStack:update(dt)
end
function love.mousepressed(x, y, button)
local mouseX, mouseY = push:toGame(x, y)
stateStack:onMouseClick(mouseX, mouseY)
end
function love.draw()
push:apply("start")
stateStack:render()
displayFPS()
push:apply("end", assets.shaders.CRT)
end
function displayFPS()
love.graphics.setFont(assets.fonts.small)
love.graphics.setColor(1, 1, 1, 0.8)
love.graphics.print("FPS: " .. tostring(love.timer.getFPS()), 15, 10)
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
end
if key == "u" then
print("Undoing last action")
eventBus:publish("action", Actions.UndoAction:new())
end
end