jackmakesthings / game-dev

return False; an experiment in AI and decision-making, via Godot Engine
GNU General Public License v2.0
3 stars 2 forks source link

tilemaps: implement fill/delete functions #48

Closed jackmakesthings closed 8 years ago

jackmakesthings commented 8 years ago

Should make working with tilemaps and navpolys much less annoying.

extends Navigation2D

var tmap = null

var size_vector = Vector2(2000,2000)
var walk_tile = 0
var blocked_tile = 1
var invisible_blocked_tile = 3

func _ready():
    tmap = get_node("TileMap")
    fill_tilemap() # change empty tiles to invisible tile with just navigation poly
    delete_squares() # remove nav tiles near the walls
    remove_child(tmap) # readd tilemap, otherwise navigation poly wont work
    add_child(tmap) 

func fill_tilemap(): 
    var tset = tmap.get_tileset()
    var end = tmap.world_to_map(size_vector)
    for x in range(end.x):
        for y in range(end.y):
            if tmap.get_cell(x,y) == -1:
                tmap.set_cell(x,y, walk_tile)

func delete_squares():
    var end = tmap.world_to_map(size_vector)
    for x in range(end.x):
        for y in range(end.y):
            if tmap.get_cell(x,y) == blocked_tile:
                tmap.set_cell(x,y, invisible_blocked_tile)

# below this is optional, creates buffers around walls

                for xx in [-1,0,1]:
                    for yy in [-1,0,1]:
                        if xx == 0 and yy == 0:
                            continue
                        if tmap.get_cell(x+xx,y+yy) == 0:
                            tmap.set_cell(x+xx,y+yy, invisible_blocked_tile)