void Start() { limbList = new List <TreeLimb>(); leafList = new List <Leaf>(); soil = FindObjectOfType <Soil>(); energy = GetComponent <Energy>(); // create trunk GameObject stem = Instantiate(limbPrefab, transform); TreeLimb tLimb = stem.GetComponent <TreeLimb>(); tLimb.SetLimbLine(false, transform.position, transform.up * 0.1f); tLimb.SetGrowTargetLength(0.3f); tLimb.SetGrowTargetWidth(0.02f); tLimb.SetColliderEnabled(true); tLimb.AttachToRb(soil.GetComponent <Rigidbody2D>()); limbList.Add(tLimb); PlantPhysics pp = stem.GetComponent <PlantPhysics>(); pp.SetComfortableAngle(3f); PlantPhysics pPhysics = tLimb.GetComponent <PlantPhysics>(); pPhysics.SetNaturalDirection(Vector3.up); tLimb.ImbueLeaf(); tLimb.bLifeCritical = true; sproutCoroutine = SproutInterval(); StartCoroutine(sproutCoroutine); }
void BreakOffTree() { bool gg = false; TreeLimb tLimb = GetComponent <TreeLimb>(); if (tLimb.bLifeCritical) { if (transform.root.GetComponent <Player>()) { FindObjectOfType <Game>().GameOver(); gg = true; } } HingeJoint2D joint = GetComponent <HingeJoint2D>(); rb.velocity = joint.connectedBody.velocity; joint.connectedBody = null; joint.enabled = false; transform.SetParent(null); bConnectedToTree = false; GetComponentInChildren <Leaf>().enabled = false; PlantPhysics[] childrenPhys = GetComponentsInChildren <PlantPhysics>(); foreach (PlantPhysics phys in childrenPhys) { phys.SetActive(false); } SetActive(false); if (!gg) { Destroy(gameObject, 5f); } }
void Start() { cam = Camera.main; limbPrototype = Instantiate(limbPrefab, null); limbPrototype.SetVisible(false); limbPrototype.SetColliderEnabled(false); closeupUI = FindObjectOfType <CloseupCameraUI>(); closeupUI.SetActive(false); closeupCamera = FindObjectOfType <CloseupCamera>(); }
public void ParentalGrowth(float lengthScale, float widthScale) { if (transform.parent != null) { TreeLimb parentLimb = transform.parent.GetComponent <TreeLimb>(); if (parentLimb != null) { parentLimb.SetGrowTargetLength(lengthScale); parentLimb.SetGrowTargetWidth(widthScale); parentLimb.ParentalGrowth(lengthScale, widthScale); } } }
void CheckOverlap() { if (Input.touchCount > 0) { Ray ray = cam.ScreenPointToRay(Input.GetTouch(0).position); bTargeting = false; Vector2 closestVector = Vector2.positiveInfinity; GameObject closestObj = null; RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction * 1.1f); if (hits.Length > 0) { foreach (RaycastHit2D hit in hits) { TreeLimb tLimb = hit.collider.gameObject.GetComponent <TreeLimb>(); if (tLimb != null) { bTargeting = true; Vector2 myPosition = cam.ScreenToWorldPoint(transform.position); Vector3 tLimbClosestPoint = tLimb.GetClosestBoundsPoints(hit.point); Vector2 limbHitPosition = new Vector2(tLimbClosestPoint.x, tLimbClosestPoint.y); Vector2 toHit = (limbHitPosition - myPosition); if (toHit.magnitude < closestVector.magnitude) { closestVector = toHit; closestObj = hit.collider.gameObject; } } } if (bTargeting && (closestObj != null)) { if (closestObj.transform.root.GetComponent <Player>()) { targetLimb = closestObj.GetComponent <TreeLimb>(); closeupUI.SetActive(true); } } } else { targetLimb = null; closeupUI.SetActive(false); } } }
void SpawnNewLimb() { bool bLimb = false; int tries = 0; while (!bLimb && (tries < (limbList.Count * limbList.Count))) { tries++; int parentLimbIndex = Random.Range(0, limbList.Count); if ((limbList[parentLimbIndex] != null)) { TreeLimb parentLimb = limbList[parentLimbIndex]; float parentLength = parentLimb.GetLimbLength(); float minimumLength = Mathf.Clamp(Mathf.Sqrt(parentLength), 0f, parentLength * 0.9f); float sproutLimbHeight = Random.Range(minimumLength, parentLength); Vector3 sproutPosition = parentLimb.transform.position + (parentLimb.transform.up * sproutLimbHeight); Quaternion spawnRotation = Quaternion.Euler(new Vector3(0f, 0f, Random.Range(-50f, 50f))); GameObject stem = Instantiate(limbPrefab, Vector3.zero, spawnRotation); stem.transform.SetParent(parentLimb.transform, false); stem.transform.position = sproutPosition; TreeLimb stemLimb = stem.GetComponent <TreeLimb>(); stemLimb.SetLimbLine(true, sproutPosition, stemLimb.transform.up * 0.1f); stemLimb.AttachToRb(parentLimb.GetComponent <Rigidbody2D>()); stemLimb.SetColliderEnabled(true); stemLimb.SetGrowTargetLength(parentLimb.GetLimbLength() * 0.36f); stemLimb.SetGrowTargetWidth(0.02f); stemLimb.ParentalGrowth(1.1f, 1.02f); PlantPhysics pPhysics = stemLimb.GetComponent <PlantPhysics>(); pPhysics.SetNaturalDirection(stemLimb.transform.up); stemLimb.ImbueLeaf(); limbList.Add(stemLimb); bLimb = true; } } }