mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 12:43:16 +02:00
Merge branch 'Development' of https://gitlab-stud.elka.pw.edu.pl/gskowron/eopsy-lab-567-project into Development
This commit is contained in:
commit
32b28cfdc6
36
theProject/Assets/Scripts/Formation.cs
Normal file
36
theProject/Assets/Scripts/Formation.cs
Normal file
@ -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<Soldier, Vector2Int> calculatePositions(Vector2int coordinates)
|
||||||
|
{
|
||||||
|
List<Soldier> soldiers = this.squad.getSoldiers();
|
||||||
|
Dictionary<Soldier, Vector2Int> soldierNewCoordinate = new Dictionary<Soldier, Vector2Int>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
11
theProject/Assets/Scripts/Formation.cs.meta
Normal file
11
theProject/Assets/Scripts/Formation.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5175e17e18d002001bf1ebde5de11b49
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
36
theProject/Assets/Scripts/Managers/SoldierSpawning.cs
Normal file
36
theProject/Assets/Scripts/Managers/SoldierSpawning.cs
Normal file
@ -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<Squad>();
|
||||||
|
TickSystem.OnTick += HandleTick;
|
||||||
|
tilemapManager = FindObjectOfType<TilemapManager>();//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);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta
Normal file
11
theProject/Assets/Scripts/Managers/SoldierSpawning.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 46491169607191d40bf2df92f03ef895
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -8,15 +8,21 @@ public class SquadManager : MonoBehaviour
|
|||||||
[SerializeField] GameObject squadPrefab;
|
[SerializeField] GameObject squadPrefab;
|
||||||
Squad playerSquad;
|
Squad playerSquad;
|
||||||
Squad enemySquad;
|
Squad enemySquad;
|
||||||
|
|
||||||
|
Vector2Int playerSpawnCoords = Vector2Int.up; //TEMP SPAWN BY BASE
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
playerSquad = Instantiate(squadPrefab).GetComponent<Squad>();
|
playerSquad = Instantiate(squadPrefab).GetComponent<Squad>();
|
||||||
playerSquad.gameObject.name = "Player Squad";
|
playerSquad.gameObject.name = "Player Squad";
|
||||||
playerSquad.transform.SetParent(transform);
|
playerSquad.transform.SetParent(transform);
|
||||||
|
playerSquad.SetOwnTeam(Entity.Team.Ally);
|
||||||
|
playerSquad.gameObject.AddComponent<SoldierSpawning>();
|
||||||
|
playerSquad.GetComponent<SoldierSpawning>().SetSpawnCoords(playerSpawnCoords);
|
||||||
|
|
||||||
enemySquad = Instantiate(squadPrefab).GetComponent<Squad>();
|
enemySquad = Instantiate(squadPrefab).GetComponent<Squad>();
|
||||||
enemySquad.gameObject.name = "Enemy Squad";
|
enemySquad.gameObject.name = "Enemy Squad";
|
||||||
enemySquad.transform.SetParent(transform);
|
enemySquad.transform.SetParent(transform);
|
||||||
|
enemySquad.SetOwnTeam(Entity.Team.Enemy);
|
||||||
}
|
}
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
@ -24,14 +30,20 @@ public class SquadManager : MonoBehaviour
|
|||||||
Debug.Log("Added initial soldiers to squad");
|
Debug.Log("Added initial soldiers to squad");
|
||||||
// add all ally soldiers to squad
|
// add all ally soldiers to squad
|
||||||
var soldiers = FindObjectsOfType<Soldier>();
|
var soldiers = FindObjectsOfType<Soldier>();
|
||||||
foreach(var soldier in soldiers)
|
var squads = new List<Squad>();
|
||||||
{
|
squads.Add(playerSquad);
|
||||||
if(soldier.GetOwnTeam() == Soldier.Team.Ally)
|
squads.Add(enemySquad);
|
||||||
{
|
|
||||||
playerSquad.TempAddSoldierToSquad(soldier);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enabled = false;
|
foreach (var soldier in soldiers)
|
||||||
|
{
|
||||||
|
foreach (var squad in squads)
|
||||||
|
{
|
||||||
|
if (soldier.GetOwnTeam() == squad.GetOwnTeam())
|
||||||
|
{
|
||||||
|
squad.AddSoldierToSquad(soldier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -104,10 +104,10 @@ public class TilemapManager : MonoBehaviour
|
|||||||
|
|
||||||
// ---------- public functions
|
// ---------- 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)
|
if (GetTileState(x, y) != TileState.free)
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
if (isBase)
|
if (isBase)
|
||||||
tiles[x, y].standingEntity = Instantiate(basePrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, basePrefab.transform.rotation).GetComponent<Base>();
|
tiles[x, y].standingEntity = Instantiate(basePrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, basePrefab.transform.rotation).GetComponent<Base>();
|
||||||
@ -115,7 +115,7 @@ public class TilemapManager : MonoBehaviour
|
|||||||
tiles[x, y].standingEntity = Instantiate(soldierPrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, soldierPrefab.transform.rotation).GetComponent<Soldier>();
|
tiles[x, y].standingEntity = Instantiate(soldierPrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, soldierPrefab.transform.rotation).GetComponent<Soldier>();
|
||||||
|
|
||||||
if (tiles[x, y].standingEntity == null)
|
if (tiles[x, y].standingEntity == null)
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
if (isAlly)
|
if (isAlly)
|
||||||
tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Ally);
|
tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Ally);
|
||||||
@ -124,7 +124,7 @@ public class TilemapManager : MonoBehaviour
|
|||||||
|
|
||||||
tiles[x, y].standingEntity.SetTileCoords(new Vector2Int(x, y));
|
tiles[x, y].standingEntity.SetTileCoords(new Vector2Int(x, y));
|
||||||
|
|
||||||
return true;
|
return tiles[x, y].standingEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool DespawnSoldier(int x, int y)
|
public bool DespawnSoldier(int x, int y)
|
||||||
|
|||||||
@ -5,6 +5,9 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class Squad : MonoBehaviour
|
public class Squad : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
private Entity.Team ownTeam;
|
||||||
|
public Entity.Team GetOwnTeam() { return ownTeam; }
|
||||||
|
public void SetOwnTeam(Entity.Team newTeam) {ownTeam = newTeam; }
|
||||||
#region Orders
|
#region Orders
|
||||||
public abstract class Order // generic order (to keep in queue)
|
public abstract class Order // generic order (to keep in queue)
|
||||||
{
|
{
|
||||||
@ -30,13 +33,21 @@ public class Squad : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
[SerializeField] private Formation formation = new Formation();
|
||||||
[SerializeField] private List<Soldier> soldiers = new List<Soldier>(); // soldiers belonging to the squad
|
[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
|
private Queue<Order> orders = new Queue<Order>(); // orders given to the squad
|
||||||
|
|
||||||
public void TempAddSoldierToSquad(Soldier soldier)
|
public void AddSoldierToSquad(Entity soldier)
|
||||||
{
|
{
|
||||||
soldiers.Add(soldier);
|
soldiers.Add(soldier);
|
||||||
|
soldier.OnDeath.AddListener(RemoveSoldierFromSquad);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveSoldierFromSquad(Entity soldier)
|
||||||
|
{
|
||||||
|
soldiers.Remove(soldier);
|
||||||
|
soldier.OnDeath.RemoveListener(RemoveSoldierFromSquad);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
@ -44,12 +55,12 @@ public class Squad : MonoBehaviour
|
|||||||
TickSystem.OnTick += HandleTick;
|
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
|
{// pass a single order to all soldiers
|
||||||
if (orders.Count < 1)
|
if (orders.Count < 1)
|
||||||
return; // for now nothing to do here
|
return; // for now nothing to do here
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user