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 tilemap_size : Vector2i = get_used_rect().end - get_used_rect().position
@onready var map_rect : Rect2i = Rect2i(Vector2i.ZERO, tilemap_size) @onready var map_rect : Rect2i = Rect2i(Vector2i.ZERO, tilemap_size)
var highlighted_tiles : Array = []
func _ready() -> void: func _ready() -> void:
var tile_size : Vector2i = get_tileset().tile_size var tile_size : Vector2i = get_tileset().tile_size
@ -15,6 +17,7 @@ func _ready() -> void:
setup_pathing() setup_pathing()
check_for_visible_gatherables()
pass pass
func setup_pathing() -> void: func setup_pathing() -> void:
@ -42,6 +45,8 @@ func update_passable() -> void:
var tile_data : TileData = get_cell_tile_data(0, coords) var tile_data : TileData = get_cell_tile_data(0, coords)
if tile_data and tile_data.get_custom_data('navtype') == 'wall': if tile_data and tile_data.get_custom_data('navtype') == 'wall':
astar.set_point_solid(coords) astar.set_point_solid(coords)
else:
astar.set_point_solid(coords, false)
pass pass
@ -51,3 +56,60 @@ func is_point_walkable(check_position : Vector2i) -> bool:
return true return true
return false 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

View file

@ -15,6 +15,10 @@ run/main_scene="res://levels/test_level.tscn"
config/features=PackedStringArray("4.2", "Forward Plus") config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[autoload]
GridUtil="*res://utilities/GridUtilitiesClass.gd"
[debug] [debug]
gdscript/warnings/untyped_declaration=1 gdscript/warnings/untyped_declaration=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cpoidoac1yoro"
path="res://.godot/imported/cave_tiles_expanded.png-d2f68687bfe493e51dd8f3d57d264494.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://resources/cave_tiles_expanded.png"
dest_files=["res://.godot/imported/cave_tiles_expanded.png-d2f68687bfe493e51dd8f3d57d264494.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

BIN
resources/mushrooms.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dlaroudodsshi"
path="res://.godot/imported/mushrooms.png-09d38c3deb1459623d3a7d9fbe50b0ba.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://resources/mushrooms.png"
dest_files=["res://.godot/imported/mushrooms.png-09d38c3deb1459623d3a7d9fbe50b0ba.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View file

@ -0,0 +1,18 @@
extends Node
class_name GridUtilitiesClass
## Get the tile coordinates of the surrounding tiles of the given tile
func get_surrounding_tiles(given_tile : Vector2, spread : int = 3) -> Array:
var surrounding_tiles : Array[Vector2] = []
var target_tile : Vector2
for y in spread:
for x in spread:
target_tile = given_tile + Vector2(x - 1, y - 1)
#if given_tile == target_tile:
# continue
surrounding_tiles.append(target_tile)
return surrounding_tiles