From ce3d7d69ee18b027bb0138a664ceff05eb7400a2 Mon Sep 17 00:00:00 2001 From: Gabriel Ksawery Skowron-Rodriguez Date: Mon, 23 May 2022 15:13:48 +0200 Subject: [PATCH 1/4] Adding initial soldiers to squads --- .../Assets/Scripts/Managers/SquadManager.cs | 25 +++++++++++++------ theProject/Assets/Scripts/Squad.cs | 12 +++++++-- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/theProject/Assets/Scripts/Managers/SquadManager.cs b/theProject/Assets/Scripts/Managers/SquadManager.cs index 37d1281c..2c784f0c 100644 --- a/theProject/Assets/Scripts/Managers/SquadManager.cs +++ b/theProject/Assets/Scripts/Managers/SquadManager.cs @@ -13,10 +13,12 @@ public class SquadManager : MonoBehaviour playerSquad = Instantiate(squadPrefab).GetComponent(); playerSquad.gameObject.name = "Player Squad"; playerSquad.transform.SetParent(transform); + playerSquad.SetOwnTeam(Entity.Team.Ally); enemySquad = Instantiate(squadPrefab).GetComponent(); enemySquad.gameObject.name = "Enemy Squad"; enemySquad.transform.SetParent(transform); + enemySquad.SetOwnTeam(Entity.Team.Enemy); } // Update is called once per frame void Update() @@ -24,14 +26,21 @@ public class SquadManager : MonoBehaviour Debug.Log("Added initial soldiers to squad"); // add all ally soldiers to squad var soldiers = FindObjectsOfType(); - foreach(var soldier in soldiers) - { - if(soldier.GetOwnTeam() == Soldier.Team.Ally) - { - playerSquad.TempAddSoldierToSquad(soldier); - } - } + var squads = new List(); + squads.Add(playerSquad); + squads.Add(enemySquad); - enabled = false; + foreach (var soldier in soldiers) + { + foreach (var squad in squads) + { + if (soldier.GetOwnTeam() == squad.GetOwnTeam()) + { + squad.AddSoldierToSquad(soldier); + soldier.OnDeath.AddListener(squad.RemoveSoldierFromSquad); + } + } + enabled = false; + } } } diff --git a/theProject/Assets/Scripts/Squad.cs b/theProject/Assets/Scripts/Squad.cs index 32b602d1..7e05b600 100644 --- a/theProject/Assets/Scripts/Squad.cs +++ b/theProject/Assets/Scripts/Squad.cs @@ -5,6 +5,9 @@ using UnityEngine; public class Squad : MonoBehaviour { + private Entity.Team ownTeam; + public Entity.Team GetOwnTeam() { return ownTeam; } + public void SetOwnTeam(Entity.Team newTeam) {ownTeam = newTeam; } #region Orders public abstract class Order // generic order (to keep in queue) { @@ -31,14 +34,19 @@ public class Squad : MonoBehaviour } #endregion - [SerializeField] private List soldiers = new List(); // soldiers belonging to the squad + [SerializeField] private List soldiers = new List(); // soldiers belonging to the squad private Queue orders = new Queue(); // orders given to the squad - public void TempAddSoldierToSquad(Soldier soldier) + public void AddSoldierToSquad(Entity soldier) { soldiers.Add(soldier); } + public void RemoveSoldierFromSquad(Entity soldier) + { + soldiers.Remove(soldier); + } + private void Awake() { TickSystem.OnTick += HandleTick; From 93ba29703e7a0866322bba843ce979ca3b33c906 Mon Sep 17 00:00:00 2001 From: Gabriel Ksawery Skowron-Rodriguez Date: Mon, 23 May 2022 16:27:39 +0200 Subject: [PATCH 2/4] made usage of adding/removing soldiers to squad easier + now only add/remove soldier to squad once and this --- theProject/Assets/Scripts/Managers/SquadManager.cs | 1 - theProject/Assets/Scripts/Squad.cs | 12 +++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/theProject/Assets/Scripts/Managers/SquadManager.cs b/theProject/Assets/Scripts/Managers/SquadManager.cs index 2c784f0c..45afd820 100644 --- a/theProject/Assets/Scripts/Managers/SquadManager.cs +++ b/theProject/Assets/Scripts/Managers/SquadManager.cs @@ -37,7 +37,6 @@ public class SquadManager : MonoBehaviour if (soldier.GetOwnTeam() == squad.GetOwnTeam()) { squad.AddSoldierToSquad(soldier); - soldier.OnDeath.AddListener(squad.RemoveSoldierFromSquad); } } enabled = false; diff --git a/theProject/Assets/Scripts/Squad.cs b/theProject/Assets/Scripts/Squad.cs index 7e05b600..42c7c5a2 100644 --- a/theProject/Assets/Scripts/Squad.cs +++ b/theProject/Assets/Scripts/Squad.cs @@ -40,11 +40,13 @@ public class Squad : MonoBehaviour public void AddSoldierToSquad(Entity soldier) { soldiers.Add(soldier); + soldier.OnDeath.AddListener(RemoveSoldierFromSquad); } public void RemoveSoldierFromSquad(Entity soldier) { soldiers.Remove(soldier); + soldier.OnDeath.RemoveListener(RemoveSoldierFromSquad); } private void Awake() @@ -52,12 +54,12 @@ public class Squad : MonoBehaviour TickSystem.OnTick += HandleTick; } - private void Start() - { - - } + private void OnDestroy() + { + TickSystem.OnTick -= HandleTick; + } - private void HandleTick(TickSystem.OnTickEventArgs eventArgs) + private void HandleTick(TickSystem.OnTickEventArgs eventArgs) {// pass a single order to all soldiers if (orders.Count < 1) return; // for now nothing to do here From 4ee480cdc03c2ac2824f601adaac34728d7d3b0b Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 23 May 2022 17:36:25 +0200 Subject: [PATCH 3/4] feat: add formation calculation --- theProject/Assets/Scripts/Formation.cs | 36 +++++++++++++++++++++ theProject/Assets/Scripts/Formation.cs.meta | 11 +++++++ theProject/Assets/Scripts/Squad.cs | 5 +++ 3 files changed, 52 insertions(+) create mode 100644 theProject/Assets/Scripts/Formation.cs create mode 100644 theProject/Assets/Scripts/Formation.cs.meta diff --git a/theProject/Assets/Scripts/Formation.cs b/theProject/Assets/Scripts/Formation.cs new file mode 100644 index 00000000..269b7a7e --- /dev/null +++ b/theProject/Assets/Scripts/Formation.cs @@ -0,0 +1,36 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Formation : MonoBehaviour +{ + [SerializeField] Squad squad; + + void Awake() + { + this.squad = squad.GetComponent(typeof(Squad)); + } + + public Dictionary calculatePositions(Vector2int coordinates) + { + List soldiers = this.squad.getSoldiers(); + Dictionary soldierNewCoordinate = new Dictionary(); + int soldierNumber = 0; + foreach (Soldier soldier in soldiers) + { + soldierNewCoordinate.Add(soldier, calculateSoldierCoordinates(soldierNumber, coordinates)); + MoveSoldierS(x, y, soldierNewCoordinate.Item1, soldierNewCoordinate.Item2); + } + return soldierNewCoordinate; + } + + // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier + private Vector2Int calculateSoldierCoordinates(in int soldierNumber, in Vector2Int coordinates) + { + // Horizontal line we change x + Vector2Int coordinates = new Vector2Int(coordinates.Item1 + soldierNumber, coordinates.Item1); + return coordinates; + } + + +} diff --git a/theProject/Assets/Scripts/Formation.cs.meta b/theProject/Assets/Scripts/Formation.cs.meta new file mode 100644 index 00000000..0583ef45 --- /dev/null +++ b/theProject/Assets/Scripts/Formation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5175e17e18d002001bf1ebde5de11b49 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/theProject/Assets/Scripts/Squad.cs b/theProject/Assets/Scripts/Squad.cs index 32b602d1..1c4c7d59 100644 --- a/theProject/Assets/Scripts/Squad.cs +++ b/theProject/Assets/Scripts/Squad.cs @@ -31,9 +31,14 @@ public class Squad : MonoBehaviour } #endregion + [SerializeField] private Formation formation = new Formation(); [SerializeField] private List soldiers = new List(); // soldiers belonging to the squad private Queue orders = new Queue(); // orders given to the squad + public List getSoldiers() + { + return soldiers; + } public void TempAddSoldierToSquad(Soldier soldier) { soldiers.Add(soldier); From a0c9226757843a0d08ae36fadc34435626d77848 Mon Sep 17 00:00:00 2001 From: Gabriel Ksawery Skowron-Rodriguez Date: Mon, 23 May 2022 17:37:04 +0200 Subject: [PATCH 4/4] Soldiers are placed on tile and spawed periodically (when possible) --- .../Scripts/Managers/SoldierSpawning.cs | 36 +++++++++++++++++++ .../Scripts/Managers/SoldierSpawning.cs.meta | 11 ++++++ .../Assets/Scripts/Managers/SquadManager.cs | 4 +++ .../Assets/Scripts/Managers/TilemapManager.cs | 8 ++--- theProject/Assets/Scripts/Squad.cs | 1 + 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 theProject/Assets/Scripts/Managers/SoldierSpawning.cs create mode 100644 theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta diff --git a/theProject/Assets/Scripts/Managers/SoldierSpawning.cs b/theProject/Assets/Scripts/Managers/SoldierSpawning.cs new file mode 100644 index 00000000..f8e43754 --- /dev/null +++ b/theProject/Assets/Scripts/Managers/SoldierSpawning.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +//[RequireComponent] +public class SoldierSpawning : MonoBehaviour +{ + [SerializeField] int spawnInterval = 1000; // ticks between spawning soldiers + [SerializeField] int lastSpawnTick = 0; + [SerializeField] Squad squad; + [SerializeField] TilemapManager tilemapManager; + [SerializeField] Vector2Int spawnCoords = Vector2Int.one * -1; + public Vector2Int GetSpawnCoords() { return spawnCoords; } + public void SetSpawnCoords(Vector2Int newSpawnCoords) { spawnCoords = newSpawnCoords; } + + private void Awake() + { + squad = GetComponent(); + TickSystem.OnTick += HandleTick; + tilemapManager = FindObjectOfType();//DEPENDENCY_INJECTION + } + + private void HandleTick(TickSystem.OnTickEventArgs tickEventArgs) + { + if (lastSpawnTick + spawnInterval > tickEventArgs.tickNumber) + return; + + Soldier spawnedSoldier = tilemapManager.SpawnSoldier(spawnCoords.x, spawnCoords.y, squad.GetOwnTeam() == Entity.Team.Ally) as Soldier; + if (spawnedSoldier == null) + return; + // managed to spawn the soldier + lastSpawnTick = tickEventArgs.tickNumber; + squad.AddSoldierToSquad(spawnedSoldier); + } +} diff --git a/theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta b/theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta new file mode 100644 index 00000000..7339a2df --- /dev/null +++ b/theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 46491169607191d40bf2df92f03ef895 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/theProject/Assets/Scripts/Managers/SquadManager.cs b/theProject/Assets/Scripts/Managers/SquadManager.cs index 45afd820..4550e4f3 100644 --- a/theProject/Assets/Scripts/Managers/SquadManager.cs +++ b/theProject/Assets/Scripts/Managers/SquadManager.cs @@ -8,12 +8,16 @@ public class SquadManager : MonoBehaviour [SerializeField] GameObject squadPrefab; Squad playerSquad; Squad enemySquad; + + Vector2Int playerSpawnCoords = Vector2Int.up; //TEMP SPAWN BY BASE private void Awake() { playerSquad = Instantiate(squadPrefab).GetComponent(); playerSquad.gameObject.name = "Player Squad"; playerSquad.transform.SetParent(transform); playerSquad.SetOwnTeam(Entity.Team.Ally); + playerSquad.gameObject.AddComponent(); + playerSquad.GetComponent().SetSpawnCoords(playerSpawnCoords); enemySquad = Instantiate(squadPrefab).GetComponent(); enemySquad.gameObject.name = "Enemy Squad"; diff --git a/theProject/Assets/Scripts/Managers/TilemapManager.cs b/theProject/Assets/Scripts/Managers/TilemapManager.cs index 3c1c2294..632b7a65 100644 --- a/theProject/Assets/Scripts/Managers/TilemapManager.cs +++ b/theProject/Assets/Scripts/Managers/TilemapManager.cs @@ -95,10 +95,10 @@ public class TilemapManager : MonoBehaviour // ---------- public functions - public bool SpawnSoldier(int x, int y, bool isAlly, bool isBase=false) + public Entity SpawnSoldier(int x, int y, bool isAlly, bool isBase=false) { if (GetTileState(x, y) != TileState.free) - return false; + return null; if (isBase) tiles[x, y].standingEntity = Instantiate(basePrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, Quaternion.identity).GetComponent(); @@ -106,7 +106,7 @@ public class TilemapManager : MonoBehaviour tiles[x, y].standingEntity = Instantiate(soldierPrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, Quaternion.identity).GetComponent(); if (tiles[x, y].standingEntity == null) - return false; + return null; if (isAlly) tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Ally); @@ -115,7 +115,7 @@ public class TilemapManager : MonoBehaviour tiles[x, y].standingEntity.SetTileCoords(new Vector2Int(x, y)); - return true; + return tiles[x, y].standingEntity; } public bool DespawnSoldier(int x, int y) diff --git a/theProject/Assets/Scripts/Squad.cs b/theProject/Assets/Scripts/Squad.cs index 42c7c5a2..172bbe6d 100644 --- a/theProject/Assets/Scripts/Squad.cs +++ b/theProject/Assets/Scripts/Squad.cs @@ -35,6 +35,7 @@ public class Squad : MonoBehaviour #endregion [SerializeField] private List soldiers = new List(); // soldiers belonging to the squad + public List GetSoldiers() { return soldiers; } private Queue orders = new Queue(); // orders given to the squad public void AddSoldierToSquad(Entity soldier)