protected void SpawnCircle(Particle p, float emitPosX, float emitPosY, float i = -1f) { //set the initial rotation/direction of the particle based on starting //particle angle and rotation of emitter if (minStartRotation == maxStartRotation) { p.Rotation = minStartRotation + rotation; } else { p.Rotation = Mathf.Random() * (maxStartRotation - minStartRotation) + minStartRotation + rotation; } //place the particle at a random radius in the circle helperPoint.X = Mathf.Random() * spawnCircle.radius; helperPoint.Y = 0; //rotate the point to a random angle in the circle helperPoint = ParticleUtils.RotatePoint(Mathf.Random() * 360, helperPoint); //offset by the circle's center helperPoint.X += spawnCircle.x; helperPoint.Y += spawnCircle.y; //rotate the point by the emitter's rotation if (rotation != 0) { ParticleUtils.RotatePoint(rotation, helperPoint); } //set the position, offset by the emitter's position p.X = emitPosX + helperPoint.X; p.Y = emitPosY + helperPoint.Y; }
public static PropertyNode <Color> CreateList(BasicTweenable <string> data) { var start = new PropertyNode <Color>(ParticleUtils.HexToRGB(data.start), 0); //only set up a next value if it is different from the starting value if (data.end != data.start) { start.next = new PropertyNode <Color>(ParticleUtils.HexToRGB(data.end), 1); } return(start); }
/// <summary> /// Sets the rotation of the emitter to a new value. /// </summary> /// <param name="newRot">The new rotation, in degrees.</param> public void Rotate(float newRot) { if (rotation == newRot) { return; } //caclulate the difference in rotation for rotating spawnPos var diff = newRot - rotation; rotation = newRot; //rotate spawnPos spawnPos = ParticleUtils.RotatePoint(diff, spawnPos); //mark the position as having changed posChanged = true; }
/// <summary> /// Initializes the particle for use, based on the properties that have to /// have been set already on the particle. /// </summary> public void Init() { //reset the age age = 0f; //set up the velocity based on the start speed and rotation velocity.X = speedList.current.value * speedMultiplier; velocity.Y = 0; velocity = ParticleUtils.RotatePoint(Rotation, velocity); if (noRotation) { Rotation = 0; } else { //convert rotation to Radians from Degrees Rotation *= ParticleUtils.DEG_TO_RADS; } //convert rotation speed to Radians from Degrees rotationSpeed *= ParticleUtils.DEG_TO_RADS; rotationAcceleration *= ParticleUtils.DEG_TO_RADS; //set alpha to inital alpha Alpha = alphaList.current.value; //set scale to initial scale Scale = new Vector2(scaleList.current.value, scaleList.current.value); //figure out what we need to interpolate doAlpha = (alphaList.current.next != null); doSpeed = (speedList.current.next != null); doScale = (scaleList.current.next != null); doColor = (colorList.current.next != null); doAcceleration = acceleration.X != 0 || acceleration.Y != 0; //_doNormalMovement can be cancelled by subclasses doNormalMovement = doSpeed || speedList.current.value != 0 || doAcceleration; //save our lerp helper oneOverLife = 1 / maxLife; //set the inital color var color = colorList.current.value; Tint = new Color(color.R, color.G, color.B); //ensure visibility Visible = true; }
protected void SpawnRect(Particle p, float emitPosX, float emitPosY, float i = -1f) { //set the initial rotation/direction of the particle based on starting //particle angle and rotation of emitter if (minStartRotation == maxStartRotation) { p.Rotation = minStartRotation + rotation; } else { p.Rotation = Mathf.Random() * (maxStartRotation - minStartRotation) + minStartRotation + rotation; } //place the particle at a random point in the rectangle helperPoint.X = Mathf.Random() * spawnRect.Width + spawnRect.X; helperPoint.Y = Mathf.Random() * spawnRect.Height + spawnRect.Y; if (rotation != 0) { helperPoint = ParticleUtils.RotatePoint(rotation, helperPoint); } p.X = emitPosX + helperPoint.X; p.Y = emitPosY + helperPoint.Y; }
public void Init(TextureRegion2D[] art, EmitterConfig config) { Cleanup(); //store the original config and particle images, in case we need to re-initialize //when the particle constructor is changed origConfig = config; origArt = art; particleImages = art; /////////////////////////// // Particle Properties // /////////////////////////// //set up the alpha if (config.alpha.HasValue) { startAlpha = PropertyNode <float> .CreateList((BasicTweenable <float>) config.alpha); } else { startAlpha = new PropertyNode <float>(1f, 0f); } //set up the speed if (config.speed.HasValue) { startSpeed = PropertyNode <float> .CreateList((BasicTweenable <float>) config.speed); minimumSpeedMultiplier = (config.minimumSpeedMultiplier.HasValue) ? (float)config.minimumSpeedMultiplier : 1f; } else { minimumSpeedMultiplier = 1f; startSpeed = new PropertyNode <float>(0f, 0f); } //set up acceleration var acceleration = config.acceleration; if (acceleration.HasValue) { //make sure we disable speed interpolation startSpeed.next = null; this.acceleration = (Vector2)acceleration; maxSpeed = (float)(config.maxSpeed.HasValue ? config.maxSpeed : float.NaN); } else { this.acceleration = new Vector2(); } //set up the scale if (config.scale.HasValue) { startScale = PropertyNode <float> .CreateList((BasicTweenable <float>) config.scale); minimumScaleMultiplier = (config.minimumScaleMultiplier.HasValue) ? (float)config.minimumScaleMultiplier : 1f; } else { startScale = new PropertyNode <float>(1f, 0f); minimumScaleMultiplier = 1f; } //set up the color if (config.color.HasValue) { startColor = PropertyNode <Color> .CreateList((BasicTweenable <string>) config.color); } else { startColor = new PropertyNode <Color>(Color.White, 0); } //set up the start rotation if (config.startRotation.HasValue) { var startRot = (RandNumber)config.startRotation; minStartRotation = startRot.min; maxStartRotation = startRot.max; } else { minStartRotation = maxStartRotation = 0f; } if (config.noRotation.HasValue && (minStartRotation != 0f || maxStartRotation != 0f)) { noRotation = (bool)config.noRotation; } else { noRotation = false; } //set up the rotation speed if (config.rotationSpeed.HasValue) { var rotSpeed = (RandNumber)config.rotationSpeed; minRotationSpeed = rotSpeed.min; maxRotationSpeed = rotSpeed.max; } else { minRotationSpeed = maxRotationSpeed = 0; } rotationAcceleration = config.rotationAcceleration.HasValue ? (float)config.rotationAcceleration : 0f; //set up the lifetime minLifetime = config.lifetime.min; maxLifetime = config.lifetime.max; //get the blend mode particleBlendMode = ParticleUtils.GetBlendMode(config.blendMode); ////////////////////////// // Emitter Properties // ////////////////////////// //reset spawn type specific settings particlesPerWave = 1; if (config.particlesPerWave.HasValue && config.particlesPerWave > 1) { particlesPerWave = (int)config.particlesPerWave; } particleSpacing = 0; angleStart = 0; Circle spawnCircle; //determine the spawn function to use switch (config.spawnType) { case "rect": spawnType = "rect"; spawnFunc = SpawnRect; var spawnRect = (Rectangle)config.spawnRect; this.spawnRect = spawnRect; break; case "circle": spawnType = "circle"; spawnFunc = SpawnCircle; spawnCircle = (Circle)config.spawnCircle; this.spawnCircle = spawnCircle; break; case "ring": spawnType = "ring"; spawnFunc = SpawnRing; spawnCircle = (Circle)config.spawnCircle; this.spawnCircle = spawnCircle; break; case "burst": spawnType = "burst"; spawnFunc = SpawnBurst; particleSpacing = (float)config.particleSpacing; angleStart = config.angleStart.HasValue ? (float)config.angleStart : 0f; break; case "point": spawnType = "point"; spawnFunc = SpawnPoint; break; default: spawnType = "point"; spawnFunc = SpawnPoint; break; } //set the spawning frequency frequency = config.frequency; spawnChance = (config.spawnChance.HasValue && config.spawnChance > 0) ? (float)config.spawnChance : 1f; //set the emitter lifetime emitterLifetime = config.emitterLifetime.HasValue ? (float)config.emitterLifetime : -1f; //set the max particles maxParticles = config.maxParticles > 0 ? (int)config.maxParticles : 1000; //determine if we should add the particle at the back of the list or not addAtBack = config.addAtBack; //reset the emitter position and rotation variables rotation = 0; ownerPos = new Vector2(); spawnPos = config.pos; prevEmitterPos = new Vector2(spawnPos.X, spawnPos.Y); //previous emitter position is invalid and should not be used for interpolation prevPosIsValid = false; //start emitting spawnTimer = 0; Emit = config.emit; }