diff --git a/Attack.cs b/Attack.cs index f76e401..796be06 100644 --- a/Attack.cs +++ b/Attack.cs @@ -52,9 +52,9 @@ public partial class Attack : RigidBody2D public void TakeAction(Node BODY) { EmitSignal(SignalName.Hit, BODY); - if (BODY is Enemy enemy) + if (BODY is Peg peg) { - enemy.TakeDamage(_damage, _commanderOwner); + peg.TakeDamage(_damage, _commanderOwner); } } diff --git a/EnemyController.cs b/EnemyController.cs deleted file mode 100644 index e60ee3c..0000000 --- a/EnemyController.cs +++ /dev/null @@ -1,213 +0,0 @@ -using Godot; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; - -public partial class EnemyController : TurnController -{ - public int _actionLoop = 0; - public PackedScene _enemyScene = GD.Load("res://Enemy.tscn"); - public List _enemies = new(); - public PlayerController _playerController; - public Dictionary> _tweenStages = new(); - public Tween _tween; - - public void AddEnemies(int ENEMY_COUNT = 1) - { - for (int i = 0; i < ENEMY_COUNT; i++) - { - Enemy newEnemy = _enemyScene.Instantiate(); - newEnemy.Death += HandleEnemyRemoval; - newEnemy.Clicked += HandleEnemyClick; - newEnemy.RightClicked += HandleEnemyRightClick; - - newEnemy._stamina = Globals._rng.Next(2,4+1); - newEnemy.Modulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF"); - newEnemy._enemyController = this; - List unoccupied = [.. _playArea._map._bottomRow.Where(c => _enemies.All(e => e._address != c))]; - Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; - SetEnemy(newEnemy, randomCell); - _enemies.Add(newEnemy); - AddChild(newEnemy); - } - } - public void AddEnemies(List POSITIONS) - { - for (int i = 0; i < POSITIONS.Count; i++) - { - Enemy newEnemy = _enemyScene.Instantiate(); - newEnemy.Death += HandleEnemyRemoval; - newEnemy.Clicked += HandleEnemyClick; - newEnemy.RightClicked += HandleEnemyRightClick; - - newEnemy._stamina = Globals._rng.Next(2,4+1); - newEnemy._hitRange = Globals._rng.Next(1,2+1); - newEnemy.GetNode("Sprite2D").SelfModulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF"); - newEnemy._enemyController = this; - - SetEnemy(newEnemy, POSITIONS[i]); - _enemies.Add(newEnemy); - AddChild(newEnemy); - } - - } - - public List GetRemainingEnemies() - { - return [.. _enemies.Where(e => e.CanMove(this) || e.CanAttack(this))]; - } - - public void HandleEnemyActions() - { - _actionLoop = 0; - _tweenStages.Clear(); - Map map = _playArea._map; - - _enemies.ForEach(e => e._path.Clear()); - - List remainingEnemies = GetRemainingEnemies(); - while (remainingEnemies.Count > 0) - { - for (int i = 0; i < remainingEnemies.Count; i++) - { - Enemy enemy = remainingEnemies[i]; - if (!enemy.CanMove(this) && !enemy.CanAttack(this)) - { - GD.Print(i); - } - HandleEnemyPathing(enemy); - HandleEnemyAttacking(enemy); - } - remainingEnemies = GetRemainingEnemies(); - _actionLoop++; - } - } - - public void HandleEnemyAttacking(Enemy ENEMY) - { - if (!ENEMY.CanAttack(this)) - { - return; - } - ENEMY.Attack(this); - } - - - public void HandleEnemyClick(Enemy ENEMY) - { - if (ENEMY._staminaRemaining <= 0){ - return; - } - TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); - List newPath = ENEMY.GetBestPath(_playArea._map); - - - 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); - } - } - - public void HandleEnemyRightClick(Enemy ENEMY) - { - HandleEnemyRemoval(ENEMY); - } - - public void HandleEnemyPathing(Enemy ENEMY) - { - if (!ENEMY.CanMove(this)) - { - return; - } - List path = ENEMY.GetBestPath(_playArea._map); - - if (path.Count == 0) - { - // ENEMY.Move(ENEMY._path.LastOrDefault(ENEMY._address), this); - return; - } - - ENEMY.Move(path[0], this); - } - - public void HandleEnemyRemoval(Enemy ENEMY_TO_REMOVE) - { - _enemies.Remove(ENEMY_TO_REMOVE); - _playArea._map.SetCellEnemy(ENEMY_TO_REMOVE._address, null); - ENEMY_TO_REMOVE.QueueFree(); - } - - public void HandleEnemySort() - { - _enemies = [.. _enemies.OrderBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))]; - } - - - public void Initiate() - { - List positions = [.. _playArea.GetNode("InitialPositions").GetUsedCells()]; - - AddEnemies(positions); - } - - public void ProcessTween() - { - _tweenStages = _tweenStages.OrderBy(s => s.Key).ToDictionary(); - GD.Print(string.Join(", ", _tweenStages.Keys)); - if (_tweenStages.Count <= 0) - { - return; - } - - if (_tween != null) - { - _tween.Kill(); - } - - _tween = CreateTween(); - - for (int i = 0; i < _tweenStages.Count; i++) - { - _tween.Chain().TweenInterval(0.5f); - List tweenSteps = _tweenStages.Values.ElementAt(i); - for (int j = 0; j < tweenSteps.Count; j++) - { - Tween step = tweenSteps[j]; - _tween.Parallel().TweenSubtween(step); - } - } - _tween.TweenCallback(Callable.From(EndTurn)); - - } - - public override void StartTurn() - { - - AddEnemies(1); - - for (int i = 0; i < _enemies.Count; i++) - { - _enemies[i].StartTurn(); - } - - HandleEnemySort(); - - HandleEnemyActions(); - - ProcessTween(); - } - - public void SetEnemy(Enemy ENEMY, Vector2I CELL) - { - if (CELL == new Vector2I(16, 14)) - { - ENEMY._track = true; - } - _playArea._map.SetCellEnemy(CELL, ENEMY); - ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); - } - -} diff --git a/Globals.cs b/Globals.cs index f650a9f..684501b 100644 --- a/Globals.cs +++ b/Globals.cs @@ -1,9 +1,10 @@ using Godot; using System; -public partial class Globals : Node +public partial class Globals : Node2D { public static Random _rng = new(); public static float _gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity"), _drag = (float)ProjectSettings.GetSetting("physics/2d/default_linear_damp"); + public static MouseHandler _mouse; } diff --git a/HostilePeg.cs b/HostilePeg.cs new file mode 100644 index 0000000..a671c12 --- /dev/null +++ b/HostilePeg.cs @@ -0,0 +1,21 @@ +using Godot; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +public partial class HostilePeg : Peg +{ + + public override void _Ready() + { + base._Ready(); + } + + // public void OnMouseEntered(){ + // _hovered = true; + // } + // public void OnMouseExited(){ + // _hovered = false; + // } +} diff --git a/Enemy.cs.uid b/HostilePeg.cs.uid similarity index 100% rename from Enemy.cs.uid rename to HostilePeg.cs.uid diff --git a/HoverableNode.cs b/HoverableNode.cs new file mode 100644 index 0000000..9b201cb --- /dev/null +++ b/HoverableNode.cs @@ -0,0 +1,48 @@ +using Godot; +using System; + +public partial class HoverableNode : Node2D +{ + [Signal] + public delegate void HoverEventHandler(HoverableNode THIS, bool IS_HOVERED); + [Signal] + public delegate void ClickEventHandler(HoverableNode THIS, int CLICK_TYPE); + + public bool _hovered; + public Area2D _bounds; + + public override void _Ready() + { + base._Ready(); + _bounds = GetNode("HoverBounds"); + _bounds.MouseEntered += OnMouseEntered; + _bounds.MouseExited += OnMouseExited; + } + + public override void _Input(InputEvent @event) + { + if (_hovered) + { + if (Input.IsActionJustReleased("lmb")) + { + EmitSignal(SignalName.Click, this, 0); + } + if (Input.IsActionJustReleased("rmb")) + { + EmitSignal(SignalName.Click, this, 1); + } + } + } + + public virtual void OnMouseEntered() + { + _hovered = true; + EmitSignal(SignalName.Hover, this,_hovered); + } + + public virtual void OnMouseExited() + { + _hovered = false; + EmitSignal(SignalName.Hover, this, _hovered); + } +} diff --git a/HoverableNode.cs.uid b/HoverableNode.cs.uid new file mode 100644 index 0000000..70415e8 --- /dev/null +++ b/HoverableNode.cs.uid @@ -0,0 +1 @@ +uid://dqvysa2n208d3 diff --git a/Main.cs b/Main.cs index f35f15a..67e2674 100644 --- a/Main.cs +++ b/Main.cs @@ -7,27 +7,28 @@ public partial class Main : Node { public PlayArea _playArea; public PlayerController _playerController; - public EnemyController _enemyController; + public PegController _pegController; public TurnController _turnController; public Random _rng = new(); public override void _Ready() { base._Ready(); + Globals._mouse = GetNode("MouseHandler"); _playArea = GetNode("PlayArea"); _playerController = GetNode("PlayerController"); - _enemyController = GetNode("EnemyController"); + _pegController = GetNode("PegController"); - _playerController._enemyController = _enemyController; - _enemyController._playerController = _playerController; + _playerController._pegController = _pegController; + _pegController._playerController = _playerController; _playerController._playArea = _playArea; - _enemyController._playArea = _playArea; + _pegController._playArea = _playArea; _playerController.TurnDone += ChangeTurn; _playerController.Death += EndGame; - _enemyController.TurnDone += ChangeTurn; + _pegController.TurnDone += ChangeTurn; - _enemyController.Initiate(); + _pegController.Initiate(); _playerController.SetUpTowers(); ChangeTurn(); } @@ -50,7 +51,7 @@ public partial class Main : Node public void ChangeTurn() { - _turnController = _turnController == _playerController ? _enemyController : _playerController; + _turnController = _turnController == _playerController ? _pegController : _playerController; _turnController.StartTurn(); } diff --git a/Map.cs b/Map.cs index 0a51a3a..487ad9f 100644 --- a/Map.cs +++ b/Map.cs @@ -12,7 +12,7 @@ public partial class Map : TileMapLayer public Vector2I _pathTakenAtlasCoordinates = new Vector2I(4, 0); public List _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); public AStarGrid2D _astar = new(); - public Dictionary _addressOccupants = new(); + public Dictionary _addressOccupants = new(); public int _firstOpenRow { get @@ -64,7 +64,7 @@ public partial class Map : TileMapLayer return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2; } - public Enemy GetOccupant(Vector2I CELL_TO_CHECK) + public Peg GetOccupant(Vector2I CELL_TO_CHECK) { return _addressOccupants[CELL_TO_CHECK]; } @@ -90,20 +90,20 @@ public partial class Map : TileMapLayer return rowCells.All(c => _astar.IsPointSolid(c)); } - public void SetCellEnemy(Vector2I ADDRESS, Enemy ENEMY) + public void SetCellPeg(Vector2I ADDRESS, Peg PEG) { - if (ENEMY != null) + if (PEG != null) { - if (ENEMY._address != -Vector2I.One) + if (PEG._address != -Vector2I.One) { - _addressOccupants[ENEMY._address] = null; - SetCellSolid(ENEMY._address); + _addressOccupants[PEG._address] = null; + SetCellSolid(PEG._address); } - ENEMY._address = ADDRESS; + PEG._address = ADDRESS; } - _addressOccupants[ADDRESS] = ENEMY; + _addressOccupants[ADDRESS] = PEG; SetCellSolid(ADDRESS); } diff --git a/MouseHandler.cs b/MouseHandler.cs new file mode 100644 index 0000000..fee446a --- /dev/null +++ b/MouseHandler.cs @@ -0,0 +1,18 @@ +using Godot; +using System; + +public partial class MouseHandler : Node +{ + public enum ClickType + { + LEFT_CLICK, + MIDDLE_CLICK, + RIGHT_CLICK, + LEFT_HOLD, + MIDDLE_HOLD, + RIGHT_HOLD, + LEFT_DOUBLE, + MIDDLE_DOUBLE, + RIGHT_DOUBLE + } +} diff --git a/MouseHandler.cs.uid b/MouseHandler.cs.uid new file mode 100644 index 0000000..44ae028 --- /dev/null +++ b/MouseHandler.cs.uid @@ -0,0 +1 @@ +uid://dewxuppyw2gtp diff --git a/Enemy.cs b/Peg.cs similarity index 50% rename from Enemy.cs rename to Peg.cs index 71ef6bd..50f2277 100644 --- a/Enemy.cs +++ b/Peg.cs @@ -2,17 +2,16 @@ using Godot; using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -public partial class Enemy : StaticBody2D +public partial class Peg : HoverableNode { [Signal] - public delegate void DeathEventHandler(Enemy THIS); + public delegate void DeathEventHandler(Peg THIS); [Signal] - public delegate void ClickedEventHandler(Enemy THIS); + public delegate void ClickedEventHandler(Peg THIS); [Signal] - public delegate void RightClickedEventHandler(Enemy THIS); - public bool _hovered = false, _track = false, _warp = false; + public delegate void RightClickedEventHandler(Peg THIS); + public bool _track = false, _warp = false; public int _damage = 1, _health = 2, _stamina, _staminaRemaining, _visibilityRange = 4, _hitRange, _attackCost = 1; public Dictionary _priorities = new() { @@ -20,9 +19,9 @@ public partial class Enemy : StaticBody2D {"movement", 0} // 0 to 999999 reserved for movement priorities }; public Vector2I _address = -Vector2I.One, _range = Vector2I.Up; - public List _path = new(), _pathStored = new(); + public List _path = new(); public float _movement = 0; - public EnemyController _enemyController; + public PegController _pegController; public Sprite2D _attack; public override void _Ready() @@ -30,7 +29,6 @@ public partial class Enemy : StaticBody2D base._Ready(); _attack = GetNode("Attack"); } - public override void _Process(double delta) { base._Process(delta); @@ -45,43 +43,39 @@ public partial class Enemy : StaticBody2D EmitSignal(SignalName.RightClicked, this); } } - } - public override void _PhysicsProcess(double delta) - { - base._PhysicsProcess(delta); } - - public void Attack(EnemyController CONTROLLER) + public void Attack() { - _attack.Visible = true; + // _attack.Visible = true; Tween subtween = CreateTween(); + subtween.TweenProperty(_attack, "visible", true, 0.0f); subtween.TweenProperty(_attack, "global_position", Vector2.Zero, 0.5f); subtween.TweenCallback(Callable.From(() => { - CONTROLLER._playerController.ChangeHealth(-1, this); + _pegController._playerController.ChangeHealth(-1, this); _attack.Position = Vector2.Zero; _attack.Visible = false; })); int key = _priorities["attack"]; - if (!CONTROLLER._tweenStages.ContainsKey(key)) + if (!_pegController._tweenStages.ContainsKey(key)) { - CONTROLLER._tweenStages[key] = new(); + _pegController._tweenStages[key] = new(); } - CONTROLLER._tweenStages[key].Add(subtween); + _pegController._tweenStages[key].Add(subtween); _staminaRemaining = Math.Max(_staminaRemaining - 2, 0); } - public bool CanAttack(EnemyController CONTROLLER) + public bool CanAttack() { return _staminaRemaining > 0 && _address.Y <= _hitRange; } - public bool CanMove(EnemyController CONTROLLER) + public bool CanMove() { // GD.Print(_staminaRemaining," ",_address.Y," ", CONTROLLER._playArea._map._firstOpenRow); - return _staminaRemaining > 0 && _address.Y > CONTROLLER._playArea._map._firstOpenRow; + return _staminaRemaining > 0 && _address.Y > _pegController._playArea._map._firstOpenRow; } public void CounterAttack(Commander COMMANDER) @@ -89,39 +83,49 @@ public partial class Enemy : StaticBody2D } - public virtual List GetBestPath(Map MAP) + public virtual List GetBestPath() { - List goals = GetGoals(MAP); - List> paths = new(); - - for (int i = 0; i < goals.Count; i++) + Map map = _pegController._playArea._map; + List goals = GetGoals(); + // List> paths = new(); + Vector2I bestGoal = goals[0]; + float bestCost = map._astar._EstimateCost(_address, bestGoal); + for (int i = 1; i < goals.Count; i++) { - paths.Add(MAP.GetPath(_address, goals[i])); + Vector2I testGoal = goals[i]; + float testCost = map._astar._EstimateCost(_address, testGoal); + if (testCost < bestCost) + { + bestCost = testCost; + bestGoal = testGoal; + } } - List bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0]; + // List bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0]; + List bestPath = map.GetPath(_address, bestGoal); return bestPath; } - public virtual List GetGoals(Map MAP) + public virtual List GetGoals() { - return [.. MAP._cells.Where(c => c.Y == Math.Max(_address.Y - _visibilityRange, MAP._firstOpenRow)).Where(c => !MAP._astar.IsPointSolid(c))]; + Map map = _pegController._playArea._map; + return [.. map._cells.Where(c => c.Y == Math.Max(_address.Y - _visibilityRange, map._firstOpenRow)).Where(c => !map._astar.IsPointSolid(c))]; } - public void Move(Vector2I PATH_STEP, EnemyController CONTROLLER) + public void Move(Vector2I PATH_STEP) { - CONTROLLER._playArea._map.SetCellEnemy(PATH_STEP, this); + _pegController._playArea._map.SetCellPeg(PATH_STEP, this); _path.Add(PATH_STEP); Tween subtween = CreateTween(); - subtween.TweenProperty(this, "global_position", CONTROLLER._playArea._map.GetCellPositionFromAddress(PATH_STEP), 0.25f); - int key = _priorities["movement"] + CONTROLLER._actionLoop; + subtween.TweenProperty(this, "global_position", _pegController._playArea._map.GetCellPositionFromAddress(PATH_STEP), 0.25f); + int key = _priorities["movement"] + _pegController._actionLoop; GD.Print(key); - if (!CONTROLLER._tweenStages.ContainsKey(key)) + if (!_pegController._tweenStages.ContainsKey(key)) { - CONTROLLER._tweenStages[key] = new(); + _pegController._tweenStages[key] = new(); } - CONTROLLER._tweenStages[key].Add(subtween); + _pegController._tweenStages[key].Add(subtween); _staminaRemaining--; } @@ -140,10 +144,4 @@ public partial class Enemy : StaticBody2D } } - public void OnMouseEntered(){ - _hovered = true; - } - public void OnMouseExited(){ - _hovered = false; - } } diff --git a/Peg.cs.uid b/Peg.cs.uid new file mode 100644 index 0000000..85a2d65 --- /dev/null +++ b/Peg.cs.uid @@ -0,0 +1 @@ +uid://g2ucwg3k342p diff --git a/PegController.cs b/PegController.cs new file mode 100644 index 0000000..1360a8f --- /dev/null +++ b/PegController.cs @@ -0,0 +1,214 @@ +using Godot; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; + +public partial class PegController : TurnController +{ + public int _actionLoop = 0; + public PackedScene _hostilePegScene = GD.Load("res://hostile_peg.tscn"); + public List _pegs = new(); + public PlayerController _playerController; + public Dictionary> _tweenStages = new(); + public Tween _tween; + + public void AddHostilePegs(int PEG_COUNT = 1) + { + for (int i = 0; i < PEG_COUNT; i++) + { + HostilePeg newHostilePeg = _hostilePegScene.Instantiate(); + newHostilePeg.Death += HandlePegRemoval; + newHostilePeg.Clicked += HandlePegClick; + newHostilePeg.RightClicked += HandlePegRightClick; + + newHostilePeg._stamina = Globals._rng.Next(2,4+1); + newHostilePeg.Modulate = new Color(newHostilePeg._stamina == 2 ? "#FF0000" : newHostilePeg._stamina == 3 ? "#00FF00" : "#0000FF"); + newHostilePeg._pegController = this; + List unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))]; + Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; + SetPeg(newHostilePeg, randomCell); + _pegs.Add(newHostilePeg); + AddChild(newHostilePeg); + } + } + public void AddHostilePegs(List POSITIONS) + { + for (int i = 0; i < POSITIONS.Count; i++) + { + HostilePeg newHostilePeg = _hostilePegScene.Instantiate(); + newHostilePeg.Death += HandlePegRemoval; + newHostilePeg.Clicked += HandlePegClick; + newHostilePeg.RightClicked += HandlePegRightClick; + + newHostilePeg._stamina = Globals._rng.Next(2,4+1); + newHostilePeg._hitRange = Globals._rng.Next(1,2+1); + newHostilePeg.GetNode("Sprite2D").SelfModulate = new Color(newHostilePeg._stamina == 2 ? "#FF0000" : newHostilePeg._stamina == 3 ? "#00FF00" : "#0000FF"); + newHostilePeg._pegController = this; + + SetPeg(newHostilePeg, POSITIONS[i]); + _pegs.Add(newHostilePeg); + AddChild(newHostilePeg); + } + + } + + public List GetRemainingPegs() + { + return [.. _pegs.Where(e => e.CanMove() || e.CanAttack())]; + } + + public void HandlePegActions() + { + _actionLoop = 0; + _tweenStages.Clear(); + Map map = _playArea._map; + + _pegs.ForEach(e => e._path.Clear()); + + List remainingPegs = GetRemainingPegs(); + while (remainingPegs.Count > 0 && _actionLoop <100) + { + for (int i = 0; i < remainingPegs.Count; i++) + { + Peg peg = remainingPegs[i]; + if (!peg.CanMove() && !peg.CanAttack()) + { + GD.Print(i); + } + HandlePegPathing(peg); + HandlePegAttacking(peg); + } + remainingPegs = GetRemainingPegs(); + _actionLoop++; + } + } + + public void HandlePegAttacking(Peg PEG) + { + if (!PEG.CanAttack()) + { + return; + } + PEG.Attack(); + } + + + public void HandlePegClick(Peg PEG) + { + if (PEG._staminaRemaining <= 0){ + return; + } + TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); + 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.SetCell(newPath[i],0,Vector2I.One); + } + } + + public void HandlePegRightClick(Peg PEG) + { + HandlePegRemoval(PEG); + } + + public void HandlePegPathing(Peg PEG) + { + if (!PEG.CanMove()) + { + return; + } + List path = PEG.GetBestPath(); + + if (path?.Count == 0) + { + // PEG.Move(PEG._path.LastOrDefault(PEG._address), this); + return; + } + + PEG.Move(path[0]); + } + + public void HandlePegRemoval(Peg PEG_TO_REMOVE) + { + _pegs.Remove(PEG_TO_REMOVE); + _playArea._map.SetCellPeg(PEG_TO_REMOVE._address, null); + PEG_TO_REMOVE.QueueFree(); + } + + 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))]; + } + + + public void Initiate() + { + List positions = [.. _playArea.GetNode("InitialPositions").GetUsedCells()]; + + AddHostilePegs(positions); + } + + public void ProcessTween() + { + _tweenStages = _tweenStages.OrderBy(s => s.Key).ToDictionary(); + GD.Print(string.Join(", ", _tweenStages.Keys)); + if (_tweenStages.Count <= 0) + { + EndTurn(); + return; + } + + if (_tween != null) + { + _tween.Kill(); + } + + _tween = CreateTween(); + + for (int i = 0; i < _tweenStages.Count; i++) + { + _tween.Chain().TweenInterval(0.5f); + List tweenSteps = _tweenStages.Values.ElementAt(i); + for (int j = 0; j < tweenSteps.Count; j++) + { + Tween step = tweenSteps[j]; + _tween.Parallel().TweenSubtween(step); + } + } + _tween.TweenCallback(Callable.From(EndTurn)); + + } + + public override void StartTurn() + { + + AddHostilePegs(1); + + for (int i = 0; i < _pegs.Count; i++) + { + _pegs[i].StartTurn(); + } + + HandlePegSort(); + + HandlePegActions(); + + ProcessTween(); + } + + public void SetPeg(Peg PEG, Vector2I CELL) + { + if (CELL == new Vector2I(16, 14)) + { + PEG._track = true; + } + _playArea._map.SetCellPeg(CELL, PEG); + PEG.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); + } + +} diff --git a/EnemyController.cs.uid b/PegController.cs.uid similarity index 100% rename from EnemyController.cs.uid rename to PegController.cs.uid diff --git a/PlayerController.cs b/PlayerController.cs index caa6ff3..cbcec34 100644 --- a/PlayerController.cs +++ b/PlayerController.cs @@ -10,7 +10,7 @@ public partial class PlayerController : TurnController public delegate void DeathEventHandler(PlayerController THIS); public bool _dead = false; public int _health, _healthMax = 100; - public EnemyController _enemyController; + public PegController _pegController; public PackedScene _commanderScene = GD.Load("res://Commander.tscn"); public List _towers = new(); diff --git a/attack.tscn b/attack.tscn index 7b5c956..377106d 100644 --- a/attack.tscn +++ b/attack.tscn @@ -25,6 +25,8 @@ shape = SubResource("CircleShape2D_7yfhp") scale = Vector2(0.5, 0.5) texture = ExtResource("2_hqc8w") +[node name="Ray" type="RayCast2D" parent="." unique_id=113171676] + [node name="PredictedPath" type="Path2D" parent="." unique_id=1505290715] modulate = Color(1, 0, 1, 1) top_level = true diff --git a/enemy.tscn b/hostile_peg.tscn similarity index 92% rename from enemy.tscn rename to hostile_peg.tscn index 4848ff0..1bf68b1 100644 --- a/enemy.tscn +++ b/hostile_peg.tscn @@ -1,6 +1,6 @@ [gd_scene format=3 uid="uid://drt7w0eqp13tu"] -[ext_resource type="Script" uid="uid://dfba4vq6jv0a6" path="res://Enemy.cs" id="1_4gyqm"] +[ext_resource type="Script" uid="uid://dfba4vq6jv0a6" path="res://HostilePeg.cs" id="1_4gyqm"] [ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="1_7k104"] [ext_resource type="Texture2D" uid="uid://m4wfj36twmqy" path="res://Art/attack.png" id="3_qi2p4"] @@ -10,7 +10,7 @@ bounce = 0.5 [sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"] radius = 12.5 -[node name="Enemy" type="StaticBody2D" unique_id=1417697759] +[node name="HostilePeg" type="StaticBody2D" unique_id=1417697759] input_pickable = true physics_material_override = SubResource("PhysicsMaterial_7k104") script = ExtResource("1_4gyqm") diff --git a/hoverable_node.tscn b/hoverable_node.tscn new file mode 100644 index 0000000..97bcdc4 --- /dev/null +++ b/hoverable_node.tscn @@ -0,0 +1,10 @@ +[gd_scene format=3 uid="uid://dse0sugndsrs"] + +[ext_resource type="Script" uid="uid://dqvysa2n208d3" path="res://HoverableNode.cs" id="1_14v3m"] + +[node name="HoverableNode" type="Node2D" unique_id=946672764] +script = ExtResource("1_14v3m") + +[node name="Bounds" type="Area2D" parent="." unique_id=783465962] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Bounds" unique_id=585114419] diff --git a/main.tscn b/main.tscn index 6873df7..fc5769f 100644 --- a/main.tscn +++ b/main.tscn @@ -2,8 +2,9 @@ [ext_resource type="Script" uid="uid://cg1m762ed04kv" path="res://Main.cs" id="1_ig7tw"] [ext_resource type="PackedScene" uid="uid://dumcridek4xy3" path="res://play_area.tscn" id="2_1bvp3"] -[ext_resource type="PackedScene" uid="uid://c6b188d2a20eq" path="res://enemy_controller.tscn" id="4_1bvp3"] +[ext_resource type="PackedScene" uid="uid://c6b188d2a20eq" path="res://peg_controller.tscn" id="4_1bvp3"] [ext_resource type="PackedScene" uid="uid://b7kvx7p0b2086" path="res://player_controller.tscn" id="4_lquwl"] +[ext_resource type="PackedScene" uid="uid://by0a5f2ft0u03" path="res://mouse_handler.tscn" id="5_lquwl"] [node name="Main" type="Node" unique_id=535208469] script = ExtResource("1_ig7tw") @@ -14,3 +15,5 @@ position = Vector2(360, 180) [node name="EnemyController" parent="." unique_id=1894449838 instance=ExtResource("4_1bvp3")] [node name="PlayerController" parent="." unique_id=364781168 instance=ExtResource("4_lquwl")] + +[node name="MouseHandler" parent="." unique_id=1823071759 instance=ExtResource("5_lquwl")] diff --git a/mouse_handler.tscn b/mouse_handler.tscn new file mode 100644 index 0000000..d208ce4 --- /dev/null +++ b/mouse_handler.tscn @@ -0,0 +1,6 @@ +[gd_scene format=3 uid="uid://by0a5f2ft0u03"] + +[ext_resource type="Script" uid="uid://dewxuppyw2gtp" path="res://MouseHandler.cs" id="1_r4hv2"] + +[node name="MouseHandler" type="Node2D" unique_id=215972463] +script = ExtResource("1_r4hv2") diff --git a/peg.tscn b/peg.tscn new file mode 100644 index 0000000..5d1b747 --- /dev/null +++ b/peg.tscn @@ -0,0 +1,32 @@ +[gd_scene format=3 uid="uid://b06qhbc336iwh"] + +[ext_resource type="Script" uid="uid://g2ucwg3k342p" path="res://Peg.cs" id="1_cavuh"] +[ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://Art/circle25r.png" id="2_cut4e"] +[ext_resource type="Texture2D" uid="uid://m4wfj36twmqy" path="res://Art/attack.png" id="3_inmy4"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] +bounce = 0.5 + +[sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"] +radius = 12.5 + +[node name="Peg" type="StaticBody2D" unique_id=1417697759] +input_pickable = true +physics_material_override = SubResource("PhysicsMaterial_7k104") +script = ExtResource("1_cavuh") + +[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_cut4e") + +[node name="Attack" type="Sprite2D" parent="." unique_id=1776738311] +visible = false +texture_filter = 1 +texture = ExtResource("3_inmy4") + +[connection signal="mouse_entered" from="." to="." method="OnMouseEntered"] +[connection signal="mouse_exited" from="." to="." method="OnMouseExited"] diff --git a/enemy_controller.tscn b/peg_controller.tscn similarity index 59% rename from enemy_controller.tscn rename to peg_controller.tscn index 61af164..961dea0 100644 --- a/enemy_controller.tscn +++ b/peg_controller.tscn @@ -1,6 +1,6 @@ [gd_scene format=3 uid="uid://c6b188d2a20eq"] -[ext_resource type="Script" uid="uid://brsi76xcgx3et" path="res://EnemyController.cs" id="1_tkpyo"] +[ext_resource type="Script" uid="uid://brsi76xcgx3et" path="res://PegController.cs" id="1_tkpyo"] -[node name="EnemyController" type="Node2D" unique_id=197453707] +[node name="PegController" type="Node2D" unique_id=197453707] script = ExtResource("1_tkpyo")