Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Shape : MonoBehaviour {
- public float speed = 1.0f;
- float LastMoveDown = 0;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- if (Input.GetKeyDown("a"))
- {
- transform.position += new Vector3(-1, 0, 0);
- Debug.Log(transform.position);
- if (!IsInGrid())
- {
- transform.position += new Vector3(1, 0, 0);
- }
- else
- {
- UpdateGameBoard();
- }
- }
- if (Input.GetKeyDown("d"))
- {
- transform.position += new Vector3(1, 0, 0);
- Debug.Log(transform.position);
- if (!IsInGrid())
- {
- transform.position += new Vector3(-1, 0, 0);
- }
- else
- {
- UpdateGameBoard();
- }
- }
- if (Input.GetKeyDown("s") || Time.time - LastMoveDown >= 1)
- {
- transform.position += new Vector3(0, -1, 0);
- Debug.Log(transform.position);
- if (!IsInGrid())
- {
- transform.position += new Vector3(0, 1, 0);
- enabled = false;
- FindObjectOfType<ShapeSpawner>().SpawnShape();
- }
- else
- {
- UpdateGameBoard();
- }
- LastMoveDown = Time.time;
- }
- if (Input.GetKeyDown("w"))
- {
- transform.Rotate(0, 0, 90);
- Debug.Log(transform.position);
- if (!IsInGrid())
- {
- transform.Rotate(0, 0, -90);
- }
- else
- {
- UpdateGameBoard();
- }
- }
- if (Input.GetKeyDown("e"))
- {
- transform.Rotate(0, 0, -90);
- Debug.Log(transform.position);
- if (!IsInGrid())
- {
- transform.Rotate(0, 0, 90);
- }
- else
- {
- UpdateGameBoard();
- }
- }
- }
- public bool IsInGrid()
- {
- foreach(Transform childBlock in transform)
- {
- Vector2 vect = RoundVector(childBlock.position);
- if (!IsInBorder(vect))
- {
- return false;
- }
- if(GameBoard.gameBoard[(int)vect.x, (int)vect.y] != null &&
- GameBoard.gameBoard[(int)vect.x, (int)vect.y].parent != transform)
- {
- return false;
- }
- }
- return true;
- }
- public Vector2 RoundVector(Vector2 vect)
- {
- return new Vector2(Mathf.Round(vect.x), Mathf.Round(vect.y));
- }
- public static bool IsInBorder(Vector2 pos)
- {
- return ((int)pos.x >= -6.0 &&
- (int)pos.x <= 5.0 &&
- (int)pos.y >= -8.0);
- }
- public void UpdateGameBoard()
- {
- for(int y = 0; y < 17; ++y)
- {
- for(int x = 0; x < 12; ++x)
- {
- if(GameBoard.gameBoard[x,y] != null &&
- GameBoard.gameBoard[x, y].parent != transform)
- {
- GameBoard.gameBoard[x, y] = null;
- }
- }
- }
- foreach(Transform childBlock in transform)
- {
- Vector2 vect = RoundVector (childBlock.position);
- GameBoard.gameBoard[(int)vect.x, (int)vect.y] = childBlock;
- Debug.Log("Cube At: " + vect.x + " " + vect.y);
- }
- GameBoard.PrintArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement