123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System;
- public class BunnyPlacement : MonoBehaviour
- {
-
-
-
-
- [Tooltip("Textures assigned to the placeholder object when placement is valid. " +
- "Index of each texture corresponds to the index of the material on the placeholder object.")]
- public Texture[] goodPlacementTex;
-
-
-
-
- [Tooltip("Textures assigned to the placeholder object when placement is not valid. " +
- "Index of each texture corresponds to the index of the material on the placeholder object.")]
- public Texture[] badPlacementTex;
-
-
-
- private Light pointlight;
-
-
-
- private ZEDControllerTracker_DemoInputs tracker;
-
-
-
- private ZEDPlaneDetectionManager zedPlane;
-
-
-
- private BunnySpawner bunnySpawner;
-
-
-
- private Transform placeholder;
-
-
-
- private bool canspawnbunny;
-
-
-
- public enum state
- {
- Idle,
- Down,
- Press,
- Up
- };
-
-
-
- public state button { get; private set; }
-
-
-
- void Awake()
- {
- canspawnbunny = false;
- tracker = GetComponent<ZEDControllerTracker_DemoInputs>();
- zedPlane = FindObjectOfType<ZEDPlaneDetectionManager>();
- bunnySpawner = GetComponent<BunnySpawner>();
- }
-
-
-
-
- public void SetPlaceholder(Transform pointer)
- {
- placeholder = pointer;
- pointlight = pointer.GetChild(0).GetComponentInChildren<Light>();
- }
-
-
-
-
-
- private void Update()
- {
- if (tracker == null)
- {
- if (Input.GetKeyDown(KeyCode.Space))
- button = state.Down;
- else if (Input.GetKey(KeyCode.Space))
- button = state.Press;
- else if (Input.GetKeyUp(KeyCode.Space))
- button = state.Up;
- else
- button = state.Idle;
- }
- else
- {
- if (tracker.CheckClickButton(ControllerButtonState.Down))
- button = state.Down;
- else if (tracker.CheckClickButton(ControllerButtonState.Held))
- button = state.Press;
- else if (tracker.CheckClickButton(ControllerButtonState.Up))
- button = state.Up;
- else button = state.Idle;
- }
-
- if (button != state.Idle)
- {
-
- if (button == state.Down)
- {
-
- bunnySpawner.canDisplayPlaceholder = true;
-
- if (bunnySpawner.baseballBat != null)
- bunnySpawner.baseballBat.SetActive(false);
-
- if (zedPlane.hitPlaneList.Count > 0)
- {
- for (int i = 0; i < zedPlane.hitPlaneList.Count; i++)
- {
- Destroy(zedPlane.hitPlaneList[i].gameObject);
- zedPlane.hitPlaneList.RemoveAt(i);
- }
- }
-
- if (!bunnySpawner.canSpawnMultipleBunnies && bunnySpawner.currentBunny != null)
- {
- Destroy(bunnySpawner.currentBunny);
- bunnySpawner.currentBunny = null;
- }
- }
-
- if (button == state.Press || button == state.Down)
- {
- if (zedPlane.hitPlaneList.Count == 0)
- {
-
- foreach (ZEDManager manager in ZEDManager.GetInstances())
- {
- if (zedPlane.DetectPlaneAtHit(manager, manager.GetMainCamera().WorldToScreenPoint(placeholder.position)))
- {
-
- ZEDPlaneGameObject currentPlane = zedPlane.hitPlaneList[zedPlane.hitPlaneList.Count - 1];
- Vector3 planeNormal = currentPlane.worldNormal;
-
- if (Vector3.Dot(planeNormal, Vector3.up) > 0.85f)
- {
-
- if (canspawnbunny == false)
- {
- canspawnbunny = true;
- bunnySpawner.placeHolderMat[0].mainTexture = goodPlacementTex[0];
- bunnySpawner.placeHolderMat[1].mainTexture = goodPlacementTex[1];
- pointlight.color = Color.blue;
- }
- else
- {
- for (int i = 0; i < zedPlane.hitPlaneList.Count; i++)
- {
- if (i == 0)
- {
- Destroy(zedPlane.hitPlaneList[i].gameObject);
- zedPlane.hitPlaneList.RemoveAt(i);
- }
- }
- }
- }
- else
- {
-
- canspawnbunny = false;
- bunnySpawner.placeHolderMat[0].mainTexture = badPlacementTex[0];
- bunnySpawner.placeHolderMat[1].mainTexture = badPlacementTex[1];
- pointlight.color = Color.red;
-
- for (int i = 0; i < zedPlane.hitPlaneList.Count; i++)
- {
- Destroy(zedPlane.hitPlaneList[i].gameObject);
- zedPlane.hitPlaneList.RemoveAt(i);
- }
- }
- break;
- }
- }
- }
- else if (zedPlane.hitPlaneList.Count > 0)
- {
- if (!Physics.Raycast(transform.position, placeholder.position - transform.position))
- {
-
- canspawnbunny = false;
- bunnySpawner.placeHolderMat[0].mainTexture = badPlacementTex[0];
- bunnySpawner.placeHolderMat[1].mainTexture = badPlacementTex[1];
- pointlight.color = Color.red;
-
- for (int i = 0; i < zedPlane.hitPlaneList.Count; i++)
- {
- Destroy(zedPlane.hitPlaneList[i].gameObject);
- zedPlane.hitPlaneList.RemoveAt(i);
- }
- }
- }
- }
-
- if (button == state.Up)
- {
-
- if (canspawnbunny)
- {
- bunnySpawner.SpawnBunny(placeholder.position);
- }
- else
- {
- for (int i = 0; i < zedPlane.hitPlaneList.Count; i++)
- {
- Destroy(zedPlane.hitPlaneList[i].gameObject);
- zedPlane.hitPlaneList.RemoveAt(i);
- }
- }
-
- canspawnbunny = false;
- bunnySpawner.canDisplayPlaceholder = false;
- }
- }
- }
- }
|