-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.lua
626 lines (557 loc) · 20.8 KB
/
editor.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
local editor = {}
local Sprites = require("modules.sprite")
local utils = require("modules.utils")
local fonts = require("modules.font")
local pause = require("modules.pause")
editor.InEditor = false
editor.IsLoaded = false
editor.CameraData = {
["CameraX"] = 400,
["CameraY"] = 200,
["CamSpeed"] = 500
}
local dcX = 0
local dcY = 0
local directions = {a = {1,0}, d = {-1,0}, w = {0,1}, s = {0,-1}}
local level = {
["Start"] = {["X"] = 0, ["Y"] = 0},
["Platforms"] = {},
["Hazards"] = {},
["Gates"] = {}
}
local fileName = nil
local sliding = false
local hue = 0.0
local saturation = 1.0
local brightness = 1.0
local movingObject = nil
function HSVtoRGB(h, s, v)
if s <= 0 then return v,v,v end
h = h*6
local c = v*s
local x = (1-math.abs((h%2)-1))*c
local m,r,g,b = (v-c), 0, 0, 0
if h < 1 then
r, g, b = c, x, 0
elseif h < 2 then
r, g, b = x, c, 0
elseif h < 3 then
r, g, b = 0, c, x
elseif h < 4 then
r, g, b = 0, x, c
elseif h < 5 then
r, g, b = x, 0, c
else
r, g, b = c, 0, x
end
return r+m, g+m, b+m
end
local function tableToString(tbl, indent)
local result = "{\n"
local nextIndent = indent .. " "
for k, v in pairs(tbl) do
if type(k) == "string" then
k = string.format("%q", k)
end
if type(v) == "string" then
v = string.format("%q", v)
elseif type(v) == "table" then
v = tableToString(v, nextIndent)
end
result = result .. string.format("%s[%s] = %s,\n", nextIndent, k, v)
end
result = result .. indent .. "}"
return result
end
local placeMode = "none"
function editor:Load()
self.buttons = {
["Player"] = {
["Sprite"] = Sprites.PlayerButton,
["Transform"] = {10, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "startPos"
end,
["Callback"] = function(self)
placeMode = "startPos"
end
},
["Spike"] = {
["Sprite"] = Sprites.SpikeButton,
["Transform"] = {70, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "spike"
end,
["Callback"] = function(self)
placeMode = "spike"
end
},
["Platform"] = {
["Sprite"] = Sprites.PlatformButton,
["Transform"] = {130, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "platform"
end,
["Callback"] = function(self)
placeMode = "platform"
end
},
["Water"] = {
["Sprite"] = Sprites.WaterButton,
["Transform"] = {190, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "waterPlatform"
end,
["Callback"] = function()
placeMode = "waterPlatform"
end
},
["Sponge"] = {
["Sprite"] = Sprites.SpongeButton,
["Transform"] = {250, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "sponge"
end,
["Callback"] = function()
placeMode = "sponge"
end
},
["Win"] ={
["Sprite"] = Sprites.WinButton,
["Transform"] = {310, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "win"
end,
["Callback"] = function()
placeMode = "win"
end
},
["Move"] = {
["Sprite"] = Sprites.MoveButton,
["Transform"] = {370, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "move"
end,
["Callback"] = function()
placeMode = "move"
end
},
["Scale"] = {
["Sprite"] = Sprites.ScaleButton,
["Transform"] = {430, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "scale"
end,
["Callback"] = function()
placeMode = "scale"
end
},
["Paint"] = {
["Sprite"] = Sprites.PaintButton,
["Transform"] = {490, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "paint"
end,
["Callback"] = function()
placeMode = "paint"
end
},
["Delete"] = {
["Sprite"] = Sprites.DeleteButton,
["Transform"] = {550, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return placeMode == "delete"
end,
["Callback"] = function()
placeMode = "delete"
end
},
["Save"] = {
["Sprite"] = Sprites.SaveButton,
["Transform"] = {610, 10, 55, 55},
["IsVisible"] = function()
return true
end,
["Selected"] = function()
return false
end,
["Callback"] = function()
if level.End then
love.filesystem.setIdentity("blue-goose-platformer")
love.filesystem.write(fileName, "return " .. tableToString(level, ""))
love.window.showMessageBox("Saved", "Save was successful!")
else
love.window.showMessageBox("Error", "You cannot save a level without an end flag!", "error")
end
end
},
}
self.HSVSliders = {
{
Sprite = "Hue",
Transform = {10, 90, 200, 25},
SliderPos = function ()
return (10 + (hue * 200))
end,
Callback = function (percentage)
hue = percentage
if hue > 1.0 then
hue = 1.0
end
if hue < 0.0 then
hue = 0.0
end
end
},
{
Sprite = "Saturation",
Transform = {10, 120, 200, 25},
SliderPos = function ()
return (10 + (saturation * 200))
end,
Callback = function (percentage)
saturation = percentage
if saturation > 1.0 then
saturation = 1.0
end
if saturation < 0.0 then
saturation = 0.0
end
end
},
{
Sprite = "Brightness",
Transform = {10, 150, 200, 25},
SliderPos = function ()
return (10 + (brightness * 200))
end,
Callback = function (percentage)
brightness = percentage
if brightness > 1.0 then
brightness = 1.0
end
if brightness < 0.0 then
brightness = 0.0
end
end
}
}
self.IsLoaded = true
end
local mX, mY = 0, 0
local placingPlatform = false
function editor:Draw()
for _,platform in pairs(level.Platforms) do
love.graphics.setColor(platform.Color.R, platform.Color.G, platform.Color.B)
love.graphics.rectangle("fill", platform.X + self.CameraData.CameraX, platform.Y + self.CameraData.CameraY, platform.W, platform.H)
love.graphics.setColor(1, 1, 1)
end
for _,hazard in pairs(level.Hazards) do
if hazard.Type == "Spike" then
love.graphics.draw(Sprites.Spike, hazard.X + self.CameraData.CameraX, hazard.Y + self.CameraData.CameraY)
elseif hazard.Type == "Sponge" then
love.graphics.draw(Sprites.Sponge, hazard.X + self.CameraData.CameraX, hazard.Y + self.CameraData.CameraY, 0, hazard.W / 536, hazard.H / 350)
end
end
for _,gate in pairs(level.Gates) do
love.graphics.draw(Sprites.Water, gate.X + self.CameraData.CameraX, gate.Y + self.CameraData.CameraY, 0, gate.W / 643, gate.H / 360)
end
if placingPlatform == true then
local sX = math.abs(love.mouse.getX() - mX)
local sY = math.abs(love.mouse.getY() - mY)
love.graphics.setColor(0,1,0,0.5)
love.graphics.rectangle("fill", math.min(mX, love.mouse.getX()), math.min(mY, love.mouse.getY()), sX, sY)
love.graphics.setColor(1,1,1,1)
end
if placeMode == "platform" or placeMode == "paint" then
for _, s in ipairs(self.HSVSliders) do
if s.Sprite == "Saturation" then
love.graphics.setColor(1,1,1,1)
love.graphics.rectangle("fill", s.Transform[1], s.Transform[2], s.Transform[3], s.Transform[4])
local r,g,b = HSVtoRGB(hue, 1, brightness)
love.graphics.setColor(r,g,b,1)
elseif s.Sprite == "Brightness" then
local r,g,b = HSVtoRGB(hue, saturation, 1)
love.graphics.setColor(r,g,b,1)
end
love.graphics.draw(Sprites[s.Sprite], s.Transform[1], s.Transform[2], 0)
love.graphics.setColor(1,1,1,1)
love.graphics.draw(Sprites.Slider, s.SliderPos(), s.Transform[2], 0, 1, 1, 5)
end
local r,g,b = HSVtoRGB(hue, saturation, brightness)
love.graphics.setColor(r,g,b,1)
love.graphics.rectangle("fill", 10, 180, 25, 25)
end
love.graphics.setColor(1, 1, 1, 0.5)
love.graphics.draw(Sprites.Player, level.Start.X + self.CameraData.CameraX, level.Start.Y + self.CameraData.CameraY)
love.graphics.setColor(1,1,1,1)
if level.End then
love.graphics.setColor(1, 1, 1, 0.5)
love.graphics.draw(Sprites.EndFlag, level.End.X + self.CameraData.CameraX, level.End.Y + self.CameraData.CameraY)
love.graphics.setColor(1,1,1,1)
end
for _,button in pairs(self.buttons) do
if button.IsVisible() then
if button.Selected() then
love.graphics.setColor(0.8,0.8,0.8)
love.graphics.draw(button.Sprite, button.Transform[1], button.Transform[2], 0, button.Transform[3] / 75, button.Transform[4] / 75)
love.graphics.setColor(1,1,1,1)
else
love.graphics.draw(button.Sprite, button.Transform[1], button.Transform[2], 0, button.Transform[3] / 75, button.Transform[4] / 75)
end
end
end
if fileName ~= nil then
love.graphics.push()
love.graphics.setFont(fonts.ValentinySubtext)
love.graphics.setColor(0,0,0)
love.graphics.printf(fileName, 190, 560, 600, "right")
love.graphics.setColor(1,1,1)
love.graphics.setFont(fonts.Valentiny)
love.graphics.pop()
end
pause:Draw()
end
local cX, cY = 0, 0
function editor:Update(dt)
if pause.Paused == true then return end
for key, data in pairs(directions) do
if love.keyboard.isDown(key) then
cX = cX + self.CameraData.CamSpeed * data[1] * dt
cY = cY + self.CameraData.CamSpeed * data[2] * dt
if placeMode == "move" and movingObject ~= nil then
movingObject.X = movingObject.X - data[1] * dt * self.CameraData.CamSpeed
movingObject.Y = movingObject.Y - data[2] * dt * self.CameraData.CamSpeed
end
if placeMode == "scale" and movingObject ~= nil then
movingObject.W = movingObject.W - data[1] * dt * self.CameraData.CamSpeed
movingObject.H = movingObject.H - data[2] * dt * self.CameraData.CamSpeed
end
end
end
self.CameraData.CameraX = self.CameraData.CameraX + cX
self.CameraData.CameraY = self.CameraData.CameraY + cY
cX, cY = 0, 0
end
function editor:MousePressed(x, y, button)
if pause.Paused then
pause:MouseClick(x, y)
return
end
if button == 1 then
local buttonPressed = false
for _,btn in pairs(self.buttons) do
if btn.IsVisible() and utils:CheckCollision(x, y, 1, 1, btn.Transform[1], btn.Transform[2], btn.Transform[3], btn.Transform[4]) then
btn.Callback(editor)
buttonPressed = true
end
end
for _, s in ipairs(self.HSVSliders) do
if utils:CheckCollision(x, y, 1, 1, s.Transform[1], s.Transform[2], s.Transform[3], s.Transform[4]) and (placeMode == "platform" or placeMode == "paint") then
local sliderPercent = (x - s.Transform[1]) / s.Transform[3]
s.Callback(sliderPercent)
sliding = true
return
end
end
if buttonPressed == true then return end
if placeMode == "startPos" then
level.Start.X = x - self.CameraData.CameraX - 10
level.Start.Y = y - self.CameraData.CameraY - 10
elseif placeMode == "spike" then
local CanPlace = true
for _, hazard in ipairs(level.Hazards) do
local distance = math.sqrt((hazard.X - (x - self.CameraData.CameraX - 40))^2 + (hazard.Y - (y - self.CameraData.CameraY - 40))^2)
if distance <= 25 then
CanPlace = false
break
end
end
if CanPlace == true then
table.insert(level.Hazards, {
["X"] = x - self.CameraData.CameraX - 40,
["Y"] = y - self.CameraData.CameraY - 40,
["Type"] = "Spike"
})
end
elseif placeMode == "platform" or placeMode == "waterPlatform" or placeMode == "sponge" then
placingPlatform = true
mX, mY = x, y
elseif placeMode == "win" then
level.End = {
["X"] = x - self.CameraData.CameraX - 40,
["Y"] = y - self.CameraData.CameraY - 40,
}
elseif placeMode == "delete" then
for index, value in pairs(level.Platforms) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
table.remove(level.Platforms, index)
end
end
for index, value in pairs(level.Gates) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
table.remove(level.Gates, index)
end
end
for index, value in pairs(level.Hazards) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W or 65, value.H or 65) then
table.remove(level.Hazards, index)
end
end
elseif placeMode == "move" then
for index, value in pairs(level.Platforms) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
movingObject = value
break
end
end
for index, value in pairs(level.Gates) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
movingObject = value
break
end
end
for index, value in pairs(level.Hazards) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W or 65, value.H or 65) then
movingObject = value
break
end
end
elseif placeMode == "scale" then
for index, value in pairs(level.Platforms) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
movingObject = value
break
end
end
for index, value in pairs(level.Gates) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
movingObject = value
break
end
end
for index, value in pairs(level.Hazards) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W or 65, value.H or 65) then
movingObject = value
break
end
end
elseif placeMode == "paint" then
for index, value in pairs(level.Platforms) do
if utils:CheckCollision(x - self.CameraData.CameraX, y - self.CameraData.CameraY, 1, 1, value.X, value.Y, value.W, value.H) then
local r, g, b = HSVtoRGB(hue, saturation, brightness)
value.Color.R = r
value.Color.G = g
value.Color.B = b
break
end
end
end
end
end
function editor:MouseReleased(x, y)
sliding = false
if movingObject ~= nil then
if placeMode == "scale" then
if movingObject.W < 0 then
movingObject.X = movingObject.X + movingObject.W
end
if movingObject.H < 0 then
movingObject.Y = movingObject.Y + movingObject.H
end
movingObject.W = math.abs(movingObject.W)
movingObject.H = math.abs(movingObject.H)
end
movingObject = nil
end
if placingPlatform == true then
local sX = math.abs(x - mX)
local sY = math.abs(y - mY)
if placeMode == "platform" then
print("platform")
local r, g, b = HSVtoRGB(hue, saturation, brightness)
table.insert(level.Platforms, {
["X"] = math.min(mX, x) - self.CameraData.CameraX,
["Y"] = math.min(mY, y) - self.CameraData.CameraY,
["W"] = sX,
["H"] = sY,
["Color"] = {
["R"] = r,
["G"] = g,
["B"] = b
}
})
elseif placeMode == "waterPlatform" then
table.insert(level.Gates, {
["X"] = math.min(mX, x) - self.CameraData.CameraX,
["Y"] = math.min(mY, y) - self.CameraData.CameraY,
["W"] = sX,
["H"] = sY,
})
elseif placeMode == "sponge" then
table.insert(level.Hazards, {
["X"] = math.min(mX, x) - self.CameraData.CameraX,
["Y"] = math.min(mY, y) - self.CameraData.CameraY,
["W"] = sX,
["H"] = sY,
["Type"] = "Sponge"
})
end
placingPlatform = false
mX, mY = 0, 0
end
end
function editor:MouseMoved(x, y, dx, dy)
for _, s in ipairs(self.HSVSliders) do
if utils:CheckCollision(x, y, 1, 1, s.Transform[1], s.Transform[2], s.Transform[3], s.Transform[4]) and sliding and (placeMode == "platform" or placeMode == "paint") then
local sliderPercent = (x - s.Transform[1]) / s.Transform[3]
s.Callback(sliderPercent)
return
end
end
if movingObject ~= nil then
if placeMode == "move" then
movingObject.X = movingObject.X + dx
movingObject.Y = movingObject.Y + dy
elseif placeMode == "scale" then
movingObject.W = movingObject.W + dx
movingObject.H = movingObject.H + dy
end
end
end
function editor:LoadLevel(name, data)
level = data
fileName = name
self.InEditor = true
end
return editor