General code improvements, progress step tracker implementation for worldgen, and work towards enemy behavior

This commit is contained in:
2024-01-23 21:45:52 +01:00
parent 3f465a708c
commit 49562ba9ce
11 changed files with 517 additions and 214 deletions

View File

@ -1,59 +1,67 @@
class_name StandardEntityMarker
extends Node2D
enum SPAWN_METHOD {
ALL,
FURTHEST,
ONCE,
NONE
}
## The method used to spawn entities
##
## ALL: Spawns all entities in the list
## FURTHEST: Spawns the entity furthest from the player
## ONCE: Spawns the entity once at a random marker
## NONE: Does not spawn any entities
enum EntitySpawnMethod { ALL, FURTHEST, ONCE, NONE }
enum ENTITY_SELECTION_METHOD {
RANDOM,
POP
}
## The method used to select entities
##
## RANDOM: Selects a random entity from the list
## POP: Removes the entity from the list after it is selected
enum EntitySelectionMethod { RANDOM, POP }
@export_category("Entity Spawning")
@export
var marker_id: String
## The ID representing the type of this marker
@export var marker_id: String
@export
var entities: Array[PackedScene] = []
## The entities that can be spawned by this marker
@export var entities: Array[PackedScene] = []
@export
var spawn_method: SPAWN_METHOD
## The method used to spawn entities
@export var spawn_method: EntitySpawnMethod
@export_range(0, 1.0)
var spawn_chance: float = 1.0
## The chance that an entity will be spawned
@export_range(0, 1.0) var spawn_chance: float = 1.0
@export
var entity_selection_method: ENTITY_SELECTION_METHOD
## The method used to select entities
@export var entity_selection_method: EntitySelectionMethod
@export_category("Marker Debug")
@export
var debug_color: Color = Color.WHITE
## The color of the debug sprite
@export var debug_color: Color = Color.WHITE
## The size of the debug sprite
@export var marker_size: int = 16
@export
var marker_size: int = 16
func _ready() -> void:
self.add_to_group("genv2:entity_marker")
## Create a debug sprite if one doesn't exist
var sprite: Sprite2D = self.get_node_or_null("Sprite2D")
if not sprite:
sprite = Sprite2D.new()
sprite.texture = _get_debug_texture()
self.add_child(sprite)
func _register(gen: StandardWorldGenerator) -> void:
## Registers this marker with the world generator
func register(gen: StandardWorldGenerator) -> void:
if self.marker_id == "":
push_error("Marker ID is empty, this marker will not be used for spawning entities.")
return
gen._register_marker("genv2:entity_marker:%s" % self.marker_id, self)
gen.register_marker("genv2:entity_marker:%s" % self.marker_id, self)
## Generates a texture for the debug sprite
func _get_debug_texture() -> Texture2D:
var texture = GradientTexture2D.new()
var gradient = Gradient.new()
gradient.colors = [self.debug_color]
@ -61,5 +69,5 @@ func _get_debug_texture() -> Texture2D:
texture.width = self.marker_size
texture.height = self.marker_size
return texture

View File

@ -9,18 +9,27 @@ const TILEMAP_LAYER = 0
@export var top: bool = false
@export var bottom: bool = false
var room_size: Vector2i
var cell_size: Vector2i
## The rect that the room occupies in the map
## from 0, 0 to room_size.x, room_size.y
var room_size: Vector2i
## The size of each tile in the room
var cell_size: Vector2i
func _init() -> void:
self.room_size = self.get_used_rect().size
self.cell_size = self.tile_set.tile_size
## Returns true when the room has no exits
func is_block() -> bool:
if self.left or self.right or self.top or self.bottom:
return false
return true
## Get the position of the exit in the room
func get_exits() -> Array[Vector2i]:
var exits: Array[Vector2i] = []
if self.left:
@ -33,6 +42,8 @@ func get_exits() -> Array[Vector2i]:
exits.append(Vector2i.DOWN)
return exits
## Get the position of the exit in the room
func get_exit_pos(exit: Vector2i) -> Vector2i:
var pos: Vector2i = Vector2i.ZERO
if exit == Vector2i.LEFT:
@ -45,75 +56,122 @@ func get_exit_pos(exit: Vector2i) -> Vector2i:
pos = Vector2i(self.room_size.x / 2, self.room_size.y - 1)
return pos
## Get the offset of the exit position from the edge of the room
func get_exit_pos_offset(edge_pos: Vector2i, exit: Vector2i) -> Vector2i:
match exit:
Vector2i.UP:
return edge_pos + Vector2i(self.room_size.x/-2, self.room_size.y * -1)
return edge_pos + Vector2i(self.room_size.x / -2, self.room_size.y * -1)
Vector2i.DOWN:
return edge_pos + Vector2i(self.room_size.x/-2, 1)
return edge_pos + Vector2i(self.room_size.x / -2, 1)
Vector2i.LEFT:
return edge_pos + Vector2i(-self.room_size.x, -self.room_size.y/2)
return edge_pos + Vector2i(-self.room_size.x, -self.room_size.y / 2)
Vector2i.RIGHT:
return edge_pos + Vector2i(1, -self.room_size.y/2)
return edge_pos + Vector2i(1, -self.room_size.y / 2)
_:
return Vector2i.ZERO
## Copy the room to the TileMap at a given position
func copy_to_map(map: TileMap) -> void:
for x in range(0, self.room_size.x):
for y in range(0, self.room_size.y):
var tile = self.get_cell_source_id(TILEMAP_LAYER, Vector2i(x, y))
var alt = self.get_cell_alternative_tile(TILEMAP_LAYER, Vector2i(x, y))
if tile != -1:
map.set_cell(TILEMAP_LAYER, Vector2i(position.x + x, position.y + y), tile, Vector2i(0, 0), alt)
else:
map.set_cell(TILEMAP_LAYER, Vector2i(position.x + x, position.y + y), -1, Vector2i(0, 0))
if tile != -1:
map.set_cell(
TILEMAP_LAYER,
Vector2i(position.x + x, position.y + y),
tile,
Vector2i(0, 0),
alt
)
else:
map.set_cell(
TILEMAP_LAYER, Vector2i(position.x + x, position.y + y), -1, Vector2i(0, 0)
)
## Seal the exit of a room by placing a tile over it
func seal_exit(map: TileMap, pos: Vector2i, exit: Vector2i) -> void:
var exit_pos = get_exit_pos(exit)
var seal_tile_id = 0
match exit:
Vector2i.UP:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0)
)
Vector2i.DOWN:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x - 1, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x + 1, pos.y), seal_tile_id, Vector2i(0, 0)
)
Vector2i.RIGHT:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0)
)
Vector2i.LEFT:
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0))
map.set_cell(TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0))
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y - 1), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y), seal_tile_id, Vector2i(0, 0)
)
map.set_cell(
TILEMAP_LAYER, exit_pos + Vector2i(pos.x, pos.y + 1), seal_tile_id, Vector2i(0, 0)
)
## Check if the room overlaps with any tiles in the map
func is_overlapping(map: TileMap) -> bool:
for x in range(0, self.room_size.x):
for y in range(0, self.room_size.y):
var tile = map.get_cell_source_id(TILEMAP_LAYER, Vector2i(self.position.x + x, self.position.y + y))
var tile = map.get_cell_source_id(
TILEMAP_LAYER, Vector2i(self.position.x + x, self.position.y + y)
)
if tile != -1:
return true
return false
# Validate room by checking that the top left tile is at 0, 0 and all exits are valid
func _validate() -> bool:
# Check for tiles where x is negative
pass
# Check for tiles where y is negative
pass
## Validate room by checking that the top left tile is at 0, 0 and all exits are valid
func _validate() -> bool:
# If exit on left, check that the middle tile on the left is empty
if self.left and self.get_cell_source_id(TILEMAP_LAYER, Vector2i(0, self.room_size.y / 2)) != -1:
if (
self.left
and self.get_cell_source_id(TILEMAP_LAYER, Vector2i(0, self.room_size.y / 2)) != -1
):
push_error("Room with exit on left must have empty middle tile on left")
return false
# If exit on right, check that the middle tile on the right is empty
if self.right and self.get_cell_source_id(TILEMAP_LAYER, Vector2i(self.room_size.x - 1, self.room_size.y / 2)) != -1:
if (
self.right
and (
self.get_cell_source_id(
TILEMAP_LAYER, Vector2i(self.room_size.x - 1, self.room_size.y / 2)
)
!= -1
)
):
push_error("Room with exit on right must have empty middle tile on right")
return false
# If exit on top, check that the middle tile on the top is empty
@ -121,18 +179,32 @@ func _validate() -> bool:
push_error("Room with exit on top must have empty middle tile on top")
return false
# If exit on bottom, check that the middle tile on the bottom is empty
if self.bottom and self.get_cell_source_id(TILEMAP_LAYER, Vector2i(self.room_size.x / 2, self.room_size.y - 1)) != -1:
if (
self.bottom
and (
self.get_cell_source_id(
TILEMAP_LAYER, Vector2i(self.room_size.x / 2, self.room_size.y - 1)
)
!= -1
)
):
push_error("Room with exit on bottom must have empty middle tile on bottom")
return false
return true
## Flip a vector
func flip_vector(vector: Vector2i) -> Vector2i:
return Vector2i(vector.x * -1, vector.y * -1)
## Disable a room exit by setting the corresponding exit variable to false
func disable_room_exit_opposite(exit: Vector2i) -> void:
self.disable_room_exit(self.flip_vector(exit))
## Disable a room exit by setting the corresponding exit variable to false
func disable_room_exit(exit: Vector2i) -> void:
if exit == Vector2i.LEFT:
self.left = false