mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 21:23:13 +02:00
The power of friendship
3 engingeers, 1 computer, like 5 bugs squashed. SoldierCount++
This commit is contained in:
parent
ca123b397c
commit
be14e2b26f
@ -10,6 +10,7 @@ GameObject:
|
|||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 6141901885681798079}
|
- component: {fileID: 6141901885681798079}
|
||||||
- component: {fileID: 6141901885681798078}
|
- component: {fileID: 6141901885681798078}
|
||||||
|
- component: {fileID: 8212255995933922627}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Squad
|
m_Name: Squad
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
@ -44,4 +45,18 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: eeef7310a48872043b4089979ec88b42, type: 3}
|
m_Script: {fileID: 11500000, guid: eeef7310a48872043b4089979ec88b42, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
formation: {fileID: 0}
|
||||||
soldiers: []
|
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}
|
||||||
|
|||||||
@ -9,23 +9,25 @@ public class Formation : MonoBehaviour
|
|||||||
|
|
||||||
void Awake()
|
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;
|
int soldierNumber = 0;
|
||||||
foreach (Entity Entity in soldiers)
|
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
|
// 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
|
// Horizontal line we change x
|
||||||
Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y);
|
Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y);
|
||||||
|
|||||||
@ -17,7 +17,8 @@ public class SquadManager : MonoBehaviour
|
|||||||
playerSquad.transform.SetParent(transform);
|
playerSquad.transform.SetParent(transform);
|
||||||
playerSquad.SetOwnTeam(Entity.Team.Ally);
|
playerSquad.SetOwnTeam(Entity.Team.Ally);
|
||||||
playerSquad.gameObject.AddComponent<SoldierSpawning>();
|
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 = Instantiate(squadPrefab).GetComponent<Squad>();
|
||||||
enemySquad.gameObject.name = "Enemy Squad";
|
enemySquad.gameObject.name = "Enemy Squad";
|
||||||
|
|||||||
@ -10,6 +10,9 @@ public class PlayerClickSystem : MonoBehaviour
|
|||||||
private bool leftMouseClicked = false;
|
private bool leftMouseClicked = false;
|
||||||
private Vector2 mousePos = Vector2.zero;
|
private Vector2 mousePos = Vector2.zero;
|
||||||
|
|
||||||
|
[SerializeField] Squad playerSquad;
|
||||||
|
public void SetPlayerSquad(Squad newSquad) { playerSquad = newSquad; } // called by Squad Manager //DEPENDENCY_INJECTION
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
bInput = new BetterInput();
|
bInput = new BetterInput();
|
||||||
@ -67,6 +70,8 @@ public class PlayerClickSystem : MonoBehaviour
|
|||||||
}
|
}
|
||||||
// a tile was hit
|
// a tile was hit
|
||||||
Debug.Log($"Tile {x},{y} was selected - DO MOVEMENT ORDER");
|
Debug.Log($"Tile {x},{y} was selected - DO MOVEMENT ORDER");
|
||||||
|
playerSquad.EnqueueOrder(new Squad.MovementOrder(new Vector2Int(x,y)));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,29 +11,37 @@ public class Squad : MonoBehaviour
|
|||||||
#region Orders
|
#region Orders
|
||||||
public abstract class Order // generic order (to keep in queue)
|
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
|
{// 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 class MovementOrder : Order // example how to add new types of orders
|
||||||
{
|
{
|
||||||
public MovementOrder(int x, int y)
|
public MovementOrder(Vector2Int movementTargetCoords)
|
||||||
{
|
{
|
||||||
this.x = x;
|
targetCoords = movementTargetCoords;
|
||||||
this.y = y;
|
|
||||||
}
|
}
|
||||||
public readonly int x;
|
public readonly Vector2Int targetCoords;
|
||||||
public readonly int y;
|
public override void Execute(Squad squad)
|
||||||
public override void PassToSoldier(Soldier targetSoldier)
|
|
||||||
{// here we would set soldier's target position for example
|
{// here we would set soldier's target position for example
|
||||||
targetSoldier.HandleMovementOrder(new Vector2Int(x, y));
|
Debug.Log(squad);
|
||||||
Debug.Log($"Soldier {targetSoldier.name} received movement order towards coordinates {x},{y}");
|
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
|
#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
|
[SerializeField] private List<Entity> soldiers = new List<Entity>(); // soldiers belonging to the squad
|
||||||
public List<Entity> GetSoldiers() { return soldiers; }
|
public List<Entity> GetSoldiers() { return soldiers; }
|
||||||
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
|
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
|
||||||
@ -53,6 +61,7 @@ public class Squad : MonoBehaviour
|
|||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
TickSystem.OnTick += HandleTick;
|
TickSystem.OnTick += HandleTick;
|
||||||
|
formation = GetComponent<Formation>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
private void OnDestroy()
|
||||||
@ -67,17 +76,11 @@ public class Squad : MonoBehaviour
|
|||||||
|
|
||||||
Order currentOrder = orders.Dequeue();
|
Order currentOrder = orders.Dequeue();
|
||||||
Debug.Log($"Passing order {currentOrder.ToString()} on tick #{eventArgs.tickNumber}");
|
Debug.Log($"Passing order {currentOrder.ToString()} on tick #{eventArgs.tickNumber}");
|
||||||
foreach (Soldier soldier in soldiers)
|
currentOrder.Execute(this);
|
||||||
{
|
|
||||||
currentOrder.PassToSoldier(soldier);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[ContextMenu("DEBUG ADD PSEUDO MOVEMENT ORDER")]
|
public void EnqueueOrder(Order orderToEnqueue)
|
||||||
public void DebugAddMovementOrder()
|
{
|
||||||
{
|
orders.Enqueue(orderToEnqueue);
|
||||||
int targetX = 4;
|
}
|
||||||
int targetY = 2;
|
|
||||||
orders.Enqueue(new MovementOrder(targetX, targetY));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user