diff --git a/Map.cs b/Map.cs index 487ad9f..aae215f 100644 --- a/Map.cs +++ b/Map.cs @@ -102,7 +102,6 @@ public partial class Map : TileMapLayer PEG._address = ADDRESS; } - _addressOccupants[ADDRESS] = PEG; SetCellSolid(ADDRESS); } diff --git a/Peg.cs b/Peg.cs index 1872c8c..e48f935 100644 --- a/Peg.cs +++ b/Peg.cs @@ -7,49 +7,54 @@ public partial class Peg : HoverableNode { [Signal] public delegate void DeathEventHandler(Peg THIS); - public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _visibility = 4, _movement = 0, _movementCost = 1; + public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _visibility = 4, _movement = 0; public Dictionary _priorities = new() { - {"ball", 1000000}, // 1000000 to 1999999 reserved for ball priorities + {"act", 1000000}, // 1000000 to 1999999 reserved for action priorities {"movement", 0} // 0 to 999999 reserved for movement priorities }; public Vector2I _address = -Vector2I.One, _range = Vector2I.Up; public List _path = new(); public PegController _pegController; - public PegAction _action; + public List _actions; + public int _distanceToGoal + { + get + { + return GetBestPath().Count; + } + } public override void _Ready() { base._Ready(); + ReprioritizeActions(); } - public virtual void Act() + public virtual bool Act(int SUB_STEP) { - if (_action == null) + PegAction action = null; + for (int i = 0; i < _actions.Count; i++) + { + if (_actions[i].MeetsCriteria(this)) + { + action = _actions[i]; + break; + } + } + if (action == null) { GD.Print("NO ACTION"); - return; + return false; } - Tween subtween = _action.CreateAnimation(this); - if (!_pegController._tweenStages.ContainsKey(_action._priority)) + Tween subtween = action.CreateAnimation(this); + int key = action._priority + SUB_STEP; + if (!_pegController._tweenStages.ContainsKey(key)) { - _pegController._tweenStages[_action._priority] = new(); + _pegController._tweenStages[key] = new(); } - _pegController._tweenStages[_action._priority].Add(subtween); - _staminaRemaining = Math.Max(_staminaRemaining - _action._cost, 0); - } - - public virtual bool CanAct() - { - return _staminaRemaining > 0 - && _address.Y <= _action._range - && _action._usesRemaining > 0 - && _staminaRemaining <= _action._cost; - } - - public virtual bool CanMove() - { - return _staminaRemaining > 0 - && _address.Y > _pegController._playArea._map._firstOpenRow; + _pegController._tweenStages[key].Add(subtween); + _staminaRemaining -= action._cost; + return true; } public virtual void CounterAct(Commander COMMANDER) @@ -67,8 +72,12 @@ public partial class Peg : HoverableNode { paths.Add(map.GetPath(_address, goals[i])); } - - return paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0]; + List> pathsOverZeroCount = [.. paths.Where(p => p.Count > 0)]; + if (pathsOverZeroCount.Count == 0) + { + return []; + } + return pathsOverZeroCount.OrderBy(p => p.Count).ToList()[0]; } public virtual List GetGoals() @@ -76,36 +85,27 @@ public partial class Peg : HoverableNode Map map = _pegController._playArea._map; return [.. map._cells.Where(c => c.Y == map._firstOpenRow).Where(c => !map._astar.IsPointSolid(c))]; } - - public virtual void Move(Vector2I PATH_STEP) - { - _pegController._playArea._map.SetCellPeg(PATH_STEP, this); - _path.Add(PATH_STEP); - - Tween subtween = CreateTween(); - subtween.TweenProperty(this, "global_position", _pegController._playArea._map.GetCellPositionFromAddress(PATH_STEP), 0.25f); - int key = _priorities["movement"] + _pegController._actionLoop; - - if (!_pegController._tweenStages.ContainsKey(key)) - { - _pegController._tweenStages[key] = new(); - } - _pegController._tweenStages[key].Add(subtween); - _staminaRemaining -= _movementCost; + public Vector2 GetPositionFromAddress() + { + return _pegController._playArea._map.GetCellPositionFromAddress(_address); + } + + public void ReprioritizeActions() + { + _actions = [.. GetNode("Actions").GetChildren().Cast()]; } public virtual void StartTurn() { _staminaRemaining = _stamina; - _action._usesRemaining = _action._usesMax; - GD.Print("ACTION ",_action._usesRemaining,", ",_action._usesRemaining); + _actions.ForEach(a => a.Reset()); } - public virtual void ChangeHealth(int DELTA, Commander BALLER) + public virtual void ChangeHealth(int DELTA, Commander COMMANDER) { _health += DELTA; - CounterAct(BALLER); + CounterAct(COMMANDER); if (_health <= 0) { EmitSignal(SignalName.Death, this); diff --git a/PegController.cs b/PegController.cs index 042717d..5bbfd2d 100644 --- a/PegController.cs +++ b/PegController.cs @@ -10,7 +10,7 @@ using System.Xml; public partial class PegController : TurnController { - public int _actionLoop = 0, _pegsCreated = 0, _pegsDestroyed = 0; + public int _pegsCreated = 0, _pegsDestroyed = 0; public List _hostilePegScenes; public List _pegs = new(); public PlayerController _playerController; @@ -64,49 +64,30 @@ public partial class PegController : TurnController } } - - public List GetRemainingPegs() - { - return [.. _pegs.Where(e => e.CanMove() || e.CanAct())]; - } public void HandlePegTurn() { - _actionLoop = 0; _tweenStages.Clear(); - Map map = _playArea._map; - _pegs.ForEach(e => e._path.Clear()); - - List remainingPegs = GetRemainingPegs(); - while (remainingPegs.Count > 0) + int loop = 0; + bool noPegsActed = false; + while (!noPegsActed) { - for (int i = 0; i < remainingPegs.Count; i++) + noPegsActed = true; + for (int i = 0; i < _pegs.Count; i++) { - Peg peg = remainingPegs[i]; - HandlePegPathing(peg); - HandlePegAction(peg); - } - remainingPegs = GetRemainingPegs(); - _actionLoop++; - if (_actionLoop == 10) - { - Peg peg_ = remainingPegs[0]; - GD.Print("LOOPMAXEDOUT"); + Peg peg = _pegs[i]; + bool pegActed = peg.Act(loop); + if (pegActed) + { + noPegsActed = false; + } } + loop++; } + } - public void HandlePegAction(Peg PEG) - { - if (PEG.CanAct()) - { - PEG.Act(); - } - - } - - public void HandlePegClick(Node CLICKED_NODE, int CLICK_TYPE) { if (CLICKED_NODE is not Peg peg) @@ -133,23 +114,6 @@ public partial class PegController : TurnController } } - public void HandlePegPathing(Peg PEG) - { - if (!PEG.CanMove()) - { - return; - } - List path = PEG.GetBestPath(); - - if (path?.Count == 0) - { - GD.Print(PEG._address, " PATH0"); - return; - } - - PEG.Move(path[0]); - } - public void HandlePegRemoval(Peg PEG_TO_REMOVE) { _pegs.Remove(PEG_TO_REMOVE); @@ -160,7 +124,7 @@ public partial class PegController : TurnController public void HandlePegSort() { - _pegs = [.. _pegs.OrderBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))]; + _pegs = [.. _pegs.OrderBy(p => p._address.Y).ThenBy(p => p._distanceToGoal).ThenBy(p => Math.Abs(p._address.X - _playArea._map._maxX / 2))]; } @@ -174,7 +138,7 @@ public partial class PegController : TurnController public void ProcessTween() { _tweenStages = _tweenStages.OrderBy(s => s.Key).ToDictionary(); - GD.Print(string.Join(", ", _tweenStages.Keys)); + // GD.Print(string.Join(", ", _tweenStages.Keys)); if (_tweenStages.Count <= 0) { EndTurn(); diff --git a/Pegs/Actions/BasicMovement.cs b/Pegs/Actions/BasicMovement.cs new file mode 100644 index 0000000..cdd5f1f --- /dev/null +++ b/Pegs/Actions/BasicMovement.cs @@ -0,0 +1,41 @@ +using Godot; +using System; +using System.Collections.Generic; + +public partial class BasicMovement : PegAction +{ + public override void _Ready() + { + base._Ready(); + _healthChange = 0; + _cost = 1; + _range = 2^32; + _usesMax = 2^32; + _usesRemaining = _usesMax; + } + + public override Tween CreateAnimation(Peg PEG) + { + List path = PEG.GetBestPath(); + PegController pegController = PEG._pegController; + Map map = pegController._playArea._map; + if (path?.Count == 0) + { + return null; + } + Vector2I cell = path[0]; + map.SetCellPeg(cell, PEG); + PEG._path.Add(cell); + + Tween subtween = CreateTween(); + subtween.TweenProperty(this, "global_position", map.GetCellPositionFromAddress(cell), 0.25f); + + return subtween; + } + + public override bool MeetsCriteria(Peg PEG) + { + return base.MeetsCriteria(PEG) && PEG._address.Y > PEG._pegController._playArea._map._firstOpenRow; + } + +} diff --git a/Pegs/Actions/BasicMovement.cs.uid b/Pegs/Actions/BasicMovement.cs.uid new file mode 100644 index 0000000..51da71a --- /dev/null +++ b/Pegs/Actions/BasicMovement.cs.uid @@ -0,0 +1 @@ +uid://hp5iucbq5brg diff --git a/Pegs/Actions/Shortbow.cs b/Pegs/Actions/Shortbow.cs index c69c9af..9e18677 100644 --- a/Pegs/Actions/Shortbow.cs +++ b/Pegs/Actions/Shortbow.cs @@ -7,13 +7,13 @@ public partial class Shortbow : PegAction public override void _Ready() { base._Ready(); - _priority = 1000; _healthChange = -1; _cost = 2; _range = 1; _usesMax = 1; _usesRemaining = _usesMax; } + public override Tween CreateAnimation(Peg PEG) { Vector2 target = PEG._pegController._playerController._towers.OrderBy(t => (t.GlobalPosition - GlobalPosition).Length()).ToList()[0].GlobalPosition; diff --git a/Pegs/Actions/Shortsword.cs b/Pegs/Actions/Shortsword.cs index de678cc..77bdefc 100644 --- a/Pegs/Actions/Shortsword.cs +++ b/Pegs/Actions/Shortsword.cs @@ -7,19 +7,19 @@ public partial class Shortsword : PegAction public override void _Ready() { base._Ready(); - _priority = 1000; _healthChange = -2; _cost = 2; _range = 0; _usesMax = 1; _usesRemaining = _usesMax; } + public override Tween CreateAnimation(Peg PEG) { - Vector2 target = PEG.GlobalPosition + (Vector2.Up * 50); + Vector2 target = Vector2.Up * PEG._pegController._playArea._map._cellSize; Tween subtween = CreateTween(); subtween.TweenProperty(_image, "visible", true, 0.0f); - subtween.TweenProperty(_image, "global_position", target, 0.5f); + subtween.TweenProperty(_image, "position", target, 0.5f); subtween.TweenCallback(Callable.From(() => { PEG._pegController._playerController.ChangeHealth(_healthChange, this); diff --git a/Pegs/Actions/basic_movement.tscn b/Pegs/Actions/basic_movement.tscn new file mode 100644 index 0000000..ed44370 --- /dev/null +++ b/Pegs/Actions/basic_movement.tscn @@ -0,0 +1,9 @@ +[gd_scene format=3 uid="uid://bup5oli00p3lg"] + +[ext_resource type="Script" uid="uid://hp5iucbq5brg" path="res://Pegs/Actions/BasicMovement.cs" id="1_u1rld"] + +[node name="BasicMovement" type="Node2D" unique_id=460007250] +script = ExtResource("1_u1rld") + +[node name="Image" type="Sprite2D" parent="." unique_id=1133735272] +visible = false diff --git a/Pegs/Actions/shortbow.tscn b/Pegs/Actions/shortbow.tscn index 42ffa32..8b42a01 100644 --- a/Pegs/Actions/shortbow.tscn +++ b/Pegs/Actions/shortbow.tscn @@ -1,8 +1,12 @@ [gd_scene format=3 uid="uid://duspilwelsiy3"] [ext_resource type="Script" uid="uid://dt7qbvowj1sm4" path="res://Pegs/Actions/Shortbow.cs" id="1_yhiab"] +[ext_resource type="Texture2D" uid="uid://32m5teus1cjj" path="res://Art/tower.png" id="2_uaien"] [node name="Shortbow" type="Node2D" unique_id=518048625] script = ExtResource("1_yhiab") [node name="Image" type="Sprite2D" parent="." unique_id=944294157] +visible = false +scale = Vector2(0.1, 0.1) +texture = ExtResource("2_uaien") diff --git a/Pegs/HostilePegs/Archer.cs b/Pegs/HostilePegs/Archer.cs index 751b9bf..45edcb8 100644 --- a/Pegs/HostilePegs/Archer.cs +++ b/Pegs/HostilePegs/Archer.cs @@ -7,6 +7,5 @@ public partial class Archer : HostilePeg { base._Ready(); _stamina = 2; - _action = GetNode("Shortbow"); } } diff --git a/Pegs/HostilePegs/Infantry.cs b/Pegs/HostilePegs/Infantry.cs index c0d8877..b137958 100644 --- a/Pegs/HostilePegs/Infantry.cs +++ b/Pegs/HostilePegs/Infantry.cs @@ -7,6 +7,5 @@ public partial class Infantry : HostilePeg { base._Ready(); _stamina = 3; - _action = GetNode("Shortsword"); } } diff --git a/Pegs/HostilePegs/archer.tscn b/Pegs/HostilePegs/archer.tscn index 79d1421..0c363b2 100644 --- a/Pegs/HostilePegs/archer.tscn +++ b/Pegs/HostilePegs/archer.tscn @@ -3,6 +3,7 @@ [ext_resource type="Script" uid="uid://b3a0x3r3yx861" path="res://Pegs/HostilePegs/Archer.cs" id="1_ij48w"] [ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="2_j7but"] [ext_resource type="PackedScene" uid="uid://duspilwelsiy3" path="res://Pegs/Actions/shortbow.tscn" id="3_c81uf"] +[ext_resource type="PackedScene" uid="uid://bup5oli00p3lg" path="res://Pegs/Actions/basic_movement.tscn" id="3_j7but"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] bounce = 0.5 @@ -27,7 +28,11 @@ texture_filter = 1 scale = Vector2(0.5, 0.5) texture = ExtResource("2_j7but") -[node name="Shortbow" parent="." unique_id=518048625 instance=ExtResource("3_c81uf")] +[node name="Actions" type="Node" parent="." unique_id=614093526] + +[node name="BasicMovement" parent="Actions" unique_id=460007250 instance=ExtResource("3_j7but")] + +[node name="Shortbow" parent="Actions" unique_id=518048625 instance=ExtResource("3_c81uf")] [node name="HoverBounds" type="Area2D" parent="." unique_id=937525982] diff --git a/Pegs/HostilePegs/infantry.tscn b/Pegs/HostilePegs/infantry.tscn index f5213db..ae877d8 100644 --- a/Pegs/HostilePegs/infantry.tscn +++ b/Pegs/HostilePegs/infantry.tscn @@ -3,6 +3,7 @@ [ext_resource type="Script" uid="uid://xlg4cblo1vf1" path="res://Pegs/HostilePegs/Infantry.cs" id="1_wlksp"] [ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="2_b77ka"] [ext_resource type="PackedScene" uid="uid://c6df6ib0qan5g" path="res://Pegs/Actions/shortsword.tscn" id="3_lwlv5"] +[ext_resource type="PackedScene" uid="uid://bup5oli00p3lg" path="res://Pegs/Actions/basic_movement.tscn" id="4_b77ka"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] bounce = 0.5 @@ -27,7 +28,11 @@ texture_filter = 1 scale = Vector2(0.5, 0.5) texture = ExtResource("2_b77ka") -[node name="Shortsword" parent="." unique_id=518048625 instance=ExtResource("3_lwlv5")] +[node name="Actions" type="Node" parent="." unique_id=2081138574] + +[node name="Shortsword" parent="Actions" unique_id=518048625 instance=ExtResource("3_lwlv5")] + +[node name="BasicMovement" parent="Actions" unique_id=460007250 instance=ExtResource("4_b77ka")] [node name="HoverBounds" type="Area2D" parent="." unique_id=937525982] diff --git a/Pegs/PegAction.cs b/Pegs/PegAction.cs index a085dc4..e92ca52 100644 --- a/Pegs/PegAction.cs +++ b/Pegs/PegAction.cs @@ -16,4 +16,16 @@ public partial class PegAction : Node2D { return null; } + + public virtual bool MeetsCriteria(Peg PEG) + { + return PEG._staminaRemaining >= _cost + && _usesRemaining > 0 + && PEG._address.Y <= _range; + } + + public virtual void Reset() + { + _usesRemaining = _usesMax; + } } diff --git a/Pegs/hostile_peg.tscn b/Pegs/hostile_peg.tscn index 575b78e..a97f4fb 100644 --- a/Pegs/hostile_peg.tscn +++ b/Pegs/hostile_peg.tscn @@ -2,7 +2,6 @@ [ext_resource type="Script" uid="uid://dfba4vq6jv0a6" path="res://Pegs/HostilePeg.cs" id="1_nc8fp"] [ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="2_0icqg"] -[ext_resource type="PackedScene" uid="uid://bj2qrn0l01us1" path="res://Pegs/peg_action.tscn" id="3_nc8fp"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] bounce = 0.5 @@ -26,7 +25,7 @@ texture_filter = 1 scale = Vector2(0.5, 0.5) texture = ExtResource("2_0icqg") -[node name="PegAction" parent="." unique_id=637569115 instance=ExtResource("3_nc8fp")] +[node name="Actions" type="Node" parent="." unique_id=218609326] [node name="HoverBounds" type="Area2D" parent="." unique_id=937525982] diff --git a/Pegs/peg_action.tscn b/Pegs/peg_action.tscn index 738a4b4..10d0ce2 100644 --- a/Pegs/peg_action.tscn +++ b/Pegs/peg_action.tscn @@ -6,3 +6,4 @@ script = ExtResource("1_8o8tf") [node name="Image" type="Sprite2D" parent="." unique_id=1133735272] +visible = false