1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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]:
_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))
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)):
y += 1
elif choice == 2 && y > 1 && !_path.has(Vector2i(x, y - 1)):
y -= 1
return _path
func draw_path():
for i in _path:
var score : int = _get_tile_score(_path, i)
var atlas_coords : Vector2i = map_config.atlas_coords["PATH_EMPTY"]
var rot : TileTransform = TileTransform.ROTATE_0
match score:
2, 8, 10:
atlas_coords = map_config.atlas_coords["PATH_STRAIGHT"]
rot = TileTransform.ROTATE_90
1, 4, 5:
atlas_coords = map_config.atlas_coords["PATH_STRAIGHT"]
3:
atlas_coords = map_config.atlas_coords["PATH_CORNER"]
rot = TileTransform.ROTATE_270
6:
atlas_coords = map_config.atlas_coords["PATH_CORNER"]
12:
atlas_coords = map_config.atlas_coords["PATH_CORNER"]
rot = TileTransform.ROTATE_90
9:
atlas_coords = map_config.atlas_coords["PATH_CORNER"]
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
|