-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
114 lines (95 loc) · 2.6 KB
/
main.lua
File metadata and controls
114 lines (95 loc) · 2.6 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
----
-- main.lua
--
-- The entry-point of all Love2D games. This is where everything starts.
----
-- lock the global namespace so trying to create a global variable causes and error
local Global = require "libs.Global"
Global.lock()
-- add these values as global variables
Global( "util","lume","Class","Log","Game","Entity","xl" )
-- Initialize the values. Order is important here.
util = require "util"
lume = require "libs.lume"
Class = require "libs.class"
Log = require "xl.Log"
Game = require "MGame"
Entity = require "objects.Entity"
xl = require "extra"
-- load more libraries which we will need
local Signal = require "hump.signal"
local Gamestate = require "hump.gamestate"
local Timer = require "hump.timer"
local startup = require "startup"
local Keymap = require "xl.Keymap"
local MainMenu = require "state.MainMenu"
-- let's have some settings!
local Settings = require "SettingManager"
-- the (intended) framerate and world:update( dt ) time
local next_time = 0
-- Love2D callbacks
function love.load(args)
startup.sanityCheck()
Settings.processSettings( Settings.DefaultSettings )
if not Settings.loadSettings("settings.txt") then
xl.AddMessage("Invalid settings file")
end
startup.dothings()
Gamestate.switch( Game )
Gamestate.push( MainMenu )
next_time = love.timer.getTime()
end
function love.update( dt )
next_time = next_time + Settings.timestep
love.window.setTitle( "FPS: " .. love.timer.getFPS() )
Gamestate.update( dt )
Timer.update( dt )
end
function love.draw()
Gamestate.draw()
xl.DScreen.draw()
-- xl.TextInterface.draw()
xl.DrawMessages()
-- clear events after everything else
Keymap.clearEvents()
local cur_time = love.timer.getTime()
if next_time <= cur_time then
next_time = cur_time
return
end
love.timer.sleep(next_time - cur_time)
end
local function delegate_signal( name, ... )
if not Gamestate[name]( ... ) then
Signal.emit( name, ... )
end
end
local function delegate_keymap( name, ... )
if not Gamestate[name]( ... ) then
Signal.emit( name, ... )
Keymap[name]( ... )
end
end
-- args: key, isrepeat
function love.keypressed( key, isrepeat )
delegate_keymap( "keypressed", key, isrepeat )
end
-- args: key, isrepeat
function love.keyreleased( ... )
delegate_signal( "keyreleased", ... )
end
-- args: x, y, button
function love.mousepressed( ... )
delegate_signal( "mousepressed", ... )
end
-- args: x, y, button
function love.mousereleased( ... )
delegate_keymap( "mousereleased", ... )
end
-- args: w, h
function love.resize( ... )
Game:resize( ... )
if Gamestate.current() ~= Game then
delegate_signal( "resize", ... )
end
end