LevelData
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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:
|
||||
Reference in New Issue
Block a user