public bool destroy_asteroid() { if (has_collided) { return(false); } score_keeper.add(points); if (spawned_debris != null) { for (int space_rock = 0; space_rock < 2; ++space_rock) { PlanetariaGameObject game_object = PlanetariaGameObject.Instantiate(spawned_debris, planetaria_transform.position, planetaria_transform.direction); Debris debris = game_object.GetComponent <Debris>(); debris.speed = this.speed; DebrisNoirs.live(game_object); // while asteroid revenge velocity (targetting satellite) is an interesting idea, // I don't think it's necessary (or would fully work as intended for that matter - it might be exploitable). // The game will probably be hard enough head/joystick controls // (and pros will still make mistakes eventually, even if its because of sleep deprivation) } } DebrisNoirs.die(this.gameObject); PlanetariaGameObject.Destroy(this.gameObject); return(true); }
public static Debris live() { Debris debris = object_pool[next_debris]; next_debris = next_debris >= object_pool.Count - 1 ? 0 : next_debris + 1; live(debris); return(debris); }
public void OnTriggerEnter(Collider collider) { if (!dead) // There's technically a small bug here, where the player can collide with debris that has already collided (with a projectile) - wraparound shots are basically impossible, so that would be for a shot on the ~ same frame { die(); Debris debris = collider.GetComponent <Debris>(); debris.destroy_asteroid(); // should not assign points, since the player died. } }
public static void expand(int capacity) // TODO: CONSIDER: For future versions, deleting an object and creating two for every object pool increment may get around the draw call dynamic batching "bug" (might be intended behaviour) -- where ~120 draw calls become ~20 (behaviour I personally expected) for the same number of objects but only after several destruction and instantiation calls. { int large_rocks_to_spawn = Mathf.Max(0, capacity - object_pool.Count); for (int spawned_root_node = 0; spawned_root_node < large_rocks_to_spawn; spawned_root_node += 1) { // Yeah, this is hardcoded boilerplate, but it gets the job done (rather than coding a recursive function for levels 1,2,3) // Large PlanetariaGameObject game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().large_debris, Vector3.forward, Vector3.up); Debris large = game_object.GetComponent <Debris>(); object_pool.Add(large); // add the root node only // Medium game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().medium_debris, Vector3.forward, Vector3.up); Debris medium1 = game_object.GetComponent <Debris>(); large.left_debris = medium1; game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().medium_debris, Vector3.forward, Vector3.up); Debris medium2 = game_object.GetComponent <Debris>(); large.right_debris = medium2; // Small game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().small_debris, Vector3.forward, Vector3.up); Debris small1 = game_object.GetComponent <Debris>(); medium1.left_debris = small1; game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().small_debris, Vector3.forward, Vector3.up); Debris small2 = game_object.GetComponent <Debris>(); medium1.right_debris = small2; game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().small_debris, Vector3.forward, Vector3.up); Debris small3 = game_object.GetComponent <Debris>(); medium2.left_debris = small3; game_object = PlanetariaGameObject.Instantiate(DebrisSpawner.self().small_debris, Vector3.forward, Vector3.up); Debris small4 = game_object.GetComponent <Debris>(); medium2.right_debris = small4; // Disable game objects until they are needed large.gameObject.SetActive(false); medium1.gameObject.SetActive(false); medium2.gameObject.SetActive(false); small1.gameObject.SetActive(false); small2.gameObject.SetActive(false); small3.gameObject.SetActive(false); small4.gameObject.SetActive(false); } }
// while I do like the idea of raycasting in update, that would require PlanetariaColliders or hacking my current raycast implementation, so true-to-original it is // Also, raycasting would be broken with relative projectile velocities public void OnTriggerEnter(Collider collider) { if (!has_collided) // make sure one projectile doesn't collide with 2+ debris. { Debris debris = collider.GetComponent <Debris>(); if (debris.destroy_asteroid()) // make sure two projectiles don't collide with the same debris (wasting one of them). { PlanetariaGameObject.Destroy(this.gameObject); has_collided = true; } } }
// while I do like the idea of raycasting in update, that would require PlanetariaColliders or hacking my current raycast implementation, so true-to-original it is // Also, raycasting would be broken with relative projectile velocities public void OnTriggerEnter(Collider collider) { if (!has_collided) // make sure one projectile doesn't collide with 2+ debris. { Debris debris = collider.GetComponent <Debris>(); if (debris.destroy_asteroid()) // make sure two projectiles don't collide with the same debris (wasting one of them). { //PlanetariaGameObject.Destroy(this.gameObject); -- the bullets are reused (as a form of object pooling) has_collided = true; mesh_renderer.enabled = false; sphere_collider.enabled = false; } } }
private void spawn_debris() { Vector3 satellite_position = satellite.position; for (int debris = 0; debris < debris_to_spawn; debris += 1) { Vector3 random_position = UnityEngine.Random.onUnitSphere; if (Vector3.Dot(random_position, satellite_position) > 0) { random_position *= -1; } Debris large_debris = DebrisNoirs.live(); large_debris.planetaria_transform.position = random_position; large_debris.speed = 0.09f; large_debris.initialize(); } }
public static void live(Debris debris) { DebrisNoirs.debris.Add(debris); debris.gameObject.SetActive(true); }
public static void die(Debris debris) { debris.has_collided = false; DebrisNoirs.debris.Remove(debris); debris.gameObject.SetActive(false); }