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

@@ -1,5 +1,7 @@
extends Node
@export var grid: Resource
@onready var _Map: Node = $Map
@onready var _Camera = $CameraController
@@ -12,17 +14,29 @@ var _MovingMarkerScene = preload("res://Scenes/Unit/marker.tscn")
var _MovingMarker: Node2D = _MovingMarkerScene.instantiate()
#@export var mapSize: int = 15
@onready var UnitCamera: SmartCamera2D = $SmartCamera2D
@onready var GroundLayer: TileMapLayer = $Map/Ground
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# add_child(_MovingMarker)
# _MovingMarker.hide()
#
# _createUnit(Vector2i(10,10))
# for unit in _Units:
# add_child(unit)
# create unit at local position (50, 50) => eg mouse position
_createUnit(GroundLayer.local_to_map(Vector2i(50, 50)))
# create unit at map position (50, 50) => tile x = 50, y = 50 in the map
_createUnit(GroundLayer.map_to_local(Vector2i(50,50)))
for unit in _Units:
add_child(unit)
$CameraController.position = get_viewport().get_camera_2d().get_screen_center_position()
# $CameraController.position = get_viewport().get_camera_2d().get_screen_center_position()
var firstUnit: Node2D = _Units[0]
# if firstUnit.is_node_ready():
# UnitCamera.target = _Units[0].get_path_to(get_parent())
# UnitCamera.target_node = _Units[0]
pass
@@ -30,7 +44,7 @@ func _ready() -> void:
func _process(delta: float) -> void:
pass
func _createUnit(pos: Vector2i):
func _createUnit(pos: Vector2i):
var newUnit = _UnitScene.instantiate()
newUnit.set("_spawnPosition", pos)
_Units.append(newUnit as Unit)
@@ -38,7 +52,8 @@ func _createUnit(pos: Vector2i):
func _input(event: InputEvent) -> void:
if event.is_action_pressed("SetMarker"):
print("Action is SetMarker")
_MovingMarker.global_position = _getMousePosition(event).position
# _MovingMarker.global_position = _getMousePosition(event).position
_MovingMarker.position = GroundLayer.local_to_map(event.position)
_MovingMarker.show()