-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
148 lines (97 loc) · 2.49 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
-- Imports
local BadVersionScreen = require("src.BadVersionScreen")
local json = require("json")
local Vec2 = require("src.Vector2")
local Game = require("src.Game")
local Display = require("src.Display")
-- Consts
local _VERSION = {love.getVersion()}
-- Vars
_Time = 0
_MousePos = Vec2()
_Game = nil
_Display = nil
function love.load()
if _VERSION[1] < 12 and false then
_Game = BadVersionScreen()
else
_Game = Game()
_Game:init()
_Display = Display()
end
end
function love.update(dt)
_Time = _Time + dt
_MousePos = Vec2(love.mouse.getPosition())
_Display:update(dt)
_Game:update(dt)
end
function love.draw()
_Game:draw()
love.graphics.setCanvas()
end
function love.mousepressed(x, y, button)
_Game:mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
_Game:mousereleased(x, y, button)
end
function love.keypressed(key)
_Game:keypressed(key)
end
function love.focus(focus)
_Game:focus(focus)
end
function love.quit()
_Game:quit()
end
-- The functions below have been copied from OpenSMCE, another project that I've created!
function _GetRainbowColor(t)
t = t * 3
local r = math.min(math.max(2 * (1 - math.abs(t % 3)), 0), 1) + math.min(math.max(2 * (1 - math.abs((t % 3) - 3)), 0), 1)
local g = math.min(math.max(2 * (1 - math.abs((t % 3) - 1)), 0), 1)
local b = math.min(math.max(2 * (1 - math.abs((t % 3) - 2)), 0), 1)
return {r, g, b}
end
function _LoadFile(path)
local file, err = io.open(path, "r")
if not file then
return
end
io.input(file)
local contents = io.read("*a")
io.close(file)
return contents
end
function _LoadJson(path)
local contents = _LoadFile(path)
assert(contents, string.format("Could not JSON-decode: %s, file does not exist", path))
local success, data = pcall(function() return json.decode(contents) end)
assert(success, string.format("JSON error: %s: %s", path, data))
assert(data, string.format("Could not JSON-decode: %s, error in file contents", path))
return data
end
function _SaveFile(path, data)
local file = io.open(path, "w")
assert(file, string.format("SAVE FILE FAIL: %s", path))
io.output(file)
io.write(data)
io.close(file)
end
function _SaveJson(path, data)
_SaveFile(path, json.encode(data))
end
function _StrSplit(s, k)
local t = {}
local l = k:len()
while true do
local n = s:find("%" .. k)
if n then
table.insert(t, s:sub(1, n - 1))
s = s:sub(n + l)
else
table.insert(t, s)
return t
end
end
end