Advertisement
Combreal

pegGame08wRelease.cs

Apr 11th, 2020
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 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. //got to create IA to tell when game is over
  8. //would be great to change shadow shade to brown
  9. //add a menu to restart
  10. //add music
  11. //add animation or at least picture on loading
  12.  
  13. public class TokenBehaviour : MonoBehaviour
  14. {
  15.     private Vector3 mOffset;
  16.     private Vector3 startingPosition;
  17.     private Vector3 slotPosition;
  18.     private float mZCoord;
  19.     private bool triggered = false;
  20.     private Rigidbody rBody;
  21.     private Collider InitialTriggerVolume;
  22.     private bool initTrigger = true;
  23.     public Text countText;
  24.     public Text winText;
  25.     public Text winInfoText;
  26.     private float fDistance;
  27.     private double dDistance;
  28.     private Vector3 intersection;
  29.     private Vector3[] TriggerVolumePositions = new Vector3[15];
  30.     private Vector3[] TokenPositions = new Vector3[15];
  31.     private float positionOffset = 0.4F;
  32.     private int pegLeftCounter = 0;
  33.  
  34.     void Start()
  35.     {
  36.         rBody = GetComponent<Rigidbody>();
  37.         SetCountText();
  38.         winText.text = "";
  39.         winInfoText.text = "";
  40.         for (int i=0;i<15;i++)
  41.         {
  42.             TriggerVolumePositions[i] = GameObject.Find("TriggerVolume"+i.ToString()).transform.position;
  43.             TokenPositions[i] = GameObject.Find("Token"+i.ToString()).transform.position;
  44.             //got to test this for optimization:
  45.             //TriggerVolumePositions[i] = GameValue.TriggerVolumePositions[i];
  46.             //TokenPositions[i] = GameValue.TokenPositions[i];
  47.         }
  48.     }
  49.  
  50.     private void OnMouseDown()
  51.     {
  52.         if (!GameValue.showMenu)
  53.         {
  54.             startingPosition = transform.position;
  55.             mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  56.             mOffset = gameObject.transform.position - GetMouseWorldPos();
  57.         }
  58.     }
  59.  
  60.     private void OnMouseUp()
  61.     {
  62.         if (!GameValue.showMenu)
  63.         {
  64.             string slotInBetweenID = slotInBetween();
  65.             string destinationSlotID = destinationSlot();
  66.             fDistance = Vector3.Distance(slotPosition, startingPosition);
  67.             dDistance = Convert.ToDouble(fDistance);
  68.             GameValue.TokenStartingPos = startingPosition;
  69.             GameValue.TokenToReactivate = "Token" + slotInBetweenID;
  70.             GameValue.OldName = gameObject.name;
  71.             GameValue.NewName = "Token" + destinationSlotID;
  72.             GameValue.InitialTriggerVolume = InitialTriggerVolume.gameObject.name;
  73.             GameValue.SlotInBetweenID = slotInBetweenID;
  74.             GameValue.DestinationSlotID = destinationSlotID;
  75.             if (triggered && (dDistance > 10.8 && dDistance < 13.4))
  76.             {
  77.                 GameValue.canCancel = true;
  78.                 transform.position = slotPosition;
  79.                 rBody.velocity = Vector3.zero;
  80.                 GameValue.numberOfPegsLeft--;
  81.                 GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  82.                 GameObject.Find("TriggerVolume" + slotInBetweenID).GetComponent<PegSlot>().freeSlot = true;
  83.                 GameObject.Find("TriggerVolume" + destinationSlotID).GetComponent<PegSlot>().freeSlot = false;
  84.                 GameObject.Find("Token" + slotInBetweenID).SetActive(false);
  85.                 gameObject.name = "Token" + destinationSlotID;
  86.                 SetCountText();
  87.             }
  88.             else
  89.             {
  90.                 transform.position = startingPosition;
  91.                 rBody.velocity = Vector3.zero;
  92.             }
  93.             if (GameValue.firstRoundDone && isGamerOver())
  94.             {
  95.                 //countText.text = "";
  96.                 for (int i = 0; i < 15; i++)
  97.                 {
  98.                     if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
  99.                     {
  100.                         pegLeftCounter++;
  101.                     }
  102.                 }
  103.                 winText.text = "Game Over\n" + pegLeftCounter + "peg left";
  104.                 if (pegLeftCounter == 1)
  105.                 {
  106.                     winInfoText.text = "You nailed it";
  107.                 }
  108.                 else if (pegLeftCounter == 2)
  109.                 {
  110.                     winInfoText.text = "Close";
  111.                 }
  112.                 else if (pegLeftCounter == 3)
  113.                 {
  114.                     winInfoText.text = "You can do better";
  115.                 }
  116.                 else if (pegLeftCounter > 3)
  117.                 {
  118.                     winInfoText.text = "Try again";
  119.                 }
  120.             }
  121.             initTrigger = true;
  122.             //testDIrectionVector();
  123.         }
  124.     }
  125.  
  126.     private Vector3 GetMouseWorldPos()
  127.     {
  128.         Vector3 mousePoint = Input.mousePosition;
  129.         mousePoint.z = mZCoord;
  130.         return Camera.main.ScreenToWorldPoint(mousePoint);
  131.     }
  132.  
  133.     private void OnMouseDrag()
  134.     {
  135.         if (!GameValue.showMenu)
  136.         {
  137.             transform.position = GetMouseWorldPos() + mOffset;
  138.             transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  139.         }
  140.     }
  141.  
  142.     void OnTriggerEnter(Collider other)
  143.     {
  144.         if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
  145.         {
  146.             triggered = true;
  147.             slotPosition = other.gameObject.transform.position;
  148.         }
  149.     }
  150.  
  151.     void OnTriggerExit(Collider other)
  152.     {
  153.         if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
  154.         {
  155.             InitialTriggerVolume = other;
  156.             initTrigger = false;
  157.         }
  158.         else if (other.gameObject.CompareTag("Trigger Volume"))
  159.         {
  160.             triggered = false;
  161.         }
  162.         else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
  163.         {
  164.             GameValue.numberOfPegsLeft--;
  165.             GameObject.Find(InitialTriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  166.             GameValue.firstRoundDone = true;
  167.             gameObject.SetActive(false);
  168.             SetCountText();
  169.             GameValue.TokenStartingPos = startingPosition;
  170.             GameValue.OldName = gameObject.name;
  171.             GameValue.InitialTriggerVolume = InitialTriggerVolume.gameObject.name;
  172.             GameValue.canCancel = true;
  173.         }
  174.     }
  175.  
  176.     void SetCountText()
  177.     {
  178.         countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
  179.     }
  180.  
  181.     private string slotInBetween()
  182.     {
  183.         string slotInBetween = "";
  184.         intersection.x = (startingPosition.x + slotPosition.x) / 2;
  185.         intersection.y = 4.54F;
  186.         intersection.z = (startingPosition.z + slotPosition.z) / 2;
  187.         for (int i = 0; i < 15; i++)
  188.         {
  189.             if(((intersection.x > TriggerVolumePositions[i].x - positionOffset) && (intersection.x < TriggerVolumePositions[i].x + positionOffset))
  190.                 && ((intersection.z > TriggerVolumePositions[i].z - positionOffset) && (intersection.z < TriggerVolumePositions[i].z + positionOffset)))
  191.             {
  192.                 slotInBetween = i.ToString();
  193.             }
  194.         }
  195.         return slotInBetween;
  196.     }
  197.  
  198.     private string destinationSlot()
  199.     {
  200.         string destinationSlot = "";
  201.         for (int i = 0; i < 15; i++)
  202.         {
  203.             if (((slotPosition.x > TokenPositions[i].x - positionOffset) && (slotPosition.x < TokenPositions[i].x + positionOffset))
  204.                 && ((slotPosition.z > TokenPositions[i].z - positionOffset) && (slotPosition.z < TokenPositions[i].z + positionOffset)))
  205.             {
  206.                 destinationSlot = i.ToString();
  207.             }
  208.         }
  209.         return destinationSlot;
  210.     }
  211.     private bool isGamerOver()
  212.     {
  213.         bool gameOver = true;
  214.         Vector3 vDirection;
  215.         Vector3 vDirectionInBetween;
  216.         for (int i = 0; i < 15; i++)//pegLeft ID
  217.         {
  218.             if (!GameObject.Find("TriggerVolume" + i).GetComponent<PegSlot>().freeSlot)
  219.             {
  220.                 for (int j = 0; j < 15; j++)//free stop at 2 distance ID
  221.                 {
  222.                     if (Vector3.Distance(TokenPositions[i], TokenPositions[j]) > 11 && Vector3.Distance(TokenPositions[i], TokenPositions[j]) < 13
  223.                         && GameObject.Find("TriggerVolume" + j).GetComponent<PegSlot>().freeSlot)
  224.                     {
  225.                         vDirection = TokenPositions[i] - TokenPositions[j];
  226.                         for (int k = 0; k < 15; k++) //pegLeft between ID.
  227.                         {
  228.                             vDirectionInBetween = TokenPositions[i] - TokenPositions[k];
  229.                             if (Vector3.Distance(vDirection / 2, vDirectionInBetween) < 1 && !GameObject.Find("TriggerVolume" + k).GetComponent<PegSlot>().freeSlot)
  230.                             {
  231.                                 gameOver = false;
  232.                                 break;
  233.                             }
  234.                         }
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.         GameValue.IsGameOver = gameOver;
  240.         return gameOver;
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement