public Composite SpawnCube( Vector2 pos, float angle, float relativeForce, float burstDuration, float burstDelay, Color color) { Composite comp = CompositePool.Instance.ForceCreateComposite(); if (comp != null) { CubeScript spawned = null; float rand = Random.value; if (rand < cubeProb) { spawned = SpawnCube(relativeForce, burstDuration, burstDelay, color); } else if (rand < cubeProb + 0.0f) { //TODO: add probability based spawning } //center random position spawned.transform.position = transform.TransformPoint(new Vector3(pos.x, pos.y, 0.0f)) + 0.5f * Vector3.one; spawned.transform.rotation *= Quaternion.AngleAxis(angle, Vector3.up); spawned.transform.parent = comp.transform; comp.Color = spawned.Color; comp.HP = 1; comp.AbsorbRigidbody(spawned.GetComponent <Rigidbody>()); Destroy(spawned.GetComponent <Rigidbody>()); comp.CreateTarget(); } return(comp); }
public void StickTo(StickyFace stickyFace) { if (stickyFace == null || stickyFace.cube.PreventSticky || cube.PreventSticky) { return; } //TODO: add stickying to bigger component (order decider) Composite root = GetComponentInParent <Composite>(); Composite stickyFaceRoot = stickyFace.GetComponentInParent <Composite>(); root.transform.rotation = stickyFaceRoot.transform.rotation; //this face's normal in world coordinates Vector3 normalWorld = cube.transform.TransformVector(localNormal).normalized; //the other face's normal in world coordinates Vector3 otherNormalWorld = stickyFace.cube.transform.TransformVector(stickyFace.localNormal).normalized; Vector3 cubeUp = cube.transform.TransformVector(new Vector3(0.0f, 1.0f, 0.0f)); //-otherNormalWorld to have faces against eachother, normals lining up root.transform.rotation *= Quaternion.AngleAxis(Vector3.SignedAngle(normalWorld, -otherNormalWorld, cubeUp), cubeUp); //target position in world coordinates to place this face (local normals match local point coordinates) Vector3 targetPos = stickyFace.cube.transform.TransformPoint(stickyFace.localNormal); root.transform.position += (targetPos - cube.transform.position); //force create because using normal create we might not be allowed //to create a new composite e.g. if the current comp number is 10 //and the max is 10, however because we destroy two composites in //the merging process the resulting number of composites will be //9 which is < 10 Composite composite = CompositePool.Instance.ForceCreateComposite(); composite.Merge(root, stickyFaceRoot); composite.Color = cube.GetComponent <CubeScript>().Color; }