Done?
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Level;
|
using Level;
|
||||||
|
using Objects;
|
||||||
|
|
||||||
public enum GameState
|
public enum GameState
|
||||||
{
|
{
|
||||||
@@ -21,6 +22,7 @@ public class GameManager : MonoBehaviour
|
|||||||
public GameState currentState { get; private set; } = GameState.Editing;
|
public GameState currentState { get; private set; } = GameState.Editing;
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
[SerializeField] private GameplayUI gameplayUI;
|
||||||
private LevelData currentLevelData;
|
private LevelData currentLevelData;
|
||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
@@ -35,10 +37,17 @@ public class GameManager : MonoBehaviour
|
|||||||
Destroy(gameObject);
|
Destroy(gameObject);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
Player.Instance.PlayerMoved += CheckWin;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Used by button OnClick
|
// Used by button OnClick
|
||||||
public void SetPlayState()
|
public void SetPlayState()
|
||||||
{
|
{
|
||||||
@@ -61,4 +70,13 @@ public class GameManager : MonoBehaviour
|
|||||||
|
|
||||||
GameStateChanged?.Invoke(currentState);
|
GameStateChanged?.Invoke(currentState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void CheckWin()
|
||||||
|
{
|
||||||
|
if (!LevelBuilder.Instance.UnoccupiedTargetExists())
|
||||||
|
{
|
||||||
|
gameplayUI.ShowWinScreen();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -184,6 +184,25 @@ namespace Level
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool UnoccupiedTargetExists()
|
||||||
|
{
|
||||||
|
// This can be optimized by creating a mapping to all target points
|
||||||
|
// But I'm not doing that
|
||||||
|
for (int y = 0; y < height; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
if (layout[y][x].cellData.isTarget && layout[y][x].cellData.cellContent != LayoutCell.LayoutCellData.CellContent.Crate)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void EditCellAt(Vector3 worldPos)
|
private void EditCellAt(Vector3 worldPos)
|
||||||
{
|
{
|
||||||
LayoutCell layoutCell = WorldToLayoutCell(worldPos);
|
LayoutCell layoutCell = WorldToLayoutCell(worldPos);
|
||||||
|
|||||||
@@ -9,8 +9,14 @@ namespace Objects
|
|||||||
{
|
{
|
||||||
public class Player : MonoBehaviour
|
public class Player : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
// Static
|
||||||
private static Player instance;
|
private static Player instance;
|
||||||
|
|
||||||
|
// Events
|
||||||
|
public delegate void PlayerMovedHandler();
|
||||||
|
public event PlayerMovedHandler PlayerMoved;
|
||||||
|
|
||||||
|
// Public
|
||||||
public Vector2Int layoutPosition;
|
public Vector2Int layoutPosition;
|
||||||
public InputActionAsset actions;
|
public InputActionAsset actions;
|
||||||
|
|
||||||
@@ -51,7 +57,7 @@ namespace Objects
|
|||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (CanMove)
|
if (CanMove && moveAction.WasPressedThisFrame())
|
||||||
{
|
{
|
||||||
Vector2 directionFloat = moveAction.ReadValue<Vector2>();
|
Vector2 directionFloat = moveAction.ReadValue<Vector2>();
|
||||||
directionFloat = Vector2.ClampMagnitude(directionFloat, 1f);
|
directionFloat = Vector2.ClampMagnitude(directionFloat, 1f);
|
||||||
@@ -94,7 +100,12 @@ namespace Objects
|
|||||||
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
|
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
|
||||||
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
|
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
|
||||||
|
|
||||||
if (targetCell != null && targetCell.IsFree)
|
if (targetCell == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetCell.IsFree)
|
||||||
{
|
{
|
||||||
// Free previous cell
|
// Free previous cell
|
||||||
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
|
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
|
||||||
@@ -109,10 +120,12 @@ namespace Objects
|
|||||||
isMoving = true;
|
isMoving = true;
|
||||||
transform.DOMove(targetCell.worldPos, 1f).OnComplete(() => isMoving = false);
|
transform.DOMove(targetCell.worldPos, 1f).OnComplete(() => isMoving = false);
|
||||||
}
|
}
|
||||||
else if (targetCell != null && targetCell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Crate)
|
else if (targetCell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Crate)
|
||||||
{
|
{
|
||||||
targetCell.crate.Move(direction);
|
targetCell.crate.Move(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PlayerMoved?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
64
Assets/Scripts/UI/GameplayUI.cs
Normal file
64
Assets/Scripts/UI/GameplayUI.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using TMPro;
|
||||||
|
using Objects;
|
||||||
|
|
||||||
|
public class GameplayUI : MonoBehaviour
|
||||||
|
{
|
||||||
|
[SerializeField] private GameObject uiContainer;
|
||||||
|
[SerializeField] private TextMeshProUGUI timerText;
|
||||||
|
[SerializeField] private TextMeshProUGUI movesText;
|
||||||
|
[SerializeField] private GameObject winUI;
|
||||||
|
|
||||||
|
private float gameplayStart;
|
||||||
|
private int moves;
|
||||||
|
private bool isRunning;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
winUI.SetActive(false);
|
||||||
|
uiContainer.SetActive(false);
|
||||||
|
GameManager.Instance.GameStateChanged += OnGameStateChanged;
|
||||||
|
Player.Instance.PlayerMoved += () => moves++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if (isRunning)
|
||||||
|
{
|
||||||
|
// Debug.Log(Time.time - gameplayStart);
|
||||||
|
timerText.text = (Time.time - gameplayStart).ToString("F0");
|
||||||
|
movesText.text = moves.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
GameManager.Instance.GameStateChanged -= OnGameStateChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void OnGameStateChanged(GameState newState)
|
||||||
|
{
|
||||||
|
bool isGameplay = newState == GameState.Gameplay;
|
||||||
|
|
||||||
|
uiContainer.SetActive(isGameplay);
|
||||||
|
winUI.SetActive(false);
|
||||||
|
|
||||||
|
if (isGameplay)
|
||||||
|
{
|
||||||
|
gameplayStart = Time.time;
|
||||||
|
moves = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
isRunning = isGameplay;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void ShowWinScreen()
|
||||||
|
{
|
||||||
|
isRunning = false;
|
||||||
|
winUI.SetActive(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
using UnityEngine;
|
|
||||||
|
|
||||||
public class GamplayOnscreenControls : MonoBehaviour
|
|
||||||
{
|
|
||||||
[SerializeField] private GameObject uiContainer;
|
|
||||||
|
|
||||||
private void Start()
|
|
||||||
{
|
|
||||||
uiContainer.SetActive(false);
|
|
||||||
GameManager.Instance.GameStateChanged += OnGameStateChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDestroy()
|
|
||||||
{
|
|
||||||
GameManager.Instance.GameStateChanged -= OnGameStateChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnGameStateChanged(GameState newState)
|
|
||||||
{
|
|
||||||
uiContainer.SetActive(newState == GameState.Gameplay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -59,6 +59,7 @@ namespace Level
|
|||||||
public void ToggleUI()
|
public void ToggleUI()
|
||||||
{
|
{
|
||||||
buttonsContainer.SetActive(!buttonsContainer.activeSelf);
|
buttonsContainer.SetActive(!buttonsContainer.activeSelf);
|
||||||
|
currentOption = PlacementOption.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user