###Map:
This part is the biggest and probably the messiest.
I have tried to make it as optimized as I could think of and as simple to read as I could, but this is far from perfect.
This is still good enough to create a 2000x2000 map in about 10 seconds with no residual lag afterwards.
It is super important that the walls do not have collision. Or at least this was the only way I found to make it work without the tiles being created at the wrong position when clicking on a wall.
Make sure this script is added to the Map node.
####Code:
extends Spatial
# Map variables
export var mapWidth = 20
export var mapHeight = 20
var mapOffsetX
var mapOffsetY
onready var undergroundBase = find_node("UndergroundBase")
# grid holding the tiles themselves
var tileGrid = []
func deleteFloors(posX, posY):
if tileGrid[posY][posX]:
undergroundBase.remove_child(tileGrid[posY][posX])
tileGrid[posY][posX] = null
updateWalls(posX, posY)
func updateFloors(posX, posY, tileId):
if tileGrid[posY][posX]:
undergroundBase.remove_child(tileGrid[posY][posX])
# create a tile at the location of the mouse after deleting the old one.
if tileId == 0:
tileGrid[posY][posX] = createFloor(posX, posY, tileId)
if tileId == 1:
tileGrid[posY][posX] = createFloor(posX, posY, tileId, "#b3d733", "#4b5039")
if tileId == 2:
tileGrid[posY][posX] = createFloor(posX, posY, tileId, "#3dbfbf", "#395050")
if tileId == 3:
tileGrid[posY][posX] = createFloor(posX, posY, tileId,"#f300f8", "#7c327e")
updateWalls(posX, posY)
func updateWalls(mouseX, mouseY):
var tile = tileGrid[mouseY][mouseX]
# Checks if the tile at the north has a wall or not.
if tileGrid[mouseY-1][mouseX]:
var temp = tileGrid[mouseY-1][mouseX]
# If there is one and we are placing a tile, remove it.
if temp.south == 1 and tile:
temp.remove_child(temp.southWall)
temp.south = 0
# If there is no tile at our current positon, add a wall.
if temp.south == 0 and !tile:
temp.southWall = createWall(temp, 0, 0.5, 0, -90)
temp.south = 1
else:
# If there is a tile at our position, and there is none north of us, add a wall.
if tile:
tile.north = 1
# Checks if the tile at the south has a wall or not.
if tileGrid[mouseY+1][mouseX]:
var temp = tileGrid[mouseY+1][mouseX]
# If there is one and we are placing a tile, remove it.
if temp.north == 1 and tile:
temp.remove_child(temp.northWall)
temp.north = 0
# If there is no tile at our current positon, add a wall.
elif temp.north == 0 and !tile:
temp.northWall = createWall(temp, 0, -0.5, 0, 90)
temp.north = 1
else:
# If there is a tile at our position, and there is none south of us, add a wall.
if tile:
tile.south = 1
# Checks if the tile at the east has a wall or not.
if tileGrid[mouseY][mouseX+1]:
var temp = tileGrid[mouseY][mouseX+1]
# If there is one and we are placing a tile, remove it.
if temp.west == 1 and tile:
temp.remove_child(temp.westWall)
temp.west = 0
# If there is no tile at our current positon, add a wall.
elif temp.west == 0 and !tile:
temp.westWall = createWall(temp, -0.5, 0, -90, 0)
temp.west = 1
else:
# If there is a tile at our position, and there is none east of us, add a wall.
if tile:
tile.east = 1
# Checks if the tile at the west has a wall or not.
if tileGrid[mouseY][mouseX-1]:
var temp = tileGrid[mouseY][mouseX-1]
# If there is one and we are placing a tile, remove it.
if temp.east == 1 and tile:
temp.remove_child(temp.eastWall)
temp.east = 0
# If there is no tile at our current positon, add a wall.
elif temp.east == 0 and !tile:
temp.eastWall = createWall(temp, 0.5, 0, 90, 0)
temp.east = 1
else:
# If there is a tile at our position, and there is none west of us, add a wall.
if tile:
tile.west = 1
# If there is a tile and the desired wall direction is active, create a child wall
# at that position and rotation.
if tile:
# North
if tile.north == 1:
tile.northWall = createWall(tile, 0, -0.5, 0, 90)
# South
if tile.south == 1:
tile.southWall = createWall(tile, 0, 0.5, 0, -90)
# East
if tile.east == 1:
tile.eastWall = createWall(tile, 0.5, 0, 90, 0)
# West
if tile.west == 1:
tile.westWall = createWall(tile, -0.5, 0, -90, 0)
Had to seperate this into multiple posts, sorry about that.