Compare commits

..

2 Commits

Author SHA1 Message Date
awu
6431603630 Using the new MapGlobal at various parts and switched away from the Grid.resource 2025-12-30 23:38:56 +01:00
awu
858122b8a1 Added a Autoload for the Mapsystem 2025-12-30 23:38:01 +01:00
9 changed files with 185 additions and 68 deletions

View File

@@ -1,4 +1,4 @@
[gd_resource type="TileSet" load_steps=6 format=3 uid="uid://duodt2t14xjc8"]
[gd_resource type="TileSet" load_steps=11 format=3 uid="uid://duodt2t14xjc8"]
[ext_resource type="Texture2D" uid="uid://cgvyfsuri6vmx" path="res://Graphics/TileMaps/kenney_rpgUrbanKit/Tilemap/tilemap.png" id="1_q1pea"]
@@ -11,6 +11,21 @@ tile_data = PackedInt32Array(0, 1, 15, 65536, 1, 16, 131072, 1, 17)
[sub_resource type="TileMapPattern" id="TileMapPattern_cfbhl"]
tile_data = PackedInt32Array(0, 655361, 16, 65536, 655361, 16, 131072, 655361, 16, 196608, 655361, 16, 262144, 655361, 16, 327680, 655361, 16, 393216, 655361, 16, 458752, 655361, 16, 1, 65537, 17, 65537, 65537, 17, 131073, 65537, 17, 196609, 65537, 17, 262145, 65537, 17, 327681, 65537, 17, 393217, 65537, 17, 458753, 65537, 17, 2, 131073, 17, 65538, 131073, 17, 131074, 131073, 17, 196610, 131073, 17, 262146, 131073, 17, 327682, 131073, 17, 393218, 131073, 17, 458754, 131073, 17, 3, 196609, 17, 65539, 196609, 17, 131075, 196609, 17, 196611, 196609, 17, 262147, 196609, 17, 327683, 196609, 17, 393219, 196609, 17, 458755, 196609, 17, 4, 262145, 17, 65540, 262145, 17, 131076, 262145, 17, 196612, 262145, 17, 262148, 262145, 17, 327684, 262145, 17, 393220, 262145, 17, 458756, 262145, 17, 5, 65537, 17, 65541, 65537, 17, 131077, 65537, 17, 196613, 65537, 17, 262149, 65537, 17, 327685, 65537, 17, 393221, 65537, 17, 458757, 65537, 17, 6, 655361, 16, 65542, 655361, 16, 131078, 655361, 16, 196614, 655361, 16, 262150, 655361, 16, 327686, 655361, 16, 393222, 655361, 16, 458758, 655361, 16)
[sub_resource type="TileMapPattern" id="TileMapPattern_o6jka"]
tile_data = PackedInt32Array(0, 1179649, 8, 65536, 1179649, 9, 131072, 1179649, 10, 1, 1245185, 8, 65537, 1245185, 9, 131073, 1245185, 10, 2, 1310721, 8, 65538, 1310721, 9, 131074, 1310721, 10)
[sub_resource type="TileMapPattern" id="TileMapPattern_ora7a"]
tile_data = PackedInt32Array(0, 1048577, 8, 65536, 1048577, 9)
[sub_resource type="TileMapPattern" id="TileMapPattern_71vg4"]
tile_data = PackedInt32Array(0, 1048577, 8, 65536, 1048577, 10)
[sub_resource type="TileMapPattern" id="TileMapPattern_edyo3"]
tile_data = PackedInt32Array(0, 1114113, 8, 65536, 1114113, 9)
[sub_resource type="TileMapPattern" id="TileMapPattern_ypif2"]
tile_data = PackedInt32Array(0, 1114113, 8, 65536, 1114113, 10)
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_vqaso"]
resource_name = "UrbanKit"
texture = ExtResource("1_q1pea")
@@ -1174,3 +1189,8 @@ sources/1 = SubResource("TileSetAtlasSource_vqaso")
pattern_0 = SubResource("TileMapPattern_vrbvq")
pattern_1 = SubResource("TileMapPattern_07llt")
pattern_2 = SubResource("TileMapPattern_cfbhl")
pattern_3 = SubResource("TileMapPattern_o6jka")
pattern_4 = SubResource("TileMapPattern_ora7a")
pattern_5 = SubResource("TileMapPattern_71vg4")
pattern_6 = SubResource("TileMapPattern_edyo3")
pattern_7 = SubResource("TileMapPattern_ypif2")

View File

@@ -6,12 +6,20 @@ var show_grid_display: bool:
set(v): show_grid_display = v; queue_redraw()
func toggle_grid_display(on: bool):
grid = MapGlobal.GetGrid()
show_grid_display = on
func getNewGrid():
grid = MapGlobal.GetGrid()
func _draw():
if not grid or not show_grid_display: return
for x in grid.region.size.x:
for y in grid.region.size.y:
var p = Vector2(x, y)
var p = Vector2(x + grid.region.position.x, y + grid.region.position.y)
var col = Color(1,0,0,0.3) if grid.is_point_solid(p) else Color(0,1,0,0.3)
draw_rect(Rect2(p*grid.cell_size, grid.cell_size), col)
func _on_player_move_finished() -> void:
getNewGrid() # Replace with function body.

View File

@@ -1,53 +1,16 @@
extends Node
@export var grid: Resource
@onready var _Map: Node = $Map
@onready var _Camera = $CameraController
# Units
var _UnitScene = preload("res://Scenes/Unit/unit.tscn")
var _Units: Array[Unit] = []
# MovingMarker
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
var aStarGrid: AStarGrid2D:
set(v): aStarGrid = v;
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
aStarGrid = MapGlobal.GenerateGridFromLayer($Map/Obstacles)
if $Player.is_node_ready():
$Player.setup($Map.getGrid())
$Player.setup(MapGlobal.GetGrid())
$GameBoard/Cursor.moved.connect($Player.setTarget)
$GameBoard/Cursor.accept_pressed.connect($Player.selectUnit)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _createUnit(pos: Vector2i):
var newUnit = _UnitScene.instantiate()
newUnit.set("_spawnPosition", pos)
_Units.append(newUnit as Unit)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("SetMarker"):
print("Action is SetMarker")
# _MovingMarker.global_position = _getMousePosition(event).position
_MovingMarker.position = GroundLayer.local_to_map(event.position)
_MovingMarker.show()
func selectUnitAt(cell: Vector2i):
pass
func _getScreenCenter():
return get_viewport().get_camera_2d().get_screen_center_position()
@@ -55,19 +18,5 @@ func _getScreenCenter():
func _getMousePosition(event: InputEvent):
return get_viewport().get_camera_2d().make_input_local(event)
#func prepareAStarGrid():
# var astarGrid = AStarGrid2D.new()
# astarGrid.cell_size = GroundLayer.tile_set.tile_size
# astarGrid.region = Rect2(Vector2.ZERO, ceil(get_viewport_rect().size / astarGrid.cell_size))
# astarGrid.update()
#
# for id in ObstacleLayer.get_used_cells():
# var data: TileData = ObstacleLayer.get_cell_tile_data(id)
# if data and data.get_custom_data('obstacle'):
# astarGrid.set_point_solid(id)
#
# %GridDisplay.grid = astarGrid
func get_cell_information(cell):
print(cell)

View File

@@ -1,7 +1,8 @@
[gd_scene load_steps=10 format=3 uid="uid://d05j5yuhlsxp0"]
[gd_scene load_steps=11 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="Script" uid="uid://76bfwrwrqfqr" path="res://Scenes/Map/map_global.gd" id="3_hryqi"]
[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"]
@@ -13,9 +14,9 @@
[node name="Main" type="Node"]
script = ExtResource("1_qw60k")
grid = ExtResource("5_p6jpk")
[node name="Map" parent="." instance=ExtResource("1_1r6ip")]
script = ExtResource("3_hryqi")
[node name="GameBoard" type="Node2D" parent="."]
script = ExtResource("4_5yls4")
@@ -42,7 +43,6 @@ scale = Vector2(2, 2)
texture = SubResource("PlaceholderTexture2D_y3v7k")
[node name="HUD" type="CanvasLayer" parent="."]
visible = false
[node name="GridDisplay" type="Control" parent="HUD"]
unique_name_in_owner = true
@@ -69,3 +69,4 @@ position = Vector2(8, 8)
[connection signal="accept_pressed" from="GameBoard/Cursor" to="." method="get_cell_information"]
[connection signal="toggled" from="HUD/GridDisplay/MarginContainer/PanelContainer/ShowGrid" to="HUD/GridDisplay" method="toggle_grid_display"]
[connection signal="MoveFinished" from="Player" to="HUD/GridDisplay" method="_on_player_move_finished"]

View File

@@ -1,16 +1,24 @@
extends CharacterBody2D
@export var SPEED = 400.0
@export var unitData = {
"MoveRange": 5,
"MoveRangeV": Vector2i(5, 5),
"Speed": 400.0
}
var grid: AStarGrid2D
var currentCell: Vector2i
var currentPoint: int
var selected: bool:
set(v):
selected = v; $PathPrev.visible = selected;
selected = v; $PathPrev.visible = selected; if selected: MapGlobal.SetRegion(calculateMovementRegion())
var moving: bool:
set(v):
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
signal MoveFinished
var targetCell: Vector2i
var movePts: Array
@@ -25,7 +33,7 @@ func setup(_grid: AStarGrid2D):
targetCell = currentCell
func pos_to_cell(pos: Vector2):
return pos / grid.cell_size
return pos / Vector2(MapGlobal.getData()["cellSize"])
func _input(event: InputEvent) -> void:
if moving: return
@@ -36,10 +44,12 @@ func setTarget(target: Vector2i):
if !selected: return
if moving: return
if target != targetCell:
movePts = grid.get_point_path(currentCell, target)
print("Setting target: ")
movePts = MapGlobal.GetGrid().get_point_path(currentCell, target)
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
$PathPrev.points = movePts
targetCell = target
print (targetCell)
func startMove():
@@ -51,10 +61,13 @@ func _physics_process(delta: float) -> void:
velocity = Vector2.ZERO
global_position = movePts[-1]
currentCell = pos_to_cell(global_position)
$PathPrev.points = []; moving = false
$PathPrev.points = [];
moving = false; MoveFinished.emit();
MapGlobal.SetRegion(calculateMovementRegion(), unitData["MoveRangeV"])
else:
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
velocity = direction * SPEED
velocity = direction * unitData["Speed"]
move_and_slide()
if (movePts[currentPoint+1] - global_position).length() < 4:
currentCell = pos_to_cell(global_position)
@@ -65,3 +78,8 @@ func selectUnit(cell: Vector2i):
selected = true
else:
selected = false
func calculateMovementRegion():
var region := Rect2i(pos_to_cell(global_position), Vector2i(unitData["MoveRange"], unitData["MoveRange"]))
region.set_center
return region

File diff suppressed because one or more lines are too long

120
Scenes/Map/map_global.gd Normal file
View File

@@ -0,0 +1,120 @@
extends Node2D
@onready var GroundLayer: TileMapLayer = $Ground
@onready var ObstacleLayer: TileMapLayer = $Obstacles
# Maybe a SubClass that is a Singleton, so we can create one grid
# and then change it per Map
var _aStarGrid: AStarGrid2D
var _gridData = {
"cellSize": Vector2.ZERO,
"fullRegion": Rect2i(0,0,0,0),
"currentRegion": Rect2i(0,0,0,0)
}
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#_aStarGrid = generateSimpleGrid()
# _aStarGrid = generateObstacleGrid()
# %GridDisplay.grid = _aStarGrid
pass
func generateSimpleGrid() -> AStarGrid2D:
var grid = newGrid(Vector2.ZERO, Rect2i(0,0,0,0))
for id in GroundLayer.get_used_cells():
var data: TileData = GroundLayer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
grid.set_point_solid(id)
return grid
func generateObstacleGrid() -> AStarGrid2D:
var grid = newGrid(Vector2.ZERO, Rect2i(0,0,0,0))
var layers = ObstacleLayer.get_children()
for layer in layers:
for id in layer.get_used_cells():
var data: TileData = layer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
grid.set_point_solid(id)
return grid
func GenerateGridFromLayer(layer: TileMapLayer):
var ground = layer.get_parent().get_node("Ground")
#var grid = newGrid(ground.tile_set.tile_size, ground.get_used_rect())
var grid = GetGrid()
if _gridData["cellSize"] == Vector2.ZERO:
SetCellSize(ground.tile_set.tile_size)
if _gridData["fullRegion"] == Rect2i(0,0,0,0):
_gridData["fullRegion"] = ground.get_used_rect()
_updateRegionFrom("fullRegion")
var childLayers = layer.get_children()
if childLayers.size() > 0:
for childLayer in childLayers:
for id in childLayer.get_used_cells():
var data: TileData = childLayer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
GetGrid().set_point_solid(id)
else:
for id in layer.get_used_cells():
var data: TileData = layer.get_cell_tile_data(id)
if data and data.get_custom_data('obstacle'):
GetGrid().set_point_solid(id)
GetGrid().update()
func newGrid(cellSize: Vector2, region: Rect2i) -> AStarGrid2D:
_gridData["cellSize"] = cellSize
_gridData["fullRegion"] = region
var _astarGrid = AStarGrid2D.new()
_astarGrid.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
_astarGrid.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
_astarGrid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES
_astarGrid.cell_size = cellSize
_astarGrid.region = region
_astarGrid.update()
_aStarGrid = _astarGrid
return _astarGrid
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func GetGrid():
if !_aStarGrid:
_aStarGrid = newGrid(Vector2.ZERO, Rect2i())
return _aStarGrid
func SetGridData(key: Variant, value: Variant):
if _gridData.has(key):
_gridData[key] = value
func getData():
return _gridData
func SetRegion(region: Rect2i, offset: Vector2i = Vector2i.ZERO):
_gridData["currentRegion"] = region
_aStarGrid.offset = offset
_updateRegionFrom("currentRegion")
func _updateRegionFrom(region: String):
_aStarGrid.region = _gridData[region]
_aStarGrid.update()
func SetCellSize(cellSize: Vector2):
_gridData["cellSize"] = cellSize
_updateCellSize()
func _updateCellSize():
_aStarGrid.cell_size = _gridData["cellSize"]
_aStarGrid.update()

View File

@@ -0,0 +1 @@
uid://76bfwrwrqfqr

View File

@@ -18,6 +18,7 @@ config/icon="uid://dorwg1gp5fr41"
[autoload]
CameraControl="*res://addons/smartcamera2D/CameraControl.gd"
MapGlobal="*res://Scenes/Map/map_global.gd"
[display]