diff --git a/Map.cs b/Map.cs index 2cb87eb..86ee4e9 100644 --- a/Map.cs +++ b/Map.cs @@ -9,9 +9,16 @@ public partial class Map : TileMapLayer public int _minX, _maxX, _minY, _maxY, _mainSource = 0; public string _isSolidString = "is_solid"; public Vector2 _cellSize, _sizeInPixels, _sizeInCells; - public List _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); + public List _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); public AStarGrid2D _astar = new(); public Dictionary _addressOccupants = new(); + public List _cells + { + get + { + return [.. GetUsedCells()]; + } + } public int _firstOpenRow { get @@ -72,7 +79,6 @@ public partial class Map : TileMapLayer { base._Ready(); _cellSize = TileSet.TileSize; - _cells = [.. GetUsedCells()]; _cells.ForEach(c => _addressOccupants[c] = null); _minX = _cells.Min(c => c.X); _maxX = _cells.Max(c => c.X); @@ -149,10 +155,8 @@ public partial class Map : TileMapLayer } PEG._address = ADDRESS; } - _addressOccupants[ADDRESS] = PEG; SetCellSolid(ADDRESS); - PEG._path.Add(ADDRESS); } public void SetCellSolid(Vector2I ADDRESS) @@ -175,11 +179,11 @@ public partial class Map : TileMapLayer EvaluateSolidCells(); } - public List GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false) + public List GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false, bool PARTIAL = false) { _astar.SetPointSolid(FROM, false); - List pathTaken = [.. _astar.GetIdPath(FROM, TO)]; + List pathTaken = [.. _astar.GetIdPath(FROM, TO, PARTIAL)]; _astar.SetPointSolid(FROM, true); if (!INCLUDE_FROM) diff --git a/Peg.cs b/Peg.cs index a2985b9..61f3eed 100644 --- a/Peg.cs +++ b/Peg.cs @@ -44,22 +44,21 @@ public partial class Peg : HoverableNode } - public virtual List GetBestPath() + public virtual List GetBestPath(bool PARTIAL = false) { Map map = _pegController._playArea._map; List goals = GetGoals(); - List> paths = new(); for (int i = 0; i < goals.Count; i++) { - paths.Add(map.GetPath(_address, goals[i])); + List path = map.GetPath(_address, goals[i], false, PARTIAL); + if (path.Count > 0) + { + return path; + } + } - List> pathsOverZeroCount = [.. paths.Where(p => p.Count > 0)]; - if (pathsOverZeroCount.Count == 0) - { - return []; - } - return pathsOverZeroCount.OrderBy(p => p.Count).ToList()[0]; + return []; } public virtual List GetVisibleCells() @@ -71,18 +70,9 @@ public partial class Peg : HoverableNode public virtual List GetGoals() { Map map = _pegController._playArea._map; - TileMapLayer pathLayer = _pegController._playArea.GetNode("PathLayer"); - List pl = [.. pathLayer.GetUsedCells()]; List visible = GetVisibleCells(); - if (_id == 4) - for (int i = 0; i < pl.Count; i++) - { - Vector2I cell = pl[i]; - pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(cell,0,Vector2I.Down + Vector2I.Right*(visible.IndexOf(cell) > -1 ? 1 : 4))); - - } List unoccupied = [.. visible.Where(c => !map._astar.IsPointSolid(c))]; - List closest = [.. unoccupied.Where(c => c.Y == (_disposition == -1 ? unoccupied.Min(c => c.Y) : unoccupied.Max(c => c.Y))).OrderByDescending(c => Math.Abs(c.X - _address.X))]; + List closest = [.. unoccupied.OrderByDescending(c => c.Y * _disposition).ThenByDescending(c => Math.Abs(c.X - _address.X))]; return closest; } diff --git a/PegController.cs b/PegController.cs index fafff24..84f651a 100644 --- a/PegController.cs +++ b/PegController.cs @@ -85,9 +85,10 @@ public partial class PegController : TurnController { action.DoImmediately(peg); string key = action._priority + ":" + loop; + if (!_tweenStages.ContainsKey(key)) { - _tweenStages[key] = new(); + _tweenStages[key] = new(); } _tweenStages[key].Add(new(peg, action)); if (!anyPegsActed) @@ -104,7 +105,6 @@ public partial class PegController : TurnController public void HandlePegClick(Node CLICKED_NODE, int CLICK_TYPE) { - TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); if (CLICKED_NODE is not Peg peg) { return; @@ -113,10 +113,21 @@ public partial class PegController : TurnController { List newPath = peg.GetBestPath(); - pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4)); - for (int i = 0; i < newPath.Count; i++) + // pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4)); + // for (int i = 0; i < newPath.Count; i++) + // { + // pathLayer.SetCell(newPath[i],0,Vector2I.One); + // } + Map map = _playArea._map; + TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); + List pl = [.. pathLayer.GetUsedCells()]; + List visible = peg.GetVisibleCells(); + + for (int i = 0; i < pl.Count; i++) { - pathLayer.SetCell(newPath[i],0,Vector2I.One); + Vector2I cell = pl[i]; + pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(cell,0,Vector2I.Down + Vector2I.Right*(visible.IndexOf(cell) > -1 ? 1 : 4))); + } } else if (CLICK_TYPE == 2) @@ -149,6 +160,7 @@ public partial class PegController : TurnController public void ProcessTween() { _tweenStages = _tweenStages.OrderBy(s => int.Parse(s.Key.Split(":")[0])).ThenBy(s => int.Parse(s.Key.Split(":")[1])).ToDictionary(); + if (_tweenStages.Count <= 0) { EndTurn(); diff --git a/Pegs/Actions/BasicMovement.cs b/Pegs/Actions/BasicMovement.cs index 9a7ec57..0b248a9 100644 --- a/Pegs/Actions/BasicMovement.cs +++ b/Pegs/Actions/BasicMovement.cs @@ -1,6 +1,7 @@ using Godot; using System; using System.Collections.Generic; +using System.Linq; public partial class BasicMovement : PegAction { @@ -17,11 +18,15 @@ public partial class BasicMovement : PegAction public override Tween CreateAnimation(Peg PEG) { + GD.Print(PEG._address); PegController pegController = PEG._pegController; Map map = pegController._playArea._map; Vector2I cell = PEG._path[0]; + Tween subtween = CreateTween(); - subtween.TweenProperty(PEG, "global_position", map.GetCellPositionFromAddress(cell), 0.25f); + Vector2 target = map.GetCellPositionFromAddress(cell); + + subtween.TweenProperty(PEG, "global_position", target, 0.25f); PEG._path.RemoveAt(0); return subtween; } @@ -36,7 +41,9 @@ public partial class BasicMovement : PegAction return; } Vector2I cell = path[0]; + map.SetCellPeg(cell, PEG); + PEG._path.Add(cell); } public override bool MeetsCriteria(Peg PEG) @@ -44,4 +51,6 @@ public partial class BasicMovement : PegAction return base.MeetsCriteria(PEG) && PEG._address.Y > PEG._pegController._playArea._map._firstOpenRow; } + + } diff --git a/Pegs/FriendlyPegs/Spearman.cs b/Pegs/FriendlyPegs/Spearman.cs new file mode 100644 index 0000000..616be7a --- /dev/null +++ b/Pegs/FriendlyPegs/Spearman.cs @@ -0,0 +1,23 @@ +using Godot; +using System; +using System.Collections.Generic; +using System.Linq; + +public partial class Spearman : FriendlyPeg +{ + public override void _Ready() + { + base._Ready(); + _stamina = 2; + _visibility = 4; + } + + public override List GetGoals() + { + Map map = _pegController._playArea._map; + List enemies = [.. _pegController._pegs.Where(p => p._disposition == -_disposition)]; + List neighboring = [.. map._cells.Where(c => enemies.Any(e => (e._address - c).Length() <= 1f))]; + List closest = [.. neighboring.OrderBy(c => (c - _address).Length())]; + return closest; + } +} diff --git a/Pegs/FriendlyPegs/Spearman.cs.uid b/Pegs/FriendlyPegs/Spearman.cs.uid new file mode 100644 index 0000000..b797254 --- /dev/null +++ b/Pegs/FriendlyPegs/Spearman.cs.uid @@ -0,0 +1 @@ +uid://dd4k8egcg3r2v diff --git a/Pegs/FriendlyPegs/spearman.tscn b/Pegs/FriendlyPegs/spearman.tscn new file mode 100644 index 0000000..fc4e9a0 --- /dev/null +++ b/Pegs/FriendlyPegs/spearman.tscn @@ -0,0 +1,36 @@ +[gd_scene format=3 uid="uid://b5rrpaj7up7w3"] + +[ext_resource type="Script" uid="uid://dd4k8egcg3r2v" path="res://Pegs/FriendlyPegs/Spearman.cs" id="1_rd6ui"] +[ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="2_v2nt8"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] +bounce = 0.5 + +[sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"] +radius = 12.5 + +[sub_resource type="CircleShape2D" id="CircleShape2D_6w6fg"] +radius = 12.5 + +[node name="Spearman" type="StaticBody2D" unique_id=1417697759] +input_pickable = true +physics_material_override = SubResource("PhysicsMaterial_7k104") +script = ExtResource("1_rd6ui") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1762191899] +shape = SubResource("CircleShape2D_4gyqm") + +[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605] +texture_filter = 1 +scale = Vector2(0.5, 0.5) +texture = ExtResource("2_v2nt8") + +[node name="Actions" type="Node2D" parent="." unique_id=2023031702] + +[node name="HoverBounds" type="Area2D" parent="." unique_id=937525982] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HoverBounds" unique_id=2142666816] +shape = SubResource("CircleShape2D_6w6fg") + +[connection signal="mouse_entered" from="." to="." method="OnMouseEntered"] +[connection signal="mouse_exited" from="." to="." method="OnMouseExited"]