Advertisement
nhoxhaizxc123456

Chessman.cs

Apr 21st, 2024
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class Chessman : MonoBehaviour
  7. {
  8.     // References
  9.     public GameObject controller;
  10.     public GameObject movePlate;
  11.  
  12.     //Position
  13.     private int xBoard = -1;
  14.     private int yBoard = -1;
  15.  
  16.     //Variable to keep track of black and white
  17.     private string player;
  18.  
  19.     // Reference for chesspiece
  20.     public Sprite black_king, black_queen, black_rook, black_bishop, black_knight, black_pawn;
  21.     public Sprite white_king, white_queen, white_rook, white_bishop, white_knight, white_pawn;
  22.  
  23.     public void Activate()
  24.     {
  25.         controller = GameObject.FindGameObjectWithTag("GameController");
  26.  
  27.         // Take the instatiated location and adjust transform
  28.         SetCoords();
  29.  
  30.         switch(this.name){
  31.             case "black_king": this.GetComponent<SpriteRenderer>().sprite = black_king; player = "black"; break;
  32.             case "black_queen": this.GetComponent<SpriteRenderer>().sprite = black_queen; player = "black"; break;
  33.             case "black_rook": this.GetComponent<SpriteRenderer>().sprite = black_rook; player = "black"; break;
  34.             case "black_bishop": this.GetComponent<SpriteRenderer>().sprite = black_bishop; player = "black"; break;
  35.             case "black_knight": this.GetComponent<SpriteRenderer>().sprite = black_knight; player = "black"; break;
  36.             case "black_pawn": this.GetComponent<SpriteRenderer>().sprite = black_pawn; player = "black"; break;
  37.            
  38.             case "white_king": this.GetComponent<SpriteRenderer>().sprite = white_king; player = "white"; break;
  39.             case "white_queen": this.GetComponent<SpriteRenderer>().sprite = white_queen; player = "white"; break;
  40.             case "white_rook": this.GetComponent<SpriteRenderer>().sprite = white_rook; player = "white"; break;
  41.             case "white_bishop": this.GetComponent<SpriteRenderer>().sprite = white_bishop; player = "white"; break;
  42.             case "white_knight": this.GetComponent<SpriteRenderer>().sprite = white_knight; player = "white"; break;
  43.             case "white_pawn": this.GetComponent<SpriteRenderer>().sprite = white_pawn; player = "white"; break;
  44.         }
  45.  
  46.     }
  47.  
  48.     public void SetCoords()
  49.     {
  50.         float x = xBoard;
  51.         float y = yBoard;
  52.        
  53.         x *= 0.572f;
  54.         y *= 0.572f;
  55.  
  56.         x += -2.0f;
  57.         y += -2.0f;
  58.  
  59.         this.transform.position = new Vector3(x,y,-1.0f);
  60.     }
  61.  
  62.     // Setter and Getter
  63.     public int GetXBoard(){
  64.         return xBoard;
  65.     }
  66.  
  67.     public int GetYBoard(){
  68.         return yBoard;
  69.     }
  70.  
  71.     public void SetXBoard(int x){
  72.         xBoard = x;
  73.     }
  74.  
  75.     public void SetYBoard(int y){
  76.         yBoard = y;
  77.     }
  78.  
  79.     private void OnMouseUp()
  80.     {
  81.         if (!controller.GetComponent<Game>().IsGameOver() && controller.GetComponent<Game>().GetCurrentPlayer() == player) {
  82.             DestroyMovePlates();
  83.  
  84.             InitiateMovePlates();
  85.         }
  86.     }
  87.  
  88.     public void DestroyMovePlates()
  89.     {
  90.         GameObject[] movePlates = GameObject.FindGameObjectsWithTag("MovePlate");
  91.         for (int i = 0; i < movePlates.Length; i++){
  92.             Destroy(movePlates[i]);
  93.         }
  94.     }
  95.  
  96.     public void InitiateMovePlates()
  97.     {
  98.         switch(this.name){
  99.             case "black_king":
  100.             case "white_king":
  101.                 SurroundMovePlate();
  102.                 break;
  103.  
  104.             case "black_queen":
  105.             case "white_queen":
  106.                 LineMovePlate(1, 0);
  107.                 LineMovePlate(0, 1);
  108.                 LineMovePlate(1, 1);
  109.                 LineMovePlate(-1, 0);
  110.                 LineMovePlate(0, -1);
  111.                 LineMovePlate(-1, -1);
  112.                 LineMovePlate(-1, 1);
  113.                 LineMovePlate(1, -1);
  114.                 break;
  115.  
  116.             case "black_rook":
  117.             case "white_rook":
  118.                 LineMovePlate(1, 0);
  119.                 LineMovePlate(0, 1);
  120.                 LineMovePlate(-1, 0);
  121.                 LineMovePlate(0, -1);
  122.                 break;
  123.  
  124.             case "black_bishop":
  125.             case "white_bishop":
  126.                 LineMovePlate(1, 1);
  127.                 LineMovePlate(-1, 1);
  128.                 LineMovePlate(-1, -1);
  129.                 LineMovePlate(1, -1);
  130.                 break;
  131.  
  132.             case "black_knight":
  133.             case "white_knight":
  134.                 LMovePlate();
  135.                 break;
  136.  
  137.             case "black_pawn":
  138.                 PawnMovePlate(xBoard, yBoard - 1);
  139.                 break;
  140.  
  141.             case "white_pawn":
  142.                 PawnMovePlate(xBoard, yBoard + 1);
  143.                 break;
  144.         }
  145.     }
  146.  
  147.     public void LineMovePlate(int xIncrement, int yIncrement)
  148.     {
  149.         Game sc = controller.GetComponent<Game>();
  150.  
  151.         int x = xBoard + xIncrement;
  152.         int y = yBoard + yIncrement;
  153.  
  154.         while(sc.PositionOnBoard(x, y) && sc.GetPosition(x, y) == null){
  155.             MovePlateSpawn(x,y);
  156.             x += xIncrement;
  157.             y += yIncrement;
  158.         }
  159.  
  160.         if (sc.PositionOnBoard(x, y) && sc.GetPosition(x, y) != null && sc.GetPosition(x, y).GetComponent<Chessman>().player != player){
  161.             MovePlateAttackSpawn(x,y);
  162.         }
  163.     }
  164.  
  165.     public void LMovePlate()
  166.     {
  167.         PointMovePlate(xBoard + 1, yBoard + 2);
  168.         PointMovePlate(xBoard - 1, yBoard + 2);
  169.         PointMovePlate(xBoard + 2, yBoard + 1);
  170.         PointMovePlate(xBoard + 2, yBoard - 1);
  171.         PointMovePlate(xBoard + 1, yBoard - 2);
  172.         PointMovePlate(xBoard - 1, yBoard - 2);
  173.         PointMovePlate(xBoard - 2, yBoard + 1);
  174.         PointMovePlate(xBoard - 2, yBoard - 1);
  175.     }
  176.  
  177.     public void SurroundMovePlate()
  178.     {
  179.         PointMovePlate(xBoard, yBoard + 1);
  180.         PointMovePlate(xBoard, yBoard - 1);
  181.         PointMovePlate(xBoard - 1, yBoard + 0);
  182.         PointMovePlate(xBoard - 1, yBoard - 1);
  183.         PointMovePlate(xBoard - 1, yBoard + 1);
  184.         PointMovePlate(xBoard + 1, yBoard + 0);
  185.         PointMovePlate(xBoard + 1, yBoard - 1);
  186.         PointMovePlate(xBoard + 1, yBoard + 1);
  187.     }
  188.  
  189.     public void PointMovePlate(int x, int y)
  190.     {
  191.         Game sc = controller.GetComponent<Game>();
  192.         if (sc.PositionOnBoard(x, y))
  193.         {
  194.             GameObject cp = sc.GetPosition(x, y);
  195.  
  196.             if (cp == null)
  197.             {
  198.                 MovePlateSpawn(x, y);
  199.             }
  200.             else if (cp.GetComponent<Chessman>().player != player)
  201.             {
  202.                 MovePlateAttackSpawn(x, y);
  203.             }
  204.         }
  205.     }
  206.  
  207.     public void PawnMovePlate(int x, int y)
  208.     {
  209.         Game sc = controller.GetComponent<Game>();
  210.         if (sc.PositionOnBoard(x, y))
  211.         {
  212.            
  213.             if (sc.GetPosition(x, y) == null)
  214.             {
  215.                 MovePlateSpawn(x, y);
  216.             }
  217.  
  218.             if (sc.GetPosition(x, y + 1) == null && yBoard == 1 && player == "white"){
  219.                     MovePlateSpawn(x, y + 1);
  220.                 }
  221.  
  222.             if (sc.GetPosition(x, y - 1) == null && yBoard == 6 && player == "black"){
  223.                 MovePlateSpawn(x, y - 1);
  224.             }
  225.  
  226.             if (sc.PositionOnBoard(x + 1, y) && sc.GetPosition(x + 1, y) != null && sc.GetPosition(x + 1, y).GetComponent<Chessman>().player != player)
  227.             {
  228.                 MovePlateAttackSpawn(x + 1, y);
  229.             }
  230.  
  231.             if (sc.PositionOnBoard(x - 1, y) && sc.GetPosition(x - 1, y) != null && sc.GetPosition(x - 1, y).GetComponent<Chessman>().player != player)
  232.             {
  233.                 MovePlateAttackSpawn(x - 1, y);
  234.             }
  235.         }
  236.     }
  237.  
  238.     public void MovePlateSpawn(int matrixX, int matrixY)
  239.     {
  240.         //Get the board value in order to convert to xy coords
  241.         float x = matrixX;
  242.         float y = matrixY;
  243.  
  244.         //Adjust by variable offset
  245.         x *= 0.572f;
  246.         y *= 0.572f;
  247.  
  248.         //Add constants (pos 0,0)
  249.         x += -2.0f;
  250.         y += -2.0f;
  251.  
  252.         //Set actual unity values
  253.         GameObject mp = Instantiate(movePlate, new Vector3(x, y, -3.0f), Quaternion.identity);
  254.  
  255.         MovePlate mpScript = mp.GetComponent<MovePlate>();
  256.         mpScript.SetReference(gameObject);
  257.         mpScript.SetCoords(matrixX, matrixY);
  258.     }
  259.  
  260.     public void MovePlateAttackSpawn(int matrixX, int matrixY)
  261.     {
  262.         //Get the board value in order to convert to xy coords
  263.         float x = matrixX;
  264.         float y = matrixY;
  265.  
  266.         //Adjust by variable offset
  267.         x *= 0.572f;
  268.         y *= 0.572f;
  269.  
  270.         //Add constants (pos 0,0)
  271.         x += -2.0f;
  272.         y += -2.0f;
  273.  
  274.         //Set actual unity values
  275.         GameObject mp = Instantiate(movePlate, new Vector3(x, y, -3.0f), Quaternion.identity);
  276.  
  277.         MovePlate mpScript = mp.GetComponent<MovePlate>();
  278.         mpScript.attack = true;
  279.         mpScript.SetReference(gameObject);
  280.         mpScript.SetCoords(matrixX, matrixY);
  281.     }
  282. }
  283.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement