Advertisement
Combreal

pegGame06w_firstVersionDone.cs

Mar 29th, 2020
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 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. public class TokenBehaviour : MonoBehaviour
  8. {
  9.     private Vector3 mOffset;
  10.     private Vector3 startingPosition;
  11.     private Vector3 slotPosition;
  12.     private float mZCoord;
  13.     private bool triggered = false;
  14.     private Rigidbody rBody;
  15.     private Collider InitialTriggerVolume;
  16.     private bool initTrigger = true;
  17.     public Text countText;
  18.     public Text winText;
  19.     private float fDistance;
  20.     private double dDistance;
  21.     private Vector3 intersection;
  22.     private Vector3[] TriggerVolumePositions = new Vector3[15];
  23.     private Vector3[] TokenPositions = new Vector3[15];
  24.     private float positionOffset = 0.4F;
  25.  
  26.     void Start()
  27.     {
  28.         rBody = GetComponent<Rigidbody>();
  29.         SetCountText();
  30.         winText.text = "";
  31.         for (int i=0;i<15;i++)
  32.         {
  33.             TriggerVolumePositions[i] = GameObject.Find("TriggerVolume"+i.ToString()).transform.position;
  34.             TokenPositions[i] = GameObject.Find("Token"+i.ToString()).transform.position;
  35.         }
  36.     }
  37.  
  38.     private void OnMouseDown()
  39.     {
  40.         startingPosition = transform.position;
  41.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  42.         mOffset = gameObject.transform.position - GetMouseWorldPos();
  43.     }
  44.  
  45.     private void OnMouseUp()
  46.     {
  47.         string slotInBetweenID = slotInBetween();
  48.         string destinationSlotID = destinationSlot();
  49.         fDistance = Vector3.Distance(slotPosition, startingPosition);
  50.         dDistance = Convert.ToDouble(fDistance);
  51.         if (triggered && (dDistance > 10.8 && dDistance < 13.4))
  52.         {
  53.             transform.position = slotPosition;
  54.             rBody.velocity = Vector3.zero;
  55.             GameValue.numberOfPegsLeft--;
  56.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  57.             GameObject.Find("TriggerVolume" + slotInBetweenID).GetComponent<PegSlot>().freeSlot = true;
  58.             GameObject.Find("TriggerVolume" + destinationSlotID).GetComponent<PegSlot>().freeSlot = false;
  59.             GameObject.Find("Token" + slotInBetweenID).SetActive(false);
  60.             gameObject.name = "Token" + destinationSlotID;
  61.             SetCountText();
  62.         }
  63.         else
  64.         {
  65.             transform.position = startingPosition;
  66.             rBody.velocity = Vector3.zero;
  67.         }
  68.     }
  69.  
  70.     private Vector3 GetMouseWorldPos()
  71.     {
  72.         Vector3 mousePoint = Input.mousePosition;
  73.         mousePoint.z = mZCoord;
  74.         return Camera.main.ScreenToWorldPoint(mousePoint);
  75.     }
  76.  
  77.     private void OnMouseDrag()
  78.     {
  79.         transform.position = GetMouseWorldPos() + mOffset;
  80.         transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  81.     }
  82.  
  83.     void OnTriggerEnter(Collider other)
  84.     {
  85.         if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
  86.         {
  87.             triggered = true;
  88.             slotPosition = other.gameObject.transform.position;
  89.         }
  90.     }
  91.  
  92.     void OnTriggerExit(Collider other)
  93.     {
  94.         if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
  95.         {
  96.             InitialTriggerVolume = other;
  97.             initTrigger = false;
  98.         }
  99.         else if (other.gameObject.CompareTag("Trigger Volume"))
  100.         {
  101.             triggered = false;
  102.         }
  103.         else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
  104.         {
  105.             GameValue.numberOfPegsLeft--;
  106.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  107.             GameValue.firstRoundDone = true;
  108.             gameObject.SetActive(false);
  109.             SetCountText();
  110.         }
  111.     }
  112.  
  113.     void SetCountText()
  114.     {
  115.         countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
  116.         if (GameValue.numberOfPegsLeft == 0)
  117.         {
  118.             winText.text = "You win!";
  119.         }
  120.     }
  121.  
  122.     private string slotInBetween()
  123.     {
  124.         string slotInBetween = "";
  125.         intersection.x = (startingPosition.x + slotPosition.x) / 2;
  126.         intersection.y = 4.54F;
  127.         intersection.z = (startingPosition.z + slotPosition.z) / 2;
  128.         for (int i = 0; i < 15; i++)
  129.         {
  130.             if(((intersection.x > TriggerVolumePositions[i].x - positionOffset) && (intersection.x < TriggerVolumePositions[i].x + positionOffset)) && ((intersection.z > TriggerVolumePositions[i].z - positionOffset) && (intersection.z < TriggerVolumePositions[i].z + positionOffset)))
  131.             {
  132.                 slotInBetween = i.ToString();
  133.             }
  134.         }
  135.         return slotInBetween;
  136.     }
  137.  
  138.     private string destinationSlot()
  139.     {
  140.         string destinationSlot = "";
  141.         for (int i = 0; i < 15; i++)
  142.         {
  143.             if (((slotPosition.x > TokenPositions[i].x - positionOffset) && (slotPosition.x < TokenPositions[i].x + positionOffset)) && ((slotPosition.z > TokenPositions[i].z - positionOffset) && (slotPosition.z < TokenPositions[i].z + positionOffset)))
  144.             {
  145.                 destinationSlot = i.ToString();
  146.             }
  147.         }
  148.         return destinationSlot;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement