public void breakApart() { GameControl.tallyPoints(pts); if (type < 4) { // break into other asteroids int count = (int)Mathf.Round(Random.Range(2f, 4f)); for (var i = 0; i < count; i++) { Vector3 offset = new Vector3(Random.Range(-0.25f, 0.25f), Random.Range(-0.25f, 0.25f), 0f); GameObject debris = Instantiate(smallAsteroid, transform.position + offset, Quaternion.Euler(0f, 0f, Random.Range(0f, 360f))); AsteroidControl astr = debris.GetComponent <AsteroidControl>(); Vector3 vel = (transform.position - debris.transform.position); if (vel == Vector3.zero) { vel = Vector3.right; } vel.Normalize(); vel *= GameControl.asteroidSpeed; astr.setVars(5, Random.Range(-0.5f, 0.5f), 9, vel); } } int particle = (int)Mathf.Round(Random.Range(0f, 2f)); // break into particles if (particle == 0 || particle == 2) { Instantiate(largeDebris, transform.position, Quaternion.Euler(90f, 0f, 0f)); } if (particle == 1 || particle == 2) { Instantiate(smallDebris, transform.position, Quaternion.Euler(90f, 0f, 0f)); } Destroy(gameObject); }
//---------------------------------------------------------------------------------------------------- //Awake() // Called on instantiation. Instantiate this instance. // // Param: // None // // Return: // Void //---------------------------------------------------------------------------------------------------- void Awake() { //x is a random number used to initialize movement and rotation should it not be set externally float x = Random.Range(-1f, 1f); //y is a random number used to initialize rotation should it not be set externally float y = Random.Range(-1f, 1f); //z is a random number used to initialize movement and rotation should it not be set externally float z = Random.Range(-1f, 1f); direction = new Vector3(x, 0, z); rotationDirection = new Vector3(z, y, z); asteroidControl = GameObject.FindObjectOfType <AsteroidControl>(); }
void Start() { GameObject gameControllerObject = GameObject.FindWithTag("GameController"); asteroidController = GetComponent <AsteroidControl>(); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent <GameController>(); } if (gameController == null) { Debug.Log("Cannot find 'GameController' script"); } }
// input, movement, collisions void Update() { // update trail trailPos = pos; pos = transform.position; if (trailDelay > 0) { trailDelay -= 1; } // input if (Input.GetMouseButtonDown(0)) { // set buffer clickBuffer = 10; } if (clickBuffer > 0) { clickBuffer -= 1; // test for dash avail. if (!dashing) { Vector2 clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (distance(clickPos, pos) > 0.8f) { // clamp cursor position Vector2 pointToClick = (clickPos - pos); pointToClick.Normalize(); pointToClick *= 0.8f; clickPos = (pos + pointToClick); } if (Mathf.Abs(clickPos.x) <= 0.7f && Mathf.Abs(clickPos.y) <= 0.7f) { target = clickPos; dashing = true; Vector3 vel = (clickPos - pos); vel.Normalize(); Instantiate(burst, transform.position + (vel * -0.08f), Quaternion.Euler(90f, 0f, transform.localEulerAngles.z)); } } } // movement if (dashing) { if (distance(target, pos) >= dashSpeed) { // move towards target Vector2 dashOffsets = target - pos; dashOffsets.Normalize(); dashOffsets *= dashSpeed; transform.position += ((Vector3.right * dashOffsets.x) + (Vector3.up * dashOffsets.y)); // move afterimage orangeChild.transform.position = Vector3.zero + (Vector3.right * trailPos.x) + (Vector3.up * trailPos.y); } else { // stop dashing transform.position = target; dashing = false; pos = target; aegis = 10; } // trail particles if (trailDelay == 0) { Instantiate(trail, transform.position, Quaternion.Euler(90f, 0f, 0f)); trailDelay = 4; } } else { // rotate to cursor position Vector3 lookPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); float angle = -Mathf.Atan2((lookPoint.x - pos.x), (lookPoint.y - pos.y)) * Mathf.Rad2Deg; transform.localEulerAngles = (Vector3.zero + (zUp * angle)); // move afterimage Vector2 pos2D = orangeChild.transform.position; if (pos2D != target) { orangeChild.transform.position = target; } orangeChild.transform.position = transform.position + ScreenManip.getOffset(0); blueChild.transform.position = transform.position + ScreenManip.getOffset(1); } // aegis if (aegis > 0) { aegis -= 1; } // collisions var collisions = Physics2D.BoxCastAll(pos, size, 0f, Vector2.zero, 0f, mask); if (collisions.Length > 0) { // colliding with an asteroid for (var i = 0; i < collisions.Length; i++) { GameObject collided = collisions[i].transform.gameObject; AsteroidControl astrCont = collided.GetComponent <AsteroidControl>(); if (astrCont.getAegis() == 0) { if (dashing) { // break asteroid astrCont.setAegis(9); astrCont.breakApart(); ScreenManip.screenShake(0.03f, 6); } else if (aegis == 0) { // die and start respawn sequence GameControl.playerDie(120); Instantiate(smallDebris, transform.position, Quaternion.Euler(90f, 0f, 0f)); ScreenManip.screenShake(0.06f, 12); } } } } }
// Move, bounce void Update() { // Update lifespan if (lifespan > 0) { lifespan -= 1; } else { Destroy(this.gameObject); } // Update aegis if (aegis > 0) { aegis -= 1; Vector2 pos = transform.position; if (!Physics2D.CircleCast(pos, size, Vector2.zero, 0f, playerMask)) { aegis = 0; } } // clamp velocity so it doesn't get out of hand if (velocity.magnitude > GameControl.asteroidSpeed) { velocity.Normalize(); velocity *= GameControl.asteroidSpeed; } // Move transform.position += velocity; transform.rotation *= Quaternion.AngleAxis(angleIncrement, zUp); // Bounce Vector2 posit = transform.position; RaycastHit2D[] others = Physics2D.CircleCastAll(posit, size, Vector2.zero, 0f, selfMask); if (others.Length > 0) { // process collisions for (var i = 0; i < others.Length; i++) { // make sure we're not colliding with ourselves if (others[i].collider.gameObject.GetInstanceID() != this.gameObject.GetInstanceID()) { AsteroidControl other = others[i].collider.gameObject.GetComponent <AsteroidControl>(); // get momentum/angular momentum of both involved float Mtot = (mass + other.getMass()); Vector3 Ptot = (mass * velocity) + (other.getMass() * other.getVelocity()); float Ltot = (mass * Mathf.Pow(this.getRadius(), 2f) * angleIncrement) + (other.getMass() * Mathf.Pow(other.getRadius(), 2f) * other.getAngSpd()); // reverse parallel vel. components Vector3 selfToOther = (other.transform.position - transform.position); selfToOther.Normalize(); Vector3 selfResult = (-selfToOther) * Vector3.Dot(velocity, selfToOther); Vector3 otherResult = (selfToOther) * Vector3.Dot(other.getVelocity(), -selfToOther); // set velocity/angular velocities accordingly velocity = GameControl.collisionDampen * ((Ptot / Mtot) + (2 * selfResult)); angleIncrement = GameControl.collisionDampen * (Ltot / (other.getMass() * Mathf.Pow(getRadius(), 2f))); Vector3 otherVel = GameControl.collisionDampen * ((Ptot / Mtot) + (2 * otherResult)); float otherAngSpd = GameControl.collisionDampen * (Ltot / (mass * Mathf.Pow(other.getRadius(), 2f))); // set for other other.setPhys(otherVel, otherAngSpd); } } } // Out-of-bounds if (Mathf.Abs(transform.position.x) > 0.9 || Mathf.Abs(transform.position.y) > 0.9) { Destroy(gameObject); } }