diff options
author | bd <bdunahu@operationnull.com> | 2025-05-22 18:18:46 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-05-22 18:18:46 -0400 |
commit | b318529cf9c01fca4ac2e1b7c7f7fccd0be4589a (patch) | |
tree | 4554f6c14799372133fd49d7ac7688a17a55fa97 | |
parent | f9d7b55634049e0ae6533fb94e058c2a368cd49b (diff) |
Use TileMapLayer in MapGenerator to display path
-rw-r--r-- | project.godot | 1 | ||||
-rw-r--r-- | scenes/path.tscn | 10 | ||||
-rw-r--r-- | scripts/map_generator.gd | 69 | ||||
-rw-r--r-- | scripts/map_generator.gd.uid (renamed from scripts/path_tester.gd.uid) | 0 | ||||
-rw-r--r-- | scripts/path_generator.gd | 12 | ||||
-rw-r--r-- | scripts/path_tester.gd | 55 | ||||
-rw-r--r-- | scripts/tile_manager.gd | 9 | ||||
-rw-r--r-- | scripts/tile_manager.gd.uid | 1 |
8 files changed, 76 insertions, 81 deletions
diff --git a/project.godot b/project.godot index 4ab933d..5e1f0ed 100644 --- a/project.godot +++ b/project.godot @@ -18,7 +18,6 @@ config/icon="res://icon.svg" [autoload] GameData="*res://scripts/game_data.gd" -TileManager="*res://scripts/tile_manager.gd" [display] diff --git a/scenes/path.tscn b/scenes/path.tscn index 5344e6c..2e11423 100644 --- a/scenes/path.tscn +++ b/scenes/path.tscn @@ -1,6 +1,10 @@ -[gd_scene load_steps=2 format=3 uid="uid://cdgeamr56c0mx"] +[gd_scene load_steps=3 format=3 uid="uid://cdgeamr56c0mx"] -[ext_resource type="Script" uid="uid://unlqpkn1bwel" path="res://scripts/path_tester.gd" id="1_1dods"] +[ext_resource type="Script" uid="uid://unlqpkn1bwel" path="res://scripts/map_generator.gd" id="1_1dods"] +[ext_resource type="TileSet" uid="uid://blp0cbjwav76t" path="res://resources/tiles.tres" id="2_sqppf"] -[node name="Path" type="Node2D"] +[node name="Map" type="Node2D"] script = ExtResource("1_1dods") + +[node name="TileMapLayer" type="TileMapLayer" parent="."] +tile_set = ExtResource("2_sqppf") diff --git a/scripts/map_generator.gd b/scripts/map_generator.gd new file mode 100644 index 0000000..328bce1 --- /dev/null +++ b/scripts/map_generator.gd @@ -0,0 +1,69 @@ +extends Node2D + +@export var grid_width : int = 20 +@export var grid_height : int = 14 +@export var tile_size : int = 16 + +@onready var tile_map : TileMapLayer = $TileMapLayer + +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) + +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 _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 = _get_tile_score(path, i) + + var atlas_coords : Vector2i = path_empty_atlas_coords + var rot : TileTransform = TileTransform.ROTATE_0 + + match score: + 2, 8, 10: + atlas_coords = path_straight_atlas_coords + rot = TileTransform.ROTATE_90 + 1, 4, 5: + atlas_coords = path_straight_atlas_coords + 3: + atlas_coords = path_corner_atlas_coords + rot = TileTransform.ROTATE_270 + 6: + atlas_coords = path_corner_atlas_coords + 12: + atlas_coords = path_corner_atlas_coords + rot = TileTransform.ROTATE_90 + 9: + atlas_coords = 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 diff --git a/scripts/path_tester.gd.uid b/scripts/map_generator.gd.uid index f5dc38a..f5dc38a 100644 --- a/scripts/path_tester.gd.uid +++ b/scripts/map_generator.gd.uid diff --git a/scripts/path_generator.gd b/scripts/path_generator.gd index e87a8f4..0e9719a 100644 --- a/scripts/path_generator.gd +++ b/scripts/path_generator.gd @@ -31,15 +31,3 @@ func generate_path(): 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 deleted file mode 100644 index 5add640..0000000 --- a/scripts/path_tester.gd +++ /dev/null @@ -1,55 +0,0 @@ -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/tile_manager.gd b/scripts/tile_manager.gd deleted file mode 100644 index 191b39f..0000000 --- a/scripts/tile_manager.gd +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index 8ffa51b..0000000 --- a/scripts/tile_manager.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://ptg2yb4qw4xj |