This commit is contained in:
2023-06-16 19:10:51 +03:00
parent cf6dbed1f5
commit 69f9b0bd4b
8 changed files with 1125 additions and 119 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using Level;
using Objects;
public enum GameState
{
@@ -21,6 +22,7 @@ public class GameManager : MonoBehaviour
public GameState currentState { get; private set; } = GameState.Editing;
// Private
[SerializeField] private GameplayUI gameplayUI;
private LevelData currentLevelData;
// Properties
@@ -35,10 +37,17 @@ public class GameManager : MonoBehaviour
Destroy(gameObject);
return;
}
instance = this;
}
private void Start()
{
Player.Instance.PlayerMoved += CheckWin;
}
// Used by button OnClick
public void SetPlayState()
{
@@ -61,4 +70,13 @@ public class GameManager : MonoBehaviour
GameStateChanged?.Invoke(currentState);
}
private void CheckWin()
{
if (!LevelBuilder.Instance.UnoccupiedTargetExists())
{
gameplayUI.ShowWinScreen();
}
}
}

View File

@@ -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)
{
LayoutCell layoutCell = WorldToLayoutCell(worldPos);

View File

@@ -9,8 +9,14 @@ namespace Objects
{
public class Player : MonoBehaviour
{
// Static
private static Player instance;
// Events
public delegate void PlayerMovedHandler();
public event PlayerMovedHandler PlayerMoved;
// Public
public Vector2Int layoutPosition;
public InputActionAsset actions;
@@ -51,7 +57,7 @@ namespace Objects
private void Update()
{
if (CanMove)
if (CanMove && moveAction.WasPressedThisFrame())
{
Vector2 directionFloat = moveAction.ReadValue<Vector2>();
directionFloat = Vector2.ClampMagnitude(directionFloat, 1f);
@@ -94,7 +100,12 @@ namespace Objects
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
if (targetCell != null && targetCell.IsFree)
if (targetCell == null)
{
return;
}
if (targetCell.IsFree)
{
// Free previous cell
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
@@ -109,10 +120,12 @@ namespace Objects
isMoving = true;
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);
}
PlayerMoved?.Invoke();
}

View 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);
}
}

View File

@@ -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);
}
}

View File

@@ -59,6 +59,7 @@ namespace Level
public void ToggleUI()
{
buttonsContainer.SetActive(!buttonsContainer.activeSelf);
currentOption = PlacementOption.None;
}