Restructure tile size usage

This commit is contained in:
gamer147
2026-04-09 08:07:25 -04:00
parent 6b46d1c274
commit e356078a9f
10 changed files with 57 additions and 42 deletions

View File

@@ -15,6 +15,10 @@ run/main_scene="res://scenes/game.tscn"
config/features=PackedStringArray("4.6", "Mobile") config/features=PackedStringArray("4.6", "Mobile")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[autoload]
BattleMapHelper="*res://scripts/autoloads/battle_map_helper.gd"
[display] [display]
window/size/viewport_width=800 window/size/viewport_width=800

View File

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

View File

@@ -0,0 +1 @@
uid://bt28d2xnvfjmf

View File

@@ -9,7 +9,6 @@ extends Node2D
@onready var fog_renderer: FogRenderer = %FogRenderer @onready var fog_renderer: FogRenderer = %FogRenderer
const DEPLOYED_UNIT_SCENE = preload("res://prefabs/deployed_unit.tscn") const DEPLOYED_UNIT_SCENE = preload("res://prefabs/deployed_unit.tscn")
const TILE_SIZE := 100.0
const SOURCE_ID: int = 0 const SOURCE_ID: int = 0
var _pending_layout: String var _pending_layout: String
@@ -26,15 +25,6 @@ func _ready() -> void:
apply_layout(map_layout) 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: func draw_wall(coords: Vector2i) -> void:
draw_custom(coords, tile_set.wall_tile_coords) 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: func _apply_deploy(deployed: DeployedUnit, coords: Vector2i) -> void:
deployed.position = coords_to_world(coords) deployed.position = BattleMapHelper.coords_to_world(coords)
add_child(deployed) add_child(deployed)
@@ -119,7 +109,7 @@ func draw_fog() -> void:
func get_map_rect() -> Rect2: func get_map_rect() -> Rect2:
if not map_layout: if not map_layout:
return Rect2() 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: func load_from_layout() -> void:

View File

@@ -4,7 +4,6 @@ extends Node2D
## Renders a fog/cave texture over every tile inside the map's bounding rect ## 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. ## 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 ## Fog tile region in aux_terrain.BMP
const FOG_RECT := Rect2(53, 53, 100, 100) 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) var tile := Vector2i(x, y)
if map_layout.is_tile_valid(tile): if map_layout.is_tile_valid(tile):
continue continue
_fog_tiles.append(Vector2(tile) * TILE_SIZE) _fog_tiles.append(Vector2(tile) * BattleMapHelper.TILE_SIZE)
queue_redraw() queue_redraw()
func _draw() -> void: func _draw() -> void:
if not atlas_texture: if not atlas_texture:
return return
var dest_size := Vector2(TILE_SIZE, TILE_SIZE) var dest_size := Vector2(BattleMapHelper.TILE_SIZE, BattleMapHelper.TILE_SIZE)
for pos in _fog_tiles: for pos in _fog_tiles:
draw_texture_rect_region( draw_texture_rect_region(
atlas_texture, atlas_texture,

View File

@@ -4,8 +4,6 @@ extends Node2D
## Renders wall textures by sampling segments from the aux_terrain texture atlas ## 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. ## 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 ## Source atlas rects (x, y, w, h) from aux_terrain.BMP
## Each edge has two half-segments that together span the full tile edge. ## 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) ## Inner corner piece size in game pixels (quarter of a tile)
const CORNER_SIZE := 50.0 const CORNER_SIZE := 50.0
## Half the tile edge length ## 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 @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: 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: for edge in edges:
_build_edge_segments(tile_origin, edge) _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_b_rect = RIGHT_LOWER_RECT
seg_a_size = Vector2(WALL_THICKNESS, HALF_EDGE) seg_a_size = Vector2(WALL_THICKNESS, HALF_EDGE)
seg_b_size = Vector2(WALL_THICKNESS, HALF_EDGE) seg_b_size = Vector2(WALL_THICKNESS, HALF_EDGE)
seg_a_offset = Vector2(TILE_SIZE - WALL_THICKNESS, 0) seg_a_offset = Vector2(BattleMapHelper.TILE_SIZE - WALL_THICKNESS, 0)
seg_b_offset = Vector2(TILE_SIZE - WALL_THICKNESS, HALF_EDGE) seg_b_offset = Vector2(BattleMapHelper.TILE_SIZE - WALL_THICKNESS, HALF_EDGE)
&"top": &"top":
seg_a_rect = TOP_LEFT_RECT seg_a_rect = TOP_LEFT_RECT
seg_b_rect = TOP_RIGHT_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_b_rect = BOTTOM_RIGHT_RECT
seg_a_size = Vector2(HALF_EDGE, WALL_THICKNESS) seg_a_size = Vector2(HALF_EDGE, WALL_THICKNESS)
seg_b_size = Vector2(HALF_EDGE, WALL_THICKNESS) seg_b_size = Vector2(HALF_EDGE, WALL_THICKNESS)
seg_a_offset = Vector2(0, TILE_SIZE - WALL_THICKNESS) seg_a_offset = Vector2(0, BattleMapHelper.TILE_SIZE - WALL_THICKNESS)
seg_b_offset = Vector2(HALF_EDGE, 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_a_offset, seg_a_size, seg_a_rect)
_queue_segment(tile_origin + seg_b_offset, seg_b_size, seg_b_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 tile_b = swap
diff = -diff diff = -diff
var origin_a := Vector2(tile_a) * TILE_SIZE var origin_a := Vector2(tile_a) * BattleMapHelper.TILE_SIZE
var origin_b := Vector2(tile_b) * TILE_SIZE var origin_b := Vector2(tile_b) * BattleMapHelper.TILE_SIZE
if diff == Vector2i.DOWN: if diff == Vector2i.DOWN:
_queue_vertical_opening(origin_a, origin_b) _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_total: float = src.size.y
var h_upper: float = floorf(h_total / 2.0) # 14 var h_upper: float = floorf(h_total / 2.0) # 14
var h_lower: float = h_total - h_upper # 15 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_upper := Rect2(src.position, Vector2(w, h_upper))
var src_lower := Rect2(src.position + Vector2(0, h_upper), Vector2(w, h_lower)) 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) _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 h: float = src.size.y
var w_left: float = floorf(w_total / 2.0) # 14 var w_left: float = floorf(w_total / 2.0) # 14
var w_right: float = w_total - w_left # 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_left := Rect2(src.position, Vector2(w_left, h))
var src_right := Rect2(src.position + Vector2(w_left, 0), Vector2(w_right, 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) _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: if has_top and has_right:
_queue_segment( _queue_segment(
tile_origin + Vector2(TILE_SIZE - CORNER_SIZE, 0), tile_origin + Vector2(BattleMapHelper.TILE_SIZE - CORNER_SIZE, 0),
corner_size, corner_size,
INNER_CORNER_UPPER_RIGHT_RECT INNER_CORNER_UPPER_RIGHT_RECT
) )
if has_bottom and has_left: if has_bottom and has_left:
_queue_segment( _queue_segment(
tile_origin + Vector2(0, TILE_SIZE - CORNER_SIZE), tile_origin + Vector2(0, BattleMapHelper.TILE_SIZE - CORNER_SIZE),
corner_size, corner_size,
INNER_CORNER_LOWER_LEFT_RECT INNER_CORNER_LOWER_LEFT_RECT
) )
if has_bottom and has_right: if has_bottom and has_right:
_queue_segment( _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, corner_size,
INNER_CORNER_LOWER_RIGHT_RECT INNER_CORNER_LOWER_RIGHT_RECT
) )

View File

@@ -45,7 +45,7 @@ func _process(_delta: float) -> void:
if input_disabled: if input_disabled:
return return
var mouse_pos := get_viewport().get_canvas_transform().affine_inverse() * get_viewport().get_mouse_position() 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: if coords != _current_grid_coords:
_current_grid_coords = coords _current_grid_coords = coords
mouse_grid_changed.emit(coords) mouse_grid_changed.emit(coords)
@@ -116,9 +116,9 @@ func _physics_process(delta: float) -> void:
else: else:
dir = Vector2(0, signf(diff.y)) dir = Vector2(0, signf(diff.y))
var next_pos := _selected_unit.position + dir * dl_map.TILE_SIZE var next_pos := _selected_unit.position + dir * BattleMapHelper.TILE_SIZE
var grid_coords := dl_map.world_to_coords(next_pos) var grid_coords := BattleMapHelper.world_to_coords(next_pos)
var current_coords := dl_map.world_to_coords(_selected_unit.position) var current_coords := BattleMapHelper.world_to_coords(_selected_unit.position)
if not dl_map.is_tile_passable(current_coords, grid_coords): if not dl_map.is_tile_passable(current_coords, grid_coords):
_goal_pos = _selected_unit.position _goal_pos = _selected_unit.position
return return
@@ -138,8 +138,8 @@ func _handle_left_click(screen_pos: Vector2) -> void:
_select_unit(clicked_unit) _select_unit(clicked_unit)
get_viewport().set_input_as_handled() get_viewport().set_input_as_handled()
elif _selected_unit: elif _selected_unit:
var snapped_pos := dl_map.snap_to_grid(world_pos) var snapped_pos := BattleMapHelper.snap_to_grid(world_pos)
var grid_coords := dl_map.world_to_coords(world_pos) var grid_coords := BattleMapHelper.world_to_coords(world_pos)
if not dl_map.is_tile_valid(grid_coords): if not dl_map.is_tile_valid(grid_coords):
return return
_goal_pos = snapped_pos _goal_pos = snapped_pos
@@ -157,11 +157,11 @@ func _select_unit(deployed: DeployedUnit) -> void:
func _get_unit_at(world_pos: Vector2) -> DeployedUnit: 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"): for deployed: DeployedUnit in get_tree().get_nodes_in_group("deployed_units"):
if not deployed.is_alive(): if not deployed.is_alive():
continue 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: if unit_snapped == snapped_coords:
return deployed return deployed
return null return null

View File

@@ -46,8 +46,8 @@ func _on_mouse_grid_changed(coords: Vector2i) -> void:
combat_map.target_tile(coords) combat_map.target_tile(coords)
func _on_combat_requested(attacker: DeployedUnit, defender: DeployedUnit) -> void: func _on_combat_requested(attacker: DeployedUnit, defender: DeployedUnit) -> void:
var atk_coords := combat_map.world_to_coords(attacker.position) var atk_coords := BattleMapHelper.world_to_coords(attacker.position)
var def_coords := combat_map.world_to_coords(defender.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 distance := absi(atk_coords.x - def_coords.x) + absi(atk_coords.y - def_coords.y)
var proposal := combat_system.create_proposal(attacker, defender, distance) var proposal := combat_system.create_proposal(attacker, defender, distance)
_set_input_disabled(true) _set_input_disabled(true)

View File

@@ -0,0 +1,3 @@
class_name BattleMapConstants extends Object
const TILE_SIZE: float = 100.0

View File

@@ -0,0 +1 @@
uid://pv4l5upgp0vu