The power of friendship

3 engingeers, 1 computer, like 5 bugs squashed.
SoldierCount++
This commit is contained in:
Maciekxdabu 2022-05-23 19:07:14 +02:00
parent ca123b397c
commit be14e2b26f
5 changed files with 57 additions and 31 deletions

View File

@ -10,6 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 6141901885681798079}
- component: {fileID: 6141901885681798078}
- component: {fileID: 8212255995933922627}
m_Layer: 0
m_Name: Squad
m_TagString: Untagged
@ -44,4 +45,18 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: eeef7310a48872043b4089979ec88b42, type: 3}
m_Name:
m_EditorClassIdentifier:
formation: {fileID: 0}
soldiers: []
--- !u!114 &8212255995933922627
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6141901885681798073}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5175e17e18d002001bf1ebde5de11b49, type: 3}
m_Name:
m_EditorClassIdentifier:
squad: {fileID: 0}

View File

@ -9,23 +9,25 @@ public class Formation : MonoBehaviour
void Awake()
{
this.squad = squad.GetComponent<Squad>();
squad = GetComponent<Squad>();
}
public Dictionary<Entity, Vector2Int> calculatePositions(Vector2Int coordinates)
public Dictionary<Entity, Vector2Int> CalculatePositions(Vector2Int coordinates)
{
List<Entity> soldiers = this.squad.GetSoldiers();
Dictionary<Entity, Vector2Int> soldierNewCoordinate = new Dictionary<Entity, Vector2Int>();
List<Entity> soldiers = squad.GetSoldiers();
Dictionary<Entity, Vector2Int> soldiersNewCoordinates = new Dictionary<Entity, Vector2Int>();
int soldierNumber = 0;
foreach (Entity Entity in soldiers)
{
soldierNewCoordinate.Add(Entity, calculateSoldierCoordinates(soldierNumber, coordinates));
soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates));
soldierNumber++;
}
return soldierNewCoordinate;
return soldiersNewCoordinates;
}
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier
private Vector2Int calculateSoldierCoordinates(in int soldierNumber, in Vector2Int coordinates)
private Vector2Int CalculateSoldierCoordinates(in int soldierNumber, in Vector2Int coordinates)
{
// Horizontal line we change x
Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y);

View File

@ -17,7 +17,8 @@ public class SquadManager : MonoBehaviour
playerSquad.transform.SetParent(transform);
playerSquad.SetOwnTeam(Entity.Team.Ally);
playerSquad.gameObject.AddComponent<SoldierSpawning>();
playerSquad.GetComponent<SoldierSpawning>().SetSpawnCoords(playerSpawnCoords);
playerSquad.GetComponent<SoldierSpawning>().SetSpawnCoords(playerSpawnCoords);//DEPENDENCY_INJECTION
FindObjectOfType<PlayerClickSystem>().SetPlayerSquad(playerSquad);//DEPENDENCY_INJECTION
enemySquad = Instantiate(squadPrefab).GetComponent<Squad>();
enemySquad.gameObject.name = "Enemy Squad";

View File

@ -10,6 +10,9 @@ public class PlayerClickSystem : MonoBehaviour
private bool leftMouseClicked = false;
private Vector2 mousePos = Vector2.zero;
[SerializeField] Squad playerSquad;
public void SetPlayerSquad(Squad newSquad) { playerSquad = newSquad; } // called by Squad Manager //DEPENDENCY_INJECTION
private void Awake()
{
bInput = new BetterInput();
@ -67,7 +70,9 @@ public class PlayerClickSystem : MonoBehaviour
}
// a tile was hit
Debug.Log($"Tile {x},{y} was selected - DO MOVEMENT ORDER");
playerSquad.EnqueueOrder(new Squad.MovementOrder(new Vector2Int(x,y)));
}
}
}

View File

@ -11,29 +11,37 @@ public class Squad : MonoBehaviour
#region Orders
public abstract class Order // generic order (to keep in queue)
{
public virtual void PassToSoldier(Soldier targetSoldier) // "translate" the order to the soldier
public virtual void Execute(Squad squad) // "translate" the order to the soldier
{// depending on implementation, for example call soldier's method to execute/plan this task
Debug.LogWarning($"Generic order passing not overriden\nSoldier {targetSoldier.name} received generic order");
Debug.LogWarning($"Generic order execution not overriden\n All soldiers received generic order");
}
}
public class MovementOrder : Order // example how to add new types of orders
{
public MovementOrder(int x, int y)
public MovementOrder(Vector2Int movementTargetCoords)
{
this.x = x;
this.y = y;
targetCoords = movementTargetCoords;
}
public readonly int x;
public readonly int y;
public override void PassToSoldier(Soldier targetSoldier)
public readonly Vector2Int targetCoords;
public override void Execute(Squad squad)
{// here we would set soldier's target position for example
targetSoldier.HandleMovementOrder(new Vector2Int(x, y));
Debug.Log($"Soldier {targetSoldier.name} received movement order towards coordinates {x},{y}");
Debug.Log(squad);
Dictionary<Entity, Vector2Int> targetCoordsDictionary = squad.
GetFormation().
CalculatePositions(
targetCoords);
foreach (Soldier soldier in targetCoordsDictionary.Keys)
{
Vector2Int targetPositionForSoldier = targetCoordsDictionary[soldier];
soldier.HandleMovementOrder(targetPositionForSoldier);
Debug.Log($"Soldier {soldier.name} received movement order towards coordinates {targetPositionForSoldier.x},{targetPositionForSoldier.y}");
}
}
}
#endregion
[SerializeField] private Formation formation = new Formation();
[SerializeField] private Formation formation;
public Formation GetFormation() { return formation; }
[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
public List<Entity> GetSoldiers() { return soldiers; }
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
@ -53,6 +61,7 @@ public class Squad : MonoBehaviour
private void Awake()
{
TickSystem.OnTick += HandleTick;
formation = GetComponent<Formation>();
}
private void OnDestroy()
@ -67,17 +76,11 @@ public class Squad : MonoBehaviour
Order currentOrder = orders.Dequeue();
Debug.Log($"Passing order {currentOrder.ToString()} on tick #{eventArgs.tickNumber}");
foreach (Soldier soldier in soldiers)
{
currentOrder.PassToSoldier(soldier);
}
currentOrder.Execute(this);
}
[ContextMenu("DEBUG ADD PSEUDO MOVEMENT ORDER")]
public void DebugAddMovementOrder()
{
int targetX = 4;
int targetY = 2;
orders.Enqueue(new MovementOrder(targetX, targetY));
}
public void EnqueueOrder(Order orderToEnqueue)
{
orders.Enqueue(orderToEnqueue);
}
}