WUT_Computer_Science/theProject/Assets/Scripts/Managers/TilemapManager.cs

196 lines
5.9 KiB
C#
Raw Normal View History

2022-05-16 14:32:01 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TilemapManager : MonoBehaviour
{
public enum TileState
2022-05-16 14:32:01 +02:00
{
free,
taken,
outOfBounds
}
public struct Tile
{
public Entity standingEntity;
2022-05-16 14:32:01 +02:00
}
[Header("Common Values")]
[SerializeField] private Vector2Int mapSize = new Vector2Int(5,5);
[SerializeField] private Vector3 WORLD_SPACE_OFFSET = new Vector3(0.5f, 1f, 0.5f);
2022-05-16 14:32:01 +02:00
[Header("Soldiers values")]
2022-05-23 12:55:42 +02:00
[SerializeField] private Vector2Int allyBaseCoord = Vector2Int.zero;
2022-05-17 22:22:04 +02:00
[SerializeField] private Vector2Int[] soldierStartingPositions = null;
2022-05-16 14:32:01 +02:00
[Header("Enemies values")]
2022-05-23 12:55:42 +02:00
[SerializeField] private Vector2Int enemyBaseCoord = Vector2Int.zero;
2022-05-17 22:22:04 +02:00
[SerializeField] private Vector2Int[] enemyStartingPositions = null;
2022-05-16 14:32:01 +02:00
[Header("References")]
[SerializeField] private Tilemap tilemap = null;
[SerializeField] private GameObject soldierPrefab = null;
2022-05-23 12:55:42 +02:00
[SerializeField] private GameObject basePrefab = null;
2022-05-16 14:32:01 +02:00
// private (do not edit) variables
2022-05-23 13:48:02 +02:00
private static TilemapManager ins;
2022-05-16 14:32:01 +02:00
private Tile[,] tiles = null;
// ---------- Unity messages
private void Awake()
{
2022-05-23 13:48:02 +02:00
ins = this;
2022-05-16 14:32:01 +02:00
tiles = new Tile[mapSize.x, mapSize.y];
}
private void Start()
{
2022-05-23 12:55:42 +02:00
//spawn bases
SpawnSoldier(allyBaseCoord.x, allyBaseCoord.y, true, true);
SpawnSoldier(enemyBaseCoord.x, enemyBaseCoord.y, false, true);
//spawn soldiers
2022-05-17 22:22:04 +02:00
foreach (Vector2Int vec in soldierStartingPositions)
SpawnSoldier(vec.x, vec.y, true);
foreach (Vector2Int vec in enemyStartingPositions)
SpawnSoldier(vec.x, vec.y, false);
2022-05-16 14:32:01 +02:00
}
private void OnValidate()
{
2022-05-17 22:22:04 +02:00
for (int i=0; i< soldierStartingPositions.Length; i++)
{
if (soldierStartingPositions[i].x < 0)
soldierStartingPositions[i].x = 0;
if (soldierStartingPositions[i].y < 0)
soldierStartingPositions[i].y = 0;
if (soldierStartingPositions[i].x >= mapSize.x)
soldierStartingPositions[i].x = mapSize.x - 1;
if (soldierStartingPositions[i].y >= mapSize.y)
soldierStartingPositions[i].y = mapSize.y - 1;
}
for (int i = 0; i < enemyStartingPositions.Length; i++)
{
if (enemyStartingPositions[i].x < 0)
enemyStartingPositions[i].x = 0;
if (enemyStartingPositions[i].y < 0)
enemyStartingPositions[i].y = 0;
if (enemyStartingPositions[i].x >= mapSize.x)
enemyStartingPositions[i].x = mapSize.x - 1;
if (enemyStartingPositions[i].y >= mapSize.y)
enemyStartingPositions[i].y = mapSize.y - 1;
}
2022-05-16 14:32:01 +02:00
}
// ---------- public functions
2022-05-23 12:55:42 +02:00
public bool SpawnSoldier(int x, int y, bool isAlly, bool isBase=false)
2022-05-16 14:32:01 +02:00
{
if (GetTileState(x, y) != TileState.free)
return false;
2022-05-23 12:55:42 +02:00
if (isBase)
tiles[x, y].standingEntity = Instantiate(basePrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, Quaternion.identity).GetComponent<Base>();
2022-05-23 13:48:02 +02:00
else
tiles[x, y].standingEntity = Instantiate(soldierPrefab, tilemap.CellToWorld(new Vector3Int(x, y, 0)) + WORLD_SPACE_OFFSET, Quaternion.identity).GetComponent<Soldier>();
2022-05-16 14:32:01 +02:00
2022-05-23 14:30:01 +02:00
if (tiles[x, y].standingEntity == null)
return false;
2022-05-16 15:09:43 +02:00
if (isAlly)
tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Ally);
2022-05-16 15:09:43 +02:00
else
tiles[x, y].standingEntity.SetOwnTeam(Base.Team.Enemy);
2022-05-16 15:09:43 +02:00
tiles[x, y].standingEntity.SetTileCoords(new Vector2Int(x, y));
2022-05-23 13:48:02 +02:00
return true;
2022-05-16 14:32:01 +02:00
}
public bool DespawnSoldier(int x, int y)
{
if (GetTileState(x, y) != TileState.taken)
return false;
Destroy(tiles[x, y].standingEntity.gameObject);
tiles[x, y].standingEntity = null;
2022-05-16 14:32:01 +02:00
Debug.Log("Despaned a soldier");
return true;
}
public Entity GetSoldier(int x, int y)
2022-05-16 14:32:01 +02:00
{
if (GetTileState(x, y) != TileState.taken)
return null;
return tiles[x,y].standingEntity;
2022-05-16 14:32:01 +02:00
}
public bool MoveEntity(int x1, int y1, int x2, int y2) // old MoveSoldier
2022-05-16 14:32:01 +02:00
{
if (GetTileState(x1, y1) == TileState.taken && GetTileState(x2, y2) == TileState.free)
{
2022-05-23 13:48:02 +02:00
// change proper values
tiles[x2, y2].standingEntity = tiles[x1, y1].standingEntity;
tiles[x1, y1].standingEntity = null;
tiles[x2, y2].standingEntity.SetTileCoords(new Vector2Int(x2, y2));
2022-05-16 14:32:01 +02:00
2022-05-23 13:48:02 +02:00
// change Soldier world position
tiles[x2, y2].standingEntity.transform.position = tilemap.CellToWorld(new Vector3Int(x2, y2, 0)) + WORLD_SPACE_OFFSET;
2022-05-16 14:32:01 +02:00
return true;
}
return false;
}
2022-05-23 13:48:02 +02:00
// ---------- public statics methods
public static bool MoveSoldierS(int x1, int y1, int x2, int y2)
{
2022-05-23 14:30:01 +02:00
return ins.MoveEntity(x1, y1, x2, y2);
2022-05-23 13:48:02 +02:00
}
2022-05-16 14:32:01 +02:00
// ---------- private methods
private TileState GetTileState(int x, int y)
{
if (x < 0 || y < 0 || x >= mapSize.x || y >= mapSize.y)
return TileState.outOfBounds;
if (tiles[x, y].standingEntity == null)
2022-05-16 14:32:01 +02:00
return TileState.free;
return TileState.taken;
}
public TileState GetTileFromWorldCoords(Vector3 worldCoords, out Tile selectedTile, out int x, out int y)
{
Vector3Int tilemapCoords = tilemap.WorldToCell(worldCoords);
TileState tileState = GetTileState(tilemapCoords.x, tilemapCoords.y);
x = tilemapCoords.x;
y = tilemapCoords.y;
if (tileState == TileState.outOfBounds)
{
selectedTile = new Tile();
return TileState.outOfBounds;
}
// valid tile selected
selectedTile = tiles[tilemapCoords.x, tilemapCoords.y];
return tileState;
}
2022-05-16 14:32:01 +02:00
}