Using tilemaplayer as grid

This commit is contained in:
gdz
2025-12-22 17:35:13 +01:00
parent 67b6198412
commit 6e1a9eff35
10 changed files with 147 additions and 22 deletions

View File

@@ -7,7 +7,7 @@ class_name Grid
# The of a cell in pixels
@export var cellSize := Vector2(16, 16)
@export var cameraPosition := Vector2.ONE
@export var cameraPosition := Vector2.ZERO
func setCameraPosition(pos: Vector2):
cameraPosition = pos
@export var cameraZoom := Vector2(2, 2)
@@ -24,14 +24,22 @@ var _halfCellSize: Vector2 = cellSize / 2
# Returns the position of a cell's center in pixels.
# We'll place units and have them move through cells using this function.
# Example:
# cellSize = (32)
# gridPosition = (2, 3)
# cameraZoom = 1
# (2,3) * 32 + 16
# (64, 96) + 16
# == (80, 112) <- This is the position ON THE SCREEN.
func calculateMapPosition(gridPosition: Vector2) -> Vector2:
return cameraPosition + (gridPosition * cellSize + _halfCellSize) / cameraZoom
return (gridPosition * cellSize + _halfCellSize) / cameraZoom
# Returns the coordinates of the cell on the grid given a position on the map.
# This is the complementary of `calculate_map_position()` above.
# When designing a level, you'll place units visually in the editor. We'll use this function to find
# the grid coordinates they're placed on, and call `calculate_map_position()` to snap them to the
# cell's center.
# This is the position IN THE GRID
func calculateGridCoordinates(mapPosition: Vector2) -> Vector2:
return (mapPosition / cellSize).floor()