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;
- public Text winInfoText;
- 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 = "";
- winInfoText.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()
- {
- if (!GameValue.showMenu)
- {
- startingPosition = transform.position;
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- mOffset = gameObject.transform.position - GetMouseWorldPos();
- }
- }
- private void OnMouseUp()
- {
- if (!GameValue.showMenu)
- {
- string slotInBetweenID = slotInBetween();
- string destinationSlotID = destinationSlot();
- fDistance = Vector3.Distance(slotPosition, startingPosition);
- dDistance = Convert.ToDouble(fDistance);
- GameValue.TokenStartingPos = startingPosition;
- GameValue.TokenToReactivate = "Token" + slotInBetweenID;
- GameValue.OldName = gameObject.name;
- GameValue.NewName = "Token" + destinationSlotID;
- GameValue.InitialTriggerVolume = InitialTriggerVolume.gameObject.name;
- GameValue.SlotInBetweenID = slotInBetweenID;
- GameValue.DestinationSlotID = destinationSlotID;
- if (triggered && (dDistance > 10.8 && dDistance < 13.4))
- {
- GameValue.canCancel = true;
- 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++;
- }
- }
- winText.text = "Game Over\n" + pegLeftCounter + "peg left";
- if (pegLeftCounter == 1)
- {
- winInfoText.text = "You nailed it";
- }
- else if (pegLeftCounter == 2)
- {
- winInfoText.text = "Close";
- }
- else if (pegLeftCounter == 3)
- {
- winInfoText.text = "You can do better";
- }
- else if (pegLeftCounter > 3)
- {
- winInfoText.text = "Try again";
- }
- }
- initTrigger = true;
- //testDIrectionVector();
- }
- }
- private Vector3 GetMouseWorldPos()
- {
- Vector3 mousePoint = Input.mousePosition;
- mousePoint.z = mZCoord;
- return Camera.main.ScreenToWorldPoint(mousePoint);
- }
- private void OnMouseDrag()
- {
- if (!GameValue.showMenu)
- {
- 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();
- GameValue.TokenStartingPos = startingPosition;
- GameValue.OldName = gameObject.name;
- GameValue.InitialTriggerVolume = InitialTriggerVolume.gameObject.name;
- GameValue.canCancel = true;
- }
- }
- 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;
- }
- }
- }
- }
- }
- }
- GameValue.IsGameOver = gameOver;
- return gameOver;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement