- Home /
Question by
JacboyX · Jul 24, 2019 at 01:01 AM ·
puzzlegeneral programming
How To Match Puzzle League Style
I want to make a puzzle game that matches 3 horizontally and vertically like in puzzle league. Here's an example: And here's the code for the board:
public class Board : MonoBehaviour
{
public static int gridwidth = 5;
public static int gridheight = 12;
public static Transform[,] grid = new Transform[gridwidth, gridheight];
// Use this for Initialization
void Start()
{
SpawnNextPiece();
}
// Update is called once per frame
void Update()
{
}
public void UpdateGrid(Piece piece)
{
for (int y = 0; y < gridheight; ++y)
{
for (int x = 0; x < gridwidth; ++x)
{
if (grid[x, y] != null)
{
if (grid[x, y].parent == piece.transform)
{
grid[x, y] = null;
}
}
}
}
foreach (Transform block in piece.transform)
{
Vector2 pos = Round(block.position);
if (pos.y < gridheight)
{
grid[(int)pos.x, (int)pos.y] = block;
}
}
}
public Transform GetTransformAtGridPosition(Vector2 pos)
{
if (pos.y > gridheight - 1)
{
return null;
}
else
{
return grid[(int)pos.x, (int)pos.y];
}
}
public void SpawnNextPiece()
{
GameObject nextPiece = (GameObject)Instantiate(Resources.Load(GetRandomPiece(), typeof(GameObject)), new Vector2(2.0f, 10.0f), Quaternion.identity);
}
public bool CheckIsInsideGrid (Vector2 pos)
{
return ((int)pos.x >= 0 && (int)pos.x < gridwidth && (int)pos.y >= 0);
}
public Vector2 Round (Vector2 pos)
{
return new Vector2(Mathf.Round(pos.x), Mathf.Round(pos.y));
}
private string GetRandomPiece()
{
int randomPiece = Random.Range(1, 7);
string randomPieceName = "Prefabs/Red";
switch (randomPiece)
{
case 1:
randomPieceName = "Prefabs/Red";
break;
case 2:
randomPieceName = "Prefabs/Orange";
break;
case 3:
randomPieceName = "Prefabs/Yellow";
break;
case 4:
randomPieceName = "Prefabs/Green";
break;
case 5:
randomPieceName = "Prefabs/Blue";
break;
case 6:
randomPieceName = "Prefabs/Purple";
break;
}
return randomPieceName;
}
}
And for the pieces:
public class Piece : MonoBehaviour
{
float fall = 0;
public float fallSpeed = 1;
//Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckUserInput();
}
void CheckUserInput()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);
if (CheckIsValidPosition())
{
FindObjectOfType<Board>().UpdateGrid(this);
}
else
{
transform.position += new Vector3(-1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);
if (CheckIsValidPosition())
{
FindObjectOfType<Board>().UpdateGrid(this);
}
else
{
transform.position += new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.position += new Vector3(0, -1, 0);
if (CheckIsValidPosition())
{
FindObjectOfType<Board>().UpdateGrid(this);
}
else
{
transform.position += new Vector3(0, 1, 0);
enabled = false;
FindObjectOfType<Board>().SpawnNextPiece();
}
}
}
bool CheckIsValidPosition()
{
foreach (Transform unit in transform)
{
Vector2 pos = FindObjectOfType<Board>().Round (unit.position);
if (FindObjectOfType<Board>().CheckIsInsideGrid (pos) == false)
{
return false;
}
if (FindObjectOfType<Board>().GetTransformAtGridPosition(pos) != null && FindObjectOfType<Board>().GetTransformAtGridPosition(pos).parent != transform)
{
return false;
}
}
return true;
}
}
The code is based off a tetris tutorial I found on YouTube, but I want to make this work. How can I create the array if the knowledge of creating and moving the pieces is all I got?
example.png
(22.8 kB)
Comment
Your answer
Follow this Question
Related Questions
Troubleshooting the spawn mechanic in a puzzle game 1 Answer
Puzzle piece rotation 0 Answers
Puzzle Piece Match Mechanic 1 Answer
Sprite Match Mechanic 0 Answers
How do I force the player to press buttons in order? 2 Answers