From c7084f54d7eec1e18b716aae8a1ca53d1f4fbc30 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Sun, 26 Nov 2023 01:44:42 +0500 Subject: [PATCH] rescaling of tilemap and game object sprites to grid context's cell size --- ...l.png-809cb096e36d6f38bdd0dc9250b08205.md5 | 3 + ....png-809cb096e36d6f38bdd0dc9250b08205.stex | Bin 0 -> 148 bytes art/tiles/brickwall.png | 3 + art/tiles/brickwall.png.import | 35 ++++++++++ nodes/Context.gd | 1 + nodes/FittingSprite.gd | 10 ++- nodes/GameObject.gd | 12 ++++ nodes/GameObject.tscn | 5 +- nodes/GridContext.gd | 14 +++- nodes/TileMapObject.gd | 17 +++++ project.godot | 12 ++++ scenes/Game.tscn | 61 +++++++++++++++++- 12 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 .import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.md5 create mode 100644 .import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.stex create mode 100644 art/tiles/brickwall.png create mode 100644 art/tiles/brickwall.png.import create mode 100644 nodes/GameObject.gd create mode 100644 nodes/TileMapObject.gd diff --git a/.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.md5 b/.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.md5 new file mode 100644 index 0000000..1b1c0f0 --- /dev/null +++ b/.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.md5 @@ -0,0 +1,3 @@ +source_md5="88802550c0ee17f57dfd0bd814e77811" +dest_md5="64a5bb7ee98d59912ece658665687037" + diff --git a/.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.stex b/.import/brickwall.png-809cb096e36d6f38bdd0dc9250b08205.stex new file mode 100644 index 0000000000000000000000000000000000000000..6fddd05bde437712079c3d299060a34fdf7eecb9 GIT binary patch literal 148 zcmZ>F2@VlpU|;}Y2vA^W1TrdsINa4KAjs3rEeFaD3$XBs2eS3~4=^)`J50!(ZFH-w zgPTQY!k1?=VihKQc(Td<1j}c6? void: - if target_size == NO_SIZE_SENTINEL: - self.target_size = texture.get_size() + # It's okay to fail here for non-grid contexts. + if get_parent().get_parent().connect("cell_size_changed", self, "_update_cell_size"): + _set_target_size(get_parent().get_parent().cell_size) + elif target_size == NO_SIZE_SENTINEL: + _set_target_size(texture.get_size()) func _set_target_size(p_size: Vector2) -> void: target_size = p_size @@ -19,3 +22,6 @@ func _set_target_size(p_size: Vector2) -> void: func _update_scale() -> void: scale = target_size / texture.get_size() + +func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void: + _set_target_size(p_new_cell_size) diff --git a/nodes/GameObject.gd b/nodes/GameObject.gd new file mode 100644 index 0000000..ab79b13 --- /dev/null +++ b/nodes/GameObject.gd @@ -0,0 +1,12 @@ +tool +extends Node2D +class_name TK_GameObject + +func _ready() -> void: + # It's okay to fail here for non-grid contexts. + if get_parent().connect("cell_size_changed", self, "_update_cell_size"): + pass + +func _update_cell_size(p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void: + var delta := p_new_cell_size / p_old_cell_size + position *= delta diff --git a/nodes/GameObject.tscn b/nodes/GameObject.tscn index b89dd57..9677940 100644 --- a/nodes/GameObject.tscn +++ b/nodes/GameObject.tscn @@ -1,3 +1,6 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://nodes/GameObject.gd" type="Script" id=1] [node name="GameObject" type="Node2D"] +script = ExtResource( 1 ) diff --git a/nodes/GridContext.gd b/nodes/GridContext.gd index 87bb432..559b3e7 100644 --- a/nodes/GridContext.gd +++ b/nodes/GridContext.gd @@ -1,10 +1,13 @@ +tool extends TK_Context class_name TK_GridContext ## todo: Cell visualization. ## todo: Integration of TileMap. -export var cell_size: Vector2 = Vector2(64, 64) +export var cell_size: Vector2 = Vector2(64, 64) setget _set_cell_size + +signal cell_size_changed(p_old_cell_size, p_new_cell_size) func local_position_to_context_position(p_position: Vector2) -> Vector2: return p_position / cell_size @@ -19,3 +22,12 @@ func try_moving(p_game_object: Node2D, p_from: Vector2, p_to: Vector2) -> bool: p_game_object.position = cell_size * p_to return true + +func _set_cell_size(p_new_cell_size: Vector2) -> void: + if p_new_cell_size.x == 0 or p_new_cell_size.y == 0: + push_error("Cell size cannot be zero") + return + + var cell_size_cache = cell_size + cell_size = p_new_cell_size + emit_signal("cell_size_changed", cell_size_cache, p_new_cell_size) diff --git a/nodes/TileMapObject.gd b/nodes/TileMapObject.gd new file mode 100644 index 0000000..4f841ec --- /dev/null +++ b/nodes/TileMapObject.gd @@ -0,0 +1,17 @@ +tool +extends TileMap + +## Dictates whether tileset collision information should be exposed to game context. +export var expose_collisions: bool = true + +func _enter_tree() -> void: + # It's okay to fail here for non-grid contexts. + if get_parent().connect("cell_size_changed", self, "_update_cell_size"): + _update_cell_size(get_parent().cell_size, get_parent().cell_size) + +func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void: + # We're assuming that all tile textures are of same dimensions. + # todo: Diagnose mismatch by walking over all textures? + var tileset_texture_size := tile_set.tile_get_texture(0).get_size() + scale = p_new_cell_size / tileset_texture_size + cell_size = p_new_cell_size / scale diff --git a/project.godot b/project.godot index ab242f5..b0321cd 100644 --- a/project.godot +++ b/project.godot @@ -129,6 +129,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://nodes/FittingSprite.gd" }, { +"base": "Node2D", +"class": "TK_GameObject", +"language": "GDScript", +"path": "res://nodes/GameObject.gd" +}, { "base": "TK_Context", "class": "TK_GridContext", "language": "GDScript", @@ -159,6 +164,7 @@ _global_script_class_icons={ "TK_Context": "", "TK_Controller": "", "TK_FittingSprite": "", +"TK_GameObject": "", "TK_GridContext": "" } @@ -173,6 +179,12 @@ config/icon="res://icon.png" Arithmetic="*res://nodes/singletons/Arithmetic.gd" InputUtils="*res://nodes/singletons/InputUtils.gd" +[display] + +window/size/height=576 +window/stretch/mode="2d" +window/stretch/aspect="keep" + [editor_plugins] enabled=PoolStringArray( "res://addons/Godoxel/plugin.cfg" ) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 708d9f5..20289c7 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -1,25 +1,82 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://nodes/FittingSprite.gd" type="Script" id=1] [ext_resource path="res://art/entities/fiend.png" type="Texture" id=2] [ext_resource path="res://nodes/4WayController.tscn" type="PackedScene" id=3] [ext_resource path="res://nodes/GridContext.tscn" type="PackedScene" id=4] [ext_resource path="res://nodes/GameObject.tscn" type="PackedScene" id=5] +[ext_resource path="res://art/tiles/brickwall.png" type="Texture" id=6] +[ext_resource path="res://nodes/TileMapObject.gd" type="Script" id=7] -[node name="Game" type="Node"] +[sub_resource type="OccluderPolygon2D" id=2] +polygon = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 ) + +[sub_resource type="ConvexPolygonShape2D" id=3] +points = PoolVector2Array( 16, 16, 0, 16, 0, 16, 16, 16 ) + +[sub_resource type="ConvexPolygonShape2D" id=4] +points = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 ) + +[sub_resource type="TileSet" id=1] +0/name = "brickwall.png 0" +0/texture = ExtResource( 6 ) +0/tex_offset = Vector2( 0, 0 ) +0/modulate = Color( 1, 1, 1, 1 ) +0/region = Rect2( 0, 0, 16, 16 ) +0/tile_mode = 0 +0/occluder_offset = Vector2( 0, 0 ) +0/occluder = SubResource( 2 ) +0/navigation_offset = Vector2( 0, 0 ) +0/shape_offset = Vector2( 0, 0 ) +0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) +0/shape = SubResource( 3 ) +0/shape_one_way = false +0/shape_one_way_margin = 1.0 +0/shapes = [ { +"autotile_coord": Vector2( 0, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 3 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +}, { +"autotile_coord": Vector2( 0, 0 ), +"one_way": false, +"one_way_margin": 1.0, +"shape": SubResource( 4 ), +"shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) +} ] +0/z_index = 0 + +[node name="Gauntlet" type="Node"] [node name="GridContext" parent="." instance=ExtResource( 4 )] +[node name="TileMap" type="TileMap" parent="GridContext"] +scale = Vector2( 4, 4 ) +tile_set = SubResource( 1 ) +cell_size = Vector2( 16, 16 ) +bake_navigation = true +format = 1 +tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 65536, 0, 0, 65542, 0, 0, 65551, 0, 0, 131072, 0, 0, 131082, 0, 0, 131087, 0, 0, 196608, 0, 0, 196618, 0, 0, 196623, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 0, 0, 262148, 0, 0, 262149, 0, 0, 262150, 0, 0, 262151, 0, 0, 262152, 0, 0, 262153, 0, 0, 262154, 0, 0, 262156, 0, 0, 262157, 0, 0, 262158, 0, 0, 262159, 0, 0, 327680, 0, 0, 327683, 0, 0, 327695, 0, 0, 393216, 0, 0, 393226, 0, 0, 393227, 0, 0, 393229, 0, 0, 393231, 0, 0, 458752, 0, 0, 458761, 0, 0, 458762, 0, 0, 458767, 0, 0, 524288, 0, 0, 524289, 0, 0, 524290, 0, 0, 524291, 0, 0, 524292, 0, 0, 524293, 0, 0, 524294, 0, 0, 524295, 0, 0, 524296, 0, 0, 524297, 0, 0, 524298, 0, 0, 524299, 0, 0, 524300, 0, 0, 524301, 0, 0, 524302, 0, 0, 524303, 0, 0 ) +script = ExtResource( 7 ) + [node name="GameObject" parent="GridContext" instance=ExtResource( 5 )] +position = Vector2( 384, 128 ) [node name="Visual" type="Sprite" parent="GridContext/GameObject"] scale = Vector2( 4, 4 ) texture = ExtResource( 2 ) centered = false script = ExtResource( 1 ) +__meta__ = { +"_edit_lock_": true +} target_size = Vector2( 64, 64 ) [node name="4WayController" parent="GridContext/GameObject" instance=ExtResource( 3 )] [node name="Camera2D" type="Camera2D" parent="GridContext/GameObject"] position = Vector2( 32, 32 ) +__meta__ = { +"_edit_lock_": true +}