summaryrefslogtreecommitdiffhomepage
path: root/config/menus/select.lua
blob: 9f0ed9b361226d6aa36b91e53ec7e3120189ca60 (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
local menu, background = ...

local Button = require "not.Button"
local Selector = require "not.Selector"
local Element = require "not.Element"
local Group = require "not.Group"

local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale()
local bx = width/2-29

local startButton = Button(menu)

if background == nil or not background:is(require "not.MenuBackground") then
	background = require "not.MenuBackground"(menu)
end

-- TODO: loadConfigs and isAvailable are duplicated in menus/select and menus/host.
local
function isAvailable (item)
	if item then
		if debug then
			return true
		end
		local at = type(item.available)
		if at == "boolean" then
			return item.available
		end
		if at == "string" then
			return false
		end
	end
end

local
function loadConfigs (dir, process)
	local items, icons = {}, {}
	for _,file in pairs(love.filesystem.getDirectoryItems(dir)) do
		local path = string.format("%s/%s", dir, file)
		if love.filesystem.isFile(path) and file ~= "readme.md" then
			local item = love.filesystem.load(path)()
			if isAvailable(item) then
				if process then
					process(item, file, path)
				end
				table.insert(icons, love.graphics.newImage(item.portrait))
				table.insert(items, item)
			end
		end
	end
	return items, icons
end

-- TODO: Clean-up menus/select, menus/host and Hero after portraits split.
local group, get
do
	local nauts, icons = loadConfigs("config/nauts")

	-- TODO: Find a better way to add empty and random entries to naut Selector.
	table.insert(icons, 1, false)
	table.insert(nauts, 1, {name = "empty"})
	table.insert(icons, 2, love.graphics.newImage("assets/portraits/random.png"))
	table.insert(nauts, 2, {name = "random"})

	group = Group(menu)

	local
	function attack (self)
		if not self.lock then
			local selected = self:getSelected()
			if selected.name == "empty" then
				return
			end
			if selected.name == "random" then
				self.index = self:rollRandom({1, 2})
			end
			if self:isUnique() then
				self.lock = true
			end
		end
	end

	for i,_ in pairs(Controller.getSets()) do
		group:addChild(Selector(nauts, group, menu))
			:set("icons", icons)
			:set("attack", attack)
			:set("getText", function (self)
					return string.upper(self:getSelected().name)
				end)
	end

	group:set("margin", 16)
	local gw, gh = group:getSize()
	group:setPosition((width - gw)/2, 55)

	function get ()
		local selection = group:callEach("getLocked")
		for i,naut in ipairs(selection) do
			selection[i] = {naut, Controller.getSets()[i]}
		end
		return selection
	end
end

return {
	background,
	group,
	startButton
		:setText("Force start")
		:setPosition(bx,134)
		:set("isEnabled", function ()
				return #get() > 1
			end)
		:set("active", function (self)
				sceneManager:changeScene(World(MAP, get()))
			end)
	,
	Button(menu)
		:setText("Go back")
		:setPosition(bx,150)
		:set("active", function (self)
				self.parent:open("host")
			end)
	,
	Element(menu)
		:setPosition(bx, 101)
		:set("the_final_countdown", 9)
		:set("draw", function (self, scale)
				if self.the_final_countdown ~= 9 then
					local x,y = self:getPosition()
					local countdown = math.max(1, math.ceil(self.the_final_countdown))
					love.graphics.setColor(255, 255, 255, 255)
					love.graphics.setFont(Font)
					love.graphics.print("Autostart in:", (x-16)*scale, (y+10)*scale, 0, scale, scale)
					love.graphics.setFont(Bold)
					love.graphics.printf(countdown, (x+40)*scale, (y)*scale, 36, "center", 0, scale, scale)
				end
			end)
		:set("update", function (self, dt)
				if #get() > 1 then
					self.the_final_countdown = self.the_final_countdown - dt
				else
					self.the_final_countdown = 9
				end
				if self.the_final_countdown < 0 then
					startButton:active()
				end
			end)
	,
}