summaryrefslogtreecommitdiffhomepage
path: root/main.lua
blob: b534665bf3daf0294c5313552f37ce524886deba (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
require "ground"
require "player"

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,0, 90,0, 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,0, 26,0, 26,30, -26,30}, "assets/platform_small.png"))
	table.insert(Platforms, Ground:new(world, 290/2-140, 180/2+50, {-26,0, 26,0, 26,30, -26,30}, "assets/platform_small.png"))
	table.insert(Platforms, Ground:new(world, 290/2, 180/2-50, {-17,0, 17,0, 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"
end

function love.update(dt)
	-- Put world in motion!
	world:update(dt)
	-- 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
	-- 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 ground
	for k,platform in pairs(Platforms) do
		love.graphics.setColor(255,255,255,255)
		love.graphics.draw(platform.sprite, platform.body:getX()-math.ceil(platform.sprite:getWidth()/2), platform.body:getY())
		if debug then
			love.graphics.setColor(220, 220, 220, 100)
			love.graphics.polygon("fill", platform.body:getWorldPoints(platform.shape:getPoints()))
		end
	end
	
	-- Draw player
	for k,naut in pairs(Nauts) do
		love.graphics.setColor(255,255,255,255)
		love.graphics.draw(naut.sprite, naut.current[naut.frame], naut.body:getX(), naut.body:getY(), naut.rotate, naut.facing, 1, 12, 15)
		if debug then
			love.graphics.setColor(50, 255, 50, 100)
			love.graphics.polygon("fill", naut.body:getWorldPoints(naut.shape:getPoints()))
			love.graphics.setColor(255,255,255,255)
			love.graphics.points(naut.body:getX()+12*naut.facing,naut.body:getY()-2)
			love.graphics.points(naut.body:getX()+6*naut.facing,naut.body:getY()+2)
			love.graphics.points(naut.body:getX()+18*naut.facing,naut.body:getY()+2)
			love.graphics.points(naut.body:getX()+12*naut.facing,naut.body:getY()+6)
		end
	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