Grid finally working and understood
This commit is contained in:
@@ -17,23 +17,19 @@ signal moved(new_cell)
|
||||
## Coordinates of the current cell the cursor is hovering.
|
||||
var cell := Vector2.ZERO:
|
||||
set(value):
|
||||
print("Setting cell to: ", value)
|
||||
# We first clamp the cell coordinates and ensure that we aren't
|
||||
# trying to move outside the grid boundaries
|
||||
var new_cell: Vector2 = grid.clamp(value)
|
||||
print("New cell: ", new_cell)
|
||||
if new_cell.is_equal_approx(cell):
|
||||
return
|
||||
|
||||
cell = new_cell
|
||||
print("Cell is ", cell)
|
||||
# If we move to a new cell, we update the cursor's position, emit
|
||||
# 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
|
||||
# global_position = grid.calculateMapPosition(cell)
|
||||
|
||||
global_position = GroundLayer.map_to_local(cell)
|
||||
print("Position is ", position)
|
||||
emit_signal("moved", cell)
|
||||
_timer.start()
|
||||
|
||||
@@ -54,7 +50,7 @@ func _ready() -> void:
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
# Navigating cells with the mouse.
|
||||
if event is InputEventMouseMotion:
|
||||
cell = grid.calculateGridCoordinates(event.position)
|
||||
cell = GroundLayer.local_to_map(event.position)
|
||||
# Trying to select something in a cell.
|
||||
elif event.is_action_pressed("Select") or event.is_action_pressed("ui_accept"):
|
||||
emit_signal("accept_pressed", cell)
|
||||
|
||||
17
Scenes/Main/grid_display.gd
Normal file
17
Scenes/Main/grid_display.gd
Normal file
@@ -0,0 +1,17 @@
|
||||
extends Control
|
||||
|
||||
var grid: AStarGrid2D:
|
||||
set(v): grid = v; queue_redraw()
|
||||
var show_grid_display: bool:
|
||||
set(v): show_grid_display = v; queue_redraw()
|
||||
|
||||
func toggle_grid_display(on: bool):
|
||||
show_grid_display = on
|
||||
|
||||
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 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)
|
||||
1
Scenes/Main/grid_display.gd.uid
Normal file
1
Scenes/Main/grid_display.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dtme43jtijpok
|
||||
@@ -18,6 +18,9 @@ var _MovingMarker: Node2D = _MovingMarkerScene.instantiate()
|
||||
|
||||
@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:
|
||||
# add_child(_MovingMarker)
|
||||
@@ -26,7 +29,7 @@ func _ready() -> void:
|
||||
# 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)))
|
||||
_createUnit(GroundLayer.map_to_local(Vector2i(25,25)))
|
||||
|
||||
for unit in _Units:
|
||||
add_child(unit)
|
||||
@@ -36,9 +39,6 @@ func _ready() -> void:
|
||||
# if firstUnit.is_node_ready():
|
||||
# UnitCamera.target = _Units[0].get_path_to(get_parent())
|
||||
# UnitCamera.target_node = _Units[0]
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
@@ -63,3 +63,20 @@ 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)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
[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"]
|
||||
[ext_resource type="Script" uid="uid://dtme43jtijpok" path="res://Scenes/Main/grid_display.gd" id="8_y3v7k"]
|
||||
|
||||
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_y3v7k"]
|
||||
|
||||
@@ -26,10 +26,12 @@ 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="Sprite2D" parent="GameBoard/Unit/PathFollow2D" index="1"]
|
||||
texture = null
|
||||
|
||||
[node name="Cursor" type="Node2D" parent="GameBoard"]
|
||||
position = Vector2(8, 8)
|
||||
script = ExtResource("7_y3v7k")
|
||||
@@ -37,7 +39,6 @@ grid = ExtResource("5_p6jpk")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="GameBoard/Cursor"]
|
||||
position = Vector2(10, -10)
|
||||
texture = ExtResource("8_hryqi")
|
||||
|
||||
[node name="Timer" type="Timer" parent="GameBoard/Cursor"]
|
||||
wait_time = 0.1
|
||||
@@ -46,3 +47,30 @@ wait_time = 0.1
|
||||
position = Vector2(50, 50)
|
||||
scale = Vector2(2, 2)
|
||||
texture = SubResource("PlaceholderTexture2D_y3v7k")
|
||||
|
||||
[node name="HUD" type="CanvasLayer" parent="."]
|
||||
|
||||
[node name="GridDisplay" type="Control" parent="HUD"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 3
|
||||
anchors_preset = 0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
script = ExtResource("8_y3v7k")
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="HUD/GridDisplay"]
|
||||
layout_mode = 0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="HUD/GridDisplay/MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="ShowGrid" type="CheckBox" parent="HUD/GridDisplay/MarginContainer/PanelContainer"]
|
||||
layout_mode = 2
|
||||
text = "Show Grid"
|
||||
|
||||
[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"]
|
||||
|
||||
[editable path="GameBoard/Unit"]
|
||||
|
||||
Reference in New Issue
Block a user