LevelData

This commit is contained in:
2023-06-16 09:51:16 +03:00
parent 36f90ddd64
commit d9fbd99072
7 changed files with 100 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using Level;
public enum GameState
{
@@ -19,6 +20,9 @@ public class GameManager : MonoBehaviour
// Public
public GameState currentState { get; private set; } = GameState.Editing;
// Private
private LevelData currentLevelData;
// Properties
public static GameManager Instance => instance;
@@ -38,6 +42,7 @@ public class GameManager : MonoBehaviour
// Used by button OnClick
public void SetPlayState()
{
currentLevelData = LevelBuilder.Instance.CreateLevelData();
SetState(GameState.Gameplay);
}
@@ -45,11 +50,12 @@ public class GameManager : MonoBehaviour
// Use by button OnClick
public void SetEditState()
{
LevelBuilder.Instance.LoadLevelData(currentLevelData);
SetState(GameState.Editing);
}
public void SetState(GameState newState)
private void SetState(GameState newState)
{
currentState = newState;

View File

@@ -100,14 +100,12 @@ namespace Level
}
else if (placementOption == LevelBuilderButtons.PlacementOption.Crate)
{
cellData.cellContent = LayoutCellData.CellContent.Crate;
GameObject crateObject = GameObject.Instantiate(cratePrefab, worldPos, Quaternion.identity);
crate = crateObject.GetComponent<Crate>();
crate.layoutPosition = layoutPosition;
// This call handles updating cellData as well
PlaceCrate();
}
else if (placementOption == LevelBuilderButtons.PlacementOption.Player)
{
// This call handles updating cellData as well
Player.Instance.Teleport(this);
}
@@ -115,6 +113,16 @@ namespace Level
}
public void PlaceCrate()
{
cellData.cellContent = LayoutCellData.CellContent.Crate;
GameObject crateObject = GameObject.Instantiate(cratePrefab, worldPos, Quaternion.identity);
crate = crateObject.GetComponent<Crate>();
crate.layoutPosition = layoutPosition;
}
public void UpdateSprites()
{
if (cellData.cellContent == LayoutCellData.CellContent.Block)

View File

@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Objects;
namespace Level
{
@@ -34,7 +35,6 @@ namespace Level
// Properties
public static LevelBuilder Instance => instance;
// Methods
private void Awake()
{
@@ -89,7 +89,53 @@ namespace Level
}
public LayoutCell GetCellAt(Vector2Int layoutPosition)
public LevelData CreateLevelData()
{
LayoutCell.LayoutCellData[][] layoutData = new LayoutCell.LayoutCellData[height][];
for (int y = 0; y < height; y++)
{
layoutData[y] = new LayoutCell.LayoutCellData[width];
for (int x = 0; x < width; x++)
{
layoutData[y][x] = layout[y][x].cellData;
}
}
LevelData levelData = new LevelData();
levelData.layout = layoutData;
levelData.width = width;
levelData.height = height;
return levelData;
}
public void LoadLevelData(LevelData levelData)
{
CreateLayout(levelData.width, levelData.height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
LayoutCell cell = layout[y][x];
cell.cellData = levelData.layout[y][x];
cell.UpdateSprites();
if (cell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Crate)
{
cell.PlaceCrate();
}
else if (cell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Player)
{
Player.Instance.Teleport(cell);
}
}
}
}
public LayoutCell GetCellAtLayout(Vector2Int layoutPosition)
{
// Check that cell with these indices exists in array
// if (0 < layoutPosition.y < height && 0 < layoutPosition.x < width)
@@ -109,7 +155,7 @@ namespace Level
Vector3Int cellPos = grid.WorldToCell(worldPos);
Vector2Int layoutPosition = ((Vector2Int) (cellPos - firstCellPos));
return GetCellAt(layoutPosition);
return GetCellAtLayout(layoutPosition);
}

View File

@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Level
{
[System.Serializable]
public class LevelData
{
public LayoutCell.LayoutCellData[][] layout;
public int width;
public int height;
}
}

View File

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

View File

@@ -25,8 +25,8 @@ namespace Objects
public void Move(Vector2Int direction)
{
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAt(layoutPosition + direction);
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
if (targetCell != null && targetCell.IsFree)
{

View File

@@ -68,7 +68,7 @@ namespace Objects
public void Teleport(LayoutCell targetCell)
{
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
// Free previous cell
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
@@ -86,8 +86,8 @@ namespace Objects
public void Move(Vector2Int direction)
{
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAt(layoutPosition + direction);
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
if (targetCell != null && targetCell.IsFree)
{
@@ -114,7 +114,7 @@ namespace Objects
private void OnLevelInitialized()
{
gameObject.SetActive(true);
Teleport(LevelBuilder.Instance.GetCellAt(new Vector2Int(0, 0)));
Teleport(LevelBuilder.Instance.GetCellAtLayout(new Vector2Int(0, 0)));
}