rescaling of tilemap and game object sprites to grid context's cell size

This commit is contained in:
veclav talica
2023-11-26 01:44:42 +05:00
parent 582489fea7
commit c7084f54d7
12 changed files with 167 additions and 6 deletions

View File

@ -1,3 +1,4 @@
tool
extends Node2D
class_name TK_Context

View File

@ -10,8 +10,11 @@ export var target_size: Vector2 = NO_SIZE_SENTINEL setget _set_target_size
# todo: Ask context about preferred sizing if there's any.
func _enter_tree() -> 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)

12
nodes/GameObject.gd Normal file
View File

@ -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

View File

@ -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 )

View File

@ -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)

17
nodes/TileMapObject.gd Normal file
View File

@ -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