summaryrefslogtreecommitdiffhomepage
path: root/menu.lua
blob: 6f0681c5657236ea2f77262189ea16d69c73438d (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
-- `Menu`
-- It is one of the last things I will need to mess around with. I'm happy and surprised everything works so far.
-- For sure I have learnt a lot about lua during this journey. Still a lot ahead. I will continue writing in the same style though, to not make it even worse.

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

require "selector"

-- Metatable of `Menu`
Menu = {
	logo = nil,
	-- move selectors to one table; make functions to retrieve selectors w or w/o controller
	selectors = nil,
	selected = nil,
	nauts = require "nautslist",
	portrait_sprite = nil,
	portrait_sheet  = require "portraits",
	scale = 4,
	countdown = 3
}

-- Constructor of `Menu`
function Menu:new ()
	-- Meta
	local o = {}
	setmetatable(o, self)
	self.__index = self
	-- initialize
	o.logo = nil
	o.selectors = {}
	o.selected  = {}
	o.portrait_sprite = love.graphics.newImage("assets/portraits.png")
	return o
end

-- Naut selector
function Menu:newSelector()
	local selector = Selector:new(self)
	local w, h = love.graphics.getWidth()/self.scale, love.graphics.getHeight()/self.scale
	local n = #self.selectors - 1
	table.insert(self.selectors, selector)
	local x = (w-79)/2+n*38
	local y = h/2-16
	selector:setPosition(x, y)
end

--
function Menu:draw()
	for _,selector in pairs(self.selectors) do
		selector:draw()
	end
	for _,selector in pairs(self.selected) do
		selector:draw()
	end
	love.graphics.print(self.countdown,2,2,0,self.scale,self.scale)
end

function Menu:update(dt)
	local state = true
	if #self.selected > 1 then
		for _,selector in pairs(self.selected) do
			state = state and selector.state
		end
	else
		state = false
	end
	if state then
		self.countdown = self.countdown - dt
	else
		self.countdown = 3
	end
	if state and self.countdown < 0 then
		self.__index = self:startGame()
	end
end

--
function Menu:unselectSelector(selector)
	local i = 0
	for _,v in pairs(self.selected) do
		if v == selector then
			i = _
			break
		end
	end
	if i ~= 0 then
		table.remove(self.selected, i)
		table.insert(self.selectors, selector)
		self:assignController(selector:getController())
		selector:clear()
	end
end

-- Controllers
function Menu:assignController(controller)
	controller:setParent(self)
end

function Menu:controllerPressed(control, controller)
	local selector = self.selectors[1]
	if selector ~= nil then
		table.remove(self.selectors, 1)
		table.insert(self.selected, selector)
		selector:assignController(controller)
		selector:controllerPressed(control)
	end
end

-- It just must be here
function Menu:controllerReleased(control, controller)
end

-- WARUDO
function Menu:startGame()
	local nauts = {}
	for _,selector in pairs(self.selected) do
		table.insert(nauts, {selector:getSelectionName(), selector:getController()})
	end
	local world = World:new("default", nauts)
	return world
end