Advertisement
Combreal

pegGame05w.cs

Mar 29th, 2020
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 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.         fDistance = Vector3.Distance(slotPosition, startingPosition);
  49.         dDistance = Convert.ToDouble(fDistance);
  50.         //Debug.Log(dDistance);
  51.         if (triggered && (dDistance > 10.8 && dDistance < 13.4))
  52.         {
  53.             //Debug.Log("SlotPos1 : " + slotPosition.ToString("F3"));
  54.             transform.position = slotPosition;
  55.             rBody.velocity = Vector3.zero;
  56.             GameValue.numberOfPegsLeft--;
  57.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  58.             GameObject.Find("TriggerVolume" + slotInBetweenID).GetComponent<PegSlot>().freeSlot = true;
  59.             GameObject.Find("TriggerVolume" + destinationSlot()).GetComponent<PegSlot>().freeSlot = false;
  60.             GameObject.Find("Token" + slotInBetweenID).SetActive(false);
  61.             //got to rename (swap) object's names
  62.             //peg when moved takes new position ID name
  63.             //peg that is removed get renamed too (like pegOut1, pegOut2, etc)
  64.             SetCountText();
  65.         }
  66.         else
  67.         {
  68.             transform.position = startingPosition;
  69.             rBody.velocity = Vector3.zero;
  70.         }
  71.     }
  72.  
  73.     private Vector3 GetMouseWorldPos()
  74.     {
  75.         Vector3 mousePoint = Input.mousePosition;
  76.         mousePoint.z = mZCoord;
  77.         return Camera.main.ScreenToWorldPoint(mousePoint);
  78.     }
  79.  
  80.     private void OnMouseDrag()
  81.     {
  82.         transform.position = GetMouseWorldPos() + mOffset;
  83.         transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  84.     }
  85.  
  86.     void OnTriggerEnter(Collider other)//other.GetComponent<PegSlot>().object.name
  87.     {
  88.         if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
  89.         {
  90.             triggered = true;
  91.             slotPosition = other.gameObject.transform.position;
  92.         }
  93.     }
  94.  
  95.     void OnTriggerExit(Collider other)
  96.     {
  97.         if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
  98.         {
  99.             InitialTriggerVolume = other;
  100.             initTrigger = false;
  101.             //Debug.Log("Exited initTrigger");
  102.         }
  103.         else if (other.gameObject.CompareTag("Trigger Volume"))
  104.         {
  105.             triggered = false;
  106.         }
  107.         else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
  108.         {
  109.             GameValue.numberOfPegsLeft--;
  110.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  111.             GameValue.firstRoundDone = true;
  112.             gameObject.SetActive(false);
  113.             SetCountText();
  114.         }
  115.     }
  116.  
  117.     void SetCountText()
  118.     {
  119.         countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
  120.         if (GameValue.numberOfPegsLeft == 0)
  121.         {
  122.             winText.text = "You win!";
  123.         }
  124.     }
  125.  
  126.     private string slotInBetween()
  127.     {
  128.         string slotInBetween = "";
  129.         intersection.x = (startingPosition.x + slotPosition.x) / 2;
  130.         intersection.y = 4.54F;
  131.         intersection.z = (startingPosition.z + slotPosition.z) / 2;
  132.         for (int i = 0; i < 15; i++)
  133.         {
  134.             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)))
  135.             {
  136.                 slotInBetween = i.ToString();
  137.             }
  138.         }
  139.         return slotInBetween;
  140.     }
  141.  
  142.     private string destinationSlot()
  143.     {
  144.         string destinationSlot = "";
  145.         for (int i = 0; i < 15; i++)
  146.         {
  147.             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)))
  148.             {
  149.                 destinationSlot = i.ToString();
  150.             }
  151.         }
  152.         return destinationSlot;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement