From e356078a9fb9d769881c8c7b47ce0892547b43cc Mon Sep 17 00:00:00 2001 From: gamer147 Date: Thu, 9 Apr 2026 08:07:25 -0400 Subject: [PATCH] Restructure tile size usage --- project.godot | 4 +++ scripts/autoloads/battle_map_helper.gd | 17 ++++++++++ scripts/autoloads/battle_map_helper.gd.uid | 1 + scripts/battle/map/combat_map.gd | 14 ++------ scripts/battle/map/fog_renderer.gd | 5 ++- scripts/battle/map/wall_renderer.gd | 34 +++++++++---------- scripts/battle/player_controller.gd | 16 ++++----- scripts/battle/strategy_phase.gd | 4 +-- scripts/constants/battle_map_constants.gd | 3 ++ scripts/constants/battle_map_constants.gd.uid | 1 + 10 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 scripts/autoloads/battle_map_helper.gd create mode 100644 scripts/autoloads/battle_map_helper.gd.uid create mode 100644 scripts/constants/battle_map_constants.gd create mode 100644 scripts/constants/battle_map_constants.gd.uid diff --git a/project.godot b/project.godot index a49fdad..588d496 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ run/main_scene="res://scenes/game.tscn" config/features=PackedStringArray("4.6", "Mobile") config/icon="res://icon.svg" +[autoload] + +BattleMapHelper="*res://scripts/autoloads/battle_map_helper.gd" + [display] window/size/viewport_width=800 diff --git a/scripts/autoloads/battle_map_helper.gd b/scripts/autoloads/battle_map_helper.gd new file mode 100644 index 0000000..85ff2c4 --- /dev/null +++ b/scripts/autoloads/battle_map_helper.gd @@ -0,0 +1,17 @@ +extends Node + +var TILE_SIZE: float: + get: + return BattleMapConstants.TILE_SIZE + + +func snap_to_grid(pos: Vector2) -> Vector2: + return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE + + +func world_to_coords(pos: Vector2) -> Vector2i: + return Vector2i(snap_to_grid(pos) / TILE_SIZE) + + +func coords_to_world(coords: Vector2i) -> Vector2: + return Vector2(coords) * TILE_SIZE diff --git a/scripts/autoloads/battle_map_helper.gd.uid b/scripts/autoloads/battle_map_helper.gd.uid new file mode 100644 index 0000000..6d19e67 --- /dev/null +++ b/scripts/autoloads/battle_map_helper.gd.uid @@ -0,0 +1 @@ +uid://bt28d2xnvfjmf diff --git a/scripts/battle/map/combat_map.gd b/scripts/battle/map/combat_map.gd index dd3d9c7..4f2c091 100644 --- a/scripts/battle/map/combat_map.gd +++ b/scripts/battle/map/combat_map.gd @@ -9,7 +9,6 @@ extends Node2D @onready var fog_renderer: FogRenderer = %FogRenderer const DEPLOYED_UNIT_SCENE = preload("res://prefabs/deployed_unit.tscn") -const TILE_SIZE := 100.0 const SOURCE_ID: int = 0 var _pending_layout: String @@ -26,15 +25,6 @@ func _ready() -> void: apply_layout(map_layout) -func snap_to_grid(pos: Vector2) -> Vector2: - return Vector2(floorf(pos.x / TILE_SIZE), floorf(pos.y / TILE_SIZE)) * TILE_SIZE - -func world_to_coords(pos: Vector2) -> Vector2i: - return Vector2i(snap_to_grid(pos) / TILE_SIZE) - -func coords_to_world(coords: Vector2i) -> Vector2: - return Vector2(coords) * TILE_SIZE - func draw_wall(coords: Vector2i) -> void: draw_custom(coords, tile_set.wall_tile_coords) @@ -73,7 +63,7 @@ func _apply_layout(layout: String) -> void: func _apply_deploy(deployed: DeployedUnit, coords: Vector2i) -> void: - deployed.position = coords_to_world(coords) + deployed.position = BattleMapHelper.coords_to_world(coords) add_child(deployed) @@ -119,7 +109,7 @@ func draw_fog() -> void: func get_map_rect() -> Rect2: if not map_layout: return Rect2() - return Rect2(Vector2.ZERO, Vector2(map_layout.size) * TILE_SIZE) + return Rect2(Vector2.ZERO, Vector2(map_layout.size) * BattleMapHelper.TILE_SIZE) func load_from_layout() -> void: diff --git a/scripts/battle/map/fog_renderer.gd b/scripts/battle/map/fog_renderer.gd index 213698b..e4321e6 100644 --- a/scripts/battle/map/fog_renderer.gd +++ b/scripts/battle/map/fog_renderer.gd @@ -4,7 +4,6 @@ extends Node2D ## Renders a fog/cave texture over every tile inside the map's bounding rect ## that is not part of any room. Future: drive visibility from map state. -const TILE_SIZE := 100.0 ## Fog tile region in aux_terrain.BMP const FOG_RECT := Rect2(53, 53, 100, 100) @@ -23,14 +22,14 @@ func draw_fog_for_layout(map_layout: MapLayout) -> void: var tile := Vector2i(x, y) if map_layout.is_tile_valid(tile): continue - _fog_tiles.append(Vector2(tile) * TILE_SIZE) + _fog_tiles.append(Vector2(tile) * BattleMapHelper.TILE_SIZE) queue_redraw() func _draw() -> void: if not atlas_texture: return - var dest_size := Vector2(TILE_SIZE, TILE_SIZE) + var dest_size := Vector2(BattleMapHelper.TILE_SIZE, BattleMapHelper.TILE_SIZE) for pos in _fog_tiles: draw_texture_rect_region( atlas_texture, diff --git a/scripts/battle/map/wall_renderer.gd b/scripts/battle/map/wall_renderer.gd index df6446c..81aeeba 100644 --- a/scripts/battle/map/wall_renderer.gd +++ b/scripts/battle/map/wall_renderer.gd @@ -4,8 +4,6 @@ extends Node2D ## Renders wall textures by sampling segments from the aux_terrain texture atlas ## and compositing them onto tile edges. Each edge is made of two half-segments. -const TILE_SIZE := 100.0 - ## Source atlas rects (x, y, w, h) from aux_terrain.BMP ## Each edge has two half-segments that together span the full tile edge. @@ -42,7 +40,9 @@ const WALL_THICKNESS := 20.0 ## Inner corner piece size in game pixels (quarter of a tile) const CORNER_SIZE := 50.0 ## Half the tile edge length -const HALF_EDGE := TILE_SIZE / 2.0 +var HALF_EDGE: float: + get: + return BattleMapHelper.TILE_SIZE / 2.0 @export var atlas_texture: Texture2D @@ -114,7 +114,7 @@ func _direction_to_edge(dir: Vector2i) -> StringName: func _build_tile_walls(tile: Vector2i, edges: Array) -> void: - var tile_origin := Vector2(tile) * TILE_SIZE + var tile_origin := Vector2(tile) * BattleMapHelper.TILE_SIZE for edge in edges: _build_edge_segments(tile_origin, edge) @@ -147,8 +147,8 @@ func _build_edge_segments(tile_origin: Vector2, edge: StringName) -> void: seg_b_rect = RIGHT_LOWER_RECT seg_a_size = Vector2(WALL_THICKNESS, HALF_EDGE) seg_b_size = Vector2(WALL_THICKNESS, HALF_EDGE) - seg_a_offset = Vector2(TILE_SIZE - WALL_THICKNESS, 0) - seg_b_offset = Vector2(TILE_SIZE - WALL_THICKNESS, HALF_EDGE) + seg_a_offset = Vector2(BattleMapHelper.TILE_SIZE - WALL_THICKNESS, 0) + seg_b_offset = Vector2(BattleMapHelper.TILE_SIZE - WALL_THICKNESS, HALF_EDGE) &"top": seg_a_rect = TOP_LEFT_RECT seg_b_rect = TOP_RIGHT_RECT @@ -161,8 +161,8 @@ func _build_edge_segments(tile_origin: Vector2, edge: StringName) -> void: seg_b_rect = BOTTOM_RIGHT_RECT seg_a_size = Vector2(HALF_EDGE, WALL_THICKNESS) seg_b_size = Vector2(HALF_EDGE, WALL_THICKNESS) - seg_a_offset = Vector2(0, TILE_SIZE - WALL_THICKNESS) - seg_b_offset = Vector2(HALF_EDGE, TILE_SIZE - WALL_THICKNESS) + seg_a_offset = Vector2(0, BattleMapHelper.TILE_SIZE - WALL_THICKNESS) + seg_b_offset = Vector2(HALF_EDGE, BattleMapHelper.TILE_SIZE - WALL_THICKNESS) _queue_segment(tile_origin + seg_a_offset, seg_a_size, seg_a_rect) _queue_segment(tile_origin + seg_b_offset, seg_b_size, seg_b_rect) @@ -187,8 +187,8 @@ func _build_opening_sprites(map_layout: MapLayout) -> void: tile_b = swap diff = -diff - var origin_a := Vector2(tile_a) * TILE_SIZE - var origin_b := Vector2(tile_b) * TILE_SIZE + var origin_a := Vector2(tile_a) * BattleMapHelper.TILE_SIZE + var origin_b := Vector2(tile_b) * BattleMapHelper.TILE_SIZE if diff == Vector2i.DOWN: _queue_vertical_opening(origin_a, origin_b) @@ -203,10 +203,10 @@ func _queue_vertical_opening(origin_upper: Vector2, origin_lower: Vector2) -> vo var h_total: float = src.size.y var h_upper: float = floorf(h_total / 2.0) # 14 var h_lower: float = h_total - h_upper # 15 - var x_offset := (TILE_SIZE - w) / 2.0 + var x_offset := (BattleMapHelper.TILE_SIZE - w) / 2.0 var src_upper := Rect2(src.position, Vector2(w, h_upper)) var src_lower := Rect2(src.position + Vector2(0, h_upper), Vector2(w, h_lower)) - _queue_segment(origin_upper + Vector2(x_offset, TILE_SIZE - h_upper), Vector2(w, h_upper), src_upper) + _queue_segment(origin_upper + Vector2(x_offset, BattleMapHelper.TILE_SIZE - h_upper), Vector2(w, h_upper), src_upper) _queue_segment(origin_lower + Vector2(x_offset, 0), Vector2(w, h_lower), src_lower) @@ -217,10 +217,10 @@ func _queue_horizontal_opening(origin_left: Vector2, origin_right: Vector2) -> v var h: float = src.size.y var w_left: float = floorf(w_total / 2.0) # 14 var w_right: float = w_total - w_left # 14 - var y_offset := (TILE_SIZE - h) / 2.0 + var y_offset := (BattleMapHelper.TILE_SIZE - h) / 2.0 var src_left := Rect2(src.position, Vector2(w_left, h)) var src_right := Rect2(src.position + Vector2(w_left, 0), Vector2(w_right, h)) - _queue_segment(origin_left + Vector2(TILE_SIZE - w_left, y_offset), Vector2(w_left, h), src_left) + _queue_segment(origin_left + Vector2(BattleMapHelper.TILE_SIZE - w_left, y_offset), Vector2(w_left, h), src_left) _queue_segment(origin_right + Vector2(0, y_offset), Vector2(w_right, h), src_right) @@ -248,19 +248,19 @@ func _build_inner_corners(_tile: Vector2i, tile_origin: Vector2, edges: Array) - ) if has_top and has_right: _queue_segment( - tile_origin + Vector2(TILE_SIZE - CORNER_SIZE, 0), + tile_origin + Vector2(BattleMapHelper.TILE_SIZE - CORNER_SIZE, 0), corner_size, INNER_CORNER_UPPER_RIGHT_RECT ) if has_bottom and has_left: _queue_segment( - tile_origin + Vector2(0, TILE_SIZE - CORNER_SIZE), + tile_origin + Vector2(0, BattleMapHelper.TILE_SIZE - CORNER_SIZE), corner_size, INNER_CORNER_LOWER_LEFT_RECT ) if has_bottom and has_right: _queue_segment( - tile_origin + Vector2(TILE_SIZE - CORNER_SIZE, TILE_SIZE - CORNER_SIZE), + tile_origin + Vector2(BattleMapHelper.TILE_SIZE - CORNER_SIZE, BattleMapHelper.TILE_SIZE - CORNER_SIZE), corner_size, INNER_CORNER_LOWER_RIGHT_RECT ) diff --git a/scripts/battle/player_controller.gd b/scripts/battle/player_controller.gd index 83d5ea9..d2543a0 100644 --- a/scripts/battle/player_controller.gd +++ b/scripts/battle/player_controller.gd @@ -45,7 +45,7 @@ func _process(_delta: float) -> void: if input_disabled: return var mouse_pos := get_viewport().get_canvas_transform().affine_inverse() * get_viewport().get_mouse_position() - var coords := dl_map.world_to_coords(mouse_pos) + var coords := BattleMapHelper.world_to_coords(mouse_pos) if coords != _current_grid_coords: _current_grid_coords = coords mouse_grid_changed.emit(coords) @@ -116,9 +116,9 @@ func _physics_process(delta: float) -> void: else: dir = Vector2(0, signf(diff.y)) - var next_pos := _selected_unit.position + dir * dl_map.TILE_SIZE - var grid_coords := dl_map.world_to_coords(next_pos) - var current_coords := dl_map.world_to_coords(_selected_unit.position) + var next_pos := _selected_unit.position + dir * BattleMapHelper.TILE_SIZE + var grid_coords := BattleMapHelper.world_to_coords(next_pos) + var current_coords := BattleMapHelper.world_to_coords(_selected_unit.position) if not dl_map.is_tile_passable(current_coords, grid_coords): _goal_pos = _selected_unit.position return @@ -138,8 +138,8 @@ func _handle_left_click(screen_pos: Vector2) -> void: _select_unit(clicked_unit) get_viewport().set_input_as_handled() elif _selected_unit: - var snapped_pos := dl_map.snap_to_grid(world_pos) - var grid_coords := dl_map.world_to_coords(world_pos) + var snapped_pos := BattleMapHelper.snap_to_grid(world_pos) + var grid_coords := BattleMapHelper.world_to_coords(world_pos) if not dl_map.is_tile_valid(grid_coords): return _goal_pos = snapped_pos @@ -157,11 +157,11 @@ func _select_unit(deployed: DeployedUnit) -> void: func _get_unit_at(world_pos: Vector2) -> DeployedUnit: - var snapped_coords := dl_map.snap_to_grid(world_pos) + var snapped_coords := BattleMapHelper.snap_to_grid(world_pos) for deployed: DeployedUnit in get_tree().get_nodes_in_group("deployed_units"): if not deployed.is_alive(): continue - var unit_snapped := dl_map.snap_to_grid(deployed.global_position) + var unit_snapped := BattleMapHelper.snap_to_grid(deployed.global_position) if unit_snapped == snapped_coords: return deployed return null diff --git a/scripts/battle/strategy_phase.gd b/scripts/battle/strategy_phase.gd index a20fb28..39da659 100644 --- a/scripts/battle/strategy_phase.gd +++ b/scripts/battle/strategy_phase.gd @@ -46,8 +46,8 @@ func _on_mouse_grid_changed(coords: Vector2i) -> void: combat_map.target_tile(coords) func _on_combat_requested(attacker: DeployedUnit, defender: DeployedUnit) -> void: - var atk_coords := combat_map.world_to_coords(attacker.position) - var def_coords := combat_map.world_to_coords(defender.position) + var atk_coords := BattleMapHelper.world_to_coords(attacker.position) + var def_coords := BattleMapHelper.world_to_coords(defender.position) var distance := absi(atk_coords.x - def_coords.x) + absi(atk_coords.y - def_coords.y) var proposal := combat_system.create_proposal(attacker, defender, distance) _set_input_disabled(true) diff --git a/scripts/constants/battle_map_constants.gd b/scripts/constants/battle_map_constants.gd new file mode 100644 index 0000000..3859253 --- /dev/null +++ b/scripts/constants/battle_map_constants.gd @@ -0,0 +1,3 @@ +class_name BattleMapConstants extends Object + +const TILE_SIZE: float = 100.0 diff --git a/scripts/constants/battle_map_constants.gd.uid b/scripts/constants/battle_map_constants.gd.uid new file mode 100644 index 0000000..9d441a8 --- /dev/null +++ b/scripts/constants/battle_map_constants.gd.uid @@ -0,0 +1 @@ +uid://pv4l5upgp0vu