-
Notifications
You must be signed in to change notification settings - Fork 1
/
isoTerrain.lua
629 lines (514 loc) · 18.2 KB
/
isoTerrain.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
627
628
629
require "block"
require "avatar"
BLOCKTYPE = {["empty"] = 0, ["grass"] = 1, ["rock"] = 2, ["blueprint"] = 3} -- allows you to easily create the new block based on name
BLOCKFILE = {[1] = "grass.png", [2] = "dirt.png", [3] = "blueprint.png"} -- stores the file names for the blocks to be drawn
Terrain = {}
Terrain.__index = Terrain
-- TODO: MUST store appropriate blocktypes and files. If user adds a new block type and saves, we NEED to record this.
function Terrain.create(grid, x, y)
local terr = {} -- our new object
setmetatable(terr,Terrain) -- make Terrain handle lookup
terr.x = x
terr.y = y
terr.grid = {}
terr.grid = grid
terr.selected = {0, 0}
terr.avatarModel = {}
terr.currentAvatar = 0
terr.nextAvId = 1
return terr
end
function Terrain:draw()
grid = self.grid
-- set up default tile type
tile = love.graphics.newImage("grass.png")
if table.getn(self.avatarModel) > 0 then
self.currentAvatar = 1
end
love.graphics.push()
love.graphics.translate(self.x, self.y)
love.graphics.setLineWidth(3)
for x = 1,table.getn(grid) do
for y = 1,table.getn(grid[1]) do
block = grid[x][y]
empty = false
if grid[x][y].type == BLOCKTYPE["empty"] then
empty = true
else
local tempType = grid[x][y].type
tile = love.graphics.newImage(BLOCKFILE[tempType])
end
-- draw block
if grid[x][y].height >= 1 then
-- draw blocks under top
-- store previous tile type
tempTile = tile
-- draw tiles under top as the default "underground" tile
tile = love.graphics.newImage("dirt.png")
-- draw all blocks under the height of the current block (so it doesn't float)
for i=0, (grid[x][y].height - 1) do
love.graphics.draw(tile,
((y-x) * (tile:getWidth() / 2)),
((x+y) * (tile:getHeight() / 4)) - ((tile:getHeight() / 2) * (table.getn(grid) / 2)) - (tile:getHeight() / 2)*i)
end
-- restore original tile type
tile = tempTile
end
if empty == false then
-- draw block
love.graphics.draw(tile,
((y-x) * (tile:getWidth() / 2)),
((x+y) * (tile:getHeight() / 4)) - ((tile:getHeight() / 2) * (table.getn(grid) / 2)) - (tile:getHeight() / 2)*grid[x][y].height)
end
-- draw any avatars on top of the block
if self.currentAvatar > 0 then
if self.avatarModel[self.currentAvatar].x == x and self.avatarModel[self.currentAvatar].y == y then
if empty == false then
local avatar = self.avatarModel[self.currentAvatar]
avatar:draw((avatar.y - avatar.x) * (BLOCK_WIDTH / 2),
((avatar.x+avatar.y) * (BLOCK_IMGHEIGHT / 4)) - ((BLOCK_IMGHEIGHT / 2) * (table.getn(self.grid) / 2)) - (BLOCK_IMGHEIGHT / 2)* (self.grid[avatar.x][avatar.y].height + avatar.height))
end
-- still need to check and increment this, even if empty, so that we still go through the rest of the array
if self.currentAvatar < table.getn(self.avatarModel) then
self.currentAvatar = self.currentAvatar + 1
-- check to make sure that the next x in the array aren't in the same position
while self.avatarModel[self.currentAvatar].x == x and self.avatarModel[self.currentAvatar].y == y and self.currentAvatar < table.getn(self.avatarModel) do
self.currentAvatar = self.currentAvatar + 1
end
end
end
end
if empty == false then
-- if this block is selected, draw a white quad to indicate selection
-- TODO: animate, slowly flash
if x == self.selected[1] and y == self.selected[2] then
love.graphics.push()
love.graphics.translate((y-x) * (tile:getWidth() / 2), ((x+y) * (tile:getHeight() / 4)) - ((tile:getHeight() / 2) * (table.getn(grid) / 2)) - (tile:getHeight() / 2)*grid[x][y].height)
love.graphics.quad("line", 0, (BLOCK_HEIGHT / 2), BLOCK_HEIGHT, 0, BLOCK_WIDTH, (BLOCK_HEIGHT / 2), BLOCK_HEIGHT, BLOCK_HEIGHT)
love.graphics.pop()
end
end
end
end
love.graphics.pop()
end
-- Save and Load functions --
function Terrain:loadGrid(fileName)
file = love.filesystem.newFile(fileName)
file:open("r")
grid = {}
data = {}
splitData = {}
j = 1
for line in file:lines() do
data[j] = line
j = j + 1
end
for key, value in pairs(data) do
grid[key] = {}
splitData[key] = {}
splitData[key] = string.split(value, ",")
for k, v in pairs(splitData[key]) do
splitData[key][k] = {}
splitData[key][k] = string.split(v, " ")
for a, b in pairs(splitData[key][k]) do
bl = Block.create(tonumber(splitData[key][k][1]), tonumber(splitData[key][k][2]))
grid[key][k] = bl
-- key and k are x and y respectively, so look at those
end
end
end
-- TODO: ensure that the grid built is always of equal width and height to avoid hell bug
-- Or fix hell bug
self.grid = grid
file:close()
end
function Terrain:saveGridAndAvatars(gridFile, avatarFile)
self:saveGrid(gridFile);
self:saveAvatars(avatarFile);
end
function Terrain:loadGridAndAvatars(gridFile, avatarFile)
self:loadGrid(gridFile);
self:loadAvatars(avatarFile);
end
function Terrain:saveGrid(fileName)
file = love.filesystem.newFile(fileName)
file:open("w")
for x = 1, table.getn(self.grid) do
for y = 1, table.getn(self.grid[1]) do
file:write(self.grid[x][y].type)
file:write(" ")
file:write(self.grid[x][y].height)
if y ~= table.getn(self.grid[1]) then
file:write(",")
end
end
if x ~= table.getn(self.grid) then
file:write("\n")
end
end
file:close()
end
-- TODO: In future, have both Grid and Avatars written to same file maybe? Two lines in between to separate?
function Terrain:saveAvatars(filename)
file = love.filesystem.newFile(filename)
file:open("w")
for i = 1, table.getn(self.avatarModel) do
-- write filename, x and y, height. Add other attributes later.
local avatar = self.avatarModel[i]
file:write(avatar.imgName)
file:write(" ")
file:write(avatar.x)
file:write(" ")
file:write(avatar.y)
file:write(" ")
file:write(avatar.height)
-- one record per line
file:write("\n")
end
file:close()
end
function Terrain:loadAvatars(filename)
file = love.filesystem.newFile(filename)
file:open("r")
local avatars = {}
j = 1
for line in file:lines() do
avatars[j] = line
j = j + 1
end
-- unlike grid, each line is own avatar
for key, value in pairs(avatars) do
avTable = string.split(value, " ")
av = {}
-- create new avatar
-- data intentionally written to file in same order as args
av = Avatar.create(avTable[1], tonumber(avTable[2]), tonumber(avTable[3]), tonumber(avTable[4]))
-- add avatar to the model
self:addAvatar(av)
end
file:close()
end
--BLOCKTYPE = {["empty"] = 0, ["grass"] = 1, ["rock"] = 2, ["blueprint"] = 3} -- allows you to easily create the new block based on name
--BLOCKFILE = {[1] = "grass.png", [2] = "dirt.png", [3] = "blueprint.png"} -- stores the file names for the blocks to be drawn
function Terrain:saveBlockTypes(filename)
-- format will be one line for #, name, filename
-- will separate into the 2 arrays in load
local file = love.filesystem.newFile(filename)
file:open("w")
-- 1 name filename
-- write empty first because it's special
file:write("0 empty")
file:write("\n")
local counter = 1
for key, value in pairs(BLOCKTYPE) do
if value ~= 0 then
local s = value .. " " .. key .. " "
local fn = BLOCKFILE[value]
s = s .. fn
file:write(s)
if counter < table.getn(BLOCKFILE) then
file:write("\n")
end
counter = counter + 1
end
end
file:close()
end
function Terrain:loadBlockTypes(filename)
local file = love.filesystem.newFile(filename)
file:open("r")
local blockTypes = {}
j = 1
for line in file:lines() do
blockTypes[j] = string.split(line, " ")
j = j + 1
end
-- first one will always be the empty block, which as no filename
for i = 1, table.getn(blockTypes) do
local index = blockTypes[i][1]
local name = blockTypes[i][2]
BLOCKTYPE[name] = tonumber(index)
if i > 1 then
local fn = blockTypes[i][3]
BLOCKFILE[tonumber(index)] = fn
end
end
file:close()
end
function Terrain:addBlock(x, y, blockType, height)
-- check if x and y are already within the bounds of the grid
if x >= 1 and x <= table.getn(self.grid) and y >= 1 and y <= table.getn(self.grid[1]) then
-- if this is the case, only replace the existing block if the height or type is different
if height ~= self.grid[x][y].height or blockType ~= self.grid[x][y].type then
self.grid[x][y] = Block.create(blockType, height)
end
end
-- Old code when adding new columns and rows was allowed
-- add new columns
--[[local newGrid = {}
for gx = 1, math.max(table.getn(self.grid), x) do
newGrid[gx] = {}
for gy = 1, math.max(table.getn(self.grid[1]), y) do
if (gx <= table.getn(self.grid)) and (gy <= table.getn(self.grid[1])) then -- if exists already, just transfer
newGrid[gx][gy] = self.grid[gx][gy]
elseif gx == x and gy == y then -- if we're on the new block we want to add
newGrid[gx][gy] = Block.create(blockType, height)
else
newGrid[gx][gy] = Block.create(0, 0)
end
end
end
self.grid = newGrid
--self:recenter() ]]
end
function Terrain:removeBlock(x, y)
if x >= 1 and x <= table.getn(self.grid) and y >= 1 and y <= table.getn(self.grid[1]) then
self.grid[x][y] = Block.create(BLOCKTYPE["empty"], 0)
end
end
function Terrain:addBlockType(name, fileName)
-- first, check if file exists
-- provide user with an error if not
if love.filesystem.isFile(fileName) == false then
return false
end
-- append to BLOCKTYPE and BLOCKFILE
-- number will be current size + 1
local number = table.getn(BLOCKFILE) + 1
BLOCKTYPE[name] = number
BLOCKFILE[number] = fileName
return true
end
function Terrain:clickCheckHeight()
if table.getn(self.grid) == 0 then
return
end
-- make sure we don't get out of bounds results
if self.selected[1] < 1 or self.selected[1] > table.getn(self.grid) then
return
elseif self.selected[2] < 1 or self.selected[2] > table.getn(self.grid[1]) then
return
end
diffX = table.getn(self.grid) - self.selected[1]
diffY = table.getn(self.grid[1]) - self.selected[2]
diff = math.min(diffX, diffY)
-- check if a tile overlaps the currently selected tile; will overlap if its height is equal to its x AND y distance from current
for i = 1, diff do
if self.grid[self.selected[1] + i][self.selected[2] + i].height == i then
self.selected[1] = self.selected[1] + i
self.selected[2] = self.selected[2] + i
break
end
end
end
function Terrain:rotate(angle)
-- want to rotate the underlying array, then drawing should take care of everything
-- initialize the new grid so we have something to work with
newGrid = {}
for i = 1, table.getn(self.grid[1]) do
newGrid[i] = {}
for j = 1, table.getn(self.grid) do
newGrid[i][j] = 0
end
end
for x = 1, table.getn(self.grid) do
for y = 1, table.getn(self.grid[1]) do
mat = {x, y, 1} -- matrix representing current index
c = math.round(math.cos(math.pi/2))
s = math.round(math.sin(math.pi/2))
rotMat = {}
if (angle == "left") then
rotMat = {{c, s, 0}, {-s, c, 0}, {0, 0, 1}}
transMat = {{1, 0, 0}, {0, 1, table.getn(self.grid) + 1}, {0, 0, 1}}
elseif angle == "right" then
rotMat = {{c, -s, 0}, {s, c, 0}, {0, 0, 1}}
transMat = {{1, 0, table.getn(self.grid[1]) + 1}, {0, 1, 0}, {0, 0, 1}}
end
res = self:multiplyMatrices(rotMat, mat) -- multiply the index matrix and the rotation matrix
-- this will leave you outside of the bounds of the array
-- use a translation matrix to return to the correct position
res = self:multiplyMatrices(transMat, res)
tx = res[1] -- new x index
ty = res[2] -- new y index
newGrid[tx][ty] = self.grid[x][y] -- take the value from the original location in grid and put it into the new grid
end
end
self.grid = newGrid -- set the rotated grid to be the grid
self:rotateAvatars(angle)
end
function Terrain:rotateAvatars(angle)
-- easiest way to maintain sort is just to readd everything
-- so, get a copy of what is in arrayModel, then empty arrayModel and readd everything with the new coordinates
local oldModel = self.avatarModel
self.avatarModel = {}
for i = 1, table.getn(oldModel) do
local avatar = oldModel[i]
local mat = {avatar.x, avatar.y, 1}
c = math.round(math.cos(math.pi/2))
s = math.round(math.sin(math.pi/2))
rotMat = {}
if (angle == "left") then
rotMat = {{c, s, 0}, {-s, c, 0}, {0, 0, 1}}
transMat = {{1, 0, 0}, {0, 1, table.getn(self.grid) + 1}, {0, 0, 1}}
elseif angle == "right" then
rotMat = {{c, -s, 0}, {s, c, 0}, {0, 0, 1}}
transMat = {{1, 0, table.getn(self.grid[1]) + 1}, {0, 1, 0}, {0, 0, 1}}
end
res = self:multiplyMatrices(rotMat, mat) -- multiply the index matrix and the rotation matrix
-- this will leave you outside of the bounds of the array
-- use a translation matrix to return to the correct position
res = self:multiplyMatrices(transMat, res)
tx = res[1] -- new x index
ty = res[2] -- new y index
local newAvatar = Avatar.create(avatar.imgName, tx, ty, avatar.height)
newAvatar.id = avatar.id
newAvatar.animations = avatar.animations
newAvatar.activeAnimation = avatar.activeAnimation
newAvatar.animating = avatar.animating
self:addAvatar(newAvatar)
end
end
function Terrain:translate(xDist, yDist)
-- check to make sure we haven't translated too far. Don't want to be able to let the map off of the screen entirely, regardless of size.
self.x = self.x + xDist
self.y = self.y + yDist
end
function Terrain:multiplyMatrices(m1, m2)
result = {}
tempres = 0
for i = 1, table.getn(m1) do
for j = 1, table.getn(m1[1]) do
tempres = tempres + m1[i][j] * m2[j]
end
result[i] = tempres
tempres = 0
end
return result
end
-- TODO: fix bug introduced by changing center of grid (adding new blocks)
function Terrain:selectTileFromMouse(x, y)
-- from http://laserbrainstudios.com/2010/08/the-basics-of-isometric-programming/
-- TODO: strongly consider switching height to 1 indexed to match everything else
x = x - (self.x + BLOCK_WIDTH)
y = y - (self.y - BLOCK_WIDTH) + (BLOCK_HEIGHT / 2)
TileX = math.floor((y / BLOCK_HEIGHT) + (x / BLOCK_WIDTH))
TileY = math.floor((y / BLOCK_HEIGHT) - (x / BLOCK_WIDTH))
self.selected = {TileY, TileX + 1}
self:clickCheckHeight()
end
function Terrain:addAvatar(avatar)
-- check if the avatar has an id other equal to 0 - it's new and we need to give it a new id
if avatar.id == 0 then
-- if it's new, make sure it isn't trying to occupy the same space as another avatar
for i = 1, table.getn(self.avatarModel) do
if avatar.x == self.avatarModel[i].x and avatar.y == self.avatarModel[i].y then
return false
end
end
avatar.id = self.nextAvId
self.nextAvId = self.nextAvId + 1
end
-- if this is the first avatar, just add it
if table.getn(self.avatarModel) > 0 then
self:insertAvatarSorted(avatar)
else -- otherwise, add at correct position to maintain sorted array
table.insert(self.avatarModel, avatar)
end
end
function Terrain:insertAvatarSorted(avatar)
-- rememeber, we can assume that the current array is already sorted
for i = 1, table.getn(self.avatarModel) do
local current = self.avatarModel[i]
if avatar.x == current.x then
-- compare y's
if avatar.y == current.y then
table.insert(self.avatarModel, i, avatar)
return
elseif avatar.y < current.y then
table.insert(self.avatarModel, i, avatar)
return
end
elseif avatar.x < current.x then
-- place here
table.insert(self.avatarModel, i, avatar)
return
end -- if greater, just move on to next in loop
end
table.insert(self.avatarModel, avatar)
end
-- TODO: add z-index
-- TODO: issue when moving past other avatars by x + 1. Jumps over all the rows containing another avatar.
-- y only seems to be an issue when walking directly into them
function Terrain:moveAvatar(avatar, x, y)
-- make sure we're only accepting integer input
x = math.floor(x)
y = math.floor(y)
for i = 1, table.getn(self.avatarModel) do
if self.avatarModel[i] == avatar then
-- ensure that x and y are within the bounds of the grid
if not self:withinGridBounds(self.avatarModel[i].x + x, self.avatarModel[i].y + y) then
return
end
-- make sure that this block is actually at a height that the avatar can move to
-- and that they are adjacent. Avatars will be able to move in a path later.
if math.abs(x) <= 1 and math.abs(y) <= 1 then -- check adjacent
local currentLocation = self.grid[self.avatarModel[i].x][self.avatarModel[i].y]
if math.abs(self.grid[self.avatarModel[i].x + x][self.avatarModel[i].y + y].height - currentLocation.height) <= self.avatarModel[i].jumpLimit then -- check jump distance
self.avatarModel[i]:move(x, y)
-- to maintain sort, should actually remove and readd avatar
local tempAvatar = self.avatarModel[i]
table.remove(self.avatarModel, i)
self:addAvatar(tempAvatar)
end
end
end
end
end
-- returns the avatar, which can then be used to move it
-- will be used to fix the issue with rotating messing with avatar movement
function Terrain:getAvatarById(id)
if id > table.getn(self.avatarModel) then
return false
end
for i = 1, table.getn(self.avatarModel) do
local av = self.avatarModel[i]
if av.id == id then
return av
end
end
end
function Terrain:withinGridBounds(x, y)
return x >= 1 and x <= table.getn(self.grid) and y >= 1 and y <= table.getn(self.grid[1])
end
-- Utility Functions
-- from http://www.programmer-tutorials.com/lua-tutorial-clamp-number-inbetween-ranges/
function math.clamp(input, min_val, max_val)
if input < min_val then
input = min_val
elseif input > max_val then
input = max_val
end
return input
end
function math.round(input)
lower = math.floor(input)
if input - lower >= 0.5 then
return math.ceil(input)
else
return lower
end
end
-- from Stack Overflow: http://stackoverflow.com/questions/1426954/split-string-in-lua by krsk9999
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end