Gameplay-Edit
This commit is contained in:
142
Assets/Scripts/Level/LayoutCell.cs
Normal file
142
Assets/Scripts/Level/LayoutCell.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Objects;
|
||||
|
||||
namespace Level
|
||||
{
|
||||
public class LayoutCell
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct LayoutCellData
|
||||
{
|
||||
public enum CellContent
|
||||
{
|
||||
None,
|
||||
Block,
|
||||
Player,
|
||||
Crate
|
||||
}
|
||||
public CellContent cellContent;
|
||||
public bool isTarget;
|
||||
|
||||
public Vector3Int cellPos;
|
||||
}
|
||||
|
||||
// Public
|
||||
public LayoutCellData cellData;
|
||||
public Vector3 worldPos;
|
||||
public Crate crate;
|
||||
public Vector2Int layoutPosition;
|
||||
|
||||
// Private
|
||||
private SpriteRenderer bgSpriteRenderer;
|
||||
private SpriteRenderer envSpriteRenderer;
|
||||
|
||||
// Properties
|
||||
public bool IsFree
|
||||
{
|
||||
get
|
||||
{
|
||||
return cellData.cellContent == LayoutCellData.CellContent.None;
|
||||
}
|
||||
}
|
||||
|
||||
private Grid grid => LevelBuilder.Instance.grid;
|
||||
private Sprite blockSprite => LevelBuilder.Instance.blockSprite;
|
||||
private Sprite environmentSprite => LevelBuilder.Instance.environmentSprite;
|
||||
private Sprite groundSprite => LevelBuilder.Instance.groundSprite;
|
||||
private GameObject spritePrefab => LevelBuilder.Instance.spritePrefab;
|
||||
private GameObject cratePrefab => LevelBuilder.Instance.cratePrefab;
|
||||
|
||||
|
||||
public LayoutCell(Vector3Int cellPos, Vector2Int layoutPos)
|
||||
{
|
||||
layoutPosition = layoutPos;
|
||||
|
||||
// Init data
|
||||
cellData = new LayoutCellData
|
||||
{
|
||||
cellContent = LayoutCellData.CellContent.None,
|
||||
isTarget = false,
|
||||
cellPos = cellPos
|
||||
};
|
||||
|
||||
worldPos = grid.GetCellCenterWorld(cellPos);
|
||||
|
||||
// Init sprite objects
|
||||
GameObject bgObject = GameObject.Instantiate(spritePrefab, worldPos, Quaternion.identity);
|
||||
bgSpriteRenderer = bgObject.GetComponent<SpriteRenderer>();
|
||||
bgSpriteRenderer.sprite = groundSprite;
|
||||
|
||||
GameObject envObject = GameObject.Instantiate(spritePrefab, worldPos, Quaternion.identity);
|
||||
envSpriteRenderer = envObject.GetComponent<SpriteRenderer>();
|
||||
envSpriteRenderer.sortingOrder = 1;
|
||||
|
||||
// Subscribe to destroy event
|
||||
LevelBuilder.Instance.LevelDestroyed += Destroy;
|
||||
}
|
||||
|
||||
|
||||
public void ChangeEnv(LevelBuilderButtons.PlacementOption placementOption)
|
||||
{
|
||||
// Clean previous contents
|
||||
cellData.cellContent = LayoutCellData.CellContent.None;
|
||||
cellData.isTarget = false;
|
||||
|
||||
if (crate)
|
||||
{
|
||||
GameObject.Destroy(crate.gameObject);
|
||||
}
|
||||
|
||||
// Place new object
|
||||
if (placementOption == LevelBuilderButtons.PlacementOption.Block)
|
||||
{
|
||||
cellData.cellContent = LayoutCellData.CellContent.Block;
|
||||
}
|
||||
else if (placementOption == LevelBuilderButtons.PlacementOption.Environment)
|
||||
{
|
||||
cellData.isTarget = true;
|
||||
}
|
||||
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;
|
||||
}
|
||||
else if (placementOption == LevelBuilderButtons.PlacementOption.Player)
|
||||
{
|
||||
Player.Instance.Teleport(this);
|
||||
}
|
||||
|
||||
UpdateSprites();
|
||||
}
|
||||
|
||||
|
||||
public void UpdateSprites()
|
||||
{
|
||||
if (cellData.cellContent == LayoutCellData.CellContent.Block)
|
||||
{
|
||||
envSpriteRenderer.sprite = blockSprite;
|
||||
}
|
||||
else if (cellData.isTarget)
|
||||
{
|
||||
envSpriteRenderer.sprite = environmentSprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
envSpriteRenderer.sprite = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
GameObject.Destroy(bgSpriteRenderer.gameObject);
|
||||
GameObject.Destroy(envSpriteRenderer.gameObject);
|
||||
LevelBuilder.Instance.LevelDestroyed -= Destroy;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Level/LayoutCell.cs.meta
Normal file
11
Assets/Scripts/Level/LayoutCell.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a9b573e03b4aad44bf34e4e06c9b347
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
129
Assets/Scripts/Level/LevelBuilder.cs
Normal file
129
Assets/Scripts/Level/LevelBuilder.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Level
|
||||
{
|
||||
public class LevelBuilder : MonoBehaviour
|
||||
{
|
||||
// Static
|
||||
private static LevelBuilder instance;
|
||||
|
||||
// Events
|
||||
public delegate void LevelDestroyedHandler();
|
||||
public event LevelDestroyedHandler LevelDestroyed;
|
||||
|
||||
public delegate void LevelInitializedHandler();
|
||||
public event LevelInitializedHandler LevelInitialized;
|
||||
|
||||
// Public
|
||||
public Sprite blockSprite;
|
||||
public Sprite environmentSprite;
|
||||
public Sprite groundSprite;
|
||||
public GameObject spritePrefab;
|
||||
public GameObject cratePrefab;
|
||||
public Grid grid;
|
||||
public LevelBuilderButtons levelBuilderButtons;
|
||||
|
||||
// Private
|
||||
private int width;
|
||||
private int height;
|
||||
private LayoutCell[][] layout;
|
||||
private Vector3Int firstCellPos;
|
||||
|
||||
// Properties
|
||||
public static LevelBuilder Instance => instance;
|
||||
|
||||
|
||||
// Methods
|
||||
private void Awake()
|
||||
{
|
||||
if (instance != null && instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
instance = this;
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (GameManager.Instance.currentState == GameState.Editing && Input.GetMouseButtonDown(0))
|
||||
{
|
||||
// Get the mouse position
|
||||
Vector3 mousePosition = Input.mousePosition;
|
||||
|
||||
// Convert the mouse position to world space if needed
|
||||
Vector3 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
|
||||
|
||||
EditCellAt(worldMousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void CreateLayout(int w, int h)
|
||||
{
|
||||
LevelDestroyed?.Invoke();
|
||||
|
||||
width = w;
|
||||
height = h;
|
||||
|
||||
// Offset first cell by half of width and height
|
||||
firstCellPos = new Vector3Int(-width / 2, -height / 2, 0);
|
||||
|
||||
layout = new LayoutCell[height][];
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
layout[y] = new LayoutCell[width];
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
Vector3Int cellPos = new Vector3Int(firstCellPos.x + x, firstCellPos.y + y, 0);
|
||||
|
||||
layout[y][x] = new LayoutCell(cellPos, new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
LevelInitialized?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
public LayoutCell GetCellAt(Vector2Int layoutPosition)
|
||||
{
|
||||
// Check that cell with these indices exists in array
|
||||
// if (0 < layoutPosition.y < height && 0 < layoutPosition.x < width)
|
||||
if (layoutPosition.y >= 0 && layoutPosition.y < height && layoutPosition.x >= 0 && layoutPosition.x < width)
|
||||
{
|
||||
return layout[layoutPosition.y][layoutPosition.x];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public LayoutCell WorldToLayoutCell(Vector3 worldPos)
|
||||
{
|
||||
Vector3Int cellPos = grid.WorldToCell(worldPos);
|
||||
Vector2Int layoutPosition = ((Vector2Int) (cellPos - firstCellPos));
|
||||
|
||||
return GetCellAt(layoutPosition);
|
||||
}
|
||||
|
||||
|
||||
private void EditCellAt(Vector3 worldPos)
|
||||
{
|
||||
LayoutCell layoutCell = WorldToLayoutCell(worldPos);
|
||||
|
||||
if (layoutCell == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
LevelBuilderButtons.PlacementOption placementOption = levelBuilderButtons.CurrentOption;
|
||||
layoutCell.ChangeEnv(placementOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Level/LevelBuilder.cs.meta
Normal file
11
Assets/Scripts/Level/LevelBuilder.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16bd348cc0368794a840f6eb88061349
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user