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

@@ -31,17 +31,24 @@ var cell := Vector2.ZERO:
# a signal, and start the cooldown timer that will limit the rate
# at which the cursor moves when we keep the direction key held
# down
position = grid.calculateMapPosition(cell)
# global_position = grid.calculateMapPosition(cell)
global_position = GroundLayer.map_to_local(cell)
print("Position is ", position)
emit_signal("moved", cell)
_timer.start()
@onready var _timer: Timer = $Timer
@onready var Map: Node = $"../../Map"
@onready var GroundLayer: TileMapLayer = $"../../Map/Ground"
func _ready() -> void:
# Map = get_parent().get_parent().get_node("Map")
# GroundLayer = Map.get_node("Ground")
#
_timer.wait_time = ui_cooldown
position = grid.calculateMapPosition(cell)
#position = grid.calculateMapPosition(cell)
position = GroundLayer.map_to_local(cell)
func _unhandled_input(event: InputEvent) -> void:

16
Scenes/Main/grid_debug.gd Normal file
View File

@@ -0,0 +1,16 @@
extends Node2D
@export var grid: Resource
func _drawO():
var gridSize = grid.size
var cellSize = grid.cellSize
var rectSize: Vector2 = Vector2(float(cellSize), float(cellSize))
var rectPos: Vector2 = Vector2.ZERO
for x in range(gridSize.x):
for y in range(gridSize.y):
rectPos = Vector2(x, y)
var cellRect := Rect2(rectPos, rectSize)
draw_rect(cellRect, Color.ALICE_BLUE, false, 2.0)

View File

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

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

View File

@@ -1,30 +1,37 @@
[gd_scene load_steps=9 format=3 uid="uid://d05j5yuhlsxp0"]
[gd_scene load_steps=10 format=3 uid="uid://d05j5yuhlsxp0"]
[ext_resource type="PackedScene" uid="uid://cywuuce71rmgb" path="res://Scenes/Map/map.tscn" id="1_1r6ip"]
[ext_resource type="Script" uid="uid://btdvxp8ckmeb3" path="res://Scenes/Main/main.gd" id="1_qw60k"]
[ext_resource type="PackedScene" uid="uid://bfvijh611aggp" path="res://Scenes/Camera/camera_controller.tscn" id="3_qw60k"]
[ext_resource type="Script" uid="uid://14cwbxcvt5dx" path="res://Scenes/Main/game_board.gd" id="4_5yls4"]
[ext_resource type="Resource" uid="uid://bpf7mj7w5kftq" path="res://Resource/Grid.tres" id="5_p6jpk"]
[ext_resource type="Script" uid="uid://dukn3yshfepum" path="res://Scenes/Main/grid_debug.gd" id="5_y3v7k"]
[ext_resource type="PackedScene" uid="uid://b1d6lktijxy3s" path="res://Scenes/Unit/move/unit.tscn" id="6_2a143"]
[ext_resource type="Script" uid="uid://cidjtc27oj1gn" path="res://Scenes/Main/cursor.gd" id="7_y3v7k"]
[ext_resource type="Texture2D" uid="uid://bckknh8k5fh1s" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tiles/tile_0448.png" id="8_hryqi"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_y3v7k"]
[node name="Main" type="Node"]
script = ExtResource("1_qw60k")
grid = ExtResource("5_p6jpk")
[node name="Map" parent="." instance=ExtResource("1_1r6ip")]
[node name="CameraController" parent="." instance=ExtResource("3_qw60k")]
grid = ExtResource("5_p6jpk")
[node name="GameBoard" type="Node2D" parent="."]
script = ExtResource("4_5yls4")
grid = ExtResource("5_p6jpk")
[node name="GridDebug" type="Node2D" parent="GameBoard"]
script = ExtResource("5_y3v7k")
grid = ExtResource("5_p6jpk")
[node name="Unit" parent="GameBoard" instance=ExtResource("6_2a143")]
visible = false
position = Vector2(400, 224)
grid = ExtResource("5_p6jpk")
[node name="Cursor" type="Node2D" parent="GameBoard"]
position = Vector2(8, 8)
script = ExtResource("7_y3v7k")
grid = ExtResource("5_p6jpk")
@@ -35,4 +42,7 @@ texture = ExtResource("8_hryqi")
[node name="Timer" type="Timer" parent="GameBoard/Cursor"]
wait_time = 0.1
[editable path="CameraController"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(50, 50)
scale = Vector2(2, 2)
texture = SubResource("PlaceholderTexture2D_y3v7k")

File diff suppressed because one or more lines are too long