-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.lua
More file actions
284 lines (253 loc) · 7.65 KB
/
editor.lua
File metadata and controls
284 lines (253 loc) · 7.65 KB
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
require("game.tilemap")
require("game.state")
require("game.conf")
game.editor = {}
local cooldown = 0.1
local currentPreset = "wall"
local lineConnectorStart = nil
local lineConnectorEnd = nil
local showConnections = false
function game.editor.load()
if game.conf.editor == false then
return
end
end
function game.editor.update(dt)
if cooldown > 0 then
cooldown = cooldown - dt
return
end
if game.conf.editor == false then
return
end
if love.mouse.isDown(1) and not love.keyboard.isScancodeDown("r") then
if not (lineConnectorStart ~= nil) then
local x, y = game.tilemap.screenToWorldPos(love.mouse.getX(), love.mouse.getY())
game.tilemap.setTileWithPreset(x, y, game.tilemap.getTilePresets()[currentPreset])
end
--game.tilemap.interact(x, y, 1)
end
if love.mouse.isDown(2) then
cooldown = 0.1
local presetKeys = {}
for k, v in pairs(game.tilemap.getTilePresets()) do
table.insert(presetKeys, k)
end
local index = 1
for i = 1, #presetKeys do
if presetKeys[i] == currentPreset then
index = i
break
end
end
index = index + 1
if index > #presetKeys then
index = 1
end
currentPreset = presetKeys[index]
end
if love.keyboard.isScancodeDown("n") then
cooldown = 0.1
newLevel()
end
if love.keyboard.isScancodeDown("c") then
cooldown = 0.1
showConnections = not showConnections
end
if love.mouse.isDown(3) then
cooldown = 0.2
local x, y = game.tilemap.screenToWorldPos(love.mouse.getX(), love.mouse.getY())
print(game.tilemap.getValue(x, y, "inverted"))
if game.tilemap.getValue(x, y, "inverted") ~= nil then
if game.tilemap.getValue(x, y, "inverted") == false then
game.tilemap.setValue(x, y, "inverted", true)
else
game.tilemap.setValue(x, y, "inverted", false)
end
end
end
if love.keyboard.isScancodeDown("s") and love.keyboard.isScancodeDown("lctrl") then
cooldown = 2
local filename = game.tilemap.getCurrentLevelPath()
game.tilemap.save(filename)
end
if love.keyboard.isScancodeDown("right") and love.keyboard.isScancodeDown("lctrl") then
cooldown = 0.2
game.loadLevel(game.state.level.current + 1)
end
if love.keyboard.isScancodeDown("left") and love.keyboard.isScancodeDown("lctrl") then
cooldown = 0.2
game.loadLevel(game.state.level.current - 1)
end
if love.keyboard.isScancodeDown("r") and love.mouse.isDown(1) then
if lineConnectorStart == nil then
lineConnectorStart = { love.mouse.getX(), love.mouse.getY() }
else
lineConnectorEnd = { love.mouse.getX(), love.mouse.getY() }
end
else
if love.keyboard.isScancodeDown("r") and lineConnectorStart ~= nil then
local x, y = game.tilemap.screenToWorldPos(lineConnectorStart[1], lineConnectorStart[2])
local endx, endy = game.tilemap.screenToWorldPos(lineConnectorEnd[1], lineConnectorEnd[2])
local currentNeededRedstone = game.tilemap.getValue(x, y, "needs_redstone")
if currentNeededRedstone == nil then
currentNeededRedstone = {}
end
-- Check if the connection already exists and remove it
for i = 1, #currentNeededRedstone do
if currentNeededRedstone[i].x == endx and currentNeededRedstone[i].y == endy then
table.remove(currentNeededRedstone, i)
game.tilemap.setValue(x, y, "needs_redstone", currentNeededRedstone)
lineConnectorStart = nil
lineConnectorEnd = nil
return
end
end
table.insert(currentNeededRedstone, { x = endx, y = endy })
game.tilemap.setValue(x, y, "needs_redstone", currentNeededRedstone)
lineConnectorStart = nil
lineConnectorEnd = nil
end
end
end
function game.editor.draw()
if game.conf.editor == false then
return
end
love.graphics.setColor(255, 255, 255)
love.graphics.setNewFont(22)
love.graphics.print("Current preset: " .. currentPreset, 0)
love.graphics.setNewFont(15)
love.graphics.print("Available presets:", 10, 40)
local i = 1
for k, v in pairs(game.tilemap.getTilePresets()) do
if k == currentPreset then
love.graphics.setColor(255, 255, 0)
else
love.graphics.setColor(255, 255, 255)
end
love.graphics.print(k, 10, 40 + i * 20)
i = i + 1
end
if lineConnectorStart ~= nil then
love.graphics.setColor(255, 0, 0)
love.graphics.circle("fill", lineConnectorStart[1], lineConnectorStart[2], 5)
if lineConnectorEnd ~= nil then
love.graphics.circle("fill", lineConnectorEnd[1], lineConnectorEnd[2], 5)
love.graphics.line(lineConnectorStart[1], lineConnectorStart[2], lineConnectorEnd[1], lineConnectorEnd[2])
end
end
game.editor.showConnections()
end
function newLevel()
local LEVEL_WIDTH = game.conf.level.width
local LEVEL_HEIGHT = game.conf.level.height
for y = 1, LEVEL_HEIGHT do
game.state.level.map[y] = {}
game.state.level.standingOn[y] = {}
for x = 1, LEVEL_WIDTH do
if x == 1 or x == LEVEL_WIDTH or y == 1 or y == LEVEL_HEIGHT then
game.tilemap.setTileWithPreset(x, y, game.tiles.background.tilePreset)
else
game.tilemap.setTileWithPreset(x, y, game.tiles.background.tilePreset)
end
--if x == 5 and y == math.floor(LEVEL_HEIGHT / 2) then
-- game.tilemap.setTileWithPreset(x, y, game.tiles.door.doorTilePresets.door_hor)
--end
--
--if x == math.floor(LEVEL_WIDTH / 2) and y == 5 then
-- game.tilemap.setTileWithPreset(x, y, game.tiles.door.doorTilePresets.door_vert)
--end
game.state.level.standingOn[y][x] = false
end
end
game.conf.level_sequence[#game.conf.level_sequence + 1] = {
filename = "game/levels/level" .. #game.conf.level_sequence + 1 .. ".json",
title = "testing stage",
minions = {
homunculus = 2,
zombie = 2,
fae = 2,
guard = 2,
},
}
game.tilemap.setLevel(#game.conf.level_sequence)
end
function game.editor.wheelmoved(x, y)
if y < 0 then
cooldown = 0.1
local presetKeys = {}
for k, v in pairs(game.tilemap.getTilePresets()) do
table.insert(presetKeys, k)
end
local index = 1
for i = 1, #presetKeys do
if presetKeys[i] == currentPreset then
index = i
break
end
end
index = index + 1
if index > #presetKeys then
index = 1
end
currentPreset = presetKeys[index]
elseif y > 0 then
cooldown = 0.1
local presetKeys = {}
for k, v in pairs(game.tilemap.getTilePresets()) do
table.insert(presetKeys, k)
end
local index = 1
for i = 1, #presetKeys do
if presetKeys[i] == currentPreset then
index = i
break
end
end
index = index - 1
if index < 1 then
index = #presetKeys
end
currentPreset = presetKeys[index]
end
end
function game.editor.showConnections(dt)
if not showConnections then
return
end
local LEVEL_WIDTH = game.conf.level.width
local LEVEL_HEIGHT = game.conf.level.height
print("Showing connections")
for y = 1, LEVEL_HEIGHT do
for x = 1, LEVEL_WIDTH do
local needsRedstone = game.tilemap.getValue(x, y, "needs_redstone")
if needsRedstone ~= nil then
print("Tile at " .. x .. "x" .. y .. " needs redstone")
for _, v in ipairs(needsRedstone) do
love.graphics.setColor(255, 0, 0)
print(" Needs redstone from " .. v.x .. "x" .. v.y .. " to " .. x .. "x" .. y)
local screenx1, screeny1 = game.tilemap.tilemapToScreen(x, y)
local screenx2, screeny2 = game.tilemap.tilemapToScreen(v.x, v.y)
-- Line with direction indicator
love.graphics.line(screenx1, screeny1, screenx2, screeny2)
local angle = math.atan2(screeny2 - screeny1, screenx2 - screenx1)
local arrowLength = 10
local arrowAngle = math.pi / 6
love.graphics.line(
screenx2,
screeny2,
screenx2 - arrowLength * math.cos(angle + arrowAngle),
screeny2 - arrowLength * math.sin(angle + arrowAngle)
)
love.graphics.line(
screenx2,
screeny2,
screenx2 - arrowLength * math.cos(angle - arrowAngle),
screeny2 - arrowLength * math.sin(angle - arrowAngle)
)
end
end
end
end
end