-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisoCreator.lua
326 lines (254 loc) · 8.59 KB
/
isoCreator.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
require "dialog"
IsoCreator = {}
IsoCreator.__index = IsoCreator
function IsoCreator.create()
local iso = {} -- our new object
setmetatable(iso,IsoCreator)
iso.blockTypesX = 0
iso.blockTypesY = 0
iso.rotateButtonsX = 650
iso.rotateButtonsY = 130
iso.rotL = love.graphics.newImage("rotateLeft.png")
iso.rotR = love.graphics.newImage("rotateRight.png")
iso.currentType = BLOCKTYPE["grass"] -- default type when creating blocks
iso.blueprint = {}
iso.newTerrain = {}
iso.creatingBlock = false
iso.blueprintVisible = true
iso.addBlockTypeVisible = false
iso.dialog = {}
iso.promptVisible = true
iso.startScreen = false
return iso
end
function IsoCreator:creatorLoad()
self.dialog = Dialog.create("addBlockType", love.graphics.getWidth()/2 - 130, love.graphics.getHeight()/2 - 75)
self.blueprint = Terrain.create(self:makeGrid("blueprint", 7, 7), 300, 228)
self.blueprintVisible = false
self.newTerrain = Terrain.create(self:makeGrid("grass", 7, 7), 300, 228)
end
-- Prompt the user to either load an existing file to edit, or create a new one
function IsoCreator:loadPrompt()
-- draw a simple dialog box with two buttons, no text boxes
end
function IsoCreator:draw()
if self.startScreen then
-- display the start screen and don't draw anything behind it
-- draw a dialog, prompt the user to either load grid and avatar files or create a new terrain
return
end
self:drawTerrains()
self:drawBlockTypes()
self:drawRotateButtons()
if self.creatingBlock then
self:updateBlockHeight()
end
if self.dialog.visible then
self.dialog:draw()
end
if self.promptVisible then
self:loadPrompt()
end
end
function IsoCreator:startNew()
self.blueprint = Terrain.create(self:makeGrid("blueprint", 7, 7), 300, 228)
self.blueprintVisible = true
self.newTerrain = Terrain.create(self:makeGrid("empty", 7, 7), 300, 228)
end
function IsoCreator:loadExisting()
-- draw a new dialog prompting the user to enter the file names for avatars and grid
end
-- draws 2 terrains: the blueprint terrain and the actual terrain being built
function IsoCreator:drawTerrains()
self.newTerrain:draw()
if self.blueprintVisible then
self.blueprint:draw()
end
end
function IsoCreator:mousepressed(x, y, button)
if self.startScreen then
-- check for mouse events on start screen only
return
end
-- if the dialog is being displayed, disable mouse clicks on the background
-- this stops the user from accidentally creating or deleting blocks
if self.dialog.visible then
self.dialog:mousepressed(x, y)
return
end
self:clickBlockTypes(x, y)
self.blueprint:selectTileFromMouse(x, y)
if button == "l" then
self.creatingBlock = true
self.newTerrain:addBlock(self.blueprint.selected[1], self.blueprint.selected[2], self.currentType, 0)
else
self.creatingBlock = false
self.newTerrain:removeBlock(self.blueprint.selected[1], self.blueprint.selected[2])
end
self:clickRotateButtons(x, y)
end
function IsoCreator:updateBlockHeight()
-- get original height from blueprint.selected
-- for every amount greater than the height by BLOCK_HEIGHT
-- increment height of block
-- on release, stop changing it
local y = love.mouse.getY()
local bx = self.blueprint.selected[1]
local by = self.blueprint.selected[2]
-- need to take into account offset
local blockY = self.blueprint.y + (bx+by) * (BLOCK_IMGHEIGHT / 4) - ((BLOCK_IMGHEIGHT / 2) * (table.getn(self.blueprint.grid) / 2)) - (BLOCK_IMGHEIGHT / 2)*0
local height = math.floor((blockY - y) / BLOCK_HEIGHT)
-- restrict to positive height only
if height < 0 then
height = 0
end
self.newTerrain:addBlock(self.blueprint.selected[1], self.blueprint.selected[2], self.currentType, height)
end
function IsoCreator:mousereleased(x, y, button)
self.creatingBlock = false
end
function IsoCreator:keypressed(key, unicode)
self.dialog:keypressed(key, unicode)
end
function IsoCreator:drawRotateButtons()
love.graphics.push()
love.graphics.translate(self.rotateButtonsX, self.rotateButtonsY)
love.graphics.draw(self.rotL, 0, 0)
love.graphics.draw(self.rotR, self.rotL:getWidth(), 0)
love.graphics.pop()
end
function IsoCreator:clickRotateButtons(x, y)
-- y is shared, so check that first
if y >= self.rotateButtonsY and y <= self.rotateButtonsY + self.rotL:getHeight() then
-- if clicking rotL
if x >= self.rotateButtonsX and x <= self.rotateButtonsX + self.rotL:getWidth() then
self.blueprint:rotate("left")
self.newTerrain:rotate("left")
end
-- if clicking rotR
if x >= self.rotateButtonsX + self.rotL:getWidth() and x <= self.rotateButtonsX + (self.rotL:getWidth() * 2) then
self.blueprint:rotate("right")
self.newTerrain:rotate("right")
end
end
end
function IsoCreator:drawBlockTypes()
love.graphics.push()
love.graphics.translate(self.blockTypesX, self.blockTypesY)
love.graphics.scale(0.5, 0.5)
for k, v in pairs(BLOCKFILE) do
local block = love.graphics.newImage(v)
love.graphics.draw(block, BLOCK_WIDTH*(k-1), 0)
if k == self.currentType then
local width = love.graphics.getLineWidth()
local r, g, b, a = love.graphics.getColor()
love.graphics.setLineWidth(3)
love.graphics.setColor(255, 255, 255)
love.graphics.line(BLOCK_WIDTH*(k-1), 70, BLOCK_WIDTH*k, 70)
love.graphics.setLineWidth(width)
love.graphics.setColor(r, g, b, a)
end
end
local add = love.graphics.newImage("addnew.png")
love.graphics.draw(add, table.getn(BLOCKFILE) * BLOCK_WIDTH, 0)
love.graphics.pop()
end
function IsoCreator:clickBlockTypes(x, y)
for k,v in pairs(BLOCKFILE) do
if x >= self.blockTypesX + BLOCK_WIDTH*(k-1)/2 and x <= self.blockTypesX + BLOCK_WIDTH*k/2 then
if y >= self.blockTypesY and y <= self.blockTypesY + BLOCK_IMGHEIGHT/2 then
self.currentType = k
end
end
end
-- if click on the add new button
if x >= self.blockTypesX + BLOCK_WIDTH*table.getn(BLOCKFILE)/2 and x <= self.blockTypesX + BLOCK_WIDTH*table.getn(BLOCKFILE) then
if y >= self.blockTypesY and y <= self.blockTypesY + BLOCK_IMGHEIGHT/2 then
-- open dialog to add a new block type
self.dialog.visible = true
end
end
end
-- simple function to generate a uniform grid
function IsoCreator:makeGrid(type, width, height)
local grid = {}
local yempty = 0
local xempty = 0
if width > height then
yempty = width - height
elseif height > width then
xempty = height - width
end
for x = 1, width + xempty do
grid[x] = {}
for y = 1, height + yempty do
if y > height or x > width then
grid[x][y] = Block.create(BLOCKTYPE["empty"], 0)
else
grid[x][y] = Block.create(BLOCKTYPE[type], 0)
end
end
end
return grid
end
function IsoCreator:makeRandomGrid(width, height)
local grid = {}
local yempty = 0
local xempty = 0
if width > height then
yempty = width - height
elseif height > width then
xempty = height - width
end
for x = 1, width + xempty do
grid[x] = {}
for y = 1, height + yempty do
if y > height or x > width then
grid[x][y] = Block.create(BLOCKTYPE["empty"], 0)
else
-- select a random block type and height
local randomType = math.random(table.getn(BLOCKFILE))
local randomHeight = math.random(5) -- arbitrary max height
grid[x][y] = Block.create(randomType, randomHeight)
end
end
end
return grid
end
function IsoCreator:makeSmartRandomGrid(width, height)
local grid = {}
local yempty = 0
local xempty = 0
if width > height then
yempty = width - height
elseif height > width then
xempty = height - width
end
local lastHeight = 0
for x = 1, width + xempty do
grid[x] = {}
for y = 1, height + yempty do
if y > height or x > width then
grid[x][y] = Block.create(BLOCKTYPE["empty"], 0)
else
-- select a random block type and height
-- ensure that the height of a block is no more than 1 away from the block prior to it
local randomType = math.random(table.getn(BLOCKFILE))
local randomHeight = math.random(lastHeight - 1, lastHeight + 1)
randomHeight = math.max(0, randomHeight)
local besideHeight = 0
if x > 1 then
besideHeight = grid[x - 1][y].height
if randomHeight > besideHeight + 2 then
randomHeight = besideHeight + 1
elseif randomHeight < besideHeight - 2 then
randomHeight = besideHeight - 1
end
end
grid[x][y] = Block.create(randomType, randomHeight)
lastHeight = randomHeight
end
end
end
return grid
end