-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopHandler.lua
276 lines (243 loc) · 9 KB
/
shopHandler.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
local IterableMap = require("include/IterableMap")
local util = require("include/util")
local Font = require("include/font")
local platformUtilities = require("utilities/platformUtilities")
local portalUtilities = require("utilities/portalUtilities")
local buttonUtilities = require("utilities/buttonUtilities")
local self = {}
local api = {}
local function SaveLevel()
LevelHandler.SaveLevel(self.levelFile.Get())
end
local function LoadLevel()
LevelHandler.LoadLevel(self.levelFile.Get())
end
function api.SnapToGrid(pos, xGridMax, yGridMax)
return {math.floor(
0.5 + pos[1] / math.min(xGridMax, self.placeGridSize.Get())) * math.min(xGridMax, self.placeGridSize.Get()),
math.floor(0.5 + pos[2] / math.min(yGridMax, self.placeGridSize.Get())) * math.min(yGridMax, self.placeGridSize.Get())
}
end
local function DoEntityClick(mousePos, button)
if self.hoveredItem and button == 1 then
if self.selectedProperty then
self.selectedProperty.SetSelected(false)
self.selectedProperty = false
end
if Global.IGNORE_MOVE_ON_SELECT and not self.selectedItem then
-- Stop entities repositioning when clicking on them followed by an accidental mouse movement.
self.ignoreMouseMove = true
end
self.selectedItem = self.hoveredItem
self.selectedOffset = util.Subtract(LevelHandler.WorldToHg(mousePos), self.selectedItem.GetPosition())
self.selectedProperty = self.selectedItem.GetDefaultSelectedProperty()
self.selectedProperty.SetSelected(true)
return true
elseif self.selectedItem and button == 2 then
if self.selectedProperty then
self.selectedProperty.SetSelected(false)
self.selectedProperty = false
end
self.selectedItem = false
return true
end
end
local function DoPropertyClick(mousePos, button, mouseMove)
if (not mouseMove) and self.selectedProperty and self.selectedProperty.HandleMousePress and self.selectedProperty.HandleMousePress(mousePos, button) then
return true
end
if (not mouseMove) and self.hoveredProperty and button == 1 then
if self.selectedProperty then
self.selectedProperty.SetSelected(false)
end
self.selectedProperty = self.hoveredProperty
self.selectedProperty.SetSelected(true)
return true
elseif self.selectedProperty and button == 1 and mousePos[1] < Global.VIEW_WIDTH + Global.MAIN_PADDING then
if self.selectedProperty.HandleWorldClick then
self.selectedProperty.HandleWorldClick(LevelHandler.WorldToHg(mousePos), mouseMove, self.selectedOffset)
return true
end
elseif self.selectedProperty and button == 2 then
api.DeselectProperty()
end
end
local function DoPropertyKeyPress(key, scancode, isRepeat)
if self.selectedProperty and self.selectedProperty.HandleKeyPress then
self.selectedProperty.HandleKeyPress(key)
end
end
function api.DeselectProperty()
if self.selectedProperty then
self.selectedProperty.SetSelected(false)
self.selectedProperty = false
end
end
function api.Update(dt)
end
function api.MousePressed(x, y, button)
self.ignoreMouseMove = false
local mousePos = self.world.GetMousePosition()
if DoPropertyClick(mousePos, button) then
return true
end
if DoEntityClick(mousePos, button) then
return true
end
end
function api.MouseMoved(x, y, button, dx, dy)
if self.ignoreMouseMove then
return
end
local mousePos = self.world.GetMousePosition()
if DoPropertyClick(mousePos, button, true) then
return true
end
end
function api.KeyPressed(key, scancode, isRepeat)
if DoPropertyKeyPress(key, scancode, isRepeat) then
return true
end
if key == "l" and (love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")) then
LoadLevel()
end
if key == "s" and (love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")) then
SaveLevel()
end
end
local function DrawEntityProperties(drawQueue, propList, mousePos)
local offset, hovered = 0, false
for i = 1, #propList do
offset, hovered = propList[i].DrawProperty(drawQueue, Global.VIEW_WIDTH + 30, offset, mousePos)
if hovered then
self.hoveredProperty = hovered
end
end
end
function api.Draw(drawQueue)
local mousePos = self.world.GetMousePosition()
self.hoveredItem = false
self.hoveredProperty = false
local hgPos = LevelHandler.WorldToHg(mousePos)
local hitEntity = EntityHandler.HitTest(hgPos)
if hitEntity then
self.hoveredItem = hitEntity
end
if self.hoveredItem then
self.hoveredItem.DrawOutline(drawQueue, "hover")
end
if self.selectedItem then
self.selectedItem.DrawOutline(drawQueue, "selected")
DrawEntityProperties(drawQueue, self.selectedItem.GetPropertyList(), mousePos)
else
DrawEntityProperties(drawQueue, self.propList, mousePos)
end
end
function api.UpdateLevelParams(level)
self.levelName.Set(level.name)
self.levelFile.Set(level.fileName)
self.levelWidth.Set(level.width)
self.levelHeight.Set(level.height)
self.timeLength.Set(level.timeLength)
self.timeSpeed.Set(level.timeSpeed)
end
local function SetupMenu()
local function SetWidth(newVal)
LevelHandler.SetWidth(newVal)
end
local function SetHeight(name)
LevelHandler.SetHeight(newVal)
end
local function SetTimeLength(newVal)
LevelHandler.SetLevelParameter("timeLength", newVal)
end
local function SetTimeSpeed(newVal)
LevelHandler.SetLevelParameter("timeSpeed", newVal)
end
local function ToggleWall(pos, fromMouseMove, offset, size)
if not fromMouseMove then
self.wallAddMode = not LevelHandler.WallAt(pos)
end
size = (size or 0) * LevelHandler.GetTileSize()
for x = pos[1] - size, pos[1] + size, LevelHandler.GetTileSize() do
for y = pos[2] - size, pos[2] + size, LevelHandler.GetTileSize() do
if self.wallAddMode then
LevelHandler.AddWall({x, y})
else
LevelHandler.RemoveWall({x, y})
end
end
end
end
local function DeleteEntity(pos)
EntityHandler.RemoveEntitiesAtPos(pos)
end
local function AddDefaultEntity(name, data)
data = data or {}
data.pos = {LevelHandler.Width() * Global.HG_GRID_SIZE * 0.5, LevelHandler.Height() * Global.HG_GRID_SIZE * 0.5}
EntityHandler.AddEntity(name, data)
end
self.levelName = NewProp.textBox(api, "Level Name", "")
self.levelFile = NewProp.textBox(api, "Level File", "")
self.saveButton = NewProp.clickButton(api, "Save (ctrl+S)", SaveLevel)
self.loadButton = NewProp.clickButton(api, "Load (ctrl+L)", LoadLevel)
self.levelPath = NewProp.heading(api, love.filesystem.getSaveDirectory() .. "/levels", 2)
self.levelWidth = NewProp.numberBox(api, "Level Width", LevelHandler.Width(), 1, false, 1, SetWidth)
self.levelHeight = NewProp.numberBox(api, "Level Height",LevelHandler.Height(), 1, false, 1, SetHeight)
self.timeLength = NewProp.numberBox(api, "Time Length (s)", LevelHandler.GetTimeLength(), 60, false, Global.FRAMES_PER_SECOND, SetTimeLength)
self.timeSpeed = NewProp.numberBox(api, "Time Speed (s/s)", LevelHandler.GetTimeSpeed(), 1, false, 1, SetTimeSpeed)
self.placeGridSize = NewProp.numberBox(api, "Place Grid Snap", 800, 100, 3200, 100)
self.toggleWallSmall = NewProp.worldClickButton(api, "Wall Brush 1", ToggleWall, {0})
self.toggleWallMedium = NewProp.worldClickButton(api, "Wall Brush 2", ToggleWall, {1})
self.toggleWallLarge = NewProp.worldClickButton(api, "Wall Brush 3", ToggleWall, {2})
self.deleteEntity = NewProp.worldClickButton(api, "Delete Entities", DeleteEntity)
self.boxSelector = NewProp.enumBox(api, "", "", {"box", "bomb", "balloon", "light"}, AddDefaultEntity, "New Box")
self.pickupSelector = NewProp.enumBox(api, "", "", Global.PICKUP_LIST,
function (name) AddDefaultEntity("pickup", {pickupType = name}) end, "New Pickup")
self.platformSelector = NewProp.enumBox(api, "", "", {"elevator", "platform", "door"},
function (name) AddDefaultEntity("platform", platformUtilities.GetDefaultPlatform(name)) end, "New Platform")
self.portalSelector = NewProp.enumBox(api, "", "", {"win", "zero", "reverse"},
function (name) AddDefaultEntity("portal", portalUtilities.GetDefaultPortal(name)) end, "New Portal")
self.buttonSelector = NewProp.enumBox(api, "", "", {"momentarySwitch", "stickySwitch"},
function (name)
local buttonType, buttonData = buttonUtilities.GetDefaultButton(name)
AddDefaultEntity(buttonType, buttonData)
end, "New Button")
self.propList = {
self.levelName,
self.levelFile,
self.saveButton,
self.loadButton,
self.levelPath,
self.levelWidth,
self.levelHeight,
self.timeLength,
self.timeSpeed,
NewProp.heading(api, ""),
self.placeGridSize,
self.toggleWallSmall,
self.toggleWallMedium,
self.toggleWallLarge,
self.deleteEntity,
NewProp.heading(api, ""),
self.boxSelector,
self.pickupSelector,
self.platformSelector,
self.portalSelector,
self.buttonSelector,
}
end
function api.ResetState()
if self.selectedProperty then
self.selectedProperty.SetSelected(false)
self.selectedProperty = false
end
self.selectedItem = false
end
function api.Initialize(world)
self = {
world = world,
}
SetupMenu()
end
return api