From f5acd70d8562c37e5ea57bcf8c49c94a1a26b0ff Mon Sep 17 00:00:00 2001 From: Crizomb Date: Tue, 28 Jan 2025 11:58:43 +0100 Subject: [PATCH] cringe bug fix im stupid in HealthHandmer --- Assets/Scenes/LevelsTest/3_Flat.unity | 81 +++++++++++++++++-- Assets/Scripts/CameraMouvement.cs | 1 - Assets/Scripts/Singletons/GameManager.cs | 1 - .../Attacks/Projectiles/AttackProjectile.cs | 2 +- .../BehaviorState/AbstractBehaviour.cs | 17 +--- .../BehaviorState/DefensiveBehaviour.cs | 19 +++-- .../BehaviorState/NeutralBehaviour.cs | 15 ++-- .../BehaviorState/OffensiveBehaviour.cs | 20 +++-- .../UnitScripts/Capacities/WitchSummon.cs | 6 +- Assets/Scripts/UnitScripts/HealthHandler.cs | 1 + Assets/Scripts/UnitScripts/MovementHandler.cs | 25 ++++-- 11 files changed, 135 insertions(+), 53 deletions(-) diff --git a/Assets/Scenes/LevelsTest/3_Flat.unity b/Assets/Scenes/LevelsTest/3_Flat.unity index e3885cc..4fc35fe 100644 --- a/Assets/Scenes/LevelsTest/3_Flat.unity +++ b/Assets/Scenes/LevelsTest/3_Flat.unity @@ -9262,16 +9262,39 @@ PrefabInstance: propertyPath: m_AnchorMax.y value: 0 objectReference: {fileID: 0} - m_RemovedComponents: [] + m_RemovedComponents: + - {fileID: 3872651080186061807, guid: 47d419944f796b5c895d7a27016647a1, type: 3} m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 3519332865738438487, guid: 47d419944f796b5c895d7a27016647a1, type: 3} + insertIndex: -1 + addedObject: {fileID: 719165817} m_SourcePrefab: {fileID: 100100000, guid: 47d419944f796b5c895d7a27016647a1, type: 3} --- !u!4 &719165806 stripped Transform: m_CorrespondingSourceObject: {fileID: 498920847641565454, guid: 47d419944f796b5c895d7a27016647a1, type: 3} m_PrefabInstance: {fileID: 719165805} m_PrefabAsset: {fileID: 0} +--- !u!1 &719165807 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 3519332865738438487, guid: 47d419944f796b5c895d7a27016647a1, type: 3} + m_PrefabInstance: {fileID: 719165805} + m_PrefabAsset: {fileID: 0} +--- !u!114 &719165817 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 719165807} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 794f919ac24609c05b9c690aaab19146, type: 3} + m_Name: + m_EditorClassIdentifier: + pathFps: 1 + distanceGoal: 0 --- !u!1001 &730637819 PrefabInstance: m_ObjectHideFlags: 0 @@ -17086,16 +17109,39 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - m_RemovedComponents: [] + m_RemovedComponents: + - {fileID: 5068148679420978154, guid: 87b84199a88b2822fb7a2a230db2f0ba, type: 3} m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 4845006117378930514, guid: 87b84199a88b2822fb7a2a230db2f0ba, type: 3} + insertIndex: -1 + addedObject: {fileID: 1339550862} m_SourcePrefab: {fileID: 100100000, guid: 87b84199a88b2822fb7a2a230db2f0ba, type: 3} --- !u!4 &1339550851 stripped Transform: m_CorrespondingSourceObject: {fileID: 8432779344647231755, guid: 87b84199a88b2822fb7a2a230db2f0ba, type: 3} m_PrefabInstance: {fileID: 1339550850} m_PrefabAsset: {fileID: 0} +--- !u!1 &1339550852 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 4845006117378930514, guid: 87b84199a88b2822fb7a2a230db2f0ba, type: 3} + m_PrefabInstance: {fileID: 1339550850} + m_PrefabAsset: {fileID: 0} +--- !u!114 &1339550862 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1339550852} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 794f919ac24609c05b9c690aaab19146, type: 3} + m_Name: + m_EditorClassIdentifier: + pathFps: 1 + distanceGoal: 0 --- !u!1001 &1346902245 PrefabInstance: m_ObjectHideFlags: 0 @@ -27717,16 +27763,39 @@ PrefabInstance: propertyPath: m_AnchorMax.y value: 0 objectReference: {fileID: 0} - m_RemovedComponents: [] + m_RemovedComponents: + - {fileID: 3872651080186061807, guid: 47d419944f796b5c895d7a27016647a1, type: 3} m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 3519332865738438487, guid: 47d419944f796b5c895d7a27016647a1, type: 3} + insertIndex: -1 + addedObject: {fileID: 2105221389} m_SourcePrefab: {fileID: 100100000, guid: 47d419944f796b5c895d7a27016647a1, type: 3} --- !u!4 &2105221378 stripped Transform: m_CorrespondingSourceObject: {fileID: 498920847641565454, guid: 47d419944f796b5c895d7a27016647a1, type: 3} m_PrefabInstance: {fileID: 2105221377} m_PrefabAsset: {fileID: 0} +--- !u!1 &2105221379 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 3519332865738438487, guid: 47d419944f796b5c895d7a27016647a1, type: 3} + m_PrefabInstance: {fileID: 2105221377} + m_PrefabAsset: {fileID: 0} +--- !u!114 &2105221389 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2105221379} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 794f919ac24609c05b9c690aaab19146, type: 3} + m_Name: + m_EditorClassIdentifier: + pathFps: 1 + distanceGoal: 0 --- !u!1001 &2109955598 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CameraMouvement.cs b/Assets/Scripts/CameraMouvement.cs index c452e81..46fa78e 100644 --- a/Assets/Scripts/CameraMouvement.cs +++ b/Assets/Scripts/CameraMouvement.cs @@ -12,7 +12,6 @@ public class CameraMouvement : MonoBehaviour if (context.phase == InputActionPhase.Performed) { _moveInput = context.ReadValue(); - print(_moveInput); } } // Start is called once before the first execution of Update after the MonoBehaviour is created diff --git a/Assets/Scripts/Singletons/GameManager.cs b/Assets/Scripts/Singletons/GameManager.cs index 90f9714..f1fc5d4 100644 --- a/Assets/Scripts/Singletons/GameManager.cs +++ b/Assets/Scripts/Singletons/GameManager.cs @@ -21,7 +21,6 @@ public class GameManager : MonoBehaviourSingletonPersistent // Delete, use only for Debug if (Input.GetKeyDown(KeyCode.Space)) { - print("OOKOKOKOKOKOK"); StartFightForAll(); } } diff --git a/Assets/Scripts/UnitScripts/Attacks/Projectiles/AttackProjectile.cs b/Assets/Scripts/UnitScripts/Attacks/Projectiles/AttackProjectile.cs index 43c37bd..73335e1 100644 --- a/Assets/Scripts/UnitScripts/Attacks/Projectiles/AttackProjectile.cs +++ b/Assets/Scripts/UnitScripts/Attacks/Projectiles/AttackProjectile.cs @@ -21,7 +21,6 @@ public class AttackProjectile : AttackHandler { if (GlobalsVariable.AliveUnitsTeamA.Count == 0) return false; } - _minecraftUnit.MovementHandler.UpdateNearest(); } float launchAngle = findLaunchAngle(); //print(launchAngle); @@ -47,6 +46,7 @@ public class AttackProjectile : AttackHandler // Source : https://en.wikipedia.org/wiki/Projectile_motion#Angle_%CE%B8_required_to_hit_coordinate_(x,_y) AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit; + if (targetUnit == null) return -1f; Vector3 diffVector = targetUnit.transform.position - spawnPos.position; Vector3 projectOnPlane = Vector3.ProjectOnPlane(diffVector, Vector3.up); diff --git a/Assets/Scripts/UnitScripts/BehaviorState/AbstractBehaviour.cs b/Assets/Scripts/UnitScripts/BehaviorState/AbstractBehaviour.cs index 136dba1..8aa5812 100644 --- a/Assets/Scripts/UnitScripts/BehaviorState/AbstractBehaviour.cs +++ b/Assets/Scripts/UnitScripts/BehaviorState/AbstractBehaviour.cs @@ -8,10 +8,9 @@ using System.Collections.Generic; public abstract class AbstractBehaviour : MonoBehaviour { [SerializeField] private float pathFps = 1.0f; - [SerializeField] private float attackFps = 5.0f; + [SerializeField] protected float distanceGoal = 0.0f; protected abstract void MoveAction(); - protected abstract void AttackAction(); protected MinecraftUnit CurrentMinecraftUnit; @@ -19,23 +18,9 @@ public abstract class AbstractBehaviour : MonoBehaviour void Start() { CurrentMinecraftUnit = GetComponent(); - StartCoroutine(attackUpdate()); StartCoroutine(pathUpdate()); } - // Path update and attack update can be expansive, so we don't do that every frame. We create custom update - // We create custom update at low fps to handle this without performance issues - - private IEnumerator attackUpdate() - { - while (true) - { - AttackAction(); - yield return new WaitForSeconds(1.0f/attackFps); - } - } - - private IEnumerator pathUpdate() { while (true) diff --git a/Assets/Scripts/UnitScripts/BehaviorState/DefensiveBehaviour.cs b/Assets/Scripts/UnitScripts/BehaviorState/DefensiveBehaviour.cs index 3dc4971..34ef830 100644 --- a/Assets/Scripts/UnitScripts/BehaviorState/DefensiveBehaviour.cs +++ b/Assets/Scripts/UnitScripts/BehaviorState/DefensiveBehaviour.cs @@ -4,11 +4,20 @@ public class DefensiveBehaviour : AbstractBehaviour { protected override void MoveAction() { - throw new System.NotImplementedException(); + if (CurrentMinecraftUnit.IsTeamA) + { + if (GlobalsVariable.AliveUnitsTeamB.Count == 0) return; + CurrentMinecraftUnit.MovementHandler.UpdateNearestFrom(GlobalsVariable.QueenA.transform); + } + else + { + if (GlobalsVariable.AliveUnitsTeamA.Count == 0) return; + CurrentMinecraftUnit.MovementHandler.UpdateNearestFrom(GlobalsVariable.QueenB.transform); + } + + Vector3 targetPos = CurrentMinecraftUnit.MovementHandler.TargetUnit.transform.position; + Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal; + CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos); } - protected override void AttackAction() - { - throw new System.NotImplementedException(); - } } diff --git a/Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs b/Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs index 52b5a36..a82da06 100644 --- a/Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs +++ b/Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs @@ -1,8 +1,8 @@ +using System; using UnityEngine; public class NeutralBehaviour : AbstractBehaviour { - [SerializeField] private float distanceGoal = 0.0f; protected override void MoveAction() { if (CurrentMinecraftUnit.IsTeamA) @@ -13,15 +13,14 @@ public class NeutralBehaviour : AbstractBehaviour { if (GlobalsVariable.AliveUnitsTeamA.Count == 0) return; } - CurrentMinecraftUnit.MovementHandler.UpdateNearest(); - Vector3 targetPos = CurrentMinecraftUnit.MovementHandler.TargetUnit.transform.position; + AbstractUnit targetUnit = CurrentMinecraftUnit.MovementHandler.TargetUnit; + if (targetUnit == null) + { + return; + } + Vector3 targetPos = targetUnit.transform.position; Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal; CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos); } - - protected override void AttackAction() - { - //CurrentMinecraftUnit.AttackHandler.Attack(); - } } diff --git a/Assets/Scripts/UnitScripts/BehaviorState/OffensiveBehaviour.cs b/Assets/Scripts/UnitScripts/BehaviorState/OffensiveBehaviour.cs index dd1ba60..ef49815 100644 --- a/Assets/Scripts/UnitScripts/BehaviorState/OffensiveBehaviour.cs +++ b/Assets/Scripts/UnitScripts/BehaviorState/OffensiveBehaviour.cs @@ -4,11 +4,19 @@ public class OffensiveBehaviour : AbstractBehaviour { protected override void MoveAction() { - throw new System.NotImplementedException(); - } - - protected override void AttackAction() - { - throw new System.NotImplementedException(); + if (CurrentMinecraftUnit.IsTeamA) + { + if (GlobalsVariable.QueenB == null) return; + } + else + { + if (GlobalsVariable.QueenA == null) return; + } + + CurrentMinecraftUnit.MovementHandler.TargetUnit = GlobalsVariable.QueenB; + Vector3 targetPos = CurrentMinecraftUnit.MovementHandler.TargetUnit.transform.position; + Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal; + CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos); } + } diff --git a/Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs b/Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs index e353ef4..ef74760 100644 --- a/Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs +++ b/Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs @@ -6,9 +6,9 @@ public class WitchSummon : BaseCapacity protected override bool CapacityCall() { - print("SUMMON"); - print(Mana); - Instantiate(summonUnit, transform.position, Quaternion.identity); + GameObject unit = Instantiate(summonUnit, transform.position, Quaternion.identity); + AbstractUnit unitScript = unit.GetComponent(); + unitScript.StartFight(); return true; } } diff --git a/Assets/Scripts/UnitScripts/HealthHandler.cs b/Assets/Scripts/UnitScripts/HealthHandler.cs index 353d09d..84b3725 100644 --- a/Assets/Scripts/UnitScripts/HealthHandler.cs +++ b/Assets/Scripts/UnitScripts/HealthHandler.cs @@ -49,6 +49,7 @@ public class HealthHandler : MonoBehaviour if (deathState == DeathSate.NotImportant) { Destroy(gameObject, delay); + return; } GlobalsVariable.AliveUnitsTeamB = new List(); diff --git a/Assets/Scripts/UnitScripts/MovementHandler.cs b/Assets/Scripts/UnitScripts/MovementHandler.cs index 04d4493..34edf60 100644 --- a/Assets/Scripts/UnitScripts/MovementHandler.cs +++ b/Assets/Scripts/UnitScripts/MovementHandler.cs @@ -15,7 +15,7 @@ public class MovementHandler : MonoBehaviour [SerializeField] private float knockbackTime = 1.2f; private float _noNavMeshDeadTime = 6.0f; - [HideInInspector] public AbstractUnit TargetUnit {get; private set; } + [HideInInspector] public AbstractUnit TargetUnit {get; set; } private MinecraftUnit _minecraftUnit; private Rigidbody _rigidbody; @@ -62,24 +62,25 @@ public class MovementHandler : MonoBehaviour TargetUnit = FindNearest(followEnemy); } - public void MoveTowardsNearest() + public void UpdateNearestFrom(Transform transform) { - MoveTowards(TargetUnit.transform.position); + TargetUnit = FindNearestFromTransform(true, transform); } + // If findEnemy, return closest ennemy else return closest ally - public AbstractUnit FindNearest(bool findEnemy) + private AbstractUnit FindNearestFromTransform(bool findEnemy, Transform from) { // Funny funny double ternary operator. List targets = findEnemy ? - _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA + _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA : _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamA : GlobalsVariable.AliveUnitsTeamB; AbstractUnit closestUnit = null; float closestDistance = float.MaxValue; foreach (AbstractUnit target in targets) { - float distanceToEnemy = (target.transform.position - transform.position).sqrMagnitude; + float distanceToEnemy = (target.transform.position - from.position).sqrMagnitude; if (distanceToEnemy < closestDistance && target != _minecraftUnit) { closestUnit = target; @@ -87,8 +88,20 @@ public class MovementHandler : MonoBehaviour } } + if (closestUnit == null) + { + print("What"); + print(targets.Count); + print(targets); + } + return closestUnit; } + + private AbstractUnit FindNearest(bool findEnemy) + { + return FindNearestFromTransform(findEnemy, transform); + } public IEnumerator TakeImpulse(Vector3 impulse) {