Using the new MapGlobal at various parts and switched away from the Grid.resource
This commit is contained in:
@@ -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"]
|
[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"]
|
[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)
|
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"]
|
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_vqaso"]
|
||||||
resource_name = "UrbanKit"
|
resource_name = "UrbanKit"
|
||||||
texture = ExtResource("1_q1pea")
|
texture = ExtResource("1_q1pea")
|
||||||
@@ -1174,3 +1189,8 @@ sources/1 = SubResource("TileSetAtlasSource_vqaso")
|
|||||||
pattern_0 = SubResource("TileMapPattern_vrbvq")
|
pattern_0 = SubResource("TileMapPattern_vrbvq")
|
||||||
pattern_1 = SubResource("TileMapPattern_07llt")
|
pattern_1 = SubResource("TileMapPattern_07llt")
|
||||||
pattern_2 = SubResource("TileMapPattern_cfbhl")
|
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")
|
||||||
|
|||||||
@@ -6,12 +6,20 @@ var show_grid_display: bool:
|
|||||||
set(v): show_grid_display = v; queue_redraw()
|
set(v): show_grid_display = v; queue_redraw()
|
||||||
|
|
||||||
func toggle_grid_display(on: bool):
|
func toggle_grid_display(on: bool):
|
||||||
|
grid = MapGlobal.GetGrid()
|
||||||
show_grid_display = on
|
show_grid_display = on
|
||||||
|
|
||||||
|
func getNewGrid():
|
||||||
|
grid = MapGlobal.GetGrid()
|
||||||
|
|
||||||
func _draw():
|
func _draw():
|
||||||
if not grid or not show_grid_display: return
|
if not grid or not show_grid_display: return
|
||||||
for x in grid.region.size.x:
|
for x in grid.region.size.x:
|
||||||
for y in grid.region.size.y:
|
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)
|
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)
|
draw_rect(Rect2(p*grid.cell_size, grid.cell_size), col)
|
||||||
|
|
||||||
|
|
||||||
|
func _on_player_move_finished() -> void:
|
||||||
|
getNewGrid() # Replace with function body.
|
||||||
|
|||||||
@@ -1,53 +1,16 @@
|
|||||||
extends Node
|
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:
|
var aStarGrid: AStarGrid2D:
|
||||||
set(v): aStarGrid = v;
|
set(v): aStarGrid = v;
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
aStarGrid = MapGlobal.GenerateGridFromLayer($Map/Obstacles)
|
||||||
if $Player.is_node_ready():
|
if $Player.is_node_ready():
|
||||||
$Player.setup($Map.getGrid())
|
$Player.setup(MapGlobal.GetGrid())
|
||||||
$GameBoard/Cursor.moved.connect($Player.setTarget)
|
$GameBoard/Cursor.moved.connect($Player.setTarget)
|
||||||
$GameBoard/Cursor.accept_pressed.connect($Player.selectUnit)
|
$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():
|
func _getScreenCenter():
|
||||||
return get_viewport().get_camera_2d().get_screen_center_position()
|
return get_viewport().get_camera_2d().get_screen_center_position()
|
||||||
|
|
||||||
@@ -55,19 +18,5 @@ func _getScreenCenter():
|
|||||||
func _getMousePosition(event: InputEvent):
|
func _getMousePosition(event: InputEvent):
|
||||||
return get_viewport().get_camera_2d().make_input_local(event)
|
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):
|
func get_cell_information(cell):
|
||||||
print(cell)
|
print(cell)
|
||||||
|
|||||||
@@ -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="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://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="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="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="Script" uid="uid://dukn3yshfepum" path="res://Scenes/Main/grid_debug.gd" id="5_y3v7k"]
|
||||||
@@ -13,9 +14,9 @@
|
|||||||
|
|
||||||
[node name="Main" type="Node"]
|
[node name="Main" type="Node"]
|
||||||
script = ExtResource("1_qw60k")
|
script = ExtResource("1_qw60k")
|
||||||
grid = ExtResource("5_p6jpk")
|
|
||||||
|
|
||||||
[node name="Map" parent="." instance=ExtResource("1_1r6ip")]
|
[node name="Map" parent="." instance=ExtResource("1_1r6ip")]
|
||||||
|
script = ExtResource("3_hryqi")
|
||||||
|
|
||||||
[node name="GameBoard" type="Node2D" parent="."]
|
[node name="GameBoard" type="Node2D" parent="."]
|
||||||
script = ExtResource("4_5yls4")
|
script = ExtResource("4_5yls4")
|
||||||
@@ -42,7 +43,6 @@ scale = Vector2(2, 2)
|
|||||||
texture = SubResource("PlaceholderTexture2D_y3v7k")
|
texture = SubResource("PlaceholderTexture2D_y3v7k")
|
||||||
|
|
||||||
[node name="HUD" type="CanvasLayer" parent="."]
|
[node name="HUD" type="CanvasLayer" parent="."]
|
||||||
visible = false
|
|
||||||
|
|
||||||
[node name="GridDisplay" type="Control" parent="HUD"]
|
[node name="GridDisplay" type="Control" parent="HUD"]
|
||||||
unique_name_in_owner = true
|
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="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="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"]
|
||||||
|
|||||||
@@ -1,16 +1,24 @@
|
|||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
@export var SPEED = 400.0
|
@export var unitData = {
|
||||||
|
"MoveRange": 5,
|
||||||
|
"MoveRangeV": Vector2i(5, 5),
|
||||||
|
"Speed": 400.0
|
||||||
|
}
|
||||||
|
|
||||||
var grid: AStarGrid2D
|
var grid: AStarGrid2D
|
||||||
var currentCell: Vector2i
|
var currentCell: Vector2i
|
||||||
var currentPoint: int
|
var currentPoint: int
|
||||||
|
|
||||||
var selected: bool:
|
var selected: bool:
|
||||||
set(v):
|
set(v):
|
||||||
selected = v; $PathPrev.visible = selected;
|
selected = v; $PathPrev.visible = selected; if selected: MapGlobal.SetRegion(calculateMovementRegion())
|
||||||
var moving: bool:
|
var moving: bool:
|
||||||
set(v):
|
set(v):
|
||||||
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
|
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
|
||||||
|
|
||||||
|
signal MoveFinished
|
||||||
|
|
||||||
var targetCell: Vector2i
|
var targetCell: Vector2i
|
||||||
var movePts: Array
|
var movePts: Array
|
||||||
|
|
||||||
@@ -25,7 +33,7 @@ func setup(_grid: AStarGrid2D):
|
|||||||
targetCell = currentCell
|
targetCell = currentCell
|
||||||
|
|
||||||
func pos_to_cell(pos: Vector2):
|
func pos_to_cell(pos: Vector2):
|
||||||
return pos / grid.cell_size
|
return pos / Vector2(MapGlobal.getData()["cellSize"])
|
||||||
|
|
||||||
func _input(event: InputEvent) -> void:
|
func _input(event: InputEvent) -> void:
|
||||||
if moving: return
|
if moving: return
|
||||||
@@ -36,10 +44,12 @@ func setTarget(target: Vector2i):
|
|||||||
if !selected: return
|
if !selected: return
|
||||||
if moving: return
|
if moving: return
|
||||||
if target != targetCell:
|
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)
|
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
|
||||||
$PathPrev.points = movePts
|
$PathPrev.points = movePts
|
||||||
targetCell = target
|
targetCell = target
|
||||||
|
print (targetCell)
|
||||||
|
|
||||||
|
|
||||||
func startMove():
|
func startMove():
|
||||||
@@ -51,10 +61,13 @@ func _physics_process(delta: float) -> void:
|
|||||||
velocity = Vector2.ZERO
|
velocity = Vector2.ZERO
|
||||||
global_position = movePts[-1]
|
global_position = movePts[-1]
|
||||||
currentCell = pos_to_cell(global_position)
|
currentCell = pos_to_cell(global_position)
|
||||||
$PathPrev.points = []; moving = false
|
$PathPrev.points = [];
|
||||||
|
moving = false; MoveFinished.emit();
|
||||||
|
MapGlobal.SetRegion(calculateMovementRegion(), unitData["MoveRangeV"])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
|
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
|
||||||
velocity = direction * SPEED
|
velocity = direction * unitData["Speed"]
|
||||||
move_and_slide()
|
move_and_slide()
|
||||||
if (movePts[currentPoint+1] - global_position).length() < 4:
|
if (movePts[currentPoint+1] - global_position).length() < 4:
|
||||||
currentCell = pos_to_cell(global_position)
|
currentCell = pos_to_cell(global_position)
|
||||||
@@ -65,3 +78,8 @@ func selectUnit(cell: Vector2i):
|
|||||||
selected = true
|
selected = true
|
||||||
else:
|
else:
|
||||||
selected = false
|
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
@@ -18,6 +18,7 @@ config/icon="uid://dorwg1gp5fr41"
|
|||||||
[autoload]
|
[autoload]
|
||||||
|
|
||||||
CameraControl="*res://addons/smartcamera2D/CameraControl.gd"
|
CameraControl="*res://addons/smartcamera2D/CameraControl.gd"
|
||||||
|
MapGlobal="*res://Scenes/Map/map_global.gd"
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user