Skip to content

Commit

Permalink
[REFACTOR]
Browse files Browse the repository at this point in the history
* created helper functions in GameController
* Made the Screen Wrap section more clean and readable
* updated readme
  • Loading branch information
ngmgit committed Sep 1, 2018
1 parent 6c40cb3 commit 7b9e8ec
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 110 deletions.
5 changes: 2 additions & 3 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ MonoBehaviour:
mPlayerStats: {fileID: 11400000, guid: 303108734d41ec445b08c016225930fa, type: 2}
gridGrpBg: {fileID: 982523839}
playerSpawn: {fileID: 11400000, guid: 7d2708b98e6847b4394764e98d1ab59a, type: 2}
rows: 12
columns: 8
--- !u!4 &557186297
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1276,9 +1278,6 @@ MonoBehaviour:
mPlayerStats: {fileID: 11400000, guid: 303108734d41ec445b08c016225930fa, type: 2}
currentPosInGrid: {x: 0, y: 0}
spawnPosition: {fileID: 11400000, guid: 7d2708b98e6847b4394764e98d1ab59a, type: 2}
nextPosition: {x: 0, y: 0, z: 0}
prevPostion: {x: 0, y: 0, z: 0}
distTemp: 0
--- !u!1 &795514745
GameObject:
m_ObjectHideFlags: 0
Expand Down
71 changes: 57 additions & 14 deletions Assets/Scripts/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,54 @@ public class GameController : MonoBehaviour {
public GridLayoutGroup gridGrpBg;
public SetSpawnPositions playerSpawn;

int columns = 6;
int rows = 8;
[Range(6, 16)]
public int rows = 8;

void Awake()
[Range(4, 10)]
public int columns = 6;

private void Awake()
{
float Hz = Screen.width;
float Vt = Screen.height;

float gridSizeHz = Hz / columns;
float gridSizeVt = Vt / rows;



// Get Row and Column length w.r.t to World Units
mPlayerStats.distance.x = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0,0)),
Camera.main.ScreenToWorldPoint(new Vector2(0, gridSizeHz)));
mPlayerStats.distance.y = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0,0)),
Camera.main.ScreenToWorldPoint(new Vector2(gridSizeVt,0)));

// single row and column size stored in pixel
mPlayerStats.grid.sizeInPixel = new Vector2(gridSizeHz, gridSizeVt);
mPlayerStats.grid.rows = rows;
mPlayerStats.grid.columns = columns;

// storing screen space bounds in World units
mPlayerStats.mbounds.lower = Camera.main.ScreenToWorldPoint (new Vector2(0,0));
mPlayerStats.mbounds.upper = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width, Screen.height));

gridGrpBg.cellSize = mPlayerStats.grid.sizeInPixel;

playerSpawn.spawnPosInGrid = new Vector2 (
mPlayerStats.grid.sizeInPixel.x * 3,
mPlayerStats.grid.sizeInPixel.y * 4);
if (columns % 2 != 0) {
columns += 1;
}

mPlayerStats.mbounds.lower = Camera.main.ScreenToWorldPoint (new Vector2(0,0));
mPlayerStats.mbounds.upper = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width, Screen.height));
}
if (rows %2 != 0) {
rows += 1;
}

public static PlayerStats.MyBounds GetScreenToWorld(float incrementInGridUnit, PlayerStats playerStats) {
playerSpawn.spawnPosInGrid = new Vector2 (mPlayerStats.grid.sizeInPixel.x * (columns * 0.5f),
mPlayerStats.grid.sizeInPixel.y * (rows * 0.5f));
}

Vector2 incrementinScreenUnits = new Vector2(playerStats.grid.sizeInPixel.x * incrementInGridUnit,
playerStats.grid.sizeInPixel.y * incrementInGridUnit);
// Gives new bounds based on the grid Offset which can increment or decrement the current bounds
public static PlayerStats.MyBounds GetScreenToWorld(float gridOffset, PlayerStats playerStats)
{
Vector2 incrementinScreenUnits = new Vector2(playerStats.grid.sizeInPixel.x * gridOffset,
playerStats.grid.sizeInPixel.y * gridOffset);

Vector2 uppperBoundsInScreenUnits = new Vector2(Screen.width, Screen.height);

Expand All @@ -58,4 +70,35 @@ public static PlayerStats.MyBounds GetScreenToWorld(float incrementInGridUnit, P

return newBounds;
}

// Given a gridOffset in grid Units can check if given position lies inside the incremented or decremented new bounds
public static bool CheckIfPlayerOutside (Vector2 currentPos, PlayerStats mPlayerStats, float gridOffset)
{
PlayerStats.MyBounds newBounds = GetScreenToWorld(gridOffset, mPlayerStats);

if ((currentPos.x >= newBounds.lower.x
&& currentPos.x <= newBounds.upper.x)
&&
(currentPos.y >= newBounds.lower.y
&& currentPos.y <= newBounds.upper.y))
{
return false;
}

return true;
}

public static bool CheckIfPrevPosInGrid (Vector2 prevPostion, PlayerStats mPlayerStats)
{
if (Mathf.Approximately(prevPostion.x, mPlayerStats.mbounds.lower.x)||
Mathf.Approximately(prevPostion.x, mPlayerStats.mbounds.upper.x)||
Mathf.Approximately(prevPostion.y, mPlayerStats.mbounds.lower.y)||
Mathf.Approximately(prevPostion.y, mPlayerStats.mbounds.upper.y))
{
return true;
}

return false;
}

}
137 changes: 46 additions & 91 deletions Assets/Scripts/PlayerMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
public class PlayerMovement : MonoBehaviour {

public PlayerStats mPlayerStats;
public Vector2 currentPosInGrid;
public SetSpawnPositions spawnPosition;

[SerializeField]
private Vector2 currentPosInGrid;
private Vector3 nextPosition;
[SerializeField]
private Vector3 prevPostion;
private Vector2 targetDir;
private Vector2 prevTargetDir;
private float currentSpeed;
private readonly int playerZ = 0;
public float distTemp;

private void Start()
{
Expand All @@ -26,7 +23,6 @@ private void Start()
SetCurrentGridPosition();
SetPosition();
MoveToPosition();
distTemp = Vector2.Distance(prevPostion, nextPosition);
}

private void MovePlayerToSpawn(Vector2 mPosition)
Expand Down Expand Up @@ -55,7 +51,10 @@ private void SetCurrentGridPosition()

private void MovePlayer()
{
if (CheckIfPrevPosInGrid() && CheckIfPlayerOutside(0.5f))
bool isOutside = GameController.CheckIfPlayerOutside(transform.position, mPlayerStats, 0.5f);
bool isPrevPosOutside = GameController.CheckIfPrevPosInGrid(prevPostion, mPlayerStats);

if (isPrevPosOutside && isOutside)
ScreenWrapPlayer();

if (CanChangePosition()) {
Expand All @@ -66,7 +65,8 @@ private void MovePlayer()
MoveToPosition();
}

private bool CanChangePosition () {
private bool CanChangePosition ()
{
float dist = Vector2.Distance(transform.position, nextPosition);
return dist <= 0 ;
}
Expand All @@ -93,111 +93,66 @@ private void SetPosition()
nextPosition.z = playerZ;
}

private void ScreenWrapPlayer() {
private void ScreenWrapPlayer()
{
Vector2 swapScreenPos;
Vector2 swapWorldPos;

// In each section new current, prev, next position for the player is set if he is outside the bounds.
switch (mPlayerStats.dir) {
case mDirection.UP:
swapScreenPos = new Vector2 (
currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
- mPlayerStats.grid.sizeInPixel.y / 2);
swapWorldPos = Camera.main.ScreenToWorldPoint(swapScreenPos);
transform.position = new Vector3 (swapWorldPos.x, swapWorldPos.y, playerZ);

swapScreenPos = new Vector2 (
currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
- mPlayerStats.grid.sizeInPixel.y);
prevPostion = Camera.main.ScreenToWorldPoint(swapScreenPos);
prevPostion.z = playerZ;

swapScreenPos = new Vector2 (currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x, playerZ);
nextPosition = Camera.main.ScreenToWorldPoint(swapScreenPos);
nextPosition.z = playerZ;
swapScreenPos = new Vector2 (currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
- mPlayerStats.grid.sizeInPixel.y / 2);
SetPlayerWrapPosition(swapScreenPos);
SetNextAndPrevPositions(new Vector2 (currentPosInGrid.x, -1));
break;

case mDirection.DOWN:
swapScreenPos = new Vector2 (
currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
(mPlayerStats.grid.rows + 0.5f) * mPlayerStats.grid.sizeInPixel.y);
swapWorldPos = Camera.main.ScreenToWorldPoint(swapScreenPos);
transform.position = new Vector3 (swapWorldPos.x, swapWorldPos.y, playerZ);

swapScreenPos = new Vector2 (
currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
(mPlayerStats.grid.rows + 1) * mPlayerStats.grid.sizeInPixel.y);
prevPostion = Camera.main.ScreenToWorldPoint(swapScreenPos);
prevPostion.z = playerZ;

swapScreenPos = new Vector2 (currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x, Screen.height);
nextPosition = Camera.main.ScreenToWorldPoint(swapScreenPos);
nextPosition.z = playerZ;
swapScreenPos = new Vector2 (currentPosInGrid.x * mPlayerStats.grid.sizeInPixel.x,
(mPlayerStats.grid.rows + 0.5f) * mPlayerStats.grid.sizeInPixel.y);
SetPlayerWrapPosition(swapScreenPos);
SetNextAndPrevPositions(new Vector2 (currentPosInGrid.x, mPlayerStats.grid.rows + 1));
break;

case mDirection.RIGHT:
swapScreenPos = new Vector2 (
- mPlayerStats.grid.sizeInPixel.x / 2,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
swapWorldPos = Camera.main.ScreenToWorldPoint(swapScreenPos);
transform.position = new Vector3 (swapWorldPos.x, swapWorldPos.y, playerZ);

swapScreenPos = new Vector2 (
- mPlayerStats.grid.sizeInPixel.x,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
prevPostion = Camera.main.ScreenToWorldPoint(swapScreenPos);
prevPostion.z = playerZ;

swapScreenPos = new Vector2 (0, currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
nextPosition = Camera.main.ScreenToWorldPoint(swapScreenPos);
nextPosition.z = playerZ;
swapScreenPos = new Vector2 (- mPlayerStats.grid.sizeInPixel.x / 2,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
SetPlayerWrapPosition(swapScreenPos);
SetNextAndPrevPositions(new Vector2 (-1, currentPosInGrid.y));
break;

case mDirection.LEFT:
swapScreenPos = new Vector2 (
(mPlayerStats.grid.columns + 0.5f) * mPlayerStats.grid.sizeInPixel.x,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
swapWorldPos = Camera.main.ScreenToWorldPoint(swapScreenPos);
transform.position = new Vector3 (swapWorldPos.x, swapWorldPos.y, 0);

swapScreenPos = new Vector2 (
(mPlayerStats.grid.columns + 1) * mPlayerStats.grid.sizeInPixel.x,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
prevPostion = Camera.main.ScreenToWorldPoint(swapScreenPos);
prevPostion.z = playerZ;

swapScreenPos = new Vector2 (Screen.width, currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
nextPosition = Camera.main.ScreenToWorldPoint(swapScreenPos);
nextPosition.z = playerZ;
swapScreenPos = new Vector2 ((mPlayerStats.grid.columns + 0.5f) * mPlayerStats.grid.sizeInPixel.x,
currentPosInGrid.y * mPlayerStats.grid.sizeInPixel.y);
SetPlayerWrapPosition(swapScreenPos);
SetNextAndPrevPositions(new Vector2 (mPlayerStats.grid.columns + 1, currentPosInGrid.y));
break;
}
}

private bool CheckIfPlayerOutside (float gridIncrementer) {

PlayerStats.MyBounds newBounds = GameController.GetScreenToWorld(gridIncrementer, mPlayerStats);
private void SetPlayerWrapPosition(Vector2 swapScreenPos)
{
Vector2 swapWorldPos = Camera.main.ScreenToWorldPoint(swapScreenPos);
transform.position = new Vector3 (swapWorldPos.x, swapWorldPos.y, playerZ);
}

if ((transform.position.x >= newBounds.lower.x
&& transform.position.x <= newBounds.upper.x)
&&
(transform.position.y >= newBounds.lower.y
&& transform.position.y <= newBounds.upper.y))
{
return false;
}
private void SetNextAndPrevPositions(Vector2 gridPosiForPrev)
{
Vector2 swapScreenPos;
Vector2 nextGridPos;

return true;
}
int rows = mPlayerStats.grid.rows;
int columns = mPlayerStats.grid.columns;

private bool CheckIfPrevPosInGrid () {
if (Mathf.Approximately(prevPostion.x, mPlayerStats.mbounds.lower.x)||
Mathf.Approximately(prevPostion.x, mPlayerStats.mbounds.upper.x)||
Mathf.Approximately(prevPostion.y, mPlayerStats.mbounds.lower.y)||
Mathf.Approximately(prevPostion.y, mPlayerStats.mbounds.upper.y))
{
return true;
}
swapScreenPos = new Vector2 (gridPosiForPrev.x * mPlayerStats.grid.sizeInPixel.x,
gridPosiForPrev.y * mPlayerStats.grid.sizeInPixel.y);
prevPostion = Camera.main.ScreenToWorldPoint(swapScreenPos);
prevPostion.z = playerZ;

return false;
nextGridPos = new Vector2(Mathf.Clamp(gridPosiForPrev.x, 0, columns), Mathf.Clamp(gridPosiForPrev.y, 0, rows));
swapScreenPos = new Vector2 (nextGridPos.x * mPlayerStats.grid.sizeInPixel.x,
nextGridPos.y * mPlayerStats.grid.sizeInPixel.y);
nextPosition = Camera.main.ScreenToWorldPoint(swapScreenPos);
nextPosition.z = playerZ;
}

private void SetPlayerDirection()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/TouchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private void Start()
}

// Update is called once per frame
void Update ()
private void Update ()
{
int count = Input.touchCount;

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
A Simple PACMAN like game in Unity for mobile devices.
A Simple PACMAN like game in Unity for mobile devices.
Checkout GLITCH on android store to get an idea.

0 comments on commit 7b9e8ec

Please sign in to comment.