2020-07-06 12:38:05 +00:00
|
|
|
extends TileMap
|
|
|
|
|
|
|
|
class_name MapTiles
|
|
|
|
|
|
|
|
export(NodePath) var extended_tilemap_node
|
|
|
|
export var occluders = ["Wall"]
|
|
|
|
|
2020-07-06 17:23:42 +00:00
|
|
|
export var replace_with = {
|
|
|
|
"Window": "Floor"
|
|
|
|
}
|
|
|
|
|
2020-07-06 12:38:05 +00:00
|
|
|
export var shadow_intensity = 0.2
|
|
|
|
|
|
|
|
onready var extended_tilemap = get_node(extended_tilemap_node) as TileMap
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# Make occluders for FOV
|
|
|
|
make_occluders()
|
|
|
|
|
|
|
|
# Convert 2x2 tiles to 1x1 if possible
|
|
|
|
convert_extended()
|
|
|
|
|
|
|
|
func convert_extended():
|
|
|
|
var extended = extended_tilemap.tile_set
|
|
|
|
for id in tile_set.get_tiles_ids():
|
|
|
|
var name = tile_set.tile_get_name(id)
|
|
|
|
var extended_id = extended.find_tile_by_name(name)
|
|
|
|
if extended_id < 0:
|
|
|
|
# Not found, skip it
|
|
|
|
continue
|
2020-07-06 17:23:42 +00:00
|
|
|
# Check if a replacement exists (for flooring)
|
|
|
|
var replacement = -1
|
|
|
|
if replace_with.has(name):
|
|
|
|
replacement = tile_set.find_tile_by_name(replace_with[name])
|
2020-07-06 12:38:05 +00:00
|
|
|
# Find all uses of this tile
|
|
|
|
for cell in get_used_cells_by_id(id):
|
|
|
|
var x = cell.x * 2
|
|
|
|
var y = cell.y * 2
|
|
|
|
extended_tilemap.set_cell(x, y, extended_id)
|
|
|
|
extended_tilemap.set_cell(x+1, y, extended_id)
|
|
|
|
extended_tilemap.set_cell(x, y+1, extended_id)
|
|
|
|
extended_tilemap.set_cell(x+1, y+1, extended_id)
|
2020-07-06 17:23:42 +00:00
|
|
|
set_cellv(cell, replacement)
|
2020-07-06 12:38:05 +00:00
|
|
|
extended_tilemap.update_bitmask_region()
|
|
|
|
extended_tilemap.update_dirty_quadrants()
|
|
|
|
|
|
|
|
func make_occluders():
|
|
|
|
var occluder_ids = []
|
|
|
|
for occluder_name in occluders:
|
|
|
|
occluder_ids.push_back(tile_set.find_tile_by_name(occluder_name))
|
|
|
|
for id in tile_set.get_tiles_ids():
|
|
|
|
if occluder_ids.find(id) < 0:
|
|
|
|
# Not an occluder, skip it
|
|
|
|
continue
|
|
|
|
# Find all uses of this tile
|
|
|
|
for cell in get_used_cells_by_id(id):
|
|
|
|
# Check sides
|
|
|
|
var occluder = Occluder.new()
|
|
|
|
occluder.ignore_sides = [
|
|
|
|
occluder_ids.find(get_cell(cell.x, cell.y-1)) >= 0, # Top
|
|
|
|
occluder_ids.find(get_cell(cell.x+1, cell.y)) >= 0, # Right
|
|
|
|
occluder_ids.find(get_cell(cell.x, cell.y+1)) >= 0, # Bottom
|
|
|
|
occluder_ids.find(get_cell(cell.x-1, cell.y)) >= 0 # Left
|
|
|
|
]
|
|
|
|
occluder.transform.origin = map_to_world(cell)
|
|
|
|
add_child(occluder)
|
|
|
|
|
|
|
|
|
|
|
|
func set_occluder_origin(origin):
|
|
|
|
for child in get_children():
|
|
|
|
if child is Occluder:
|
|
|
|
child.origin = origin
|