Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class TokenBehaviour : MonoBehaviour
- {
- private Vector3 mOffset;
- private Vector3 startingPosition;
- private Vector3 slotPosition;
- private float mZCoord;
- private bool triggered = false;
- private Rigidbody rBody;
- private Collider InitialTriggerVolume;
- private bool initTrigger = true;
- public Text countText;
- public Text winText;
- private float fDistance;
- private double dDistance;
- private Vector3 intersection;
- private Vector3[] TriggerVolumePositions = new Vector3[15];
- private Vector3[] TokenPositions = new Vector3[15];
- private float positionOffset = 0.4F;
- void Start()
- {
- rBody = GetComponent<Rigidbody>();
- SetCountText();
- winText.text = "";
- for (int i=0;i<15;i++)
- {
- TriggerVolumePositions[i] = GameObject.Find("TriggerVolume"+i.ToString()).transform.position;
- TokenPositions[i] = GameObject.Find("Token"+i.ToString()).transform.position;
- }
- }
- private void OnMouseDown()
- {
- startingPosition = transform.position;
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- mOffset = gameObject.transform.position - GetMouseWorldPos();
- }
- private void OnMouseUp()
- {
- string slotInBetweenID = slotInBetween();
- string destinationSlotID = destinationSlot();
- fDistance = Vector3.Distance(slotPosition, startingPosition);
- dDistance = Convert.ToDouble(fDistance);
- if (triggered && (dDistance > 10.8 && dDistance < 13.4))
- {
- transform.position = slotPosition;
- rBody.velocity = Vector3.zero;
- GameValue.numberOfPegsLeft--;
- GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
- GameObject.Find("TriggerVolume" + slotInBetweenID).GetComponent<PegSlot>().freeSlot = true;
- GameObject.Find("TriggerVolume" + destinationSlotID).GetComponent<PegSlot>().freeSlot = false;
- GameObject.Find("Token" + slotInBetweenID).SetActive(false);
- gameObject.name = "Token" + destinationSlotID;
- SetCountText();
- }
- else
- {
- transform.position = startingPosition;
- rBody.velocity = Vector3.zero;
- }
- }
- private Vector3 GetMouseWorldPos()
- {
- Vector3 mousePoint = Input.mousePosition;
- mousePoint.z = mZCoord;
- return Camera.main.ScreenToWorldPoint(mousePoint);
- }
- private void OnMouseDrag()
- {
- transform.position = GetMouseWorldPos() + mOffset;
- transform.position = new Vector3(transform.position.x, 6, transform.position.z);
- }
- void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
- {
- triggered = true;
- slotPosition = other.gameObject.transform.position;
- }
- }
- void OnTriggerExit(Collider other)
- {
- if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
- {
- InitialTriggerVolume = other;
- initTrigger = false;
- }
- else if (other.gameObject.CompareTag("Trigger Volume"))
- {
- triggered = false;
- }
- else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
- {
- GameValue.numberOfPegsLeft--;
- GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
- GameValue.firstRoundDone = true;
- gameObject.SetActive(false);
- SetCountText();
- }
- }
- void SetCountText()
- {
- countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
- if (GameValue.numberOfPegsLeft == 0)
- {
- winText.text = "You win!";
- }
- }
- private string slotInBetween()
- {
- string slotInBetween = "";
- intersection.x = (startingPosition.x + slotPosition.x) / 2;
- intersection.y = 4.54F;
- intersection.z = (startingPosition.z + slotPosition.z) / 2;
- for (int i = 0; i < 15; i++)
- {
- 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)))
- {
- slotInBetween = i.ToString();
- }
- }
- return slotInBetween;
- }
- private string destinationSlot()
- {
- string destinationSlot = "";
- for (int i = 0; i < 15; i++)
- {
- 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)))
- {
- destinationSlot = i.ToString();
- }
- }
- return destinationSlot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement