This commit is contained in:
2025-03-10 15:44:54 +08:00
parent 438eff1f7a
commit 495c9c64a5
97 changed files with 532 additions and 32 deletions
+83 -2
View File
@@ -23,11 +23,20 @@ var grid_data: Array = [] # 3D array [floor][row][column]
var astar_by_floor = {} # Dictionary of AStar2D instances per floor
var path = []
# Update the obstacle items array to use your specified item indices
@export var obstacle_items: Array[int] = [12, 13, 14, 15] # Updated mesh library indices
@export var obstacle_directions: Array[Direction] = [] # Store the directions of placed obstacles
# Direction and movement systems
enum Direction {
NORTHWEST, NORTH, NORTHEAST,
WEST, CENTER, EAST,
SOUTHWEST, SOUTH, SOUTHEAST
SOUTHWEST, SOUTH, SOUTHEAST,
BLOCKED_NORTH = 10,
BLOCKED_EAST = 11,
BLOCKED_SOUTH = 12,
BLOCKED_WEST = 13
}
var diagonal_movement: bool = false
@@ -299,7 +308,9 @@ func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInf
is_walkable = is_walkable and \
is_position_valid(adjacent1) and is_cell_walkable(adjacent1, floor_index) and \
is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index)
is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index) and \
not is_blocked_by_obstacle(current_pos, adjacent1, floor_index) and \
not is_blocked_by_obstacle(current_pos, adjacent2, floor_index)
if diagonal_movement or not is_diagonal_direction(dir):
neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
@@ -461,3 +472,73 @@ func _set(property, value):
func set_diagonal_movement(enable: bool):
diagonal_movement = enable
initialize_astar()
# Add this function to check if a movement is blocked by an obstacle
func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 0) -> bool:
# Check if there's a horizontal obstacle
if from_pos.y == to_pos.y: # Moving horizontally
# Check for vertical obstacles that would block horizontal movement
var min_x = min(from_pos.x, to_pos.x)
var max_x = max(from_pos.x, to_pos.x)
for x in range(min_x, max_x + 1):
var cell_index = get_cell_item(Vector3i(x, floor_index, from_pos.y))
if cell_index in obstacle_items:
var obstacle_idx = obstacle_items.find(cell_index)
if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
var dir = obstacle_directions[obstacle_idx]
if dir == Direction.BLOCKED_NORTH or dir == Direction.BLOCKED_SOUTH:
return true
# Check if there's a vertical obstacle
if from_pos.x == to_pos.x: # Moving vertically
# Check for horizontal obstacles that would block vertical movement
var min_y = min(from_pos.y, to_pos.y)
var max_y = max(from_pos.y, to_pos.y)
for y in range(min_y, max_y + 1):
var cell_index = get_cell_item(Vector3i(from_pos.x, floor_index, y))
if cell_index in obstacle_items:
var obstacle_idx = obstacle_items.find(cell_index)
if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
var dir = obstacle_directions[obstacle_idx]
if dir == Direction.BLOCKED_EAST or dir == Direction.BLOCKED_WEST:
return true
return false
func place_obstacle(pos: Vector3i, obstacle_item: int, direction: Direction) -> bool:
# Always place on floor 3
pos.y = 3
if get_cell_item(pos) != -1:
return false # Cell is already occupied
set_cell_item(pos, obstacle_item)
# Store the direction of the obstacle
var item_index = obstacle_items.find(obstacle_item)
if item_index == -1:
item_index = 0 # Default to first item if not found
while obstacle_directions.size() <= item_index:
obstacle_directions.append(Direction.CENTER) # Default
obstacle_directions[item_index] = direction
# Update the cell's orientation based on direction
var orientation = 0
match direction:
Direction.BLOCKED_NORTH:
orientation = 0 # Default orientation
Direction.BLOCKED_EAST:
orientation = 1 # 90 degrees clockwise
Direction.BLOCKED_SOUTH:
orientation = 2 # 180 degrees
Direction.BLOCKED_WEST:
orientation = 3 # 270 degrees clockwise
set_cell_item(pos, obstacle_item, orientation)
# Re-initialize A* pathfinding to account for the new obstacle
initialize_astar()
return true
@@ -0,0 +1 @@
uid://bja8ixryvthu0
@@ -0,0 +1 @@
uid://fmgdfwvicktv
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bnkcae3aoavgh"]
[ext_resource type="Script" path="res://addons/enhanced_gridmap/enhanced_gridmap_dock.gd" id="1_abcde"]
[ext_resource type="Script" uid="uid://fmgdfwvicktv" path="res://addons/enhanced_gridmap/enhanced_gridmap_dock.gd" id="1_abcde"]
[node name="Enhanced GridMap" type="ScrollContainer"]
anchors_preset = 15
@@ -1,8 +1,9 @@
[gd_resource type="MeshLibrary" load_steps=11 format=3 uid="uid://54tpx8cmksfc"]
[gd_resource type="MeshLibrary" load_steps=12 format=3 uid="uid://54tpx8cmksfc"]
[ext_resource type="ArrayMesh" uid="uid://dqguomxd16u0i" path="res://assets/models/meshes/start.res" id="1_xdwel"]
[ext_resource type="ArrayMesh" uid="uid://dspusnbkr74hg" path="res://assets/models/meshes/hover.res" id="2_5gp4i"]
[ext_resource type="Material" uid="uid://bsyh0x4cy5qyr" path="res://assets/models/meshes/end.tres" id="3_qi66w"]
[ext_resource type="ArrayMesh" uid="uid://dtr46jmckif0p" path="res://assets/models/meshes/block.res" id="4_8v5xv"]
[ext_resource type="ArrayMesh" uid="uid://d4himvyb81in8" path="res://assets/models/meshes/non-walkable.res" id="4_h83ju"]
[ext_resource type="ArrayMesh" uid="uid://bgvropltcot0q" path="res://assets/models/meshes/normal.res" id="5_san4u"]
[ext_resource type="ArrayMesh" uid="uid://36tgon3b60db" path="res://assets/models/tiles/tile_heart.tres" id="6_r6sve"]
@@ -18,65 +19,103 @@ size = Vector2(1, 1)
item/0/name = "normal"
item/0/mesh = ExtResource("1_xdwel")
item/0/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/0/mesh_cast_shadow = 1
item/0/shapes = []
item/0/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/0/navigation_layers = 1
item/1/name = "hover"
item/1/mesh = ExtResource("2_5gp4i")
item/1/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/1/mesh_cast_shadow = 1
item/1/shapes = []
item/1/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/1/navigation_layers = 1
item/2/name = "start"
item/2/mesh = ExtResource("1_xdwel")
item/2/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/2/mesh_cast_shadow = 1
item/2/shapes = []
item/2/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/2/navigation_layers = 1
item/3/name = "end"
item/3/mesh = SubResource("PlaneMesh_ti6kf")
item/3/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/3/mesh_cast_shadow = 1
item/3/shapes = []
item/3/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/3/navigation_layers = 1
item/4/name = "non-walkable"
item/4/mesh = ExtResource("4_h83ju")
item/4/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/4/mesh_cast_shadow = 1
item/4/shapes = []
item/4/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/4/navigation_layers = 1
item/6/name = "grass"
item/6/mesh = ExtResource("5_san4u")
item/6/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/6/mesh_cast_shadow = 1
item/6/shapes = []
item/6/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/6/navigation_layers = 1
item/7/name = "tile_heart"
item/7/mesh = ExtResource("6_r6sve")
item/7/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
item/7/mesh_cast_shadow = 1
item/7/shapes = []
item/7/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/7/navigation_layers = 1
item/8/name = "tile_diamond"
item/8/mesh = ExtResource("8_rj3ss")
item/8/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
item/8/mesh_cast_shadow = 1
item/8/shapes = []
item/8/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/8/navigation_layers = 1
item/9/name = "tile_star"
item/9/mesh = ExtResource("9_y8bbi")
item/9/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
item/9/mesh_cast_shadow = 1
item/9/shapes = []
item/9/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/9/navigation_layers = 1
item/10/name = "tile_coin"
item/10/mesh = ExtResource("9_44311")
item/10/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
item/10/mesh_cast_shadow = 1
item/10/shapes = []
item/10/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/10/navigation_layers = 1
item/11/name = ""
item/11/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/11/name = "obstacle_block"
item/11/mesh = ExtResource("4_8v5xv")
item/11/mesh_transform = Transform3D(1.65, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0.5)
item/11/mesh_cast_shadow = 1
item/11/shapes = []
item/11/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/11/navigation_mesh_transform = Transform3D(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
item/11/navigation_layers = 1
item/12/name = "obstacle_block_h"
item/12/mesh = ExtResource("4_8v5xv")
item/12/mesh_transform = Transform3D(1.65, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0.5)
item/12/mesh_cast_shadow = 1
item/12/shapes = []
item/12/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/12/navigation_layers = 1
item/13/name = "obstacle_block_v"
item/13/mesh = ExtResource("4_8v5xv")
item/13/mesh_transform = Transform3D(1.65, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0.5)
item/13/mesh_cast_shadow = 1
item/13/shapes = []
item/13/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/13/navigation_layers = 1
item/14/name = ""
item/14/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/14/mesh_cast_shadow = 1
item/14/shapes = []
item/14/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/14/navigation_layers = 1
item/15/name = ""
item/15/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/15/mesh_cast_shadow = 1
item/15/shapes = []
item/15/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
item/15/navigation_layers = 1
+1
View File
@@ -0,0 +1 @@
uid://fcdsysbyx6pt