Destructible terrain!

This commit is contained in:
Dan 2024-06-02 15:39:46 +01:00
parent cc80783d16
commit 9f224b3d69
9 changed files with 1548 additions and 188 deletions

View file

@ -0,0 +1,14 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://bpverily2kmvm"]
[ext_resource type="Shader" path="res://shaders/monochrome.gdshader" id="1_l5g1d"]
[resource]
shader = ExtResource("1_l5g1d")
shader_parameter/brightness = 0.0
shader_parameter/contrast = 1.0
shader_parameter/saturation = 0.0
shader_parameter/redVal = 1.0
shader_parameter/greenVal = 1.0
shader_parameter/blueVal = 1.0
shader_parameter/tint_color = Color(1, 1, 1, 1)
shader_parameter/tint_effect_factor = 0.0

View file

@ -7,6 +7,8 @@ var astar : AStarGrid2D = AStarGrid2D.new()
@onready var tilemap_size : Vector2i = get_used_rect().end - get_used_rect().position
@onready var map_rect : Rect2i = Rect2i(Vector2i.ZERO, tilemap_size)
var highlighted_tiles : Array = []
func _ready() -> void:
var tile_size : Vector2i = get_tileset().tile_size
@ -15,6 +17,7 @@ func _ready() -> void:
setup_pathing()
check_for_visible_gatherables()
pass
func setup_pathing() -> void:
@ -42,6 +45,8 @@ func update_passable() -> void:
var tile_data : TileData = get_cell_tile_data(0, coords)
if tile_data and tile_data.get_custom_data('navtype') == 'wall':
astar.set_point_solid(coords)
else:
astar.set_point_solid(coords, false)
pass
@ -51,3 +56,60 @@ func is_point_walkable(check_position : Vector2i) -> bool:
return true
return false
func _unhandled_input(event : InputEvent) -> void:
pass
if event.is_action_pressed("move_to"):
var click_position : Vector2 = get_global_mouse_position()
var tile_position : Vector2i = local_to_map(click_position)
Log.pr(tile_position)
## Check if the clicked tile is a wall
## Get the surrounding wall tiles and redraw them
## We spread to a 3x3 area for blowing up walls because it was very
## weird with 1x1 cells
var update_cells : Array = GridUtil.get_surrounding_tiles(tile_position)
set_cells_terrain_connect(0, update_cells, 0, 0)
## Get all the co-ordinates around the given tile_position coordinates
## This will redraw the surrounding cells to make the terrains look better
var surrounding_cells : Array = GridUtil.get_surrounding_tiles(tile_position, 5)
redraw_surrounding_tiles(surrounding_cells)
update_passable()
# Check if the tile is already highlighted
#if highlighted_tiles.has(tile_position):
# Change the tile to destroyed tile and remove from highlighted set
# set_cellv(tile_position, DESTROYED_TILE)
# highlighted_tiles.erase(tile_position)
func redraw_surrounding_tiles(positions : Array) -> void:
var walls : Array = []
for cell_pos : Vector2 in positions:
var tile_data : TileData = get_cell_tile_data(0, cell_pos)
if tile_data and tile_data.get_custom_data('navtype') == 'wall':
walls.append(cell_pos)
set_cells_terrain_connect(0, walls, 0, 1)
func check_for_visible_gatherables() -> void:
## Loop through the tiles on the gatherables layer and check if the thing on
# the layer below them is a wall, if it is they need to be hidden.
for i in tilemap_size.x:
for j in tilemap_size.y:
var coords : Vector2i = Vector2i(i, j)
var tile_data : TileData = get_cell_tile_data(1, coords)
if tile_data:
# Get the tile data from the base layer
var below_tile_data : TileData = get_cell_tile_data(0, coords)
if below_tile_data and below_tile_data.get_custom_data('navtype') == 'wall':
Log.pr("Gatherable is on a wall tile, it should be hidden!")
# Log.pr(below_tile_data)
#if tile_data and tile_data.get_custom_data('navtype') == 'gatherable':
# var below_tile_data : TileData = get_cell_tile_data(0, coords + Vector2i(0, 1))
# if below_tile_data and below_tile_data.get_custom_data('navtype') == 'wall':
# set_cellv(coords, -1)

File diff suppressed because one or more lines are too long