-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.lua
284 lines (257 loc) · 7.53 KB
/
game.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
require 'entities/player'
serpent = require 'lib.serpent'
anim8 = require 'lib/anim8'
gui = require 'lib.quickie'
game = {
title = 'APRI50',
seed = 14,
debug = false,
graphics = {
fullscreen = false,
mode = { }
},
map = {
size = 3000
},
speed = 1,
speed_text = '>',
map_debug = 0,
use_shaders = true,
show_sun = true,
dt = 0.05,
map_density = 1 / 15000,
gravity = 9.8,
time = 0, -- counts dt. 1 (about 50 ticks) is 1 hour
time_minutes = 15 * 0.075,
time_hours = 4 * 15 * 0.075,
time_days = 24 * 4 * 15 * 0.075,
renderer = require('renderers/default'),
fonts = { },
version = require('version'),
url = 'http://ananasblau.com/apri50',
dna_length = 24,
dna_chars = {'A', 'C', 'G', 'T'},
evolution_kits_to_start = 7,
colors = require('colors'),
shaders = { },
layers = {
buildings = 20,
plants = 30,
animals = 35,
machines = 40,
vehicles = 41,
player = 50
},
player = Player(),
icon_size = 32,
images = {},
save_filename = 'game.sav'
}
game.renderer.colors = game.colors
function game.randomDnaMatcher(places)
local dna = {}
for i=1, game.dna_length do
dna[i] = ' '
end
places = math.min(places, game.dna_length)
repeat
local n = love.math.random(1, game.dna_length)
if dna[n] == ' ' then
dna[n] = game.dna_chars[love.math.random(1, #game.dna_chars)]
places = places - 1
end
until places == 0
return dna
end
function game.randomDnaMatchers(how_many, places)
local dna = {}
for i=1, how_many do
table.insert(dna, game.randomDnaMatcher(places))
end
return dna
end
function game.randomDnaMatcherString(places)
return table.concat(game.randomDnaMatcher(places))
end
function game:shader(name)
if not self.shaders[name] then
print('Loading shader ' .. name)
self.shaders[name] = love.graphics.newShader('shader/' .. name .. '.glsl')
end
return self.shaders[name]
end
function game:image(path)
if not self.images[path] then
self.images[path] = love.graphics.newImage(path)
end
return self.images[path]
end
function game:scaledImage(path)
local image = game:image(path)
return image, {
x = game.graphics.mode.width / image:getWidth(),
y = game.graphics.mode.height / image:getHeight()
}
end
-- number_of_quads can be nil if the quads are in one row only
function game:imageWithQuads(image, number_of_quads)
local image = game:image(image)
if not number_of_quads then
number_of_quads = math.floor(image:getWidth() / image:getHeight())
end
local quads = {}
local size = image:getWidth() / number_of_quads
for i = 1, number_of_quads do
quads[i] = love.graphics.newQuad((i - 1) * size, 0, size, size, size * number_of_quads, size)
end
return image, quads
end
function game:quadFromImage(image_name, quad_number, number_of_quads)
local image, quads = game:imageWithQuads(image_name, number_of_quads)
assert(quads[quad_number], 'quad ' .. quad_number .. ' not found in image ' .. image_name)
return image, quads[quad_number]
end
function game.createAnimation(image_path, grid_options, animation_options)
local image = game:image(image_path)
local grid = anim8.newGrid(grid_options[1], grid_options[2], image:getWidth(), image:getHeight())
local animation = anim8.newAnimation(grid(unpack(animation_options[2])), animation_options[3])
return {
draw = function(self, x, y)
self.animation:draw(self.image, x or 0, y or 0)
end,
image = image,
animation = animation
}
end
function game:createFonts(offset)
local font_file = 'fonts/Comfortaa-Regular.ttf'
local mono_font = 'fonts/LiberationMono-Bold.ttf'
self.fonts = {
lineHeight = (14 + offset) * 1.7,
small = love.graphics.newFont(font_file, 10 + offset),
regular = love.graphics.newFont(font_file, 14 + offset),
large = love.graphics.newFont(font_file, 20 + offset),
very_large = love.graphics.newFont(font_file, 48 + offset),
mono_small = love.graphics.newFont(mono_font, 10 + offset),
mono_regular = love.graphics.newFont(mono_font, 16 + offset),
title = love.graphics.newFont(font_file, 128 + offset)
}
end
function game.setFont(font)
love.graphics.setFont(game.fonts[font])
end
function game:setMode(mode)
self.graphics.mode = mode
love.window.setMode(mode.width, mode.height, {fullscreen = mode.fullscreen or self.graphics.fullscreen})
if self.graphics.mode.height < 600 then
self:createFonts(-2)
else
self:createFonts(0)
end
end
function game.setState(state)
assert(state, 'game state missing')
game.log('Switching to state ' .. (state.name or state.__class.__name))
state.last_state = state.last_state or game.current_state
game.current_state = state
end
function game:startMenu()
love.mouse.setVisible(true)
game.current_state = StartMenu()
end
function game:start()
game.stopped = false
love.mouse.setVisible(true)
require('game_plays.colony')
if false and love.filesystem.exists(game.save_filename) then
game:load()
else
game.game_play = GamePlay.Colony()
end
game.setState(game.game_play.map_state)
game.renderer.map_view = game.current_state.view
--game:save()
end
function game:mutator()
game.current_state = Mutator()
end
function game:showCredits()
game.current_state = State(self, 'Credits', CreditsView())
end
function game.log(message)
if not game.log_file then
game.log_file = love.filesystem.newFile('apri50.log', 'a')
end
message = '[' .. game:timeInWords() .. '] ' .. message
print(message)
game.log_file:write(message .. "\r\n")
end
function game.tickTime(dt)
game.time = game.time + dt * game.speed
end
function game:timeInWords()
self.time_string = ''
local t = self.time
local days = ((math.floor(t / game.time_days)) % 365) + 1
if days > 0 then
self.time_string = 'Day ' .. days .. ' '
end
local hours = (math.floor(t / game.time_hours)) % 24
local minutes = (math.floor(t / game.time_minutes) * 15) % 60
self.time_string = self.time_string .. ' ' .. hours .. ':' .. minutes .. 'hrs'
return self.time_string
end
setmetatable(game, {
__serialize = function(self)
return {
game_play = self.game_play,
time = self.time,
map = self.map
}
end
})
function game.recursiveMerge(destination, source, seen)
local seen = seen and seen or {}
if type(source) ~= 'table' then
seen[source] = true
return source
end
for k, v in pairs(source) do
destination[k] = nil
if type(v) == 'table' then
if not seen[v] then
if v.__deserialize then
local func = v.__deserialize
v.__deserialize = nil
local args = game.recursiveMerge({}, v, seen)
destination[k] = func(args)
else
seen[v] = true
if type(v) == 'table' then
destination[k] = game.recursiveMerge({}, v, seen)
else
destination[k] = v
end
end
end
else
destination[k] = game.recursiveMerge({}, v, seen)
end
end
return destination
end
function game:load()
--game = Tserial.unpack(love.filesystem.read(game.save_filename or 'game.sav'), true)
game.save_filename = game.save_filename or 'game.sav'
game.log('Loading game ' .. self.save_filename)
local file, length = love.filesystem.read(self.save_filename)
local tmp = loadstring(file)()
--local ok, tmp = serpent.load(file)
game.recursiveMerge(game, tmp)
game.log('Game loaded')
end
function game:save()
game.save_filename = game.save_filename or 'game.sav'
love.filesystem.write(game.save_filename, serpent.dump(game, {nocode = false, indent=' '}))
game.log('Game saved to ' .. game.save_filename)
end