Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Refactor strict to code best practices and C# 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-bondar committed Nov 22, 2019
1 parent 3a8f7a8 commit 2c13d2c
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 219 deletions.
119 changes: 54 additions & 65 deletions Assets/Scripts/Core/Board.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,30 @@
using UnityEngine;
using System.Collections;

using System.Collections;
using UnityEngine;

public class Board : MonoBehaviour
{
private Transform[,] grid;

public ParticlePlayer[] rowGlowFx = new ParticlePlayer[4];

public Transform emptySprite;

public int height = 30;
public int width = 10;

public int header = 8;

private Transform[,] grid;

public int completedRows = 0;

public ParticlePlayer[] rowGlowFx = new ParticlePlayer[4];

private void Awake()
{
grid = new Transform[width, height];
}

private void Start()
{
DrawEmptyCells();
}
private void Start() => DrawEmptyCells();

private bool IsWithinBoard(int x, int y)
{
return (x >= 0 && x < width && y >= 0);
}
private void Awake() => grid = new Transform[width, height];

private bool IsOccupied(int x, int y, Shape shape)
{
return (grid[x, y] != null && grid[x, y].parent != shape.transform);
}
private bool IsWithinBoard(int x, int y) => (x >= 0 && x < width && y >= 0);

public bool IsValidPosition(Shape shape)
{
foreach (Transform child in shape.transform)
{
Vector2 pos = Vectorf.Round(child.position);

if (!IsWithinBoard((int)pos.x, (int)pos.y))
{
return false;
}

if (IsOccupied((int)pos.x, (int)pos.y, shape))
{
return false;
}
}

return true;
}
private bool IsOccupied(int x, int y, Shape shape) =>
grid[x, y] != null && grid[x, y].parent != shape.transform;

private void DrawEmptyCells()
{
Expand All @@ -80,20 +49,6 @@ private void DrawEmptyCells()
}
}

public void StoreShapeInGrid(Shape shape)
{
if (shape == null)
{
return;
}

foreach (Transform child in shape.transform)
{
Vector2 pos = Vectorf.Round(child.position);
grid[(int)pos.x, (int)pos.y] = child;
}
}

private bool IsComplete(int y)
{
for (int x = 0; x < width; x++)
Expand Down Expand Up @@ -141,6 +96,49 @@ private void ShiftRowsDown(int startY)
}
}

private void ClearRowFx(int idx, int y)
{
if (rowGlowFx[idx])
{
rowGlowFx[idx].transform.position = new Vector3(0, y, -2);
rowGlowFx[idx].Play();
}
}

public bool IsValidPosition(Shape shape)
{
foreach (Transform child in shape.transform)
{
Vector2 pos = Vectorf.Round(child.position);

if (!IsWithinBoard((int)pos.x, (int)pos.y))
{
return false;
}

if (IsOccupied((int)pos.x, (int)pos.y, shape))
{
return false;
}
}

return true;
}

public void StoreShapeInGrid(Shape shape)
{
if (shape == null)
{
return;
}

foreach (Transform child in shape.transform)
{
Vector2 pos = Vectorf.Round(child.position);
grid[(int)pos.x, (int)pos.y] = child;
}
}

public IEnumerator ClearAllRows()
{
completedRows = 0;
Expand Down Expand Up @@ -180,13 +178,4 @@ public bool IsOverLimit(Shape shape)

return false;
}

private void ClearRowFx(int idx, int y)
{
if (rowGlowFx[idx])
{
rowGlowFx[idx].transform.position = new Vector3(0, y, -2);
rowGlowFx[idx].Play();
}
}
}
9 changes: 3 additions & 6 deletions Assets/Scripts/Core/Ghost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public class Ghost : MonoBehaviour
{
public Color color = new Color(1f, 1f, 1f, 0.2f);

private Shape ghostShape = null;

private bool hitBottom = false;

public Color color = new Color(1f, 1f, 1f, 0.2f);

public void DrawGhost(Shape originalShape, Board gameBoard)
{
if (!ghostShape)
Expand Down Expand Up @@ -44,8 +44,5 @@ public void DrawGhost(Shape originalShape, Board gameBoard)
}
}

public void Reset()
{
Destroy(ghostShape.gameObject);
}
public void Reset() => Destroy(ghostShape.gameObject);
}
26 changes: 6 additions & 20 deletions Assets/Scripts/Core/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,16 @@ public class Shape : MonoBehaviour

public Vector3 queueOffset;

private void Move(Vector3 moveDirection)
{
transform.position += moveDirection;
}
private void Move(Vector3 moveDirection) => transform.position += moveDirection;

public void MoveLeft()
{
Move(new Vector3(-1, 0, 0));
}
public void MoveLeft() => Move(new Vector3(-1, 0, 0));

public void MoveRight()
{
Move(new Vector3(1, 0, 0));
}

public void MoveUp()
{
Move(new Vector3(0, 1, 0));
}
public void MoveRight() => Move(new Vector3(1, 0, 0));

public void MoveDown()
{
Move(new Vector3(0, -1, 0));
}
public void MoveUp() => Move(new Vector3(0, 1, 0));

public void MoveDown() => Move(new Vector3(0, -1, 0));

public void RotateRight()
{
Expand Down
57 changes: 27 additions & 30 deletions Assets/Scripts/Core/Spawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ public class Spawner : MonoBehaviour
{
public Shape[] allShapes;
public Transform[] queuedXforms = new Transform[3];
public ParticlePlayer spawnFx;

private Shape[] queuedShapes = new Shape[3];

private float queueScale = 0.5f;

public ParticlePlayer spawnFx;

private void Start()
{
InitQueue();
}
private void Start() => InitQueue();

private Shape GetRandomShape()
{
Expand All @@ -33,29 +29,6 @@ private Shape GetRandomShape()
}
}

public Shape SpawnShape()
{
Shape shape = null;
shape = GetQueuedShape();
shape.transform.position = transform.position;

StartCoroutine(GrowShape(shape, transform.position, 0.25f));

if (spawnFx)
{
spawnFx.Play();
}

if (shape)
{
return shape;
}
else
{
Debug.Log("WARNING! Invalid shape in spawner!");
return null;
}
}

private void InitQueue()
{
Expand Down Expand Up @@ -106,7 +79,7 @@ private Shape GetQueuedShape()
return firstShape;
}

IEnumerator GrowShape(Shape shape, Vector3 position, float growTime = 0.5f)
private IEnumerator GrowShape(Shape shape, Vector3 position, float growTime = 0.5f)
{
float size = 0f;

Expand All @@ -126,4 +99,28 @@ IEnumerator GrowShape(Shape shape, Vector3 position, float growTime = 0.5f)

shape.transform.localScale = Vector3.one;
}

public Shape SpawnShape()
{
Shape shape = null;
shape = GetQueuedShape();
shape.transform.position = transform.position;

StartCoroutine(GrowShape(shape, transform.position, 0.25f));

if (spawnFx)
{
spawnFx.Play();
}

if (shape)
{
return shape;
}
else
{
Debug.Log("WARNING! Invalid shape in spawner!");
return null;
}
}
}
56 changes: 28 additions & 28 deletions Assets/Scripts/Managers/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class GameController : MonoBehaviour

public bool isPaused = false;

enum Direction { none, left, right, up, down }
private enum Direction { none, left, right, up, down }

private Direction swipeDirection = Direction.none;
private Direction swipeEndDirection = Direction.none;
Expand Down Expand Up @@ -268,7 +268,7 @@ private void GameOver()
gameOver = true;
}

IEnumerator GameOverRoutine()
private IEnumerator GameOverRoutine()
{
if (gameOverFx)
{
Expand Down Expand Up @@ -359,6 +359,32 @@ private void LateUpdate()
}
}

private void SwipeHandler(Vector2 swipeMovement)
{
swipeDirection = GetDirection(swipeMovement);
}

private void SwipeEndHandler(Vector2 swipeMovement)
{
swipeEndDirection = GetDirection(swipeMovement);
}

private Direction GetDirection(Vector2 swipeMovement)
{
Direction swipeDirection = Direction.none;

if (Mathf.Abs(swipeMovement.x) > Mathf.Abs(swipeMovement.y))
{
swipeDirection = swipeMovement.x >= 0 ? Direction.right : Direction.left;
}
else
{
swipeDirection = swipeMovement.y >= 0 ? Direction.up : Direction.down;
}

return swipeDirection;
}

public void Restart()
{
Time.timeScale = 1f;
Expand Down Expand Up @@ -429,30 +455,4 @@ public void Hold()
ghost.Reset();
}
}

private void SwipeHandler(Vector2 swipeMovement)
{
swipeDirection = GetDirection(swipeMovement);
}

private void SwipeEndHandler(Vector2 swipeMovement)
{
swipeEndDirection = GetDirection(swipeMovement);
}

private Direction GetDirection(Vector2 swipeMovement)
{
Direction swipeDirection = Direction.none;

if (Mathf.Abs(swipeMovement.x) > Mathf.Abs(swipeMovement.y))
{
swipeDirection = swipeMovement.x >= 0 ? Direction.right : Direction.left;
}
else
{
swipeDirection = swipeMovement.y >= 0 ? Direction.up : Direction.down;
}

return swipeDirection;
}
}
Loading

0 comments on commit 2c13d2c

Please sign in to comment.