summaryrefslogtreecommitdiff
path: root/src/tools/world_generator/scripts
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-03 21:13:19 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-03 21:13:19 -0600
commitf922fe4669080d1633e0a345a3f8981867c9e841 (patch)
tree7c2c98bfca20fda37fe34ba99e2d3caa9a39ca02 /src/tools/world_generator/scripts
parent898877f09808691a5e5d45850d27ae85f270db16 (diff)
add working world gen, fixed food placing, minor refactoring
Diffstat (limited to 'src/tools/world_generator/scripts')
-rw-r--r--src/tools/world_generator/scripts/walker_head.gd70
-rw-r--r--src/tools/world_generator/scripts/walker_unit.gd98
-rw-r--r--src/tools/world_generator/scripts/world_generator.gd12
3 files changed, 180 insertions, 0 deletions
diff --git a/src/tools/world_generator/scripts/walker_head.gd b/src/tools/world_generator/scripts/walker_head.gd
new file mode 100644
index 0000000..3bf7e3a
--- /dev/null
+++ b/src/tools/world_generator/scripts/walker_head.gd
@@ -0,0 +1,70 @@
+extends Node2D
+
+export(NodePath) var TILEMAP_NP: NodePath
+export(PackedScene) var WALKER_UNIT_NP: PackedScene
+export(int, 5, 100, 1) var starting_unit_count: int = 5
+export(int, 5, 10, 1) var initial_safe_zone_size: int = 5
+
+onready var tilemap: TileMap = get_node(TILEMAP_NP)
+
+var max_x: int = 0
+var max_y: int = 0
+var map_margin: int = 10
+var units: int = 0
+
+
+func _ready() -> void:
+ Event.connect("world_gen_walker_started", self, "_on_world_gen_walker_started")
+ Event.connect("world_gen_walker_finished", self, "_on_world_gen_walker_finished")
+ Event.connect("world_gen_walker_died", self, "_on_world_gen_walker_died")
+ Event.connect("world_gen_spawn_walker_unit", self, "_on_world_gen_spawn_walker_unit")
+ _place_safe_zone()
+ _spawn_walker_units()
+ _fill_empty_space()
+
+
+func _place_safe_zone() -> void:
+ for i in initial_safe_zone_size:
+ for j in initial_safe_zone_size:
+ tilemap.set_cell(i, j, Global.WORLD_TILE_PATH)
+ tilemap.set_cell(-i, -j, Global.WORLD_TILE_PATH)
+
+
+func _spawn_walker_units() -> void:
+ for i in starting_unit_count:
+ _spawn_walker_unit(Vector2.ZERO)
+
+
+func _spawn_walker_unit(spawn_position: Vector2) -> void:
+ var walker_unit: Node2D = WALKER_UNIT_NP.instance()
+ add_child(walker_unit)
+ units += 1
+ walker_unit.position = spawn_position
+ walker_unit.start(units)
+
+
+func _fill_empty_space() -> void:
+ var rect: Rect2 = tilemap.get_used_rect()
+ var margin: int = map_margin
+ for x in range(-margin, rect.size.x + margin):
+ for y in range (-margin, rect.size.y + margin ):
+ var poses: Vector2= Vector2(rect.position.x + x, rect.position.y + y)
+ if tilemap.get_cell(int(poses.x), int(poses.y)) == TileMap.INVALID_CELL:
+ tilemap.set_cellv(poses, Global.WORLD_TILE_WALL)
+
+
+func _on_world_gen_walker_started(id: int) -> void:
+ print("Walker unit %s started." % id)
+
+
+func _on_world_gen_walker_finished(id: int) -> void:
+ print("Walker unit %s finished." % id)
+
+
+func _on_world_gen_walker_died(id: int) -> void:
+ print("Walker unit %s died." % id)
+
+
+func _on_world_gen_spawn_walker_unit(location: Vector2) -> void:
+ print("Spawning new walking unit.")
+ _spawn_walker_unit(location)
diff --git a/src/tools/world_generator/scripts/walker_unit.gd b/src/tools/world_generator/scripts/walker_unit.gd
new file mode 100644
index 0000000..cfa8f91
--- /dev/null
+++ b/src/tools/world_generator/scripts/walker_unit.gd
@@ -0,0 +1,98 @@
+extends Node2D
+
+enum Direction {
+ LEFT,
+ RIGHT,
+ UP,
+ DOWN
+}
+
+var direction: Dictionary = {
+ Direction.LEFT: Vector2.LEFT,
+ Direction.RIGHT: Vector2.RIGHT,
+ Direction.UP: Vector2.UP,
+ Direction.DOWN: Vector2.DOWN
+}
+
+var _walker_head: Node2D
+var _tilemap: TileMap
+
+var given_birth: bool = false
+var walked_straight: bool = false
+
+var path_length: int
+var birth_chance: float
+var death_chance: float
+var walk_straight_chance: float
+var walk_straight_length: float
+
+
+func start(id: int) -> void:
+ Event.emit_signal("world_gen_walker_started", id)
+ _walker_head = get_parent()
+ _tilemap = _walker_head.tilemap
+ _randomize_stats()
+
+ var path_steps: Array = _get_path_steps()
+ var walker_died: bool = _set_path_tiles(path_steps)
+
+ if walker_died:
+ Event.emit_signal("world_gen_walker_died", id)
+ else:
+ Event.emit_signal("world_gen_walker_finished", id)
+ queue_free()
+
+
+func _randomize_stats() -> void:
+ randomize()
+ path_length = int(rand_range(50, 200))
+ birth_chance = rand_range(0.001, 0.01)
+ death_chance = rand_range(0.0, 0.005) / 2.0
+ walk_straight_chance = rand_range(0.001, 0.025)
+ walk_straight_length = rand_range(4, 20)
+
+
+func _get_path_steps() -> Array:
+ var path_steps: Array = []
+ var step: int
+ for _i in path_length:
+ step = randi() % Direction.size()
+ path_steps.append(step)
+
+ if randf() < walk_straight_chance and not walked_straight:
+ for j in walk_straight_length:
+ path_steps.append(step)
+ walked_straight = true
+ return path_steps
+
+
+func _set_path_tiles(path_steps: Array) -> bool:
+ # get initial tile location
+ var location: Vector2 = get_parent().global_position * Global.TILE_SIZE
+ var move_direction: Vector2
+ _set_tile(location, 0)
+ for step in path_steps:
+ move_direction = direction[step]
+ location += move_direction * Global.TILE_SIZE
+ _set_tile(location, Global.WORLD_TILE_PATH)
+
+ if randf() < birth_chance and not given_birth:
+ Event.emit_signal("world_gen_spawn_walker_unit", location)
+ given_birth = true
+
+ if randf() < death_chance:
+ return true
+ return false
+
+
+func _set_tile(location: Vector2, tile: int) -> void:
+ var coordinates: Vector2 = _tilemap.world_to_map(location)
+ _update_map_size(coordinates)
+ _tilemap.set_cellv(coordinates, tile)
+
+
+func _update_map_size(coordinates: Vector2) -> void:
+ if coordinates.x > _walker_head.max_x:
+ _walker_head.max_x = coordinates.x
+ if coordinates.y > _walker_head.max_y:
+ _walker_head.max_y = coordinates.y
diff --git a/src/tools/world_generator/scripts/world_generator.gd b/src/tools/world_generator/scripts/world_generator.gd
new file mode 100644
index 0000000..5e8626b
--- /dev/null
+++ b/src/tools/world_generator/scripts/world_generator.gd
@@ -0,0 +1,12 @@
+extends Node2D
+
+export(NodePath) var TILEMAP_NP: NodePath
+onready var tilemap: TileMap = get_node(TILEMAP_NP)
+
+
+func get_valid_map_coords() -> Array:
+ return tilemap.get_used_cells_by_id(Global.WORLD_TILE_PATH)
+
+
+func get_centered_world_position(location: Vector2) -> Vector2:
+ return tilemap.map_to_world(location) + Vector2.ONE * Global.TILE_SIZE / 2.0