Advertisement
Combreal

pegGame04w.cs

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