Advertisement
Combreal

pegGame08w1rstVer.cs

Apr 4th, 2020
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.52 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. //got to create IA to tell when game is over
  8. //would be great to change shadow shade to brown
  9. //add a menu to restart
  10. //add music
  11. //add animation or at least picture on loading
  12.  
  13. public class TokenBehaviour : MonoBehaviour
  14. {
  15.     private Vector3 mOffset;
  16.     private Vector3 startingPosition;
  17.     private Vector3 slotPosition;
  18.     private float mZCoord;
  19.     private bool triggered = false;
  20.     private Rigidbody rBody;
  21.     private Collider InitialTriggerVolume;
  22.     private bool initTrigger = true;
  23.     public Text countText;
  24.     public Text winText;
  25.     private float fDistance;
  26.     private double dDistance;
  27.     private Vector3 intersection;
  28.     private Vector3[] TriggerVolumePositions = new Vector3[15];
  29.     private Vector3[] TokenPositions = new Vector3[15];
  30.     private float positionOffset = 0.4F;
  31.     private int pegLeftCounter = 0;
  32.  
  33.     void Start()
  34.     {
  35.         rBody = GetComponent<Rigidbody>();
  36.         SetCountText();
  37.         winText.text = "";
  38.         for (int i=0;i<15;i++)
  39.         {
  40.             TriggerVolumePositions[i] = GameObject.Find("TriggerVolume"+i.ToString()).transform.position;
  41.             TokenPositions[i] = GameObject.Find("Token"+i.ToString()).transform.position;
  42.             //got to test this for optimization:
  43.             //TriggerVolumePositions[i] = GameValue.TriggerVolumePositions[i];
  44.             //TokenPositions[i] = GameValue.TokenPositions[i];
  45.         }
  46.     }
  47.  
  48.     private void OnMouseDown()
  49.     {
  50.         startingPosition = transform.position;
  51.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  52.         mOffset = gameObject.transform.position - GetMouseWorldPos();
  53.     }
  54.  
  55.     private void OnMouseUp()
  56.     {
  57.         string slotInBetweenID = slotInBetween();
  58.         string destinationSlotID = destinationSlot();
  59.         fDistance = Vector3.Distance(slotPosition, startingPosition);
  60.         dDistance = Convert.ToDouble(fDistance);
  61.         if (triggered && (dDistance > 10.8 && dDistance < 13.4))
  62.         {
  63.             transform.position = slotPosition;
  64.             rBody.velocity = Vector3.zero;
  65.             GameValue.numberOfPegsLeft--;
  66.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  67.             GameObject.Find("TriggerVolume" + slotInBetweenID).GetComponent<PegSlot>().freeSlot = true;
  68.             GameObject.Find("TriggerVolume" + destinationSlotID).GetComponent<PegSlot>().freeSlot = false;
  69.             GameObject.Find("Token" + slotInBetweenID).SetActive(false);
  70.             gameObject.name = "Token" + destinationSlotID;
  71.             SetCountText();
  72.         }
  73.         else
  74.         {
  75.             transform.position = startingPosition;
  76.             rBody.velocity = Vector3.zero;
  77.         }
  78.         if(GameValue.firstRoundDone && isGamerOver())
  79.         {
  80.             //countText.text = "";
  81.             for (int i = 0; i < 15; i++)
  82.             {
  83.                 if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
  84.                 {
  85.                     pegLeftCounter++;
  86.                 }
  87.             }
  88.                 //create different message for >3 , 3, 2 and 1 peg left
  89.                 winText.text = "Game Over\n" + pegLeftCounter + "peg left";
  90.         }
  91.         initTrigger = true;
  92.         //testDIrectionVector();
  93.     }
  94.  
  95.     private Vector3 GetMouseWorldPos()
  96.     {
  97.         Vector3 mousePoint = Input.mousePosition;
  98.         mousePoint.z = mZCoord;
  99.         return Camera.main.ScreenToWorldPoint(mousePoint);
  100.     }
  101.  
  102.     private void OnMouseDrag()
  103.     {
  104.         transform.position = GetMouseWorldPos() + mOffset;
  105.         transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  106.     }
  107.  
  108.     void OnTriggerEnter(Collider other)
  109.     {
  110.         if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
  111.         {
  112.             triggered = true;
  113.             slotPosition = other.gameObject.transform.position;
  114.         }
  115.     }
  116.  
  117.     void OnTriggerExit(Collider other)
  118.     {
  119.         if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
  120.         {
  121.             InitialTriggerVolume = other;
  122.             initTrigger = false;
  123.         }
  124.         else if (other.gameObject.CompareTag("Trigger Volume"))
  125.         {
  126.             triggered = false;
  127.         }
  128.         else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
  129.         {
  130.             GameValue.numberOfPegsLeft--;
  131.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  132.             GameValue.firstRoundDone = true;
  133.             gameObject.SetActive(false);
  134.             SetCountText();
  135.         }
  136.     }
  137.  
  138.     void SetCountText()
  139.     {
  140.         countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
  141.     }
  142.  
  143.     private string slotInBetween()
  144.     {
  145.         string slotInBetween = "";
  146.         intersection.x = (startingPosition.x + slotPosition.x) / 2;
  147.         intersection.y = 4.54F;
  148.         intersection.z = (startingPosition.z + slotPosition.z) / 2;
  149.         for (int i = 0; i < 15; i++)
  150.         {
  151.             if(((intersection.x > TriggerVolumePositions[i].x - positionOffset) && (intersection.x < TriggerVolumePositions[i].x + positionOffset))
  152.                 && ((intersection.z > TriggerVolumePositions[i].z - positionOffset) && (intersection.z < TriggerVolumePositions[i].z + positionOffset)))
  153.             {
  154.                 slotInBetween = i.ToString();
  155.             }
  156.         }
  157.         return slotInBetween;
  158.     }
  159.  
  160.     private string destinationSlot()
  161.     {
  162.         string destinationSlot = "";
  163.         for (int i = 0; i < 15; i++)
  164.         {
  165.             if (((slotPosition.x > TokenPositions[i].x - positionOffset) && (slotPosition.x < TokenPositions[i].x + positionOffset))
  166.                 && ((slotPosition.z > TokenPositions[i].z - positionOffset) && (slotPosition.z < TokenPositions[i].z + positionOffset)))
  167.             {
  168.                 destinationSlot = i.ToString();
  169.             }
  170.         }
  171.         return destinationSlot;
  172.     }
  173.     private bool isGamerOver()
  174.     {
  175.         bool gameOver = true;
  176.         Vector3 vDirection;
  177.         Vector3 vDirectionInBetween;
  178.         for (int i = 0; i < 15; i++)//pegLeft ID
  179.         {
  180.             if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
  181.             {
  182.                 for (int j = 0; j < 15; j++)//free stop at 2 distance ID
  183.                 {
  184.                     if (Vector3.Distance(TokenPositions[i], TokenPositions[j]) > 11 && Vector3.Distance(TokenPositions[i], TokenPositions[j]) < 13
  185.                         && GameObject.Find("TriggerVolume" + j).GetComponent<PegSlot>().freeSlot)
  186.                     {
  187.                         vDirection = TokenPositions[i] - TokenPositions[j];
  188.                         for (int k = 0; k < 15; k++) //pegLeft between ID.
  189.                         {
  190.                             vDirectionInBetween = TokenPositions[i] - TokenPositions[k];
  191.                             if (Vector3.Distance(vDirection / 2, vDirectionInBetween) < 1 && !GameObject.Find("TriggerVolume" + k).GetComponent<PegSlot>().freeSlot)
  192.                             {
  193.                                 gameOver = false;
  194.                                 break;
  195.                             }
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.         return gameOver;
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement