Advertisement
Combreal

pegGame07wSecondVer.cs

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