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

-- Pretend you didn't see this
-- This is work for scene manager
Scene = nil
function changeScene(scene)
	if Scene ~= nil then
		Scene:delete()
	end
	Scene = scene
end

-- Should be moved to scene/camera
function getScale()
	return getRealScale()
	--return math.max(1, math.floor(love.graphics.getWidth() / 320)-1, math.floor(love.graphics.getHeight() / 180)-1)
end
function getRealScale()
	return math.max(1, math.floor(math.max(love.graphics.getWidth() / 320, love.graphics.getHeight() / 180)))
end
-- Should be moved to Sprite metaclass (non-existent yet)
function newImage(path)
	local imagedata = love.image.newImageData(path)
	local transparency = function(x, y, r, g, b, a)
		if (r == 0 and g == 128 and b == 64) or
		   (r == 0 and g == 240 and b ==  6) then
			a = 0
		end
		return r, g, b, a
	end
	imagedata:mapPixel(transparency)
	local image = love.graphics.newImage(imagedata)
	return image
end

-- Require
require "world"
require "camera"
require "menu"
require "controller"
require "music"

-- Temporary debug
debug = false

-- Load
function love.load()
	-- Graphics
	love.graphics.setBackgroundColor(90, 90, 90)
	love.graphics.setDefaultFilter("nearest", "nearest")

	-- Font
	Font = love.graphics.newImageFont("assets/font-normal.png", " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,:;-_/\\!@#$%^&*?=+~`|'\"()[]{}<>", -1)
	Bold = love.graphics.newImageFont("assets/font-big.png", " 0123456789AEFILNORSTUW", -2)
	Font:setLineHeight(9/16)
	love.graphics.setFont(Font)

	-- Menu bijaczes
	m = Menu:new()

	-- Controllers
	Controllers = {}
	table.insert(Controllers, Controller:new())
	table.insert(Controllers, Controller:new(nil, "a", "d", "w", "s", "g", "h"))
	m:assignController(Controllers[1])
	m:assignController(Controllers[2])

	-- Scene
	changeScene(m)
end

-- Gamepad
function love.joystickadded(joystick)
	love.joystick.loadGamepadMappings("gamecontrollerdb.txt")
	table.insert(Controllers, Controller:new(joystick, "dpleft", "dpright", "dpup", "dpdown", "a", "b"))
	m:assignController(Controllers[#Controllers])
end

function love.gamepadpressed(joystick, button)
	print(button, "pressed")
	for _,controller in pairs(Controllers) do
		controller:gamepadpressed(joystick, button)
	end
end

function love.gamepadreleased(joystick, button)
	print(button, "released")
	for _,controller in pairs(Controllers) do
		controller:gamepadreleased(joystick, button)
	end
end

-- Update
function love.update(dt)
	Scene:update(dt)
end

-- KeyPressed
function love.keypressed(key)
	-- Controllers
	for _,controller in pairs(Controllers) do
		controller:keypressed(key)
	end
	-- Misc global input
	if key == "f5" then
		debug = not debug
	end
	if key == "escape" or key == "f1" then
		love.event.quit()
	end
	if key == "f6" and debug then
		local map = Scene:getMapName()
		local nauts = {}
		for _,naut in pairs(Scene:getNautsAll()) do
			table.insert(nauts, {naut.name, naut.controller})
		end
		local new = World:new(map, nauts)
		changeScene(new)
	end
end

-- KeyReleased
function love.keyreleased(key)
	-- Controllers
	for _,controller in pairs(Controllers) do
		controller:keyreleased(key)
	end
end

-- Draw
function love.draw()
	Scene:draw()
	if debug then
		local scale = getScale()
		love.graphics.setColor(255, 0, 0, 255)
		love.graphics.print("Debug ON", 10, 10, 0, scale, scale)
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.print("Current FPS: "..tostring(love.timer.getFPS()), 10, 10+9*scale, 0, scale, scale)
	end
end