Compare commits

..

3 Commits

Author SHA1 Message Date
awu
ca236b4941 Using new grid and player 2025-12-26 04:10:56 +01:00
awu
62185f522d Started refactoring for easier use 2025-12-26 04:10:39 +01:00
Aaron Wuthenow
6f667506c4 New version of player. Working together with the AStarGrid 2025-12-26 04:09:29 +01:00
5 changed files with 95 additions and 11 deletions

View File

@@ -40,6 +40,8 @@ func _ready() -> void:
# UnitCamera.target = _Units[0].get_path_to(get_parent())
# UnitCamera.target_node = _Units[0]
$Player.setup($Map.getGrid())
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=10 format=3 uid="uid://d05j5yuhlsxp0"]
[gd_scene load_steps=13 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"]
@@ -8,9 +8,16 @@
[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="Script" uid="uid://dtme43jtijpok" path="res://Scenes/Main/grid_display.gd" id="8_y3v7k"]
[ext_resource type="Script" uid="uid://dm40sxvvnbdrn" path="res://Scenes/Main/player.gd" id="9_hryqi"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_y3v7k"]
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_imbg2"]
size = Vector2(16, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_hfg1r"]
size = Vector2(32, 32)
[node name="Main" type="Node"]
script = ExtResource("1_qw60k")
grid = ExtResource("5_p6jpk")
@@ -70,6 +77,20 @@ layout_mode = 2
layout_mode = 2
text = "Show Grid"
[node name="Player" type="CharacterBody2D" parent="."]
script = ExtResource("9_hryqi")
[node name="PathPrev" type="Line2D" parent="Player"]
top_level = true
width = 2.0
default_color = Color(0, 0.3372549, 0.3372549, 1)
[node name="Sprite2D" type="Sprite2D" parent="Player"]
texture = SubResource("PlaceholderTexture2D_imbg2")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
shape = SubResource("RectangleShape2D_hfg1r")
[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"]

52
Scenes/Main/player.gd Normal file
View File

@@ -0,0 +1,52 @@
extends CharacterBody2D
@export var SPEED = 400.0
var grid: AStarGrid2D
var currentCell: Vector2i
var currentPoint: int
var moving: bool:
set(v):
moving = v; $PathPrev.visible = not moving; set_physics_process(moving)
var targetCell: Vector2i
var movePts: Array
func _ready(): moving = false
func setup(_grid: AStarGrid2D):
grid = _grid
currentCell = pos_to_cell(global_position)
targetCell = currentCell
func pos_to_cell(pos: Vector2):
return pos / grid.cell_size
func _input(event: InputEvent) -> void:
if moving: return
if event is InputEventMouseButton:
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT: startMove()
elif event is InputEventMouseMotion:
var target = Vector2i(pos_to_cell(event.position))
if target != targetCell:
movePts = grid.get_point_path(currentCell, target)
movePts = (movePts as Array).map(func (point): return point + grid.cell_size/2.0)
$PathPrev.points = movePts
targetCell = target
func startMove():
if movePts.is_empty(): return
currentPoint = 0; moving = true
func _physics_process(delta: float) -> void:
if currentPoint == movePts.size() - 1:
velocity = Vector2.ZERO
global_position = movePts[-1]
currentCell = pos_to_cell(global_position)
$PathPrev.points = []; moving = false
else:
var direction = (movePts[currentPoint+1] - movePts[currentPoint]).normalized()
velocity = direction * SPEED
move_and_slide()
if (movePts[currentPoint+1] - global_position).length() < 4:
currentCell = pos_to_cell(global_position)
currentPoint += 1

View File

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

View File

@@ -13,10 +13,7 @@ func _ready() -> void:
%GridDisplay.grid = astarGrid
func generateSimpleGrid() -> AStarGrid2D:
var grid = AStarGrid2D.new()
grid.cell_size = Vector2(GroundLayer.tile_set.tile_size)
grid.region = GroundLayer.get_used_rect()
grid.update()
var grid = newGrid()
for id in GroundLayer.get_used_cells():
var data: TileData = GroundLayer.get_cell_tile_data(id)
@@ -26,20 +23,31 @@ func generateSimpleGrid() -> AStarGrid2D:
return grid
func generateObstacleGrid() -> AStarGrid2D:
astarGrid = AStarGrid2D.new()
astarGrid.cell_size = Vector2(GroundLayer.tile_set.tile_size)
astarGrid.region = GroundLayer.get_used_rect()
astarGrid.update()
var grid = newGrid()
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'):
astarGrid.set_point_solid(id)
grid.set_point_solid(id)
return astarGrid
return grid
func newGrid() -> AStarGrid2D:
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 = Vector2(GroundLayer.tile_set.tile_size)
_astarGrid.region = GroundLayer.get_used_rect()
_astarGrid.update()
return _astarGrid
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func getGrid():
return astarGrid