-
Notifications
You must be signed in to change notification settings - Fork 15
/
inputSelectState.lua
200 lines (179 loc) · 5.06 KB
/
inputSelectState.lua
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local State = require("state")
local Label = require("label")
local Menu = require("menu")
local AILevelSelection = require("aiLevelSelection")
local Cursor = require("cursor")
local LevelSelectionState = require("levelSelectionState")
local InputSelectState = {}
InputSelectState.__index = InputSelectState
setmetatable(InputSelectState, State)
function InputSelectState.create(parent)
local self = setmetatable(State.create(), InputSelectState)
self:addComponent(Label.create("PRESS ACTION BUTTON TO JOIN", 0, 25, WIDTH, "center"))
self.menu = self:addComponent(Menu.create((WIDTH-200)/2, 320, 200, 32, 24, self))
self.leaveButtons = {}
self.aiLevelSelections = {}
for i=1,4 do
self.leaveButtons[i] = self.menu:addButton("LEAVE", "leave"..i, -90+i*150, 258, 130, 32)
self.leaveButtons[i].visible = false
local level = config["ai"..i.."level"]
self.aiLevelSelections[i] = self:addComponent(AILevelSelection.create(-90+i*150, 258, level))
end
self.menu:addButton("CONTINUE", "continue")
self.menu:addButton("BACK", "back")
self.bg = ResMgr.getImage("bg_stars.png")
self.imgColors = ResMgr.getImage("icon_colors.png")
self.iconKeyboard = ResMgr.getImage("icon_keyboard.png")
self.iconMouse = ResMgr.getImage("icon_mouse.png")
self.iconController = ResMgr.getImage("icon_controller.png")
self.iconAI = ResMgr.getImage("icon_ai.png")
return self
end
function InputSelectState:draw()
love.graphics.draw(self.bg, 0, 0)
love.graphics.draw(self.imgColors, 62, 116)
-- Draw titles
local player = 1
local ai = 1
for i=1,4 do
love.graphics.setColor(23, 23, 23, 255)
love.graphics.rectangle("fill", -88+i*150, 66, 126, 32)
love.graphics.setColor(241, 148, 0, 255)
love.graphics.rectangle("line", -87.5+i*150, 66.5, 126, 32)
love.graphics.setColor(255, 255, 255, 255)
if self.inputs[i] then
love.graphics.printf("PLAYER "..player, -100+i*150, 73, 150, "center")
player = player+1
local t = self.inputs[i]:getType()
if t == Input.TYPE_KEYBOARD then
love.graphics.draw(self.iconKeyboard, -85+i*150, 119)
elseif t == Input.TYPE_MOUSE then
love.graphics.draw(self.iconMouse, -85+i*150, 119)
elseif t == Input.TYPE_JOYSTICK then
love.graphics.draw(self.iconController, -85+i*150, 119)
end
else
love.graphics.printf("AI "..ai, -100+i*150, 73, 150, "center")
ai = ai+1
love.graphics.draw(self.iconAI, -85+i*150, 119)
end
end
end
function InputSelectState:keypressed(k)
for i=1,4 do
if self.inputs[i] then
self.inputs[i]:keypressed(k)
end
end
local found = false
for i=1,4 do
if self.inputs[i] and self.inputs[i]:getType() == Input.TYPE_KEYBOARD then
found = true
break
end
end
if found == false then
self:addInput(KeyboardInput.create())
end
end
function InputSelectState:joystickpressed(joy, button)
for i=1,4 do
if self.inputs[i] then
self.inputs[i]:joystickpressed(joy, button)
end
end
local found = false
for i=1,4 do
if self.inputs[i] and self.inputs[i]:getType() == Input.TYPE_JOYSTICK then
if self.inputs[i].joystick == joy then
found = true
break
end
end
end
if found == false then
self:addInput(JoystickInput.create(joy))
end
end
function InputSelectState:mousepressed(x, y, button)
for i=1,4 do
if self.inputs[i] then
self.inputs[i]:mousepressed(x, y, button)
end
end
local found = false
for i=1,4 do
if self.inputs[i] and self.inputs[i]:getType() == Input.TYPE_MOUSE then
found = true
break
end
end
if found == false then
self:addInput(MouseInput.create())
end
end
function InputSelectState:addInput(input)
for i=1,4 do
if self.inputs[i] == nil then
self.inputs[i] = input
self.cursors[i] = Cursor.create(-25+i*150, 165, i)
self.cursors[i]:addInput(input)
self.leaveButtons[i].visible = true
self.aiLevelSelections[i].visible = false
playSound("click")
return
end
end
end
function InputSelectState:buttonPressed(id, source)
if id == "leave1" then
playSound("click")
self.inputs[1] = nil
self.cursors[1] = nil
self.leaveButtons[1].visible = false
self.aiLevelSelections[1].visible = true
return true
elseif id == "leave2" then
playSound("click")
self.inputs[2] = nil
self.cursors[2] = nil
self.leaveButtons[2].visible = false
self.aiLevelSelections[2].visible = true
return true
elseif id == "leave3" then
playSound("click")
self.inputs[3] = nil
self.cursors[3] = nil
self.leaveButtons[3].visible = false
self.aiLevelSelections[3].visible = true
return true
elseif id == "leave4" then
playSound("click")
self.inputs[4] = nil
self.cursors[4] = nil
self.leaveButtons[4].visible = false
self.aiLevelSelections[4].visible = true
return true
elseif id == "continue" then
playSound("quack")
for i=1,4 do
if self.inputs[i] == nil then
self.inputs[i] = NullInput.create()
end
end
popState()
pushState(LevelSelectionState.create(self))
return true
elseif id == "back" then
playSound("quack")
popState()
return true
end
return false
end
function InputSelectState:leave()
for i=1,4 do
config:update("ai"..i.."level", self.aiLevelSelections[i]:getSelection())
end
end
return InputSelectState