From 61539e9250827a75acc21392c76f710f6ba88603 Mon Sep 17 00:00:00 2001 From: cojoedmo Date: Thu, 25 Jun 2026 03:14:21 -0400 Subject: [PATCH] still updating enemy movement and pathing and tweening --- Art/attack.png | Bin 0 -> 250 bytes Art/attack.png.import | 40 +++++++++++++ Enemy.cs | 72 +++++++++++++++-------- EnemyController.cs | 129 +++++++++++++++++++++--------------------- enemy.tscn | 7 +++ 5 files changed, 160 insertions(+), 88 deletions(-) create mode 100644 Art/attack.png create mode 100644 Art/attack.png.import diff --git a/Art/attack.png b/Art/attack.png new file mode 100644 index 0000000000000000000000000000000000000000..d80f1cdea6519e38be73253bf7147243f12c33f6 GIT binary patch literal 250 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4F%}28J2BoosZ$T+u%tWsIx;Y9 z?C1WI$O_~uBzpw;GB8xBF)%c=FfjZA3N^f7U???UV0e|lz+g3lfkC`r&aOZkpaf@u zM`SSrgPt-7Ggd6MF9Qm)mw5WRvcF=JWR+rS+@$svC=}r7;us=vx%a|G-Ub5!mV*Zs zU3bafTEO=_gyrQ#3kAlyrDwzbUozyh?J4kce=)B`_sP+>m74ZtpO3~{Oe;wGdiJVF mrUlpCiyOob?X~kS{UH5uZJe09x0Eo@Yz9wPKbLh*2~7Z#9ZTc@ literal 0 HcmV?d00001 diff --git a/Art/attack.png.import b/Art/attack.png.import new file mode 100644 index 0000000..a6b4c46 --- /dev/null +++ b/Art/attack.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://m4wfj36twmqy" +path="res://.godot/imported/attack.png-c625624f48e39eb3c9d0bdfb20bcabb8.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Art/attack.png" +dest_files=["res://.godot/imported/attack.png-c625624f48e39eb3c9d0bdfb20bcabb8.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Enemy.cs b/Enemy.cs index f2c077a..65ccea5 100644 --- a/Enemy.cs +++ b/Enemy.cs @@ -13,7 +13,7 @@ public partial class Enemy : StaticBody2D [Signal] public delegate void RightClickedEventHandler(Enemy THIS); public bool _hovered = false, _track = false, _warp = false; - public int _damage = 1, _health = 2, _stamina, _staminaRemaining, _visibilityRange = 4, _hitRange; + public int _damage = 1, _health = 2, _stamina, _staminaRemaining, _visibilityRange = 4, _hitRange, _attackCost = 1; public Dictionary _priorities = new() { {"attack", 0}, // 0 to 999999 reserved for attacking priorities @@ -23,10 +23,12 @@ public partial class Enemy : StaticBody2D public List _path = new(), _pathStored = new(); public float _movement = 0; public EnemyController _enemyController; + public Sprite2D _attack; public override void _Ready() { base._Ready(); + _attack = GetNode("Attack"); } public override void _Process(double delta) @@ -48,32 +50,38 @@ public partial class Enemy : StaticBody2D { base._PhysicsProcess(delta); } - - public void AnimateMovement(EnemyController CONTROLLER) + + public void Attack(EnemyController CONTROLLER) { - for (int i = 0; i < _path.Count; i++) + _attack.Visible = true; + Tween subtween = CreateTween(); + subtween.TweenProperty(_attack, "global_position", Vector2.Zero, 0.5f); + subtween.TweenCallback(Callable.From(() => { - Tween subtween = CreateTween(); - subtween.TweenProperty(this, "global_position", CONTROLLER._playArea._map.GetCellPositionFromAddress(_path[i]), 0.25f); - int key = _priorities["movement"] + i; + CONTROLLER._playerController.ChangeHealth(-1, this); + _attack.Position = Vector2.Zero; + _attack.Visible = false; + })); + int key = _priorities["attack"]; - if (!CONTROLLER._tweenStages.ContainsKey(key)) - { - CONTROLLER._tweenStages[key] = new(); - } - CONTROLLER._tweenStages[key].Add(subtween); + if (!CONTROLLER._tweenStages.ContainsKey(key)) + { + CONTROLLER._tweenStages[key] = new(); } - + CONTROLLER._tweenStages[key].Add(subtween); + _staminaRemaining = Math.Max(_staminaRemaining - 2, 0); + } - public void Attack(PlayerController PLAYER) + public bool CanAttack(EnemyController CONTROLLER) { - PLAYER.ChangeHealth(-1, this); + return _staminaRemaining > 0 && _address.Y <= _hitRange; } - public bool CanAttack() + public bool CanMove(EnemyController CONTROLLER) { - return _address.Y <= _hitRange; + // GD.Print(_staminaRemaining," ",_address.Y," ", CONTROLLER._playArea._map._firstOpenRow); + return _staminaRemaining > 0 && _address.Y > CONTROLLER._playArea._map._firstOpenRow; } public void CounterAttack(Commander COMMANDER) @@ -81,11 +89,6 @@ public partial class Enemy : StaticBody2D } - public virtual List GetGoals(Map MAP) - { - return [.. MAP._cells.Where(c => c.Y == Math.Max(_address.Y - _visibilityRange, MAP._firstOpenRow)).Where(c => !MAP._astar.IsPointSolid(c))]; - } - public virtual List GetBestPath(Map MAP) { List goals = GetGoals(MAP); @@ -95,10 +98,33 @@ public partial class Enemy : StaticBody2D { paths.Add(MAP.GetPath(_address, goals[i])); } - List bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ThenBy(p => p.Last().X % 2).ToList()[0]; + List bestPath = paths.Where(p => p.Count > 0).OrderBy(p => p.Count).ToList()[0]; return bestPath; } + public virtual List GetGoals(Map 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) + { + CONTROLLER._playArea._map.SetCellEnemy(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; + + if (!CONTROLLER._tweenStages.ContainsKey(key)) + { + CONTROLLER._tweenStages[key] = new(); + } + + CONTROLLER._tweenStages[key].Add(subtween); + _staminaRemaining--; + } + public void StartTurn() { _staminaRemaining = _stamina; diff --git a/EnemyController.cs b/EnemyController.cs index e2d1a6c..40a3998 100644 --- a/EnemyController.cs +++ b/EnemyController.cs @@ -7,9 +7,9 @@ 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 List _enemies = new(), _remainingEnemies = new(); public PlayerController _playerController; public Dictionary> _tweenStages = new(); public Tween _tween; @@ -19,7 +19,7 @@ public partial class EnemyController : TurnController for (int i = 0; i < ENEMY_COUNT; i++) { Enemy newEnemy = _enemyScene.Instantiate(); - newEnemy.Death += RemoveEnemy; + newEnemy.Death += HandleEnemyRemoval; newEnemy.Clicked += HandleEnemyClick; newEnemy.RightClicked += HandleEnemyRightClick; @@ -38,13 +38,13 @@ public partial class EnemyController : TurnController for (int i = 0; i < POSITIONS.Count; i++) { Enemy newEnemy = _enemyScene.Instantiate(); - newEnemy.Death += RemoveEnemy; + 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.Modulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF"); + newEnemy.GetNode("Sprite2D").SelfModulate = new Color(newEnemy._stamina == 2 ? "#FF0000" : newEnemy._stamina == 3 ? "#00FF00" : "#0000FF"); newEnemy._enemyController = this; SetEnemy(newEnemy, POSITIONS[i]); @@ -53,60 +53,36 @@ public partial class EnemyController : TurnController } } - - public void BuildAnimationTween() - { - _tweenStages.Clear(); - for (int i= 0; i < _enemies.Count; i++) - { - Enemy enemy = _enemies[i]; - enemy.AnimateMovement(this); - } - _tweenStages.OrderBy(s => s.Key); - } - public void BuildEnemyPaths() + public void HandleEnemyActions() { + _actionLoop = 0; + _tweenStages.Clear(); Map map = _playArea._map; _enemies.ForEach(e => e._path.Clear()); - while (_enemies.Any(e => e._staminaRemaining > 0)) + + _remainingEnemies = [.. _enemies.Where(e => e.CanMove(this) || e.CanAttack(this))]; + while (_remainingEnemies.Count > 0) { - for (int i = 0; i < _enemies.Count; i++) + for (int i = 0; i < _remainingEnemies.Count; i++) { - Enemy enemy = _enemies[i]; - if (enemy._staminaRemaining <= 0 || enemy._address.Y <= map._firstOpenRow) - { - GD.Print(enemy._staminaRemaining <= 0, enemy._address.Y <= map._firstOpenRow); - enemy._path.Add(enemy._path.LastOrDefault(enemy._address)); - continue; - } - - List path = enemy.GetBestPath(map); - - if (path.Count == 0) - { - enemy._path.Add(enemy._path.LastOrDefault(enemy._address)); - continue; - } - Vector2I cell = path[0]; - map.SetCellEnemy(cell, enemy); - enemy._path.Add(cell); - - enemy._staminaRemaining--; + Enemy enemy = _remainingEnemies[i]; + HandleEnemyPathing(enemy); + HandleEnemyAttacking(enemy); + HandleEnemyStaminaCheck(enemy); } + _actionLoop++; } } - public void EnemyAttacks() + public void HandleEnemyAttacking(Enemy ENEMY) { - List attackingEnemies = [.. _enemies.Where(e => e.CanAttack())]; - for (int i = 0; i < attackingEnemies.Count; i++) + if (!ENEMY.CanAttack(this)) { - Enemy enemy = attackingEnemies[i]; - enemy.Attack(_playerController); - enemy._staminaRemaining = Math.Max(enemy._staminaRemaining - 2, 0); + return; } + ENEMY.Attack(this); } @@ -128,7 +104,44 @@ public partial class EnemyController : TurnController public void HandleEnemyRightClick(Enemy ENEMY) { - RemoveEnemy(ENEMY); + HandleEnemyRemoval(ENEMY); + } + + public void HandleEnemyPathing(Enemy ENEMY) + { + if (!ENEMY.CanMove(this)) + { + return; + } + List path = ENEMY.GetBestPath(_playArea._map); + + if (path.Count == 0) + { + 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 HandleEnemyStaminaCheck(Enemy ENEMY) + { + if (ENEMY._staminaRemaining <= 0) + { + _remainingEnemies.Remove(ENEMY); + return; + } } public void Initiate() @@ -140,7 +153,8 @@ public partial class EnemyController : TurnController public void ProcessTween() { - + _tweenStages.OrderBy(s => s.Key); + if (_tweenStages.Count <= 0) { return; @@ -167,20 +181,9 @@ public partial class EnemyController : TurnController } - public void RemoveEnemy(Enemy ENEMY_TO_REMOVE) - { - _enemies.Remove(ENEMY_TO_REMOVE); - _playArea._map.SetCellEnemy(ENEMY_TO_REMOVE._address, null); - ENEMY_TO_REMOVE.QueueFree(); - } - - public void SortEnemies() - { - _enemies = [.. _enemies.OrderBy(e => e._address.Y).ThenBy(e => e._path.Count).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))]; - } - public override void StartTurn() { + AddEnemies(1); for (int i = 0; i < _enemies.Count; i++) @@ -188,13 +191,9 @@ public partial class EnemyController : TurnController _enemies[i].StartTurn(); } - SortEnemies(); - - EnemyAttacks(); + HandleEnemySort(); - BuildEnemyPaths(); - - BuildAnimationTween(); + HandleEnemyActions(); ProcessTween(); } diff --git a/enemy.tscn b/enemy.tscn index 8a792fd..4848ff0 100644 --- a/enemy.tscn +++ b/enemy.tscn @@ -2,6 +2,7 @@ [ext_resource type="Script" uid="uid://dfba4vq6jv0a6" path="res://Enemy.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"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] bounce = 0.5 @@ -18,8 +19,14 @@ script = ExtResource("1_4gyqm") shape = SubResource("CircleShape2D_4gyqm") [node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605] +texture_filter = 1 scale = Vector2(0.5, 0.5) texture = ExtResource("1_7k104") +[node name="Attack" type="Sprite2D" parent="." unique_id=1776738311] +visible = false +texture_filter = 1 +texture = ExtResource("3_qi2p4") + [connection signal="mouse_entered" from="." to="." method="OnMouseEntered"] [connection signal="mouse_exited" from="." to="." method="OnMouseExited"]