summaryrefslogtreecommitdiffhomepage
path: root/main.lua
blob: bee3ef38e33b01480dfbc7596da3be684e4f41d4 (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
-- "NOTNAUTS"
-- WHOLE CODE HAS FLAG OF "need a cleanup"

require "ground"
require "player"
require "camera"

debug = false

function love.load ()
	-- Graphics
	love.graphics.setBackgroundColor(189, 95, 93)
	love.graphics.setDefaultFilter("nearest", "nearest")
	
	-- World physics
	love.physics.setMeter(64)
	world = love.physics.newWorld(0, 9.81*64, true)
	world:setCallbacks(beginContact, endContact, preSolve, postSolve)
	
	-- Platforms (`Ground`)
	Platforms = {}
	table.insert(Platforms, Ground:new(world, 290/2, 180/2, {-91,1, 90,1, 90,10, 5,76, -5,76, -91,10}, "assets/platform_big.png"))
	table.insert(Platforms, Ground:new(world, 290/2+140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png"))
	table.insert(Platforms, Ground:new(world, 290/2-140, 180/2+50, {-26,1, 26,1, 26,30, -26,30}, "assets/platform_small.png"))
	table.insert(Platforms, Ground:new(world, 290/2, 180/2-50, {-17,1, 17,1, 17,17, -17,17}, "assets/platform_top.png"))
	
	-- Nauts (`Player`)
	Nauts = {}
	table.insert(Nauts, Player:new(world, 290/2-10, 180/2 - 80, "assets/leon.png"))
	table.insert(Nauts, Player:new(world, 290/2+10, 180/2 - 80, "assets/lonestar.png"))
	
	-- Temporary settings for second player
	Nauts[2].name = "Player2"
	Nauts[2].key_left = "a"
	Nauts[2].key_right = "d"
	Nauts[2].key_up = "w"
	Nauts[2].key_down = "s"
	Nauts[2].key_jump = "h"
	Nauts[2].key_hit = "g"
	
	-- Camera
	camera = Camera:new()
end

function love.update (dt)
	-- Put world in motion!
	world:update(dt)
	camera:moveFollow()
	-- Players
	for k,naut in pairs(Nauts) do
		naut:update(dt)
	end
end

function love.keypressed (key)
	-- Switch hitbox display on/off
	if key == "x" then
		debug = not debug
	end
	--
	if key == "z" then
		camera.scale = (camera.scale % 4) + 1
	end
	-- Players
	for k,naut in pairs(Nauts) do
		naut:keypressed(key)
	end
end

function love.keyreleased(key)
	-- Players
	for k,naut in pairs(Nauts) do
		naut:keyreleased(key)
	end
end

function love.draw ()
	-- Draw SOME background
	-- I'm already bored with solid color!
	love.graphics.setColor(193, 100, 99, 255)
	love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()*0.25)
	love.graphics.setColor(179, 82, 80, 255)
	love.graphics.rectangle("fill", 0, love.graphics.getHeight()*0.8, love.graphics.getWidth(), love.graphics.getHeight()*0.2)
	
	local offset_x, offset_y = camera:getOffsets()
	local scale = camera.scale
	-- Draw ground
	for k,platform in pairs(Platforms) do
		platform:draw(offset_x, offset_y, scale, debug)
	end
	
	-- Draw player
	for k,naut in pairs(Nauts) do
		naut:draw(offset_x, offset_y, scale, debug)
	end
end

function beginContact (a, b, coll)
	local x,y = coll:getNormal()
	if y == -1 then
		print(b:getUserData().name .. " is not in air")
		b:getUserData().inAir = false
		b:getUserData().jumpdouble = true
	end
end

function endContact (a, b, coll)
	print(b:getUserData().name .. " is in air")
	b:getUserData().inAir = true
end