diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/path_generator.gd | 48 | ||||
-rw-r--r-- | scripts/path_tester.gd | 55 | ||||
-rw-r--r-- | scripts/path_tester.gd.uid | 1 | ||||
-rw-r--r-- | scripts/tile_manager.gd | 9 | ||||
-rw-r--r-- | scripts/tile_manager.gd.uid | 1 |
5 files changed, 107 insertions, 7 deletions
diff --git a/scripts/path_generator.gd b/scripts/path_generator.gd index fae2c76..e87a8f4 100644 --- a/scripts/path_generator.gd +++ b/scripts/path_generator.gd @@ -1,11 +1,45 @@ -extends Node +extends Object +class_name PathGenerator -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. +var _grid_width : int +var _grid_height : int +var _path : Array[Vector2i] -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass +func _init(width:int, height:int): + _grid_width = width + _grid_height = height + +func generate_path(): + _path.clear() + + var x : int = 0 + var y : int = randi_range(1, _grid_height-2) + + while x < _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 == _grid_width - 1: + x += 1 + elif choice == 1 && y < _grid_height - 2 && !_path.has(Vector2i(x, y + 1)): + y += 1 + elif choice == 2 && y > 1 && !_path.has(Vector2i(x, y - 1)): + y -= 1 + return _path + +func get_tile_score(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 diff --git a/scripts/path_tester.gd b/scripts/path_tester.gd new file mode 100644 index 0000000..5add640 --- /dev/null +++ b/scripts/path_tester.gd @@ -0,0 +1,55 @@ +extends Node2D + +@export var grid_width : int = 20 +@export var grid_height : int = 14 +@export var tile_size : int = 16 + +var _pg : PathGenerator +var path_straight_atlas_coords : Vector2i = Vector2i(8, 1) +var path_corner_atlas_coords : Vector2i = Vector2i(9, 1) +var path_empty_atlas_coords : Vector2i = Vector2i(0, 0) + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + _pg = PathGenerator.new(grid_width, grid_height) + _display_path() + +func _display_path(): + var _path : Array[Vector2i] = [] + while _path.size() < 35: + _path = _pg.generate_path() + + for i in _path: + var score : int = _pg.get_tile_score(i) + + var atlas_coords : Vector2i = path_empty_atlas_coords + var rotation : float = 0.0 + + match score: + 2, 8, 10: + atlas_coords = path_straight_atlas_coords + rotation = 90.0 + 1, 4, 5: + atlas_coords = path_straight_atlas_coords + 3: + atlas_coords = path_corner_atlas_coords + rotation = -90.0 + 6: + atlas_coords = path_corner_atlas_coords + 12: + atlas_coords = path_corner_atlas_coords + rotation = 90 + 9: + atlas_coords = path_corner_atlas_coords + rotation = 180 + + var tile : Sprite2D = Sprite2D.new() + tile.texture = TileManager.get_tile_texture(atlas_coords) + tile.global_rotation_degrees = rotation + _display_tile(tile, i) + +func _display_tile(tile : Sprite2D, pos : Vector2i): + tile.global_position = (pos * tile_size) + (Vector2i.ONE * tile_size / 2) + add_child(tile) + + diff --git a/scripts/path_tester.gd.uid b/scripts/path_tester.gd.uid new file mode 100644 index 0000000..f5dc38a --- /dev/null +++ b/scripts/path_tester.gd.uid @@ -0,0 +1 @@ +uid://unlqpkn1bwel diff --git a/scripts/tile_manager.gd b/scripts/tile_manager.gd new file mode 100644 index 0000000..191b39f --- /dev/null +++ b/scripts/tile_manager.gd @@ -0,0 +1,9 @@ +extends Node + +@export var tile_set : TileSet = preload("res://resources/tiles.tres") + +func get_tile_texture(atlas_coords: Vector2i) -> Texture: + var source : TileSetAtlasSource = tile_set.get_source(0) + var texture_region : Rect2i = source.get_tile_texture_region(atlas_coords) + var tile_image : Image = source.texture.get_image().get_region(texture_region) + return ImageTexture.create_from_image(tile_image) diff --git a/scripts/tile_manager.gd.uid b/scripts/tile_manager.gd.uid new file mode 100644 index 0000000..8ffa51b --- /dev/null +++ b/scripts/tile_manager.gd.uid @@ -0,0 +1 @@ +uid://ptg2yb4qw4xj |