Now About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-09-29 23:04:54 +0100
committerLucas Fryzek <lucas.fryzek@fryzekconcepts.com>2024-09-29 23:04:54 +0100
commite668733042757942c0719a3c5966796b1032ba93 (patch)
treeab40716f1c7d4a1c3e3efb6bd8200b91377b07ec
parenta10570d865106dcaf1037979cad1788da03e989d (diff)
bsp_level_generator: Add more tiles & move tile logic to level gen
Move more of the tile generation logic like player starting pos, level exit, enemy location, etc to bsp_level_generation and have dungeon_manager just place nodes based on whats in the grid.
-rw-r--r--scripts/bsp_level_generator.gd53
-rw-r--r--scripts/dungeon_manager.gd68
-rw-r--r--scripts/tile.gd2
3 files changed, 69 insertions, 54 deletions
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd
index 0f38b96..543eb1d 100644
--- a/scripts/bsp_level_generator.gd
+++ b/scripts/bsp_level_generator.gd
@@ -133,8 +133,8 @@ func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void:
func get_tile(grid: Array[Tile.Tile], pos: Vector2i) -> Tile.Tile:
return grid[pos.y * grid_width + pos.x]
-
-func populate_grid(grid: Array[Tile.Tile]) -> void:
+
+func place_lockers(grid: Array[Tile.Tile]) -> void:
for room in rooms:
var num_lockers = randi_range(0, 2)
print("Generating ", num_lockers, "Lockers")
@@ -153,6 +153,55 @@ func populate_grid(grid: Array[Tile.Tile]) -> void:
and get_tile(grid, pos + Vector2i(0, -1)) != Tile.Tile.DOOR:
grid[pos.y * grid_width + pos.x] = Tile.Tile.LOCKER
found = true
+
+func place_player(grid: Array[Tile.Tile]) -> Rect2i:
+ var player_room: Rect2i = rooms.pick_random()
+ var player_pos = Vector2i(player_room.position.x + player_room.size.x / 2, player_room.position.y + player_room.size.y / 2)
+
+ grid[player_pos.y * grid_width + player_pos.x] = Tile.Tile.PLAYER
+ return player_room
+
+func place_enemies(grid: Array[Tile.Tile], player_room: Rect2i) -> void:
+ var num_enemies = randi_range(4, 10)
+
+ for i in range(num_enemies):
+ var found_pos = false
+ while not found_pos:
+ var room: Rect2i = rooms.pick_random()
+ if room == player_room:
+ continue
+
+ var pos = Vector2i(randi_range(room.position.x, room.end.x - 1), randi_range(room.position.y, room.end.y - 1))
+ if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR:
+ grid[pos.y * grid_width + pos.x] = Tile.Tile.ENEMY
+ found_pos = true
+
+func place_exit(grid: Array[Tile.Tile], player_room: Rect2i) -> void:
+ var found_pos = false
+
+ while not found_pos:
+ var room: Rect2i = rooms.pick_random()
+ if room == player_room:
+ continue
+
+ var pos = Vector2i(randi_range(room.position.x, room.end.x - 1), randi_range(room.position.y, room.end.y - 1))
+ var left = pos + Vector2i.LEFT
+ var right = pos + Vector2i.RIGHT
+ var up = pos + Vector2i.UP
+ var down = pos + Vector2i.DOWN
+ if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR \
+ and grid[left.y * grid_width + left.x] == Tile.Tile.FLOOR \
+ and grid[right.y * grid_width + right.x] == Tile.Tile.FLOOR \
+ and grid[up.y * grid_width + up.x] == Tile.Tile.FLOOR \
+ and grid[down.y * grid_width + down.x] == Tile.Tile.FLOOR:
+ grid[pos.y * grid_width + pos.x] = Tile.Tile.PORTAL
+ found_pos = true
+
+func populate_grid(grid: Array[Tile.Tile]) -> void:
+ place_lockers(grid)
+ var player_room = place_player(grid)
+ place_exit(grid, player_room)
+ place_enemies(grid, player_room)
func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vector3) -> void:
var index = len(array[Mesh.ARRAY_VERTEX])
diff --git a/scripts/dungeon_manager.gd b/scripts/dungeon_manager.gd
index 39b6131..e08814f 100644
--- a/scripts/dungeon_manager.gd
+++ b/scripts/dungeon_manager.gd
@@ -19,56 +19,24 @@ func spawn_level() -> void:
bsp_level.connect("grid_generated", _grid_generated)
$LevelContainer.add_child(bsp_level)
-func create_enemies() -> void:
- var num_enemies = randi_range(4, 10)
-
- for i in range(num_enemies):
- var enemy = enemy_inst.instantiate()
-
- # Target is controlled through enemy wander AI
- enemy.target = null
-
- var found_pos = false
- while not found_pos:
- var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1))
-
- if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR:
+func place_objects() -> void:
+ for y in range(grid_height):
+ for x in range(grid_width):
+ var pos = Vector2i(x, y)
+ if grid[pos.y * grid_width + pos.x] == Tile.Tile.PLAYER:
+ $Player.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5)
+ elif grid[pos.y * grid_width + pos.x] == Tile.Tile.ENEMY:
+ var enemy = enemy_inst.instantiate()
+ # Target is controlled through enemy wander AI
+ enemy.target = null
enemy.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5)
- found_pos = true
- print("Placing at ", pos)
-
- $LevelContainer.add_child.call_deferred(enemy)
-
-func create_exit() -> void:
- var found_pos = false
- var exit: Area3D = exit_inst.instantiate()
- exit.connect("body_entered", _on_player_exit)
- while not found_pos:
- var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1))
- var left = pos + Vector2i.LEFT
- var right = pos + Vector2i.RIGHT
- var up = pos + Vector2i.UP
- var down = pos + Vector2i.DOWN
-
- if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR \
- and grid[left.y * grid_width + left.x] == Tile.Tile.FLOOR \
- and grid[right.y * grid_width + right.x] == Tile.Tile.FLOOR \
- and grid[up.y * grid_width + up.x] == Tile.Tile.FLOOR \
- and grid[down.y * grid_width + down.x] == Tile.Tile.FLOOR:
- exit.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5)
- found_pos = true
-
- $LevelContainer.add_child.call_deferred(exit)
+ $LevelContainer.add_child.call_deferred(enemy)
+ elif grid[pos.y * grid_width + pos.x] == Tile.Tile.PORTAL:
+ var exit: Area3D = exit_inst.instantiate()
+ exit.connect("body_entered", _on_player_exit)
+ exit.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5)
+ $LevelContainer.add_child.call_deferred(exit)
-func place_player() -> void:
- var found_pos = false
- while not found_pos:
- var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1))
-
- if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR:
- $Player.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5)
- found_pos = true
-
func reset_level() -> void:
for child in $LevelContainer.get_children():
print("Child is ", child)
@@ -90,6 +58,4 @@ func _grid_generated(p_grid: Array[Tile.Tile], p_grid_width: int, p_grid_height:
grid_width = p_grid_width
grid_height = p_grid_height
- create_enemies()
- create_exit()
- place_player()
+ place_objects()
diff --git a/scripts/tile.gd b/scripts/tile.gd
index 38bc445..384b42c 100644
--- a/scripts/tile.gd
+++ b/scripts/tile.gd
@@ -1,3 +1,3 @@
extends Node
-enum Tile {FLOOR, WALL, DOOR, LOCKER}
+enum Tile {FLOOR, WALL, DOOR, LOCKER, PLAYER, ENEMY, PORTAL}