summaryrefslogtreecommitdiffhomepage
path: root/world.lua
blob: 01fed0b0721ba606ef9564b9c616c9849c81a149 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
-- `World`
-- Used to manage physical world and everything inside it: clouds, platforms, nauts, background etc.

-- WHOLE CODE HAS FLAG OF "need a cleanup"

require "ground"
require "player"
require "cloud"
require "effect"
require "decoration"
require "ray"

-- Metatable of `World`
-- nils initialized in constructor
World = {
	-- inside
	world = nil,
	Nauts = nil,
	Platforms = nil,
	Clouds = nil,
	Decorations = nil,
	Effects = nil,
	Rays = nil,
	camera = nil,
	-- cloud generator
	clouds_delay = 5,
	-- Map
	map = nil,
	background = nil,
	-- Gameplay status
	lastNaut = false,
	-- "WINNER"
	win_move = 0
}

-- Constructor of `World` ZA WARUDO!
function World:new(map, ...)
	-- Meta
	local o = {}
	setmetatable(o, self)
	self.__index = self
	-- Physical world initialization
	love.physics.setMeter(64)
	o.world = love.physics.newWorld(0, 9.81*64, true)
	o.world:setCallbacks(o.beginContact, o.endContact)
	-- Empty tables for objects
	local n = {}
	o.Nauts = n
	local p     = {}
	o.Platforms = {}
	local c  = {}
	o.Clouds = c
	local e   = {}
	o.Effects = e
	local d = {}
	o.Decorations = d
	local r = {}
	o.Rays = r
	-- Random init
	math.randomseed(os.time())
	-- Map
	local map = map or "default"
	o:loadMap(map)
	-- Nauts
	o:spawnNauts(...)
	-- Create camera
	o.camera = Camera:new(o)
	return o
end

-- The end of the world
function World:delete()
	self.world:destroy()
	for _,platform in pairs(self.Platforms) do
	 	platform:delete()
	end
	for _,naut in pairs(self.Nauts) do
		naut:delete()
	end
	self = nil
end

-- Load map from file
function World:loadMap(name)
	local name = name or "default"
	name = "maps/" .. name .. ".lua"
	local map = love.filesystem.load(name)
	self.map = map()
	-- Platforms
	for _,platform in pairs(self.map.platforms) do
		self:createPlatform(platform.x, platform.y, platform.shape, platform.sprite)
	end
	-- Decorations
	for _,decoration in pairs(self.map.decorations) do
		self:createDecoration(decoration.x, decoration.y, decoration.sprite)
	end
	-- Background
	self.background = love.graphics.newImage(self.map.background)
	-- Clouds
	if self.map.clouds then
		for i=1,6 do
			self:randomizeCloud(false)
		end
	end
end

-- Spawn all the nauts for the round
function World:spawnNauts(...)
	local params = {...}
	local nauts = nil
	if type(params[1][1]) == "table" then
		nauts = params[1]
	else
		nauts = params
	end
	for _,naut in pairs(nauts) do
		local x,y = self:getSpawnPosition()
		local spawn = self:createNaut(x, y, naut[1])
		spawn:assignController(naut[2])
	end
end

-- Get respawn location
function World:getSpawnPosition()
	local n = math.random(1, #self.map.respawns)
	return self.map.respawns[n].x, self.map.respawns[n].y
end

-- Add new platform to the world
function World:createPlatform(x, y, polygon, sprite)
	table.insert(self.Platforms, Ground:new(self, self.world, x, y, polygon, sprite))
end

-- Add new naut to the world
function World:createNaut(x, y, name)
	local naut = Player:new(self, self.world, x, y, name)
	table.insert(self.Nauts, naut)
	return naut
end

-- Add new decoration to the world
function World:createDecoration(x, y, sprite)
	table.insert(self.Decorations, Decoration:new(x, y, sprite))
end

-- Add new cloud to the world
function World:createCloud(x, y, t, v)
	table.insert(self.Clouds, Cloud:new(x, y, t, v))
end

-- Randomize Cloud creation
function World:randomizeCloud(outside)
	if outside == nil then
		outside = true
	else
		outside = outside
	end
	local x,y,t,v
	local m = self.map
	if outside then
		x = m.center_x-m.width*1.2+math.random(-50,20)
	else
		x = math.random(m.center_x-m.width/2,m.center_x+m.width/2)
	end
	y = math.random(m.center_y-m.height/2, m.center_y+m.height/2)
	t = math.random(1,3)
	v = math.random(8,18)
	self:createCloud(x, y, t, v)
end

-- Add an effect behind nauts
function World:createEffect(name, x, y)
	table.insert(self.Effects, Effect:new(name, x, y))
end

-- Add a ray
function World:createRay(naut)
	table.insert(self.Rays, Ray:new(naut, self))
end

-- get Nauts functions
-- more than -1 lives
function World:getNautsPlayable()
	local nauts = {}
	for _,naut in pairs(self.Nauts) do
		if naut.lives > -1 then
			table.insert(nauts, naut)
		end
	end
	return nauts
end
-- are alive
function World:getNautsAlive()
	local nauts = {}
	for _,naut in self.Nauts do
		if naut.alive then
			table.insert(nauts, naut)
		end
	end
	return nauts
end
-- all of them
function World:getNautsAll()
	return self.Nauts
end

-- get Map name
function World:getMapName()
	return self.map.name
end

-- Event: when player is killed
function World:onNautKilled(naut)
	self.camera:startShake()
	self:createRay(naut)
	local nauts = self:getNautsPlayable()
	if self.lastNaut then
		local m = Menu:new()
		for _,controller in pairs(Controllers) do
			m:assignController(controller)
		end
		changeScene(m)
	elseif #nauts < 2 then
		self.lastNaut = true
	end
end

-- Update ZU WARUDO
function World:update(dt)
	-- Physical world
	self.world:update(dt)
	-- Camera
	self.camera:update(dt)
	-- Nauts
	for _,naut in pairs(self.Nauts) do
		naut:update(dt)
	end
	-- Clouds
	if self.map.clouds then
		-- generator
		local n = table.getn(self.Clouds)
		self.clouds_delay = self.clouds_delay - dt
		if self.clouds_delay < 0 and
		   n < 18
		then
			self:randomizeCloud()
			self.clouds_delay = self.clouds_delay + World.clouds_delay -- World.clouds_delay is initial
		end
		-- movement
		for _,cloud in pairs(self.Clouds) do
			if cloud:update(dt) > 340 then
				table.remove(self.Clouds, _)
			end
		end
	end
	-- Effects
	for _,effect in pairs(self.Effects) do
		if effect:update(dt) then
			table.remove(self.Effects, _)
		end
	end
	-- Rays
	for _,ray in pairs(self.Rays) do
		if ray:update(dt) then
			table.remove(self.Rays, _)
		end
	end
	-- Bounce `winner`
	self.win_move = self.win_move + dt
	if self.win_move > 2 then
		self.win_move = self.win_move - 2
	end
end

function World:getBounce(f)
	local f = f or 1
	return math.sin(self.win_move*f*math.pi)
end

-- Draw
function World:draw()
	-- Camera stuff
	local offset_x, offset_y = self.camera:getOffsets()
	local scale = self.camera.scale
	local scaler = self.camera.scaler
	
	-- Background
	love.graphics.draw(self.background, 0, 0, 0, scaler, scaler)
	
	-- This needs to be reworked!
	-- Draw clouds
	for _,cloud in pairs(self.Clouds) do
		cloud:draw(offset_x, offset_y, scale)
	end

	-- Draw decorations
	for _,decoration in pairs(self.Decorations) do
		decoration:draw(offset_x, offset_y, scale)
	end

	-- Draw effects
	for _,effect in pairs(self.Effects) do
		effect:draw(offset_x,offset_y, scale)
	end

	-- Draw player
	for _,naut in pairs(self.Nauts) do
		naut:draw(offset_x, offset_y, scale, debug)
	end

	-- Draw ground
	for _,platform in pairs(self.Platforms) do
		platform:draw(offset_x, offset_y, scale, debug)
	end

	-- Draw rays
	for _,ray in pairs(self.Rays) do
		ray:draw(offset_x, offset_y, scale)
	end

	-- draw center
	if debug then
		local c = self.camera
		local w, h = love.graphics.getWidth(), love.graphics.getHeight()
		love.graphics.setColor(130,130,130)
		love.graphics.setLineWidth(1)
		love.graphics.setLineStyle("rough")
		local cx, cy = c:getPositionScaled()
		local x1, y1 = c:translatePosition(self.map.center_x, cy)
		local x2, y2 = c:translatePosition(self.map.center_x, cy+h)
		love.graphics.line(x1,y1,x2,y2)
		local x1, y1 = c:translatePosition(cx, self.map.center_y)
		local x2, y2 = c:translatePosition(cx+w, self.map.center_y)
		love.graphics.line(x1,y1,x2,y2)
		love.graphics.setColor(200,200,200)
		love.graphics.setLineStyle("rough")
		local cx, cy = c:getPositionScaled()
		local x1, y1 = c:translatePosition(0, cy)
		local x2, y2 = c:translatePosition(0, cy+h)
		love.graphics.line(x1,y1,x2,y2)
		local x1, y1 = c:translatePosition(cx, 0)
		local x2, y2 = c:translatePosition(cx+w, 0)
		love.graphics.line(x1,y1,x2,y2)
	end

	-- Draw HUDs
	for _,naut in pairs(self.Nauts) do
		-- I have no idea where to place them T_T
		-- let's do: bottom-left, bottom-right, top-left, top-right
		local w, h = love.graphics.getWidth()/scale, love.graphics.getHeight()/scale
		local y, e = 1, 1
		if _ < 3 then y, e = h-33, 0 end
		naut:drawHUD(1+(_%2)*(w-34), y, scale, e)
	end
	
	-- Draw winner
	if self.lastNaut then
		local w, h = love.graphics.getWidth()/scale, love.graphics.getHeight()/scale
		local angle = self:getBounce(2)
		local dy = self:getBounce()*3
		love.graphics.setFont(Bold)
		love.graphics.printf("WINNER",(w/2)*scale,(42+dy)*scale,336,"center",(angle*5)*math.pi/180,scale,scale,168,12)
		love.graphics.setFont(Font)
		love.graphics.printf("rofl, now kill yourself", w/2*scale, 18*scale, 160, "center", 0, scale, scale, 80, 3)
	end
end

-- beginContact
function World.beginContact(a, b, coll)
	if a:getCategory() == 1 then
		local x,y = coll:getNormal()
		if y < -0.6 then
			print(b:getUserData().name .. " is not in air")
			-- Move them to Player
			b:getUserData().inAir = false
			b:getUserData().jumpnumber = 2
			b:getUserData().salto = false
			b:getUserData():createEffect("land")
		end
		local vx, vy = b:getUserData().body:getLinearVelocity()
		if math.abs(x) == 1 or (y == -1 and x == 0) then
			b:getUserData():playSound(3)
		end
	end
end

-- endContact
function World.endContact(a, b, coll)
	if a:getCategory() == 1 then
		print(b:getUserData().name .. " is in air")
		-- Move them to Player
		b:getUserData().inAir = true
	end
end