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;
- //got to create IA to tell when game is over
- //would be great to change shadow shade to brown
- //add a menu to restart
- //add music
- //add animation or at least picture on loading
- 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;
- private int pegLeftCounter = 0;
- 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;
- //got to test this for optimization:
- //TriggerVolumePositions[i] = GameValue.TriggerVolumePositions[i];
- //TokenPositions[i] = GameValue.TokenPositions[i];
- }
- }
- 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;
- }
- if(GameValue.firstRoundDone && isGamerOver())
- {
- //countText.text = "";
- for (int i = 0; i < 15; i++)
- {
- if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
- {
- pegLeftCounter++;
- }
- }
- //create different message for >3 , 3, 2 and 1 peg left
- winText.text = "Game Over\n" + pegLeftCounter + "peg left";
- }
- initTrigger = true;
- //testDIrectionVector();
- }
- 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();
- }
- 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;
- }
- private bool isGamerOver()
- {
- bool gameOver = true;
- Vector3 vDirection;
- Vector3 vDirectionInBetween;
- for (int i = 0; i < 15; i++)//pegLeft ID
- {
- if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
- {
- for (int j = 0; j < 15; j++)//free stop at 2 distance ID
- {
- if (Vector3.Distance(TokenPositions[i], TokenPositions[j]) > 11 && Vector3.Distance(TokenPositions[i], TokenPositions[j]) < 13
- && GameObject.Find("TriggerVolume" + j).GetComponent<PegSlot>().freeSlot)
- {
- vDirection = TokenPositions[i] - TokenPositions[j];
- for (int k = 0; k < 15; k++) //pegLeft between ID.
- {
- vDirectionInBetween = TokenPositions[i] - TokenPositions[k];
- if (Vector3.Distance(vDirection / 2, vDirectionInBetween) < 1 && !GameObject.Find("TriggerVolume" + k).GetComponent<PegSlot>().freeSlot)
- {
- gameOver = false;
- break;
- }
- }
- }
- }
- }
- }
- return gameOver;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement