From f922fe4669080d1633e0a345a3f8981867c9e841 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Fri, 3 Jun 2022 21:13:19 -0600
Subject: add working world gen, fixed food placing, minor refactoring

---
 src/tools/world_generator/scenes/WalkerHead.tscn   |   6 ++
 src/tools/world_generator/scenes/WalkerUnit.tscn   |   6 ++
 .../world_generator/scenes/WorldGenerator.tscn     |  65 ++++++++++++++
 src/tools/world_generator/scripts/walker_head.gd   |  70 +++++++++++++++
 src/tools/world_generator/scripts/walker_unit.gd   |  98 +++++++++++++++++++++
 .../world_generator/scripts/world_generator.gd     |  12 +++
 src/tools/world_generator/sprites/black.png        | Bin 0 -> 99 bytes
 src/tools/world_generator/sprites/black.png.import |  35 ++++++++
 src/tools/world_generator/sprites/white.png        | Bin 0 -> 99 bytes
 src/tools/world_generator/sprites/white.png.import |  35 ++++++++
 10 files changed, 327 insertions(+)
 create mode 100644 src/tools/world_generator/scenes/WalkerHead.tscn
 create mode 100644 src/tools/world_generator/scenes/WalkerUnit.tscn
 create mode 100644 src/tools/world_generator/scenes/WorldGenerator.tscn
 create mode 100644 src/tools/world_generator/scripts/walker_head.gd
 create mode 100644 src/tools/world_generator/scripts/walker_unit.gd
 create mode 100644 src/tools/world_generator/scripts/world_generator.gd
 create mode 100644 src/tools/world_generator/sprites/black.png
 create mode 100644 src/tools/world_generator/sprites/black.png.import
 create mode 100644 src/tools/world_generator/sprites/white.png
 create mode 100644 src/tools/world_generator/sprites/white.png.import

(limited to 'src/tools/world_generator')

diff --git a/src/tools/world_generator/scenes/WalkerHead.tscn b/src/tools/world_generator/scenes/WalkerHead.tscn
new file mode 100644
index 0000000..a62b9ed
--- /dev/null
+++ b/src/tools/world_generator/scenes/WalkerHead.tscn
@@ -0,0 +1,6 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://tools/world_generator/scripts/walker_head.gd" type="Script" id=1]
+
+[node name="WalkerHead" type="Node2D"]
+script = ExtResource( 1 )
diff --git a/src/tools/world_generator/scenes/WalkerUnit.tscn b/src/tools/world_generator/scenes/WalkerUnit.tscn
new file mode 100644
index 0000000..bf10698
--- /dev/null
+++ b/src/tools/world_generator/scenes/WalkerUnit.tscn
@@ -0,0 +1,6 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://tools/world_generator/scripts/walker_unit.gd" type="Script" id=1]
+
+[node name="WalkerUnit" type="Node2D"]
+script = ExtResource( 1 )
diff --git a/src/tools/world_generator/scenes/WorldGenerator.tscn b/src/tools/world_generator/scenes/WorldGenerator.tscn
new file mode 100644
index 0000000..32ed939
--- /dev/null
+++ b/src/tools/world_generator/scenes/WorldGenerator.tscn
@@ -0,0 +1,65 @@
+[gd_scene load_steps=8 format=2]
+
+[ext_resource path="res://tools/world_generator/sprites/white.png" type="Texture" id=1]
+[ext_resource path="res://tools/world_generator/sprites/black.png" type="Texture" id=2]
+[ext_resource path="res://tools/world_generator/scenes/WalkerUnit.tscn" type="PackedScene" id=3]
+[ext_resource path="res://tools/world_generator/scenes/WalkerHead.tscn" type="PackedScene" id=4]
+[ext_resource path="res://tools/world_generator/scripts/world_generator.gd" type="Script" id=5]
+
+[sub_resource type="ConvexPolygonShape2D" id=2]
+points = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 )
+
+[sub_resource type="TileSet" id=1]
+0/name = "white.png 0"
+0/texture = ExtResource( 1 )
+0/tex_offset = Vector2( 0, 0 )
+0/modulate = Color( 1, 1, 1, 1 )
+0/region = Rect2( 0, 0, 16, 16 )
+0/tile_mode = 0
+0/occluder_offset = Vector2( 0, 0 )
+0/navigation_offset = Vector2( 0, 0 )
+0/shape_offset = Vector2( 0, 0 )
+0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
+0/shape_one_way = false
+0/shape_one_way_margin = 0.0
+0/shapes = [  ]
+0/z_index = 0
+1/name = "black.png 1"
+1/texture = ExtResource( 2 )
+1/tex_offset = Vector2( 0, 0 )
+1/modulate = Color( 1, 1, 1, 1 )
+1/region = Rect2( 0, 0, 16, 16 )
+1/tile_mode = 0
+1/occluder_offset = Vector2( 0, 0 )
+1/navigation_offset = Vector2( 0, 0 )
+1/shape_offset = Vector2( 0, 0 )
+1/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
+1/shape = SubResource( 2 )
+1/shape_one_way = false
+1/shape_one_way_margin = 1.0
+1/shapes = [ {
+"autotile_coord": Vector2( 0, 0 ),
+"one_way": false,
+"one_way_margin": 1.0,
+"shape": SubResource( 2 ),
+"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 )
+} ]
+1/z_index = 0
+
+[node name="WorldGenerator" type="Node2D"]
+script = ExtResource( 5 )
+TILEMAP_NP = NodePath("TileMap")
+
+[node name="TileMap" type="TileMap" parent="."]
+tile_set = SubResource( 1 )
+cell_size = Vector2( 16, 16 )
+cell_quadrant_size = 4
+format = 1
+
+[node name="WalkerHead" parent="." instance=ExtResource( 4 )]
+TILEMAP_NP = NodePath("../TileMap")
+WALKER_UNIT_NP = ExtResource( 3 )
+
+[node name="Camera" type="Camera2D" parent="."]
+current = true
+zoom = Vector2( 5, 5 )
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
diff --git a/src/tools/world_generator/sprites/black.png b/src/tools/world_generator/sprites/black.png
new file mode 100644
index 0000000..d59329c
Binary files /dev/null and b/src/tools/world_generator/sprites/black.png differ
diff --git a/src/tools/world_generator/sprites/black.png.import b/src/tools/world_generator/sprites/black.png.import
new file mode 100644
index 0000000..baaf7e2
--- /dev/null
+++ b/src/tools/world_generator/sprites/black.png.import
@@ -0,0 +1,35 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/black.png-c132963832bee437653237bacfeb5776.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://tools/world_generator/sprites/black.png"
+dest_files=[ "res://.import/black.png-c132963832bee437653237bacfeb5776.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=false
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=false
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+process/normal_map_invert_y=false
+stream=false
+size_limit=0
+detect_3d=false
+svg/scale=1.0
diff --git a/src/tools/world_generator/sprites/white.png b/src/tools/world_generator/sprites/white.png
new file mode 100644
index 0000000..a449dec
Binary files /dev/null and b/src/tools/world_generator/sprites/white.png differ
diff --git a/src/tools/world_generator/sprites/white.png.import b/src/tools/world_generator/sprites/white.png.import
new file mode 100644
index 0000000..434e753
--- /dev/null
+++ b/src/tools/world_generator/sprites/white.png.import
@@ -0,0 +1,35 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/white.png-a9cc063b7d400d5bbfefc663f8a2b60f.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://tools/world_generator/sprites/white.png"
+dest_files=[ "res://.import/white.png-a9cc063b7d400d5bbfefc663f8a2b60f.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=false
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=false
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+process/normal_map_invert_y=false
+stream=false
+size_limit=0
+detect_3d=false
+svg/scale=1.0
-- 
cgit v1.2.3-70-g09d2