diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/bsp_level_generator.gd | 35 | ||||
-rw-r--r-- | scripts/dungeon_manager.gd | 36 | ||||
-rw-r--r-- | scripts/enemy.gd | 14 | ||||
-rw-r--r-- | scripts/tile.gd | 3 |
4 files changed, 64 insertions, 24 deletions
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd index ac095ef..6985469 100644 --- a/scripts/bsp_level_generator.gd +++ b/scripts/bsp_level_generator.gd @@ -10,15 +10,15 @@ extends Node3D @export var wall_thickness: float = 1.3 @onready var mesh: MeshInstance3D = $MeshInstance3D +enum Direction {LEFT, RIGHT} + +signal grid_generated(grid: Array[Tile], p_grid_width: int, p_grid_height: int) var min_room_size: int = min_dim * min_dim var grid_width = width + 1 var grid_height = height + 1 -enum Direction {LEFT, RIGHT} -enum Tile {FLOOR, WALL} - class BSPNode: var axis: int var min_dims: Vector2i @@ -64,11 +64,11 @@ func generate_level(axis: int, min_space: Vector2i, max_space: Vector2i, depth: func is_leaf(node: BSPNode) -> bool: return node.left == null and node.right == null -func generate_grid(map: BSPNode, grid: Array[Tile]): +func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void: if is_leaf(map): for y in range(map.min_dims.y, map.max_dims.y - 1): for x in range(map.min_dims.x, map.max_dims.x - 1): - grid[(y+1) * grid_width + (x+1)] = Tile.FLOOR + grid[(y+1) * grid_width + (x+1)] = Tile.Tile.FLOOR else: generate_grid(map.left, grid) generate_grid(map.right, grid) @@ -93,15 +93,15 @@ func generate_grid(map: BSPNode, grid: Array[Tile]): # Check if there are two spaces to connect # And ensure no door already exists in the space - if grid[room_left.y * grid_width + room_left.x] == Tile.FLOOR \ - and grid[room_right.y * grid_width + room_right.x] == Tile.FLOOR \ - and grid[door_pos.y * grid_width + door_pos.x] == Tile.WALL: + if grid[room_left.y * grid_width + room_left.x] == Tile.Tile.FLOOR \ + and grid[room_right.y * grid_width + room_right.x] == Tile.Tile.FLOOR \ + and grid[door_pos.y * grid_width + door_pos.x] == Tile.Tile.WALL: have_door = true # Place grid for mesh - grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR + grid[door_pos.y * grid_width + door_pos.x] = Tile.Tile.FLOOR door_pos[map.axis] -= 1 - grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR + grid[door_pos.y * grid_width + door_pos.x] = Tile.Tile.FLOOR tries += 1 if tries > 1000: @@ -109,7 +109,7 @@ func generate_grid(map: BSPNode, grid: Array[Tile]): get_tree().quit() return -func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vector3): +func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vector3) -> void: var index = len(array[Mesh.ARRAY_VERTEX]) array[Mesh.ARRAY_VERTEX].append(wall_thickness*pos) array[Mesh.ARRAY_VERTEX].append(wall_thickness*(pos+dim[0])) @@ -128,14 +128,14 @@ func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vec array[Mesh.ARRAY_INDEX].append(index + 2) array[Mesh.ARRAY_INDEX].append(index + 3) -func generate_geo(grid: Array[Tile], array: Array): +func generate_geo(grid: Array[Tile.Tile], array: Array) -> void: for y in range(grid_height): for x in range(grid_width): var tile = grid[y * grid_width + x] - if tile == Tile.FLOOR: + if tile == Tile.Tile.FLOOR: generate_plane(array, Vector3(x, 0, y), [Vector3(1, 0, 0), Vector3(0, 0, 1)], Vector3.UP) - elif tile == Tile.WALL: + elif tile == Tile.Tile.WALL: generate_plane(array, Vector3(x, 2, y), [Vector3(0, 0, 1), Vector3(0, -2, 0)], Vector3.RIGHT) generate_plane(array, Vector3(x, 2, y+1), [Vector3(1, 0, 0), Vector3(0, -2, 0)], Vector3.BACK) generate_plane(array, Vector3(x+1, 2, y+1), [Vector3(0, 0, -1), Vector3(0, -2, 0)], Vector3.LEFT) @@ -147,9 +147,9 @@ func _ready() -> void: var min_space = Vector2i(0, 0) var max_space = Vector2i(width, height) var map = generate_level(starting_axis, min_space, max_space) - var grid: Array[Tile] = [] + var grid: Array[Tile.Tile] = [] grid.resize(grid_width * grid_height) - grid.fill(Tile.WALL) + grid.fill(Tile.Tile.WALL) var surface_array = [] surface_array.resize(Mesh.ARRAY_MAX) @@ -159,11 +159,10 @@ func _ready() -> void: surface_array[Mesh.ARRAY_NORMAL] = PackedVector3Array() generate_grid(map, grid) + grid_generated.emit(grid, grid_width, grid_height) generate_geo(grid, surface_array) mesh.mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) var tri_mesh = mesh.mesh.create_trimesh_shape() $NavigationRegion3D/StaticBody3D/CollisionShape3D.shape = tri_mesh $NavigationRegion3D.bake_navigation_mesh() - - #ResourceSaver.save(mesh.mesh, "res://map.tres", ResourceSaver.FLAG_COMPRESS) diff --git a/scripts/dungeon_manager.gd b/scripts/dungeon_manager.gd new file mode 100644 index 0000000..270a5a0 --- /dev/null +++ b/scripts/dungeon_manager.gd @@ -0,0 +1,36 @@ +extends Node3D + +var grid: Array[Tile.Tile] = [] +var grid_width: int +var grid_height: int + +var enemy_inst = preload("res://prefabs/enemy.tscn") + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + +func create_enemies() -> void: + var num_enemies = randi_range(1, 10) + + for i in range(num_enemies): + var enemy = enemy_inst.instantiate() + enemy.target = $Player + + 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: + enemy.position = Vector3(pos.x, 0, pos.y) + found_pos = true + print("Placing at ", pos) + + add_child.call_deferred(enemy) + +func _grid_generated(p_grid: Array[Tile.Tile], p_grid_width: int, p_grid_height: int) -> void: + grid = p_grid + grid_width = p_grid_width + grid_height = p_grid_height + + create_enemies() diff --git a/scripts/enemy.gd b/scripts/enemy.gd index 716493c..7060ab2 100644 --- a/scripts/enemy.gd +++ b/scripts/enemy.gd @@ -18,7 +18,8 @@ var astar: AStar3D var tar_vec: Vector3 var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity") -var original_color: Color +var original_material: StandardMaterial3D +@onready var red_material = preload("res://models/red_material.tres") var mesh_ready = false @@ -27,6 +28,7 @@ func _ready() -> void: motion_mode = MotionMode.MOTION_MODE_GROUNDED NavigationServer3D.connect("map_changed", map_changed) + original_material = $MeshInstance3D.get_active_material(0) func map_changed(_rid): print("Map changed") @@ -117,9 +119,7 @@ func _physics_process(delta: float) -> void: func on_hit(_hit_position: Vector3) -> void: if $HitTimer.is_stopped(): - var material: StandardMaterial3D = $MeshInstance3D.get_active_material(0) - original_color = material.albedo_color - material.albedo_color = Color(1.0, 0.0, 0.0, 1.0) + $MeshInstance3D.set_surface_override_material(0, red_material) $HitTimer.start() func _on_level_generator_astar_created(in_astar: AStar3D) -> void: @@ -127,6 +127,8 @@ func _on_level_generator_astar_created(in_astar: AStar3D) -> void: has_astar = true func _on_hit_timer_timeout() -> void: - var material: StandardMaterial3D = $MeshInstance3D.get_active_material(0) - material.albedo_color = original_color + $MeshInstance3D.set_surface_override_material(0, original_material) $HitTimer.stop() + + get_parent().remove_child(self) + queue_free() diff --git a/scripts/tile.gd b/scripts/tile.gd new file mode 100644 index 0000000..6243b9c --- /dev/null +++ b/scripts/tile.gd @@ -0,0 +1,3 @@ +extends Node + +enum Tile {FLOOR, WALL} |