forked from UsernameIsInUse/courtengine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
138 lines (122 loc) · 4.27 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
require "config" -- controls text file
require "code/events/index"
require "code/utils/index"
require "code/screens/index"
require "code/assets"
require "code/episode"
require "code/scene"
require "code/scriptloader"
function love.load(arg)
InitGlobalConfigVariables()
love.window.setMode(WindowWidth, WindowHeight, {})
love.graphics.setDefaultFilter("nearest")
love.graphics.setLineStyle("rough")
Renderable = love.graphics.newCanvas(GraphicsWidth, GraphicsHeight)
ScreenShake = 0
DtReset = false -- so scene load times don't factor into dt
LoadAssets()
Episode = NewEpisode(settings.episode_path)
local arguments = {}
local argIndex = 1
-- First pass through the arguments to see what we're requesting
while argIndex <= #arg do
if arg[argIndex] == "debug" then
arguments.debug = true
argIndex = argIndex + 1
else
arguments[arg[argIndex]] = arg[argIndex + 1]
argIndex = argIndex + 2
end
end
-- Initialize the game based on our arguments
if arguments.debug then
controls.debug = arguments.debug
end
if arguments.script ~= nil then
CurrentScene = NewScene(arguments.script)
CurrentScene:update(0)
else
-- Select the first scene in the loaded episode
CurrentScene = NewScene(Episode.scenes[1])
end
if arguments.skip ~= nil then
for i=1, tonumber(arguments.skip) do
table.remove(CurrentScene.stack, 1)
CurrentScene.currentEventIndex = CurrentScene.currentEventIndex + 1
end
elseif arguments.script == nil then
-- Title screen will take the player to the next scene on keypress
screens.title.displayed = true
-- This normally is triggered on keypress, but since we're showing
-- the title manually, call this manually too
screens.title.onDisplay()
end
end
-- love.update and love.draw get called 60 times per second
-- transfer the update and draw over to the current game scene
function love.update(dt)
if DtReset then
dt = 1/60
DtReset = false
end
Episode:update(dt)
end
function love.keypressed(key)
local currentDisplayedScreen
local nextScreenToDisplay
for screenName, screenConfig in pairs(screens) do
-- See if another screen is currently showing so we know whether
-- or other screens can be displayed
-- TODO: Is there a case where screens need to stack?
if screenConfig.displayed then
currentDisplayedScreen = screenName
end
if screenConfig.displayKey and key == screenConfig.displayKey and
(screenConfig.displayCondition == nil or screenConfig.displayCondition()) then
if screenName == currentDisplayedScreen then
screenConfig.displayed = false
else
nextScreenToDisplay = screenConfig
end
elseif screenConfig.displayed and screenConfig.onKeyPressed then
screenConfig.onKeyPressed(key)
end
end
if nextScreenToDisplay and currentDisplayedScreen == nil then
nextScreenToDisplay.displayed = true
if nextScreenToDisplay.onDisplay then
nextScreenToDisplay.onDisplay()
end
end
end
function love.draw()
love.graphics.setColor(unpack(colors.white))
love.graphics.setCanvas(Renderable)
love.graphics.clear(unpack(colors.black))
CurrentScene:draw()
love.graphics.setCanvas()
local dx,dy = 0,0
if ScreenShake > 0 then
dx = love.math.random()*choose{1,-1}*2
dy = love.math.random()*choose{1,-1}*2
end
love.graphics.setColor(unpack(colors.white))
love.graphics.draw(
Renderable,
dx*love.graphics.getWidth()/GraphicsWidth,
dy*love.graphics.getHeight()/GraphicsHeight,
0,
love.graphics.getWidth()/GraphicsWidth,
love.graphics.getHeight()/GraphicsHeight
)
for screenName, screenConfig in pairs(screens) do
if screenConfig.displayed then
screenConfig.draw()
end
end
if controls.debug then
love.graphics.setColor(unpack(colors.red))
love.graphics.print(tostring(love.timer.getFPS( )), 10, 10)
love.graphics.setColor(unpack(colors.white))
end
end