Merge branch 'Development' into 'kuchy-formation'

made usage of adding/removing soldiers to squad easier

See merge request gskowron/eopsy-lab-567-project!16
This commit is contained in:
Krzysztof Stefan Rudnicki 2022-05-23 17:41:58 +02:00
commit 549ca3865f
11 changed files with 253 additions and 1094 deletions

View File

@ -26,9 +26,9 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 403095692180922766}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalScale: {x: 0.99, y: 0.99, z: 0.99}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 5390027776247701102}
@ -36,7 +36,7 @@ Transform:
- {fileID: 4966879261024258644}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!65 &2567001691605468863
BoxCollider:
m_ObjectHideFlags: 0

View File

@ -26,7 +26,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 403095692180922766}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -36,7 +36,7 @@ Transform:
- {fileID: 5994269345490125713}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!65 &2567001691605468863
BoxCollider:
m_ObjectHideFlags: 0

File diff suppressed because it is too large Load Diff

View 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);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 46491169607191d40bf2df92f03ef895
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,15 +8,21 @@ 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<Squad>();
playerSquad.gameObject.name = "Player Squad";
playerSquad.transform.SetParent(transform);
playerSquad.SetOwnTeam(Entity.Team.Ally);
playerSquad.gameObject.AddComponent<SoldierSpawning>();
playerSquad.GetComponent<SoldierSpawning>().SetSpawnCoords(playerSpawnCoords);
enemySquad = Instantiate(squadPrefab).GetComponent<Squad>();
enemySquad.gameObject.name = "Enemy Squad";
enemySquad.transform.SetParent(transform);
enemySquad.SetOwnTeam(Entity.Team.Enemy);
}
// Update is called once per frame
void Update()
@ -24,14 +30,20 @@ public class SquadManager : MonoBehaviour
Debug.Log("Added initial soldiers to squad");
// add all ally soldiers to squad
var soldiers = FindObjectsOfType<Soldier>();
foreach(var soldier in soldiers)
{
if(soldier.GetOwnTeam() == Soldier.Team.Ally)
{
playerSquad.TempAddSoldierToSquad(soldier);
}
}
var squads = new List<Squad>();
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);
}
}
enabled = false;
}
}
}

View File

@ -37,6 +37,7 @@ public class TilemapManager : MonoBehaviour
[SerializeField] private Tilemap tilemap = null;
[SerializeField] private GameObject soldierPrefab = null;
[SerializeField] private GameObject basePrefab = null;
[SerializeField] private TileBase[] tilesToDraw = null;
// private (do not edit) variables
@ -55,6 +56,14 @@ public class TilemapManager : MonoBehaviour
private void Start()
{
//draw tilemap
TileBase[] tilesToDrawTemp = new TileBase[mapSize.x * mapSize.y];
for (int i=0; i<tilesToDrawTemp.Length; i++)
{
tilesToDrawTemp[i] = tilesToDraw[Random.Range(0, tilesToDraw.Length)];
}
tilemap.SetTilesBlock(new BoundsInt(0, 0, 0, mapSize.x, mapSize.y, 1), tilesToDrawTemp);
//spawn bases
SpawnSoldier(allyBaseCoord.x, allyBaseCoord.y, true, true);
SpawnSoldier(enemyBaseCoord.x, enemyBaseCoord.y, false, true);
@ -95,18 +104,18 @@ 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<Base>();
tiles[x, y].standingEntity = Instantiate(basePrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, basePrefab.transform.rotation).GetComponent<Base>();
else
tiles[x, y].standingEntity = Instantiate(soldierPrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, Quaternion.identity).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)
return false;
return null;
if (isAlly)
tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Ally);
@ -115,7 +124,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)

View File

@ -35,10 +35,25 @@ public class PlayerClickSystem : MonoBehaviour
Camera camera = Camera.main;
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(mousePos);
RaycastHit2D hit2D = Physics2D.GetRayIntersection(ray);
Vector3 tileCoord = new Vector3(-1, -1, -1);
bool hitted = false;
if (Physics.Raycast(ray, out hit))
{
Vector3 hitWorldPosition = hit.point;
tileCoord = hit.point;
hitted = true;
}
else if (hit2D.collider != null)
{
tileCoord = hit2D.point;
hitted = true;
}
if (hitted)
{
Vector3 hitWorldPosition = tileCoord;
Debug.Log($"Hit at {hitWorldPosition}");
TilemapManager.Tile selectedTile;

View File

@ -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)
{
@ -30,18 +33,21 @@ public class Squad : MonoBehaviour
}
}
#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
public List<Soldier> getSoldiers()
{
return soldiers;
}
public void TempAddSoldierToSquad(Soldier soldier)
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()
@ -49,12 +55,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

View File

@ -1,20 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 13b75c95f34a00d4e8c04f76b73312e6, type: 3}
m_Name: New Animated Tile
m_EditorClassIdentifier:
m_AnimatedSprites: []
m_MinSpeed: 1
m_MaxSpeed: 1
m_AnimationStartTime: 0
m_AnimationStartFrame: 0
m_TileColliderType: 0

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 3b3905470e7d3cc4c9a6dae3aab82ab8
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant: