Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Movement : MonoBehaviour {
- public Transform[] Whites;
- public Transform[] Blacks;
- public Vector3[] whitesPos;
- public Vector3[] blacksPos;
- public Vector3[] whitesDeadPos;
- public Transform[] whitesDead;
- public Vector3[] blacksDeadPos;
- public Transform[] blacksDead;
- public Transform[] startingBlocksWhite;
- public Transform[] startingBlocksBlack;
- public Transform grabbedPawn;
- public GameObject placeBlock;
- Ray ray;
- RaycastHit hit;
- public void Start()
- {
- for(int i = 0; i < 16; i++)
- {
- Whites[i].transform.position = startingBlocksWhite[i].transform.position;
- Blacks[i].transform.position = startingBlocksBlack[i].transform.position;
- }
- SavePos();
- for(int g = 0; g < 16; g++)
- {
- Whites[g].transform.position = whitesPos[g];
- Blacks[g].transform.position = blacksPos[g];
- }
- SavePos();
- for(int h = 0; h < 15; h++)
- {
- whitesDeadPos[h] = whitesDead[h].transform.position;
- blacksDeadPos[h] = blacksDead[h].transform.position;
- }
- }
- public void Update()
- {
- ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- if (Input.GetMouseButton(0))
- {
- if (hit.transform.tag == "Chess")
- {
- grabbedPawn = hit.transform;
- grabbedPawn.gameObject.layer = 8;
- for (int i = 0; i < 16; i++)
- {
- Whites[i].GetComponent<BoxCollider>().enabled = false;
- Blacks[i].GetComponent<BoxCollider>().enabled = false;
- }
- } else if (hit.transform.tag == "Chess Black")
- {
- grabbedPawn = hit.transform;
- grabbedPawn.gameObject.layer = 9;
- for (int i = 0; i < 16; i++)
- {
- Whites[i].GetComponent<BoxCollider>().enabled = false;
- Blacks[i].GetComponent<BoxCollider>().enabled = false;
- }
- }
- }
- else if (Input.GetMouseButtonUp(0))
- {
- if (hit.transform.tag != "Random")
- {
- placeBlock = GameObject.Find(hit.transform.name);
- if (placeBlock != null)
- {
- if (grabbedPawn != null)
- {
- Vector3 blockPos = new Vector3(placeBlock.transform.position.x, placeBlock.transform.position.y, 0);
- grabbedPawn.transform.position = blockPos;
- if (grabbedPawn.transform.tag == "Chess")
- {
- for (int i = 0; i < 16; i++)
- {
- if(grabbedPawn.transform.position == Whites[i].transform.position || grabbedPawn.transform.position == Blacks[15].transform.position)
- {
- if (grabbedPawn.transform.gameObject.layer != Whites[i].transform.gameObject.layer)
- {
- InvalidMove();
- }
- } else
- {
- WhiteKill();
- }
- }
- } else if (grabbedPawn.transform.tag == "Chess Black")
- {
- for (int i = 0; i < 16; i++)
- {
- if (grabbedPawn.transform.position == Blacks[i].transform.position || grabbedPawn.transform.position == Whites[15].transform.position)
- {
- if (grabbedPawn.transform.gameObject.layer != Blacks[i].transform.gameObject.layer)
- {
- InvalidMove();
- }
- } else
- {
- BlackKill();
- }
- }
- }
- grabbedPawn.gameObject.layer = 0;
- SavePos();
- }
- }
- }
- else
- {
- InvalidMove();
- }
- for (int f = 0; f < 16; f++)
- {
- Whites[f].GetComponent<BoxCollider>().enabled = true;
- Blacks[f].GetComponent<BoxCollider>().enabled = true;
- grabbedPawn = null;
- Vector3 whitesPos = new Vector3(Whites[f].transform.position.x, Whites[f].transform.position.y, 0);
- Vector3 blacksPos = new Vector3(Blacks[f].transform.position.x, Blacks[f].transform.position.y, 0);
- Whites[f].transform.position = whitesPos;
- Blacks[f].transform.position = blacksPos;
- }
- }
- }
- if(grabbedPawn != null)
- {
- grabbedPawn.GetComponent<BoxCollider>().enabled = false;
- Vector3 mousePos = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, -1);
- grabbedPawn.transform.position = mousePos;
- }
- }
- public void InvalidMove()
- {
- placeBlock = null;
- for(int i = 0; i < 16; i++)
- {
- Whites[i].transform.position = whitesPos[i];
- Blacks[i].transform.position = blacksPos[i];
- }
- }
- public void SavePos()
- {
- for(int i = 0; i < 16; i++)
- {
- whitesPos[i] = new Vector3(Whites[i].transform.position.x, Whites[i].transform.position.y, 0);
- blacksPos[i] = new Vector3(Blacks[i].transform.position.x, Blacks[i].transform.position.y, 0);
- }
- }
- public void WhiteKill()
- {
- for(int i = 0; i < 16; i++)
- {
- if(grabbedPawn.transform.position == Blacks[i].transform.position)
- {
- Blacks[i].transform.position = blacksDeadPos[i];
- Blacks[i].transform.gameObject.layer = 2;
- }
- }
- }
- public void BlackKill()
- {
- for (int i = 0; i < 15; i++)
- {
- if (grabbedPawn.transform.position == Whites[i].transform.position)
- {
- Whites[i].transform.position = whitesDeadPos[i];
- Whites[i].transform.gameObject.layer = 2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment