public static void Detach(CollectibleObject collectible, float diameter) { if (OnDetach != null) { OnDetach(collectible, diameter); } }
private void RecalculateRadius() { sphere.radius = Mathf.Pow((3 * volume) / (4 * Mathf.PI), ONE_THIRD); //check to see if we're big enough for irregular objects to stop making us bounce irregularly int irregulars = irregularCollectibles.Count; for (int i = irregulars - 1; i >= 0; --i) { CollectibleObject collectible = irregularCollectibles[i]; if (!collectible.IsIrregular(sphere.radius)) { Debug.Log("irregular rolled in"); irregularCollectibles.RemoveAt(i); if (collectible.rB == null) { collectible.rB = collectible.gameObject.AddComponent <Rigidbody>(); } collectible.rB.mass = 0; collectible.rB.detectCollisions = false; collectible.rB.isKinematic = true; } } }
public void SetCollected(CollectibleObject collected) { collectedRoot.SetActive(true); collectedText.text = string.Format(collectedFormat, collected.displayName); collectedObjectDisplay.Init(collected.GetMeshFilter().sharedMesh, collected.GetMaterial(), collected.transform.localScale); if (fade != null) { StopCoroutine(fade); } fade = StartCoroutine(Fade()); }
void OnDetach(CollectibleObject detached) { audioSource.PlayOneShot(detached.detachClip); //this could be improved by using a Dictionary and adding some sort of id to collectibles. collectibles.Remove(detached); if (irregularCollectibles.Contains(detached)) { irregularCollectibles.Remove(detached); } if (!detached.IsIrregular(sphere.radius)) { rB.mass -= detached.mass; } mass -= detached.mass; volume -= detached.volume; RecalculateRadius(); detached.Detach(this); EventManager.Detach(detached, sphere.radius * 2); }
private void OnAttach(CollectibleObject attached, float diameter) { view.SetCollected(attached); }
private bool OnContact(Collision collision) { bool rolledUp = false; Transform t = collision.transform; CollectibleObject collectible = t.GetComponent <CollectibleObject>(); if (collectible) { if (collectible.mass < mass * ROLL_UP_MAX_RATIO) { if (!collectible.collected) { AudioClip clip = collectible.GetRandomCollectAudio(); if (clip) { audioSource.PlayOneShot(clip); } //collect the thing we hit! collectible.collected = true; rolledUp = true; collectible.gameObject.layer = 9; collectible.Attach(this); t.parent = transform; volume += collectible.volume; mass += collectible.mass; rB.mass += collectible.mass; RecalculateRadius(); collectibles.Add(collectible); Vector3 delta = (collectible.transform.position - transform.position); float distance = delta.magnitude - sphere.radius; Vector3 direction = delta.normalized; collectible.transform.position = collectible.transform.position - direction * distance; EventManager.Attach(collectible, sphere.radius * 2); //large or irregular objects will make our katamari bounce differently until it grows large enough if (collectible.IsIrregular(sphere.radius)) { Destroy(collectible.rB); irregularCollectibles.Add(collectible); } else { collectible.rB.mass = 0; collectible.rB.detectCollisions = false; collectible.rB.isKinematic = true; } } } else { //decide how many objects to break off, then break them off. float magnitude = collision.relativeVelocity.magnitude; while (magnitude >= BREAK_OFF_THRESHOLD && collectibles.Count > 0) { CollectibleObject toRemove = collectibles[collectibles.Count - 1]; OnDetach(toRemove); magnitude -= 4.0f; } } } return(rolledUp); }