summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-05-22 21:30:44 -0400
committerbd <bdunahu@operationnull.com>2025-05-22 21:30:44 -0400
commit69013e2a35969fbb52cd0c3db07787698eb8c5a3 (patch)
tree8888d3d9b7b49b73800779329df8206b5d0d5c0d
parent5af13cd2c4e87c6e273d4c8d84c06cd6a320b427 (diff)
Move path draw function to path_generator
-rw-r--r--scripts/map_generator.gd65
-rw-r--r--scripts/path_generator.gd67
2 files changed, 68 insertions, 64 deletions
diff --git a/scripts/map_generator.gd b/scripts/map_generator.gd
index 4ef2aa5..0b92ab1 100644
--- a/scripts/map_generator.gd
+++ b/scripts/map_generator.gd
@@ -1,70 +1,19 @@
extends Node2D
-@onready var tile_map : TileMapLayer = $TileMapLayer
@onready var map_config : MapGeneratorResource = preload("res://resources/map_generator_resource.tres")
var _path : Array[Vector2i]
-
-enum TileTransform {
- ROTATE_0 = 0,
- ROTATE_90 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_H,
- ROTATE_180 = TileSetAtlasSource.TRANSFORM_FLIP_H | TileSetAtlasSource.TRANSFORM_FLIP_V,
- ROTATE_270 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_V,
-}
+var _pg : PathGenerator
func _ready() -> void:
- _generate_path()
- _draw_path()
+ _make_path()
-func _generate_path():
- var pg : PathGenerator = PathGenerator.new()
+func _make_path():
+ _pg = PathGenerator.new($TileMapLayer)
_path = []
- while _path.size() < 35:
- _path = pg.generate_path()
- _draw_path()
-
-func _draw_path():
- for i in _path:
- var score : int = _get_tile_score(_path, i)
-
- var atlas_coords : Vector2i = map_config.path_empty_atlas_coords
- var rot : TileTransform = TileTransform.ROTATE_0
-
- match score:
- 2, 8, 10:
- atlas_coords = map_config.path_straight_atlas_coords
- rot = TileTransform.ROTATE_90
- 1, 4, 5:
- atlas_coords = map_config.path_straight_atlas_coords
- 3:
- atlas_coords = map_config.path_corner_atlas_coords
- rot = TileTransform.ROTATE_270
- 6:
- atlas_coords = map_config.path_corner_atlas_coords
- 12:
- atlas_coords = map_config.path_corner_atlas_coords
- rot = TileTransform.ROTATE_90
- 9:
- atlas_coords = map_config.path_corner_atlas_coords
- rot = TileTransform.ROTATE_180
-
- _display_tile(atlas_coords, rot, i)
-
-func _display_tile(coords : Vector2i, rot : TileTransform, pos : Vector2i):
- tile_map.set_cell(pos, 0, coords, rot)
-
-func _get_tile_score(path : Array[Vector2i], tile : Vector2i) -> int:
- var score : int = 0
- var x : int = tile.x
- var y : int = tile.y
-
- score += 1 if path.has(Vector2i(x, y - 1)) else 0
- score += 2 if path.has(Vector2i(x + 1, y)) else 0
- score += 4 if path.has(Vector2i(x, y + 1)) else 0
- score += 8 if path.has(Vector2i(x - 1, y)) else 0
-
- return score
+ while _path.size() < map_config.min_path_size:
+ _path = _pg.generate_path()
+ _pg.draw_path()
func get_enemy_path() -> Array[Vector2i]:
return _path
-
diff --git a/scripts/path_generator.gd b/scripts/path_generator.gd
index cf48175..30ffd3c 100644
--- a/scripts/path_generator.gd
+++ b/scripts/path_generator.gd
@@ -3,24 +3,79 @@ extends Object
class_name PathGenerator
var map_config : MapGeneratorResource = preload("res://resources/map_generator_resource.tres")
+var _tile_map : TileMapLayer
+var _path : Array[Vector2i]
+
+enum TileTransform {
+ ROTATE_0 = 0,
+ ROTATE_90 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_H,
+ ROTATE_180 = TileSetAtlasSource.TRANSFORM_FLIP_H | TileSetAtlasSource.TRANSFORM_FLIP_V,
+ ROTATE_270 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_V,
+}
+
+func _init(tile_map_layer: TileMapLayer):
+ _tile_map = tile_map_layer
func generate_path() -> Array[Vector2i]:
- var path : Array[Vector2i]
+ _path.clear()
var x : int = 0
var y : int = randi_range(1, map_config.grid_height - 3)
while x < map_config.grid_width:
- if not path.has(Vector2i(x,y)):
- path.append(Vector2i(x, y))
+ if not _path.has(Vector2i(x,y)):
+ _path.append(Vector2i(x, y))
var choice : int = randi_range(0,2)
# every even tile goes right to leave room for towers
if choice == 0 || x % 2 == 0 || x == map_config.grid_width - 1:
x += 1
- elif choice == 1 && y < map_config.grid_height - 3 && !path.has(Vector2i(x, y + 1)):
+ elif choice == 1 && y < map_config.grid_height - 3 && !_path.has(Vector2i(x, y + 1)):
y += 1
- elif choice == 2 && y > 1 && !path.has(Vector2i(x, y - 1)):
+ elif choice == 2 && y > 1 && !_path.has(Vector2i(x, y - 1)):
y -= 1
- return path
+
+ return _path
+
+func draw_path():
+ for i in _path:
+ var score : int = _get_tile_score(_path, i)
+
+ var atlas_coords : Vector2i = map_config.path_empty_atlas_coords
+ var rot : TileTransform = TileTransform.ROTATE_0
+
+ match score:
+ 2, 8, 10:
+ atlas_coords = map_config.path_straight_atlas_coords
+ rot = TileTransform.ROTATE_90
+ 1, 4, 5:
+ atlas_coords = map_config.path_straight_atlas_coords
+ 3:
+ atlas_coords = map_config.path_corner_atlas_coords
+ rot = TileTransform.ROTATE_270
+ 6:
+ atlas_coords = map_config.path_corner_atlas_coords
+ 12:
+ atlas_coords = map_config.path_corner_atlas_coords
+ rot = TileTransform.ROTATE_90
+ 9:
+ atlas_coords = map_config.path_corner_atlas_coords
+ rot = TileTransform.ROTATE_180
+
+ _display_tile(atlas_coords, rot, i)
+
+func _display_tile(coords : Vector2i, rot : TileTransform, pos : Vector2i):
+ _tile_map.set_cell(pos, 0, coords, rot)
+
+func _get_tile_score(path : Array[Vector2i], tile : Vector2i) -> int:
+ var score : int = 0
+ var x : int = tile.x
+ var y : int = tile.y
+
+ score += 1 if path.has(Vector2i(x, y - 1)) else 0
+ score += 2 if path.has(Vector2i(x + 1, y)) else 0
+ score += 4 if path.has(Vector2i(x, y + 1)) else 0
+ score += 8 if path.has(Vector2i(x - 1, y)) else 0
+
+ return score