summaryrefslogtreecommitdiff
path: root/scripts/path_generator.gd
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/path_generator.gd')
-rw-r--r--scripts/path_generator.gd67
1 files changed, 61 insertions, 6 deletions
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