LevelData
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Level;
|
||||||
|
|
||||||
public enum GameState
|
public enum GameState
|
||||||
{
|
{
|
||||||
@@ -19,6 +20,9 @@ public class GameManager : MonoBehaviour
|
|||||||
// Public
|
// Public
|
||||||
public GameState currentState { get; private set; } = GameState.Editing;
|
public GameState currentState { get; private set; } = GameState.Editing;
|
||||||
|
|
||||||
|
// Private
|
||||||
|
private LevelData currentLevelData;
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
public static GameManager Instance => instance;
|
public static GameManager Instance => instance;
|
||||||
|
|
||||||
@@ -38,6 +42,7 @@ public class GameManager : MonoBehaviour
|
|||||||
// Used by button OnClick
|
// Used by button OnClick
|
||||||
public void SetPlayState()
|
public void SetPlayState()
|
||||||
{
|
{
|
||||||
|
currentLevelData = LevelBuilder.Instance.CreateLevelData();
|
||||||
SetState(GameState.Gameplay);
|
SetState(GameState.Gameplay);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,11 +50,12 @@ public class GameManager : MonoBehaviour
|
|||||||
// Use by button OnClick
|
// Use by button OnClick
|
||||||
public void SetEditState()
|
public void SetEditState()
|
||||||
{
|
{
|
||||||
|
LevelBuilder.Instance.LoadLevelData(currentLevelData);
|
||||||
SetState(GameState.Editing);
|
SetState(GameState.Editing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void SetState(GameState newState)
|
private void SetState(GameState newState)
|
||||||
{
|
{
|
||||||
currentState = newState;
|
currentState = newState;
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,21 @@ namespace Level
|
|||||||
cellData.isTarget = true;
|
cellData.isTarget = true;
|
||||||
}
|
}
|
||||||
else if (placementOption == LevelBuilderButtons.PlacementOption.Crate)
|
else if (placementOption == LevelBuilderButtons.PlacementOption.Crate)
|
||||||
|
{
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateSprites();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void PlaceCrate()
|
||||||
{
|
{
|
||||||
cellData.cellContent = LayoutCellData.CellContent.Crate;
|
cellData.cellContent = LayoutCellData.CellContent.Crate;
|
||||||
|
|
||||||
@@ -106,13 +121,6 @@ namespace Level
|
|||||||
crate = crateObject.GetComponent<Crate>();
|
crate = crateObject.GetComponent<Crate>();
|
||||||
crate.layoutPosition = layoutPosition;
|
crate.layoutPosition = layoutPosition;
|
||||||
}
|
}
|
||||||
else if (placementOption == LevelBuilderButtons.PlacementOption.Player)
|
|
||||||
{
|
|
||||||
Player.Instance.Teleport(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateSprites();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void UpdateSprites()
|
public void UpdateSprites()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Objects;
|
||||||
|
|
||||||
namespace Level
|
namespace Level
|
||||||
{
|
{
|
||||||
@@ -34,7 +35,6 @@ namespace Level
|
|||||||
// Properties
|
// Properties
|
||||||
public static LevelBuilder Instance => instance;
|
public static LevelBuilder Instance => instance;
|
||||||
|
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
private void Awake()
|
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
|
// Check that cell with these indices exists in array
|
||||||
// if (0 < layoutPosition.y < height && 0 < layoutPosition.x < width)
|
// if (0 < layoutPosition.y < height && 0 < layoutPosition.x < width)
|
||||||
@@ -109,7 +155,7 @@ namespace Level
|
|||||||
Vector3Int cellPos = grid.WorldToCell(worldPos);
|
Vector3Int cellPos = grid.WorldToCell(worldPos);
|
||||||
Vector2Int layoutPosition = ((Vector2Int) (cellPos - firstCellPos));
|
Vector2Int layoutPosition = ((Vector2Int) (cellPos - firstCellPos));
|
||||||
|
|
||||||
return GetCellAt(layoutPosition);
|
return GetCellAtLayout(layoutPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
14
Assets/Scripts/Level/LevelData.cs
Normal file
14
Assets/Scripts/Level/LevelData.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/Level/LevelData.cs.meta
Normal file
11
Assets/Scripts/Level/LevelData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: beaee564e821eb4469ad8aa252905b23
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -25,8 +25,8 @@ namespace Objects
|
|||||||
|
|
||||||
public void Move(Vector2Int direction)
|
public void Move(Vector2Int direction)
|
||||||
{
|
{
|
||||||
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
|
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
|
||||||
LayoutCell targetCell = LevelBuilder.Instance.GetCellAt(layoutPosition + direction);
|
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
|
||||||
|
|
||||||
if (targetCell != null && targetCell.IsFree)
|
if (targetCell != null && targetCell.IsFree)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ namespace Objects
|
|||||||
|
|
||||||
public void Teleport(LayoutCell targetCell)
|
public void Teleport(LayoutCell targetCell)
|
||||||
{
|
{
|
||||||
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
|
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
|
||||||
|
|
||||||
// Free previous cell
|
// Free previous cell
|
||||||
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
|
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
|
||||||
@@ -86,8 +86,8 @@ namespace Objects
|
|||||||
public void Move(Vector2Int direction)
|
public void Move(Vector2Int direction)
|
||||||
{
|
{
|
||||||
|
|
||||||
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
|
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
|
||||||
LayoutCell targetCell = LevelBuilder.Instance.GetCellAt(layoutPosition + direction);
|
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
|
||||||
|
|
||||||
if (targetCell != null && targetCell.IsFree)
|
if (targetCell != null && targetCell.IsFree)
|
||||||
{
|
{
|
||||||
@@ -114,7 +114,7 @@ namespace Objects
|
|||||||
private void OnLevelInitialized()
|
private void OnLevelInitialized()
|
||||||
{
|
{
|
||||||
gameObject.SetActive(true);
|
gameObject.SetActive(true);
|
||||||
Teleport(LevelBuilder.Instance.GetCellAt(new Vector2Int(0, 0)));
|
Teleport(LevelBuilder.Instance.GetCellAtLayout(new Vector2Int(0, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user