Sorry, long complicated question!
I’ve gone into CodeMonkey’s Xcom game prototype to see how the cover system was done there, and am in the process of bringing it into this tutorial project to play around with it here. However, the prototype is setup slightly differently to this project, and I have a floor system entirely absent from the prototype, so the process is not entirely straightforward.
I’ve got as far as the LevelGrid changes in Awake, where I need the levelGrid to run raycasts at each gridPosition to see if there’s a cover object on it, and store the coverType on the gridPosition.
Here’s the prototype code I’m trying to borrow:
private void Awake() {
Instance = this;
grid = new GridXZ<GridPosition>(30, 30, 2f, Vector3.zero, (GridXZ<GridPosition> g, int x, int y) => new GridPosition(g, x, y));
// Setup level obstacles
LevelGrid.Instance.GetWidthHeight(out int width, out int height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Vector3 worldPosition = LevelGrid.Instance.GetWorldPosition(x, y);
if (Physics.Raycast(worldPosition + Vector3.down * 5f, Vector3.up, out RaycastHit raycastHit, 10f, pathfindingObstaclesLayerMask)) {
// Something at this position, cover?
if (raycastHit.collider.TryGetComponent(out CoverObject coverObject)) {
grid.GetGridObject(x, y).SetCoverType(coverObject.GetCoverType());
}
}
}
}
}
grid
seems to translate to gridSystem
, with GridXY
translating to GridSystem
, if I’m understanding everything correctly. The GetWidthHeight()
I think is a combination of GetWidth
and GetHeight
on my LevelGrid script. The constructor is also written in a different way, but I think it works the same.
Here’s what I have at the moment:
private void Awake()
{
if (Instance != null)
{
Debug.LogError("There's more than one LevelGrid! " + transform + " - " + Instance);
Destroy(gameObject);
return;
}
Instance = this;
gridSystemList = new List<GridSystem<GridObject>>();
for (int floor = 0; floor < floorAmount; floor++)
{
GridSystem<GridObject> gridSystem = new GridSystem<GridObject>(width, height, cellSize, floor, FLOOR_HEIGHT,
(GridSystem<GridObject> g, GridPosition gridPosition) => new GridObject(g, gridPosition));
width = LevelGrid.Instance.GetWidth();
height = LevelGrid.Instance.GetHeight();
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
Vector3 worldPosition = LevelGrid.Instance.GetWorldPosition(x, z);
if (Physics.Raycast(worldPosition + Vector3.down * 1f, Vector3.up, out RaycastHit raycastHit, 10f, obstaclesLayerMask))
{
// Something at this position, cover?
if (raycastHit.collider.TryGetComponent(out CoverObject coverObject))
{
gridSystem.GetGridObject(x, z).SetCoverType(coverObject.GetCoverType());
}
}
}
}
gridSystemList.Add(gridSystem);
}
}
The only two errors I have remaining relate to LevelGrid.Instance.GetWorldPosition(x, z)
and gridSystem.GetGridObject(x, z)
. Both are error CS1501: “No overload for method (method) takes 2 arguments”. The script in CodeMonkey’s prototype does contain two arguments, but investigating the problem I realised the functions on the prototype work very differently, requiring two ints while mine require only a GridPosition, and filling it with gridPosition doesn’t work.
I wondered if an issue is the for (int x = 0; x < with; x++)
stuff, as I copied that over but I’m not 100% sure if my project can use it. The other thought I had was whether I was right to put most of the ocde inside for (int floor = 0; floor < floorAmount; floor++) {
. Regardless, I’m beginning to get to the limits of my understanding here.
Can anyone tell what’s wrong with this? Also any advice on how to reduce the code down a bit would be appreciated - it’s quite hard to read now with nested fors, ifs and brackets everywhere.