-
-
Save michaelperce/21c0addff39052c396e98a4f47af2d88 to your computer and use it in GitHub Desktop.
Selection of a few scripts from Golden Loft. All of these scripts were written solely by myself
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ClawGameController : MonoBehaviour | |
{ | |
public bool shouldResetForTesting = false; | |
public bool isEnabled = true, hasCompletedLesson = false; | |
public float timeToReturnToStart = 5f; | |
public GameObject FinalHandle; | |
public AudioClip[] audioLessons; | |
private AudioSource lessonPlayer; | |
public AudioSource AmbientMusicPlayer; | |
public AudioClip[] introAndLoop; | |
public TextMeshProTweenColor[] displayArrays; | |
public TextMeshProTweenColor[] glowingSlideBarArray; | |
public TextMeshProTweenColor[] afterwardsDisplayArray; | |
public ParticleSystem turnedOnSparks; | |
public Transform LightHolder; | |
public GameObject LightObjectToClone; | |
[Tooltip("Valid values are 2, 3, 5, 6, 10, or 15")] | |
public int numLights = 5; | |
public float timePerEachLight = .25f; | |
private MovingLight[] lightArray; | |
private float clockTimer = 0; | |
private GameObject rewardHandle; | |
private SelectableObject button; | |
private Claw_Slider handle; | |
private Animator clawAnimationController; | |
private bool handleHasMoved = false; | |
private Controller masterT; | |
// | |
// | |
//This block of code is called when the scene starts | |
// | |
// | |
void Start() | |
{ | |
shouldResetForTesting = false; | |
masterT = FindObjectOfType<Controller>(); | |
button = GetComponentInChildren<SelectableObject>(); | |
button.OnInteract += LeverTriggered; | |
StartCoroutine(LateStart()); | |
} | |
IEnumerator LateStart() | |
{ | |
yield return new WaitForSeconds(0.2f); | |
lessonPlayer = GetComponent<AudioSource>(); | |
rewardHandle = GetComponentInChildren<PickUppableObject>().gameObject; | |
handle = GetComponentInChildren<Claw_Slider>(); | |
clawAnimationController = GetComponent<Animator>(); | |
button.disableTemporarily(true); | |
lightArray = new MovingLight[numLights]; | |
for (int i = 0; i < numLights; i++) | |
{ | |
GameObject g = Instantiate(LightObjectToClone, LightHolder.localPosition, Quaternion.identity, LightHolder); | |
MovingLight ml = g.GetComponent<MovingLight>(); | |
ml.SetDimensionsEachSide(); | |
ml.SetIndex(i * 30 / numLights); | |
lightArray[i] = ml; | |
g.SetActive(false); | |
} | |
turnedOnSparks.Stop(); | |
} | |
// | |
// | |
//This block of code manages turning the machine on and off | |
// | |
// | |
public void StartUpEngine() | |
{ | |
isEnabled = true; | |
button.disableTemporarily(false); | |
masterT.SetLesson(audioLessons[0]); | |
handle.hasPlayedLesson = false; | |
handle.GetComponent<AudioSource>().enabled = true; | |
foreach (MovingLight ml in lightArray) | |
{ | |
ml.gameObject.SetActive(true); | |
} | |
foreach (TextMeshProTweenColor tmp in displayArrays) | |
{ | |
tmp.gameObject.SetActive(true); | |
tmp.FadeIn(); | |
} | |
AmbientMusicPlayer.clip = introAndLoop[0]; | |
AmbientMusicPlayer.Play(); | |
turnedOnSparks.Play(); | |
} | |
public void ShutDownEngine() | |
{ | |
//uncomment this code if you want the player to be able to replay lesson very easily | |
//handle.hasPlayedLesson = false; | |
//handle.GetComponent<AudioSource>().enabled = true; | |
foreach (MovingLight ml in lightArray) | |
{ | |
ml.gameObject.SetActive(false); | |
} | |
foreach (TextMeshProTweenColor tmp in displayArrays) | |
{ | |
tmp.FadeOut(); | |
} | |
foreach (TextMeshProTweenColor tmp in glowingSlideBarArray) | |
{ | |
tmp.FadeOut(); | |
} | |
isEnabled = false; | |
hasCompletedLesson = true; | |
StartCoroutine(MusicFade()); | |
} | |
//Fades the music over time | |
IEnumerator MusicFade() | |
{ | |
float startTime = 0; | |
float currentVolume = AmbientMusicPlayer.volume; | |
while (startTime < 3) | |
{ | |
yield return null; | |
startTime += Time.deltaTime; | |
AmbientMusicPlayer.volume = Mathf.Lerp(currentVolume, 0, startTime / 3); | |
} | |
AmbientMusicPlayer.Stop(); | |
foreach (TextMeshProTweenColor tmp in afterwardsDisplayArray) | |
{ | |
tmp.FadeIn(); | |
} | |
turnedOnSparks.Stop(); | |
masterT.incrementMainPathCompleted(); | |
} | |
// | |
// | |
//This block of code manages how the lights move around | |
// | |
// | |
private void Update() | |
{ | |
if (shouldResetForTesting) | |
{ | |
shouldResetForTesting = false; | |
StartUpEngine(); | |
} | |
if (isEnabled) | |
{ | |
if (clockTimer >= timePerEachLight) | |
{ | |
clockTimer = 0; | |
foreach (MovingLight ml in lightArray) | |
{ | |
ml.IncrementIndex(); | |
} | |
} | |
else | |
{ | |
clockTimer += Time.deltaTime; | |
} | |
if (!AmbientMusicPlayer.isPlaying) | |
{ | |
AmbientMusicPlayer.clip = introAndLoop[1]; | |
AmbientMusicPlayer.Play(); | |
} | |
} | |
} | |
// | |
// | |
//Scripts below here are called when the button is pushed | |
// | |
// | |
public void LeverTriggered(Controller t) | |
{ | |
if(!t.isPlayingMainPathLesson() && isEnabled) | |
{ | |
button.disableTemporarily(true); | |
button.GetComponent<AudioSource>().Play(); | |
// | |
//If the value is correct, do the animation where the handle comes with | |
// | |
if (isEnabled && !masterT.isPlayingMainPathLesson()) | |
{ | |
if ((handle.value > 34 - 0.1 && handle.value < 34 + .1) && !handleHasMoved) | |
{ | |
clawAnimationController.SetBool("CorrectValue", true); | |
StartCoroutine(WaitThenReturn(true)); | |
//if the correct value has been inputted, don't play that animation again | |
handleHasMoved = true; | |
} | |
else | |
{ | |
clawAnimationController.SetBool("CompleteMiss", true); | |
StartCoroutine(WaitThenReturn(false)); | |
} | |
} | |
else | |
{ | |
StartCoroutine(ButtonDownAndUp()); | |
} | |
} | |
} | |
IEnumerator ButtonDownAndUp() | |
{ | |
//Make the button go down then back up | |
Vector3 startPos = button.transform.localPosition; | |
Vector3 tempPos = startPos; | |
tempPos.y = 3.36f; | |
float timeSinceStart = 0; | |
float timeToDepress = 0.25f; | |
while (timeSinceStart < timeToDepress) | |
{ | |
yield return null; | |
timeSinceStart += Time.deltaTime; | |
button.transform.localPosition = Vector3.Lerp(startPos, tempPos, timeSinceStart / timeToDepress); | |
} | |
timeSinceStart = 0; | |
while (timeSinceStart < timeToDepress) | |
{ | |
yield return null; | |
timeSinceStart += Time.deltaTime; | |
button.transform.localPosition = Vector3.Lerp(tempPos, startPos, timeSinceStart / timeToDepress); | |
} | |
button.disableTemporarily(false); | |
} | |
IEnumerator WaitThenReturn(bool isCorrectValue) | |
{ | |
Vector3 tempPos; | |
if (isCorrectValue) | |
{ | |
masterT.SetLesson(audioLessons[2]); | |
} | |
rewardHandle.transform.parent.GetComponent<Animator>().enabled = false; | |
yield return StartCoroutine(ButtonDownAndUp()); | |
button.disableTemporarily(true); | |
// | |
//Wait for the grabbing animation to be done | |
// | |
yield return new WaitForSeconds(1f); | |
while (clawAnimationController.GetCurrentAnimatorStateInfo(0).normalizedTime < 1 && !clawAnimationController.GetCurrentAnimatorStateInfo(0).IsName("Default")) | |
{ | |
yield return null; | |
} | |
// | |
//Automatically push the handle back towards the starting position | |
//If you have the correct value, also pull the CabinetHandle back | |
// | |
Vector3 velocity = new Vector3((handle.defaultValue - handle.rightistValue) / 4, 0, 0); | |
while (handle.transform.localPosition.x > handle.defaultValue + .1f || handle.transform.localPosition.x < handle.defaultValue - .1f) | |
{ | |
yield return null; | |
if (handle.transform.localPosition.x > handle.defaultValue) | |
{ | |
handle.transform.localPosition -= velocity * Time.deltaTime; | |
if (isCorrectValue) | |
{ | |
rewardHandle.transform.parent.transform.localPosition -= velocity * Time.deltaTime; | |
} | |
} | |
else if (handle.transform.localPosition.x < handle.defaultValue) | |
{ | |
handle.transform.localPosition += velocity * Time.deltaTime; | |
if (isCorrectValue) | |
{ | |
rewardHandle.transform.parent.transform.localPosition += velocity * Time.deltaTime; | |
} | |
} | |
} | |
handle.transform.localPosition = handle.startingPosition; | |
tempPos = rewardHandle.transform.localPosition; | |
yield return new WaitForSeconds(1f); | |
//Reset | |
clawAnimationController.SetBool("CompleteMiss", false); | |
clawAnimationController.SetBool("CorrectValue", false); | |
rewardHandle.transform.localPosition = tempPos; | |
while (handle.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().enabled) | |
{ | |
yield return null; | |
} | |
// | |
//If correct value, make the handle fall (Animation Controlled) | |
// | |
if (isCorrectValue) | |
{ | |
rewardHandle.transform.parent.GetComponent<Animator>().enabled = true; | |
rewardHandle.transform.parent.GetComponent<Animator>().SetBool("IsTimeToFall", true); | |
clawAnimationController.enabled = false; | |
ShutDownEngine(); | |
} | |
button.disableTemporarily(false); | |
} | |
public void SetFinished() | |
{ | |
handle.hasPlayedLesson = true; | |
handle.GetComponent<AudioSource>().enabled = false; | |
masterT.SetLesson(null); | |
AmbientMusicPlayer.Stop(); | |
FinalHandle.SetActive(true); | |
foreach (MovingLight ml in lightArray) | |
{ | |
ml.gameObject.SetActive(false); | |
} | |
foreach (TextMeshProTweenColor tmp in displayArrays) | |
{ | |
tmp.FadeOut(); | |
} | |
foreach (TextMeshProTweenColor tmp in glowingSlideBarArray) | |
{ | |
tmp.FadeOut(); | |
} | |
isEnabled = false; | |
hasCompletedLesson = true; | |
foreach (TextMeshProTweenColor tmp in afterwardsDisplayArray) | |
{ | |
tmp.FadeIn(); | |
} | |
turnedOnSparks.Stop(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using TMPro; | |
public class Claw_Slider : InteractableHolder | |
{ | |
public float OverallLengthForSlider = 55f; | |
public float speed = 1f; | |
//0 <= value <= 55 | |
public float value = 0; | |
private float oldValue = 0; | |
public float timeToFinishSlide = .25f; | |
public float lockTime = 0.25f; | |
public int correctValue = 5; | |
public TMP_FontAsset CorrectValueMaterial; | |
private TMP_FontAsset DefaultMaterial; | |
public Transform bigOverSmall; | |
public TextMeshPro EqualSign; | |
public TextMeshPro answers; | |
public Transform overallOverBig; | |
public Transform[] leftBars; | |
public Transform[] rightBars; | |
public TextMeshProTweenColor helperText; | |
[HideInInspector] public float leftistValue; | |
[HideInInspector] public float rightistValue; | |
[HideInInspector] public float defaultValue; | |
[HideInInspector] public Vector3 startingPosition; | |
private float lengthPerSegment; | |
private ClawGameController masterController; | |
private Controller masterT; | |
private float initZ; | |
private float startFinishTime = 0; | |
private float currentLockTime = 0; | |
private float targetAngleToFinish; | |
private float tempValue; | |
private bool canPlaySFX = false, isLocked = false, canUpdate = false, canFadeHelperText = false; | |
private ShmancyLock parentLock; | |
private AudioSource SFX; | |
void Start() | |
{ | |
StartCoroutine(LateStart()); | |
} | |
IEnumerator LateStart() | |
{ | |
yield return new WaitForSeconds(0.075f); | |
tempValue = value; | |
intensity = 0f; | |
initZ = transform.localPosition.x; | |
currentRenderer = GetComponent<Renderer>(); | |
parentLock = GetComponentInParent<ShmancyLock>(); | |
SFX = GetComponent<AudioSource>(); | |
masterController = transform.parent.GetComponent<ClawGameController>(); | |
startingPosition = transform.localPosition; | |
DefaultMaterial = EqualSign.font; | |
leftistValue = 62.26f; | |
rightistValue = -52.37f; | |
defaultValue = 49.68982f; | |
startingPosition.x = defaultValue; | |
transform.localPosition = startingPosition; | |
masterT = FindObjectOfType<Controller>(); | |
lengthPerSegment = (leftistValue - rightistValue) / OverallLengthForSlider; | |
UpdateValues(); | |
yield return StartCoroutine(Adjustment()); | |
canUpdate = true; | |
} | |
override protected void Update() | |
{ | |
if (canUpdate) | |
{ | |
base.Update(); | |
if (masterController.isEnabled) | |
{ | |
if (!isLocked) | |
{ | |
//Change the position based on intensity | |
Vector3 newLocation = transform.localPosition; | |
newLocation += new Vector3(speed * intensity * Time.deltaTime, 0, 0); | |
newLocation.x = Mathf.Clamp(newLocation.x, rightistValue, leftistValue); | |
transform.localPosition = newLocation; | |
float relativeChange = Mathf.Abs((initZ - transform.localPosition.x)); | |
valChanged(relativeChange); | |
// | |
//Check to see if the handle should lock a little | |
// | |
if (value > 33.8f && oldValue < 33.8f || | |
value < 34.2f && oldValue > 34.2f) | |
{ | |
isLocked = true; | |
value = 34; | |
oldValue = 34; | |
//given a value of 34, calculate the correct position | |
float precisePercent = Mathf.InverseLerp(0, 55, value); | |
float precisePosition = Mathf.Lerp(leftistValue, rightistValue, precisePercent); | |
transform.localPosition = new Vector3(precisePosition, transform.localPosition.y, transform.localPosition.z); | |
} | |
if (isLocked) | |
{ | |
currentLockTime = lockTime; | |
} | |
} | |
// | |
//Whether locked or not | |
// | |
if (currentLockTime <= 0f) | |
{ | |
isLocked = false; | |
} | |
else | |
{ | |
currentLockTime -= Time.deltaTime; | |
} | |
UpdateValues(); | |
} | |
// | |
//If the machine is disabled, make helper text appear and disappear | |
// | |
else if (!masterController.hasCompletedLesson) | |
{ | |
if (intensity != 0 && !canFadeHelperText) | |
{ | |
canFadeHelperText = true; | |
helperText.FadeIn(); | |
} | |
else if (intensity == 0 && canFadeHelperText) | |
{ | |
canFadeHelperText = false; | |
helperText.FadeOut(); | |
} | |
} | |
} | |
} | |
public void UpdateValues() | |
{ | |
//total width is "55" | |
//current position divided by length of one segment is value | |
//total minus value is other segment | |
value = Mathf.Abs((transform.localPosition.x - leftistValue) / lengthPerSegment); | |
//update the displays | |
bigOverSmall.GetChild(0).GetComponent<TextMeshPro>().text = FormatNumber(value, 3); | |
bigOverSmall.GetChild(1).GetComponent<TextMeshPro>().text = FormatNumber(OverallLengthForSlider - value, 3); | |
overallOverBig.GetChild(0).GetComponent<TextMeshPro>().text = FormatNumber(OverallLengthForSlider, 3); | |
overallOverBig.GetChild(1).GetComponent<TextMeshPro>().text = FormatNumber(value, 3); | |
answers.text = "\t " + FormatNumber(value / (OverallLengthForSlider - value), 4) + "\t\t\t " + FormatNumber(OverallLengthForSlider / value, 4); | |
//Update the length of the colorful bars | |
//.915 is max length for displaybars | |
foreach (Transform t in leftBars) | |
{ | |
t.localScale = new Vector3(0.915f * value / OverallLengthForSlider, .215f, 1); | |
} | |
foreach (Transform t in rightBars) | |
{ | |
t.localScale = new Vector3(0.915f * (OverallLengthForSlider - value) / OverallLengthForSlider, .215f, 1); | |
} | |
//update oldvalue | |
if (Mathf.Abs(value - oldValue) > 0.05f) | |
{ | |
oldValue = value; | |
} | |
//check to see if you should update the color of the equals sign | |
if (value > 34 - 0.001f && value < 34 + 0.001f) | |
{ | |
EqualSign.font = CorrectValueMaterial; | |
EqualSign.color = Color.red; | |
} | |
else | |
{ | |
EqualSign.font = DefaultMaterial; | |
EqualSign.color = Color.black; | |
} | |
} | |
//Truncates the numberToFormat to be a constanst number of digits | |
public string FormatNumber(float numberToFormat, int numPlaces) | |
{ | |
//Return NaN when the number is effectively infinite | |
if (numberToFormat >= 1000000) | |
{ | |
return "NaN"; | |
} | |
float f = numberToFormat; | |
string formatText = ""; | |
//If the number is more than 1000, transition to k -> 1.12k | |
if (f >= 1000) | |
{ | |
return FormatNumber(f / 1000, numPlaces - 1) + "k"; | |
} | |
else if (f > (34.0f / 21.0) - 0.001f && f < (34.0f / 21.0) + 0.001f) | |
{ | |
return "1.618"; | |
} | |
while (formatText.Length < numPlaces) | |
{ | |
formatText += "0"; | |
} | |
int decimalIndex = 0; | |
//Determine where the decimal should go | |
do | |
{ | |
decimalIndex++; | |
f /= 10; | |
} while (f >= 1 - 0.001f); | |
formatText = formatText.Insert(decimalIndex, "."); | |
//return the formatted number | |
return numberToFormat.ToString(formatText); | |
} | |
public override void StartInteraction() | |
{ | |
currentRenderer.material = MaterialHighlight; | |
SetHolding(true); | |
if (masterController.isEnabled && !hasPlayedLesson && lessonOnFirstGrab) | |
{ | |
FindObjectOfType<Controller>().AddToAudioQueue(lessonOnFirstGrab); | |
hasPlayedLesson = true; | |
StartCoroutine(FadeInCorrectTime(masterController.glowingSlideBarArray)); | |
} | |
} | |
IEnumerator FadeInCorrectTime(TextMeshProTweenColor[] arrayOfBars) | |
{ | |
yield return new WaitForSeconds(.2f); | |
while(masterT.GetCurrentClip() != lessonOnFirstGrab) | |
{ | |
yield return null; | |
} | |
// | |
//Equation Values are expected to be Numerator, Denominator, Quotient | |
// | |
float timeSinceStart = 0; | |
int childIndex = 0; | |
//ChildIndex 0 and 1 are the numbers, 2 is the answer number | |
float currentChildDrawTime = arrayOfBars[childIndex].drawTime; | |
float currentChildFadeInPoint = arrayOfBars[childIndex].timeToBeCompletelyVisible; | |
while (masterT.isPlayingMainPathLesson()) | |
{ | |
timeSinceStart += Time.deltaTime; | |
yield return null; | |
//If it is time to fade in the next object(s) | |
if (timeSinceStart > currentChildFadeInPoint - currentChildDrawTime) | |
{ | |
arrayOfBars[childIndex].FadeIn(); | |
childIndex++; | |
if (childIndex >= arrayOfBars.Length) | |
{ | |
yield break; | |
} | |
currentChildDrawTime = arrayOfBars[childIndex].drawTime; | |
currentChildFadeInPoint = arrayOfBars[childIndex].timeToBeCompletelyVisible; | |
} | |
} | |
} | |
override public void EndInteraction() | |
{ | |
base.EndInteraction(); | |
intensity = 0; | |
//Adjust to closest whole value | |
if (masterController.isEnabled) | |
{ | |
StartCoroutine(Adjustment()); | |
} | |
} | |
//Moves the slider to the closest number | |
public IEnumerator Adjustment() | |
{ | |
//Calculate needed position with goal value | |
//lerp between current pos and that pos over a certain amount of time | |
//Round the currentvalue to the nearest 0.1f | |
float goalValue = Mathf.Round(value); | |
Vector3 startPosition = transform.localPosition; | |
Vector3 goalPosition = startPosition; | |
goalPosition.x = leftistValue - (goalValue * lengthPerSegment); | |
float timeSinceStart = 0f; | |
while (timeSinceStart < timeToFinishSlide) | |
{ | |
yield return null; | |
transform.localPosition = Vector3.Lerp(startPosition, goalPosition, timeSinceStart / timeToFinishSlide); | |
timeSinceStart += Time.deltaTime; | |
UpdateValues(); | |
} | |
transform.localPosition = goalPosition; | |
UpdateValues(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class HandleDropAnimationController : MonoBehaviour | |
{ | |
public Animator dropclaw; | |
public AudioClip soundOnHit; | |
public PickUppableObject secretChildHandle; | |
private AudioSource SFXMaker; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
SFXMaker = GetComponent<AudioSource>(); | |
} | |
public void StartSound() | |
{ | |
SFXMaker.clip = soundOnHit; | |
SFXMaker.Play(); | |
} | |
public void DoneSliding() | |
{ | |
Vector3 tempPos = transform.GetChild(0).localPosition; | |
dropclaw.enabled = true; | |
transform.GetChild(0).gameObject.SetActive(false); | |
secretChildHandle.gameObject.SetActive(true); | |
secretChildHandle.disableTemporarily(false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MovingLight : MonoBehaviour | |
{ | |
public int index = 0; | |
private Vector3[] startDimensionsEachSide; | |
public void IncrementIndex() | |
{ | |
index++; | |
if(index >= 30) | |
{ | |
index -= 30; | |
} | |
UpdatePosition(); | |
} | |
public void SetIndex(int newLocation) | |
{ | |
index = newLocation; | |
UpdatePosition(); | |
} | |
public void SetDimensionsEachSide() | |
{ | |
startDimensionsEachSide = new Vector3[3]; | |
startDimensionsEachSide[0] = new Vector3(76.23f, 80.34f, -31.771f); | |
startDimensionsEachSide[1] = new Vector3(71.86f, 80.34f, 26.93f); | |
startDimensionsEachSide[2] = new Vector3(-66.999f, 80.34f, 22.229f); | |
} | |
public void UpdatePosition() | |
{ | |
Vector3 directionToMove; | |
if(index < 7) | |
{ | |
directionToMove = new Vector3(0, 0, 9 * index); | |
transform.localRotation = Quaternion.Euler(0, 0, 0); | |
transform.localPosition = startDimensionsEachSide[0] + directionToMove; | |
} | |
else if(index < 23) | |
{ | |
directionToMove = new Vector3(-9 * (index-7), 0, 0); | |
transform.localRotation = Quaternion.Euler(0, 270, 0); | |
transform.localPosition = startDimensionsEachSide[1] + directionToMove; | |
} | |
else | |
{ | |
directionToMove = new Vector3(0, 0, -9 * (index-23)); | |
transform.localRotation = Quaternion.Euler(0, 180, 0); | |
transform.localPosition = startDimensionsEachSide[2] + directionToMove; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PickUppableObject : BringObjectClose | |
{ | |
public float timeToFadeIn = 1f; | |
public GameObject ObjectToClone; | |
public Vector3 positionInHand; | |
public Vector3 customRotationForKeys; | |
public GameObject ExtraObjectToHighlight; | |
public LockedObject thisKeyGoesToThisLock; | |
private Renderer extraObjectToRender; | |
public GameObject enabledObjectOnPickup; | |
private GameObject ClonedObject; | |
private Transform handPosition; | |
private bool canChangeOverlay = true; | |
private int overlayIndex = 0; | |
private float startFadeTime = 0; | |
private Controller theController; | |
private AudioSource minorLessonPlayer; | |
private int numKids; | |
private bool hasPlayedOneShotYet = false; | |
override protected void Start() | |
{ | |
base.Start(); | |
if (ExtraObjectToHighlight) | |
extraObjectToRender = ExtraObjectToHighlight.GetComponent<Renderer>(); | |
OnInteract += Clone; | |
StartCoroutine(LateStart()); | |
} | |
IEnumerator LateStart() | |
{ | |
yield return new WaitForSeconds(.15f); | |
//Keys should be empty | |
if (!ObjectToClone) | |
{ | |
ObjectToClone = this.gameObject; | |
} | |
//Keys in hand get disabled | |
else if (ObjectToClone.Equals(gameObject)) | |
{ | |
isDisabled = true; | |
} | |
//Other Pickups | |
else | |
{ | |
ObjectToClone.SetActive(false); | |
} | |
numKids = ObjectToClone.transform.childCount; | |
minorLessonPlayer = FindObjectOfType<MinorObjectLessonPlayer>().gameObject.GetComponent<AudioSource>(); | |
} | |
// | |
//Highlight manager | |
// | |
override protected void Update() | |
{ | |
base.Update(); | |
if (!isDisabled) | |
{ | |
if (highlightOn && ExtraObjectToHighlight) | |
{ | |
if(extraObjectToRender.sharedMaterial != MaterialHighlight) | |
{ | |
extraObjectToRender.material = MaterialHighlight; | |
} | |
} | |
else if (ExtraObjectToHighlight) | |
{ | |
if(extraObjectToRender.sharedMaterial != MaterialDefault) | |
{ | |
extraObjectToRender.material = MaterialDefault; | |
} | |
} | |
highlightOn = false; | |
} | |
} | |
// | |
// | |
//Code that Runs on interaction | |
// | |
// | |
private void Clone(Controller t) | |
{ | |
//if you don't have a hand position | |
//or | |
//active hand is not the same as hand position | |
//set hand position to the right look for remote | |
if (!handPosition || | |
t.leftHandRemoteTracker.thisHandIsActive && handPosition.gameObject != t.leftHandRemoteTracker.gameObject || | |
t.rightHandRemoteTracker.thisHandIsActive && handPosition.gameObject != t.rightHandRemoteTracker.gameObject) | |
{ | |
if (t.leftHandRemoteTracker.thisHandIsActive) | |
{ | |
handPosition = t.leftHandRemoteTracker.transform; | |
} | |
else if (t.rightHandRemoteTracker.thisHandIsActive) | |
{ | |
handPosition = t.rightHandRemoteTracker.transform; | |
} | |
else | |
{ | |
handPosition = t.NoHandDesloc.transform.GetComponentInChildren<LookForRemote>().transform; | |
} | |
} | |
//This is for the basic Image Pickup | |
if (this.gameObject.transform.childCount > 0) | |
{ | |
ObjectToClone.SetActive(true); | |
GetComponent<AudioSource>().Play(); | |
ObjectToClone.transform.SetParent(handPosition); | |
ObjectToClone.transform.localEulerAngles = new Vector3(0, 90, 60); | |
ObjectToClone.transform.localPosition = positionInHand; | |
if (lessonOnPickup && !gameObject.CompareTag("Shell")) | |
{ | |
minorLessonPlayer.Stop(); | |
minorLessonPlayer.clip = lessonOnPickup; | |
minorLessonPlayer.Play(); | |
} | |
//Basic Image With Overlays | |
if (ObjectToClone.transform.childCount > 0) | |
{ | |
overlayIndex = 0; | |
StopCoroutine(OverlayFadeMaster(t)); | |
StartCoroutine(OverlayFadeMaster(t)); | |
} | |
} | |
//Pick up for keys | |
else | |
{ | |
disableTemporarily(true); | |
isDisabled = true; | |
currentRenderer.material = MaterialDisabled; | |
ObjectToClone = this.gameObject; | |
GameObject temp = Instantiate(ObjectToClone, handPosition.position, Quaternion.identity, handPosition); | |
temp.transform.localEulerAngles = customRotationForKeys; | |
temp.transform.localPosition = positionInHand; | |
MeshCollider clonedMeshCollider = temp.GetComponent<MeshCollider>(); | |
BoxCollider clonedBoxCollider = temp.GetComponent<BoxCollider>(); | |
if (clonedMeshCollider) | |
{ | |
clonedMeshCollider.enabled = false; | |
} | |
else if (clonedBoxCollider) | |
{ | |
clonedBoxCollider.enabled = false; | |
} | |
temp.GetComponent<Renderer>().material = MaterialDisabled; | |
ClonedObject = temp; | |
theController = t; | |
if (thisKeyGoesToThisLock) | |
{ | |
thisKeyGoesToThisLock.disableTemporarily(false); | |
} | |
if (lessonOnPickup) | |
{ | |
masterT.AddToAudioQueue(lessonOnPickup); | |
} | |
} | |
this.gameObject.GetComponent<MeshRenderer>().enabled = false; | |
// | |
//Extra behavior for weird cornercases | |
// | |
if (this.gameObject.CompareTag("PickUpBasic")) | |
{ | |
} | |
else if (this.gameObject.CompareTag("Shell")) | |
{ | |
if(overlayIndex == 0) | |
{ | |
t.SetLesson(lessonOnPickup); | |
overlayIndex++; | |
} | |
//Make orbs appear | |
if(t.numAtticMainPathCompleted < 11) | |
{ | |
t.incrementMainPathCompleted(); | |
} | |
enabledObjectOnPickup.SetActive(true); | |
if(!hasPlayedOneShotYet) | |
{ | |
hasPlayedOneShotYet = true; | |
minorLessonPlayer.GetComponent<MinorObjectLessonPlayer>().canUpdate = true; | |
} | |
} | |
else | |
{ | |
if (gameObject.transform.localScale.x > 10) | |
{ | |
ClonedObject.transform.localScale = new Vector3(0.9114001f, 0.9114001f, 0.9114001f); | |
} | |
else if (gameObject.transform.localScale.x > 1.001 && gameObject.transform.localScale.x < 1.05f) | |
{ | |
ClonedObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f); | |
} | |
ClonedObject.SetActive(true); | |
ClonedObject.GetComponent<PickUppableObject>().isDisabled = true; | |
} | |
} | |
// | |
//Pickup Items with overlays | |
// | |
IEnumerator OverlayFadeMaster(Controller t) | |
{ | |
float startTime = 0f; | |
ObjectToClone.transform.GetChild(0).gameObject.SetActive(true); | |
TextMeshProTweenColor child = ObjectToClone.transform.GetChild(overlayIndex).GetComponent<TextMeshProTweenColor>(); | |
while (startTime < minorLessonPlayer.clip.length && minorLessonPlayer.clip == lessonOnPickup) | |
{ | |
startTime += Time.deltaTime; | |
yield return null; | |
if (overlayIndex < numKids && startTime >= child.timeToBeCompletelyVisible - child.drawTime) | |
{ | |
if (overlayIndex > 0) | |
{ | |
ObjectToClone.transform.GetChild(overlayIndex - 1).GetComponent<TextMeshProTweenColor>().FadeOut(); | |
} | |
child.FadeIn(); | |
overlayIndex++; | |
if (overlayIndex < numKids) | |
{ | |
ObjectToClone.transform.GetChild(overlayIndex).gameObject.SetActive(true); | |
child = ObjectToClone.transform.GetChild(overlayIndex).GetComponent<TextMeshProTweenColor>(); | |
} | |
} | |
} | |
foreach (Transform trans in ObjectToClone.transform) | |
{ | |
trans.GetComponent<TextMeshProTweenColor>().FadeOut(); | |
} | |
yield return new WaitForSeconds(child.drawTime); | |
disableTemporarily(false); | |
} | |
// | |
// | |
//When the player tries to put the item down | |
// | |
// | |
override public void StopHolding(Controller t) | |
{ | |
base.StopHolding(t); | |
//Basic Image Putdown | |
if (this.gameObject.CompareTag("PickUpBasic")) | |
{ | |
StopAllCoroutines(); | |
if (ObjectToClone.transform.childCount > 0) | |
{ | |
overlayIndex = 0; | |
foreach(Transform trans in ObjectToClone.transform) | |
{ | |
trans.GetComponent<TextMeshProTweenColor>().SetZero(); | |
} | |
} | |
ObjectToClone.SetActive(false); | |
ObjectToClone.transform.SetParent(this.gameObject.transform); | |
this.gameObject.GetComponent<MeshRenderer>().enabled = true; | |
minorLessonPlayer.Stop(); | |
if (lessonOnPutDown) | |
{ | |
minorLessonPlayer.clip = lessonOnPutDown; | |
minorLessonPlayer.Play(); | |
} | |
} | |
else if (this.gameObject.CompareTag("Shell")) | |
{ | |
StopAllCoroutines(); | |
ObjectToClone.SetActive(false); | |
ObjectToClone.transform.SetParent(this.gameObject.transform); | |
this.gameObject.GetComponent<MeshRenderer>().enabled = true; | |
theController = t; | |
//Make orbs disappear | |
//enabledObjectOnPickup.SetActive(false); | |
} | |
//Stop Holding Keys | |
else | |
{ | |
t.SetLesson(null); | |
Destroy(ClonedObject); | |
disableTemporarily(false); | |
if (thisKeyGoesToThisLock) | |
{ | |
thisKeyGoesToThisLock.disableTemporarily(true); | |
} | |
this.gameObject.GetComponent<MeshRenderer>().enabled = true; | |
theController.SetLesson(null); | |
} | |
} | |
//Special edge case scenario | |
override public void StopHoldingNoBack() | |
{ | |
base.StopHoldingNoBack(); | |
theController.SetPickedObject(null); | |
theController.incrementMainPathCompleted(); | |
Destroy(ClonedObject); | |
} | |
public void SetFinished(int shouldPlaySoundAtIndex) | |
{ | |
theController = FindObjectOfType<Controller>(); | |
this.gameObject.GetComponent<MeshRenderer>().enabled = false; | |
if(theController.numAtticMainPathCompleted == shouldPlaySoundAtIndex) | |
{ | |
thisKeyGoesToThisLock.Unlock(theController); | |
theController.SetLesson(lessonOnPutDown); | |
} | |
else | |
{ | |
thisKeyGoesToThisLock.Unlock(null); | |
} | |
this.gameObject.SetActive(false); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment