Im creating a dr.mario style game however code its not functioning correctly
Im creating a dr.mario style game however code its not functioning correctly . so i started with creating a tetris game however im getting stuck on how to incorporate match 4 instead of delete line and how to split the game object for it to drop the leftovers till it hits something or the ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pill : MonoBehaviour
{
private float pTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 8;
public Vector3 rotationPoint;
private static Transform[,] grid = new Transform[width, height];
// Start is called before the first frame update
void Start()
{
}
void CheckForLines()
{
for (int i = height -1; i >= 0; i--)
{
if(HasLine(i))
{
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i)
{
for (int j = 0; j < width; j++)
{
if (grid[j, i] == null){
return false;
}
}
return true;
}
void DeleteLine(int i)
{
for (int j = 0; j < width; j++)
{
Destroy(grid[j,i].gameObject);
grid[j,i ] = null;
}
}
void RowDown(int i )
{
for(int y = i; y < height; y++)
{
for (int j = 0; j < width; j++)
{
if(grid[j,y] != null)
{
grid[j, y - 1] = grid[j, y];
grid[j, y] = null;
grid[j, y - i].transform.position -= new Vector3(0,1,0);
}
}
}
}
void AddToGrid()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
grid[roundedX, roundedY] = children;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);
if(!ValidMove())
{
transform.position -= new Vector3(-1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);
if(!ValidMove())
{
transform.position -= new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), 90);
if(!ValidMove())
{
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0,0,1), -90);
}
}
if(Time.time - pTime > (Input.GetKey(KeyCode.DownArrow) ? fallTime / 10 : fallTime))
{
transform.position += new Vector3(0, -1, 0);
if(!ValidMove())
{
transform.position -= new Vector3(0, -1, 0);
AddToGrid();
CheckForLines();
this.enabled = false;
FindObjectOfType<spawner>().NewPill();
}
pTime = Time.time;
}
}
bool ValidMove()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
if(roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height)
{
return false;
}
if(grid[roundedX, roundedY] != null)
{
return false;
}
}
return true;
}
}
screenshot-2021-11-22-230502.png
(221.7 kB)
Comment