IEnumerator DoGenerate() { float h, s, v; Color.RGBToHSV(color, out h, out s, out v); //Color color = colors[currentColor%colors.Length]; for (int it = 0; it < _numIterations; it++) { for (int i = 0; i < _numLiquid; i++) { GameObject temp = (GameObject)Instantiate(_prefab); temp.transform.parent = transform; float x = Random.Range(-1.0f, 1.0f) * _width / 2; float y = Random.Range(-1.0f, 1.0f) * _height / 2; temp.transform.localPosition = new Vector3(x, y, 0); temp.transform.localScale = Vector3.one * scale; LiquidParticle p = temp.GetComponent <LiquidParticle>(); if (p != null) { p.liquidType = liquidType; temp.GetComponent <Renderer>().material.color = Random.ColorHSV(h - 0.01f, h + 0.01f, s - 0.1f, s + 0.1f, v - 0.1f, v + 0.1f); } Rigidbody2D rb2D = temp.GetComponent <Rigidbody2D>(); rb2D.mass = density; } yield return(new WaitForSeconds(timeBetweenDrops)); } }
void Remove(LiquidParticle particle) { if (contents.ContainsKey(particle.liquidType)) { contents[particle.liquidType].Remove(particle); } }
void Add(LiquidParticle particle) { if (!contents.ContainsKey(particle.liquidType)) { contents[particle.liquidType] = new List <LiquidParticle>(); } contents[particle.liquidType].Add(particle); }
/** * Spring constructor: ensures spring is correctly stored in both particles it is attached to */ public Spring(float restLength, LiquidParticle a, LiquidParticle b) { this.RestLength = restLength; this.A = a; this.B = b; a.ParticleToSpring.Add(b, this); b.ParticleToSpring.Add(a, this); }
void OnTriggerExit2D(Collider2D col) { Rigidbody2D otherRb = col.GetComponent <Rigidbody2D>(); LiquidParticle particle = col.GetComponent <LiquidParticle>(); if (particle != null) { particle.gameObject.layer = 8; particle.currentContainer = null; particle.rb2DParent = null; Remove(particle); } }
/// OnCollisionEnter2D /// This is where we would handle collisions between particles and call functions like our setState to change /// partcle types. Or we could just flat out destroy them etc.. /// a_otherParticle: The collision with another particle. Obviously not limited to particles so do a check in the method void OnCollisionEnter2D(Collision2D a_otherParticle) { LiquidParticle scr = a_otherParticle.gameObject.GetComponent <LiquidParticle>(); if (scr != null) { Debug.Log((int)currentState + (int)scr.currentState); if ((int)currentState + (int)scr.currentState == 1) { GameObject gas = Instantiate <GameObject>(gameObject, transform.position, transform.rotation); gas.GetComponent <LiquidParticle>().SetState(LiquidStates.Gas); Destroy(gameObject); Destroy(a_otherParticle.gameObject); } } }
/** * Returns a vector from particle1 to particle2 if they are neighbors, null otherwise */ private Vector2?GetOffsetIfNeighbors(LiquidParticle particle1, LiquidParticle particle2) { if (!particle1.PotentialNeighbors.Contains(particle2)) { return(null); } var offset = particle2.NewPosition - particle1.NewPosition; if (offset.LengthSquared() < Math.Pow(this.InteractionRadius, 2)) { return(offset); } return(null); }
void OnTriggerEnter2D(Collider2D col) { Rigidbody2D otherRb = col.GetComponent <Rigidbody2D>(); LiquidParticle particle = col.GetComponent <LiquidParticle>(); if (particle != null) { particle.currentContainer = transform.parent; particle.rb2DParent = rigidBody; particle.gameObject.layer = 11; Add(particle); /* if (!audioSource.isPlaying) * { * audioSource.Play(); * } * else * { * audioSource.time = 0.1f; * }*/ } }
void Update() { m_spawnTimer += Time.deltaTime; if (m_spawnTimer >= SPAWN_INTERVAL) { //Create a new particle GameObject newLiquidParticle = Instantiate(particle) as GameObject; // Spawn a particle newLiquidParticle.transform.position = transform.position; // Relocate to the spawner position newLiquidParticle.GetComponent <Rigidbody>().AddForce(particleForce); //Configure the new particle LiquidParticle particleScript = newLiquidParticle.GetComponent <LiquidParticle>(); // Get the particle script particleScript.SetLifeTime(particleLifetime); // Set each particle lifetime particleScript.SetState(particlesState); // Set the particle State //Keep the scene tidy and add the particle to the container transform newLiquidParticle.transform.SetParent(m_sceneParticleHolder); //Reset spawn timer m_spawnTimer = 0.0f; } }