Advertisement
nhoxhaizxc123456

MovePlate.cs

Apr 21st, 2024
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MovePlate : MonoBehaviour
  6. {
  7.     public GameObject controller;
  8.  
  9.     GameObject reference = null;
  10.  
  11.     // Board position, not world position
  12.     int matrixX;
  13.     int matrixY;
  14.  
  15.     // false: movement, true: attack
  16.     public bool attack = false;
  17.  
  18.     public void Start()
  19.     {
  20.         if (attack){
  21.             // Change to red
  22.             gameObject.GetComponent<SpriteRenderer>().color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
  23.         }
  24.  
  25.     }
  26.  
  27.     public void OnMouseUp()
  28.     {
  29.         controller = GameObject.FindGameObjectWithTag("GameController");
  30.  
  31.         if (attack)
  32.         {
  33.             GameObject cp = controller.GetComponent<Game>().GetPosition(matrixX, matrixY);
  34.  
  35.             if (cp.name == "white_king") controller.GetComponent<Game>().Winner("black");
  36.             if (cp.name == "black_king") controller.GetComponent<Game>().Winner("white");
  37.  
  38.             Destroy(cp);
  39.         }
  40.  
  41.         // Clear old postion after moving chess piece
  42.         controller.GetComponent<Game>().SetPositionEmpty(reference.GetComponent<Chessman>().GetXBoard(), reference.GetComponent<Chessman>().GetYBoard());
  43.  
  44.         // Set new position for chess piece after moving
  45.         reference.GetComponent<Chessman>().SetXBoard(matrixX);
  46.         reference.GetComponent<Chessman>().SetYBoard(matrixY);
  47.         reference.GetComponent<Chessman>().SetCoords();
  48.  
  49.         controller.GetComponent<Game>().SetPosition(reference);
  50.  
  51.         controller.GetComponent<Game>().NextTurn();
  52.  
  53.         // Destroy move plate
  54.         reference.GetComponent<Chessman>().DestroyMovePlates();
  55.  
  56.         controller.GetComponent<Game>().UpdateBoardState();
  57.     }
  58.  
  59.     public void SetCoords(int x, int y)
  60.     {
  61.         matrixX = x;
  62.         matrixY = y;
  63.     }
  64.  
  65.     // Setter and Getter
  66.     public void SetReference(GameObject obj){
  67.         reference = obj;
  68.     }
  69.  
  70.     public GameObject GetReference(){
  71.         return reference;
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement