From 1903e1a1aab8df2dc63d9f64be1e4dbd20d3b0ba Mon Sep 17 00:00:00 2001 From: cojoedmo Date: Fri, 12 Jun 2026 02:03:17 -0400 Subject: [PATCH] starting to work on enemy attacks and player health --- Commander.cs | 13 +------------ Enemy.cs | 6 +++--- EnemyController.cs | 19 +++++++++++-------- Main.cs | 8 +++++++- PlayerController.cs | 15 +++++++++++++++ 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Commander.cs b/Commander.cs index 0248845..11b641f 100644 --- a/Commander.cs +++ b/Commander.cs @@ -3,11 +3,9 @@ using System; public partial class Commander : Sprite2D { - [Signal] - public delegate void DeathEventHandler(Commander THIS); [Signal] public delegate void ActionsUpEventHandler(); - public int _health = 10, _actionsMax = 1, _actions; + public int _actionsMax = 1, _actions; public PackedScene _attackScene = GD.Load("res://Attack.tscn"); public Attack _attack; public PlayerController _playerController; @@ -47,15 +45,6 @@ public partial class Commander : Sprite2D } } - public void TakeDamage(int DAMAGE, Enemy ATTACKER) - { - _health -= DAMAGE; - if (_health <= 0) - { - EmitSignal(SignalName.Death, this); - } - } - public void ShootCurrentAttack(Vector2 FORCE){ _attack.Shoot(FORCE); diff --git a/Enemy.cs b/Enemy.cs index 142860e..83685ae 100644 --- a/Enemy.cs +++ b/Enemy.cs @@ -12,7 +12,7 @@ public partial class Enemy : StaticBody2D [Signal] public delegate void RightClickedEventHandler(Enemy THIS); public bool _hovered = false, _track = false; - public int _damage = 1, _health = 2, _speed, _speedRemaining, _visibilityRange = 4, _hitRange = 1; + public int _damage = 1, _health = 2, _speed, _speedRemaining, _visibilityRange = 4, _hitRange; public Vector2I _address = -Vector2I.One, _range = Vector2I.Up; public List _path = new(); public float _movement = 0; @@ -43,9 +43,9 @@ public partial class Enemy : StaticBody2D base._PhysicsProcess(delta); } - public void Attack(Commander COMMANDER) + public void Attack(PlayerController PLAYER) { - COMMANDER.TakeDamage(_damage, this); + PLAYER.ChangeHealth(-1, this); } public void CounterAttack(Commander COMMANDER) diff --git a/EnemyController.cs b/EnemyController.cs index 9dcda18..6ff3a83 100644 --- a/EnemyController.cs +++ b/EnemyController.cs @@ -40,6 +40,7 @@ public partial class EnemyController : TurnController newEnemy.RightClicked += HandleEnemyRightClick; newEnemy._speed = Globals._rng.Next(2,4+1); + newEnemy._hitRange = Globals._rng.Next(1,2+1); newEnemy.Modulate = new Color(newEnemy._speed == 2 ? "#FF0000" : newEnemy._speed == 3 ? "#00FF00" : "#0000FF"); newEnemy._enemyController = this; @@ -50,14 +51,15 @@ public partial class EnemyController : TurnController } - public void EnemyAttacks() + public List EnemyAttacks() { List attackingEnemies = [.. _enemies.Where(e => e._address.Y <= e._hitRange)]; for (int i = 0; i < attackingEnemies.Count; i++) { Enemy enemy = attackingEnemies[i]; - // enemy.Attack(); + enemy.Attack(_playerController); } + return attackingEnemies; } public void GetRemainingSpeed(Enemy ENEMY) @@ -93,12 +95,13 @@ public partial class EnemyController : TurnController AddEnemies(positions); } - public void MoveEnemies() + public void MoveEnemies(List EXCLUDE_ENEMIES) { Map map = _playArea._map; - for (int i = 0; i < _enemies.Count; i++) + List movingEnmies = [.. _enemies.Where(e => !EXCLUDE_ENEMIES.Contains(e))]; + for (int i = 0; i < movingEnmies.Count; i++) { - Enemy enemy = _enemies[i]; + Enemy enemy = movingEnmies[i]; GetRemainingSpeed(enemy); if (enemy._speedRemaining > 0) @@ -112,7 +115,7 @@ public partial class EnemyController : TurnController } } - List remainingEnemies = GetRemainingEnemies(_enemies); + List remainingEnemies = GetRemainingEnemies(movingEnmies); _enemies.ForEach(e => e._path.Clear()); if (remainingEnemies.Count == 0) { @@ -196,9 +199,9 @@ public partial class EnemyController : TurnController { AddEnemies(1); - MoveEnemies(); + List attackingEnemies = EnemyAttacks(); - EnemyAttacks(); + MoveEnemies(attackingEnemies); } public void SetEnemy(Enemy ENEMY, Vector2I CELL) diff --git a/Main.cs b/Main.cs index ca4ab5a..247942f 100644 --- a/Main.cs +++ b/Main.cs @@ -24,6 +24,7 @@ public partial class Main : Node _enemyController._playArea = _playArea; _playerController.TurnDone += ChangeTurn; + _playerController.Death += EndGame; _enemyController.TurnDone += ChangeTurn; _enemyController.Initiate(); @@ -36,7 +37,7 @@ public partial class Main : Node base._Process(delta); if (Input.IsActionJustPressed("escape")) { - GetTree().Quit(); + } if (Input.IsActionJustPressed("changeTurn")) { @@ -62,4 +63,9 @@ public partial class Main : Node } } + public void EndGame(PlayerController PLAYER) + { + GetTree().Quit(); + } + } diff --git a/PlayerController.cs b/PlayerController.cs index 95292e6..def6101 100644 --- a/PlayerController.cs +++ b/PlayerController.cs @@ -5,15 +5,30 @@ using System.Linq; public partial class PlayerController : TurnController { + [Signal] + public delegate void DeathEventHandler(PlayerController THIS); + public int _health, _healthMax = 100; public EnemyController _enemyController; public PackedScene _commanderScene = GD.Load("res://Commander.tscn"); public List _towers = new(); public override void _Ready() { + _health = _healthMax; base._Ready(); } + public void ChangeHealth(int DAMAGE, Node IMPETUS = null) + { + _health += DAMAGE; + _health = Math.Clamp(_health, 0, _healthMax); + GD.Print(_health); + if (_health == 0) + { + EmitSignal(SignalName.Death, this); + } + } + public void CheckForTurnEnd() { if (_towers.Where(t => t._commander != null).ToList().All(t=>t._commander._actions == 0))