Advertisement
Combreal

pegGame07w.cs

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