From f24523da7cfbdec419257d2fe0167f505329823a Mon Sep 17 00:00:00 2001 From: Maciekxdabu <40292375+Maciekxdabu@users.noreply.github.com> Date: Fri, 3 Jun 2022 13:26:03 +0200 Subject: [PATCH] merloj: Communication p2 - can now find Soldiers in viewRange --- .../Assets/Scripts/Entities/Communication.cs | 46 +++++++++++++++++-- theProject/Assets/Scripts/Entities/Entity.cs | 2 +- theProject/Assets/Scripts/Entities/Soldier.cs | 25 ++++++++-- .../Assets/Scripts/Managers/TilemapManager.cs | 5 ++ 4 files changed, 68 insertions(+), 10 deletions(-) diff --git a/theProject/Assets/Scripts/Entities/Communication.cs b/theProject/Assets/Scripts/Entities/Communication.cs index 3d32f88c..5bbf6a37 100644 --- a/theProject/Assets/Scripts/Entities/Communication.cs +++ b/theProject/Assets/Scripts/Entities/Communication.cs @@ -16,10 +16,14 @@ public class Communication : MonoBehaviour // ---------- public methods - public void Initialize(float _viewRange, Soldier _mySoldier, Entity.Team _myTeam) + private void Awake() + { + mySoldier = GetComponent(); + } + + public void Initialize(float _viewRange, Entity.Team _myTeam) { viewRange = _viewRange; - mySoldier = _mySoldier; myTeam = _myTeam; } @@ -30,10 +34,24 @@ public class Communication : MonoBehaviour result.enemiesSpotted = new List(); // look for soldiers in vincinity + Soldier[] soldiersFound = GetSoldiersInVincinity(); // send Keep Alive (myTeam) - reset TTL (myTTL) + foreach (Soldier sold in soldiersFound) + { + if (sold.GetOwnTeam() == myTeam) + { + result.resetTTL = true; + break; + } + } + Debug.Log(result.resetTTL); // look fo enemies (enemyTeam) - add Enemies to enemiesSpotted (send List) + if (result.resetTTL)//only check for enemies if you can see your squad + { + + } return result; } @@ -42,9 +60,27 @@ public class Communication : MonoBehaviour private Soldier[] GetSoldiersInVincinity() { - //TO DO - implement - Physics.SphereCastAll() + List soldiersFound = new List(); - return null; + Vector2Int curPos = mySoldier.tileCoord; + int range = mySoldier.rangeView; + int rangeSQR = range * range; + + for (int i=curPos.x - range; i <= curPos.x + range; i++) + { + for (int j = curPos.y - range; j <= curPos.y + range; j++) + { + Vector2Int curTile = new Vector2Int(i, j); + if ( (curTile - curPos).sqrMagnitude <= rangeSQR && curTile != curPos)//is current Tile in a circular range (and not our tile) + { + Soldier newSoldier = (Soldier)TilemapManager.GetSoldierS(i, j); + + if (newSoldier != null) + soldiersFound.Add(newSoldier); + } + } + } + + return soldiersFound.ToArray(); } } diff --git a/theProject/Assets/Scripts/Entities/Entity.cs b/theProject/Assets/Scripts/Entities/Entity.cs index 2a593dce..2683f68b 100644 --- a/theProject/Assets/Scripts/Entities/Entity.cs +++ b/theProject/Assets/Scripts/Entities/Entity.cs @@ -21,7 +21,7 @@ public class Entity : MonoBehaviour [HideInInspector] public UnityEvent OnDeath = new UnityEvent(); - [SerializeField] protected Vector2Int tileCoord; + public Vector2Int tileCoord; [SerializeField] protected Vector3 WORLD_SPACE_OFFSET = new Vector3(0.5f, 0.5f, 0.5f); diff --git a/theProject/Assets/Scripts/Entities/Soldier.cs b/theProject/Assets/Scripts/Entities/Soldier.cs index c2189d81..a94bf314 100644 --- a/theProject/Assets/Scripts/Entities/Soldier.cs +++ b/theProject/Assets/Scripts/Entities/Soldier.cs @@ -59,7 +59,7 @@ public class Soldier : Entity [Header("Soldier Values")] [SerializeField] private float rangeAttack = 100; - [SerializeField] private float rangeView = 1; + public int rangeView = 1; [SerializeField] private float damageAttack = 1; [SerializeField] private int speedAttack = 1; // ticks between attacks [SerializeField] private int lastAttackTick = -1; @@ -69,11 +69,21 @@ public class Soldier : Entity [SerializeField] private Team enemyType; [SerializeField] private Vector2Int movementDestination = Vector2Int.zero; - [SerializeField] private bool hasReachedDestination = true; - // Start is called before the first frame update + [SerializeField] private bool hasReachedDestination = true; + + private Communication ourCommunication = null; + + protected override void Awake() + { + base.Awake(); + ourCommunication = GetComponent(); + } + protected override void Start(){ base.Start(); SetEnemyTag(); + + ourCommunication.Initialize(rangeView, myTeam); } public void SetEnemyTag() @@ -84,7 +94,14 @@ public class Soldier : Entity protected override void HandleTick(TickSystem.OnTickEventArgs tickEventArgs) { - // base.HandleTick(tickEventArgs); + // base.HandleTick(tickEventArgs); + + Communication.CommunicationResult cResult = ourCommunication.HandleCommuncation(); + + if (cResult.resetTTL) + { + + } ref Queue queueToHandle = ref interrupts; if (interrupts.Count < 1) // if no interrupt actions to do, handle regular queue diff --git a/theProject/Assets/Scripts/Managers/TilemapManager.cs b/theProject/Assets/Scripts/Managers/TilemapManager.cs index 576273fa..a0494917 100644 --- a/theProject/Assets/Scripts/Managers/TilemapManager.cs +++ b/theProject/Assets/Scripts/Managers/TilemapManager.cs @@ -172,6 +172,11 @@ public class TilemapManager : MonoBehaviour return ins.MoveEntity(x1, y1, x2, y2); } + public static Entity GetSoldierS(int x, int y) + { + return ins.GetSoldier(x, y); + } + // ---------- private methods private TileState GetTileState(int x, int y)