/// <summary> /// Run the application /// </summary> public void Go() { // Make the particle emitter. emit = new ParticleRectangleEmitter(particles); emit.Frequency = 50000; // 100000 every 1000 updates. emit.LifeFullMin = 20; emit.LifeFullMax = 50; emit.LifeMin = 10; emit.LifeMax = 30; emit.DirectionMin = -2; // shoot up in radians. emit.DirectionMax = -1; emit.ColorMin = Color.DarkBlue; emit.ColorMax = Color.LightBlue; emit.SpeedMin = 5; emit.SpeedMax = 20; emit.MaxSize = new SizeF(5, 5); emit.MinSize = new SizeF(1, 1); // Make the first particle (a pixel) ParticlePixel first = new ParticlePixel(Color.White, 100, 200, new Vector(0, 0, 0), -1); particles.Add(first); // Add it to the system if (File.Exists(Path.Combine(dataDirectory, "marble1.png"))) { filePath = ""; } // Make the second particle (an animated sprite) AnimationCollection anim = new AnimationCollection(); SurfaceCollection surfaces = new SurfaceCollection(); surfaces.Add(Path.Combine(filePath, Path.Combine(dataDirectory, "marble1.png")), new Size(50, 50)); anim.Add(surfaces, 1); AnimatedSprite marble = new AnimatedSprite(anim); marble.Animate = true; ParticleSprite second = new ParticleSprite(marble, 200, 200, new Vector(-7, -9, 0), 500); second.Life = -1; particles.Add(second); // Add it to the system // Add some manipulators to the particle system. ParticleGravity grav = new ParticleGravity(0.5f); particles.Manipulators.Add(grav); // Gravity of 0.5f particles.Manipulators.Add(new ParticleFriction(0.1f)); // Slow down particles particles.Manipulators.Add(vort); // A particle vortex fixed on the mouse particles.Manipulators.Add(new ParticleBoundary(SdlDotNet.Graphics.Video.Screen.Size)); // fix particles on screen. Events.Run(); }
private static void SpawnRain() { if (spawnTimer.TimeElapsedInMilliSeconds > 50) { spawnTimer.Reset(); int[] indices = GameWorld.ChunkManager.GetVisibleIndexes(); if (indices == null) { return; } int width = GameWorld.ChunkManager.GetVisibileWidth(); int starting = indices[0]; // Do the top row as always. for (int i = starting; i < starting + width; i++) { Tile tile = GameWorld.GetTile(i); ParticleSystem.Add(Particles.ParticleType.Rain, CalcHelper.GetRandXAndY(tile.DrawRectangle), new Vector2(0, 8), Color.Blue); } // This spawns particles on areas that have just been put into view. var newIndices = indices.Except(lastIndices); foreach (var i in newIndices) { Tile tile = GameWorld.GetTile(i); ParticleSystem.Add(Particles.ParticleType.Rain, CalcHelper.GetRandXAndY(tile.DrawRectangle), new Vector2(0, 8), Color.Blue); } lastIndices = indices; } }
public override void Update(Tile tile) { if (active && radius < 500) { _particleTimer.Increment(); if (_particleTimer.TimeElapsedInMilliSeconds > 10) { _particleTimer.Reset(); radius++; Vector2 position = new Vector2(tile.GetDrawRectangle().Center.X, tile.GetDrawRectangle().Center.Y); lastDeg %= 360; for (int i = lastDeg; i < lastDeg + changeInDeg; i += 1) { double rads = Math.PI * i / 180; float x = (float)Math.Cos(rads); float y = (float)Math.Sin(rads); ParticleSystem.Add(Particles.ParticleType.BlueFire, new Vector2(radius * x, radius * y) + position, new Vector2(x, y) * 2 * (float)TMBAW_Game.Random.NextDouble(), Color.White); x *= -1; y *= -1; ParticleSystem.Add(Particles.ParticleType.BlueFire, new Vector2(radius * x, radius * y) + position, new Vector2(x, y) * 2 * (float)TMBAW_Game.Random.NextDouble(), Color.White); } lastDeg += changeInDeg; } } base.Update(tile); }
public override void Update(Tile tile) { _fireParticleTimer.Increment(); _smokeParticleTimer.Increment(); _heatTimer.Increment(); //Flame if (_fireParticleTimer.TimeElapsedInMilliSeconds > 500) { _fireParticleTimer.Reset(); float velY = (float)-TMBAW_Game.Random.NextDouble(); Color color = Color.White; if (tile.Id == TMBAW_Game.TileType.GreenTorch) { color = Color.Green; } ParticleSystem.Add(ParticleType.Flame, new Vector2(tile.GetDrawRectangle().Center.X, tile.GetDrawRectangle().Center.Y - 10), new Vector2(0, velY), color); } // Heat effect. if (_heatTimer.TimeElapsedInMilliSeconds > 100) { _heatTimer.Reset(); float velY = (float)-TMBAW_Game.Random.NextDouble(); ParticleSystem.Add(ParticleType.HeatEffect, new Vector2(tile.GetDrawRectangle().Center.X, tile.GetDrawRectangle().Top), CalcHelper.GetRandXAndY(new Rectangle(-10, -10, 20, 0)) / 10, Color.White); } // Smoke if (_smokeParticleTimer.TimeElapsedInMilliSeconds > 400) { _smokeParticleTimer.Reset(); float velY = (float)-TMBAW_Game.Random.NextDouble(); ParticleSystem.Add(ParticleType.Flame, new Vector2(tile.GetDrawRectangle().Center.X, tile.GetDrawRectangle().Center.Y - 10), new Vector2(0, velY), new Color(30, 30, 30, 150)); } base.Update(tile); }
/// <summary> /// Creates destruction specific particles around an area. /// </summary> /// <param name="rect"></param> private static void CreateDestructionParticles(Rectangle rect) { for (var i = 0; i < 5; i++) { ParticleSystem.Add(ParticleType.Smoke, CalcHelper.GetRandXAndY(rect), new Vector2(TMBAW_Game.Random.Next(-10, 10) / 10f, TMBAW_Game.Random.Next(-10, 10) / 10f), Color.White); } }
public override void Update(Tile tile) { // Entity collision and damage. // TODO: Change collision detection so that only entities in the same chunk get checked. if (_isSpittingFire) { foreach (var entity in GameWorld.Entities) { if (entity.CollRectangle.Intersects(_collRectangle)) { entity.TakeDamage(null, Damage); } } foreach (Player player in GameWorld.GetPlayers()) { if (player.CollRectangle.Intersects(_collRectangle)) { player.TakeDamage(null, Damage); } } } // Particles and visual effects. _spitTimer.Increment(); if (_isSpittingFire) { tile.AnimationStopped = false; _flameSound.PlayIfStopped(); if (_spitTimer.TimeElapsedInMilliSeconds < FireTime) { _fireBallTimer.Increment(); if (_fireBallTimer.TimeElapsedInMilliSeconds > ParticleTime) { _fireBallTimer.Reset(); ParticleSystem.Add(Particles.ParticleType.FireBall, _leftCornerTile + new Vector2(3, 0) * 2, CalcHelper.GetRandXAndY(new Rectangle(-10, -50, 20, 30)) / 10f, Color.White); ParticleSystem.Add(Particles.ParticleType.FireBall, _leftCornerTile + new Vector2(12, 0) * 2, CalcHelper.GetRandXAndY(new Rectangle(-10, -50, 20, 30)) / 10f, Color.White); } } else { _isSpittingFire = false; _spitTimer.Reset(); } } else { _flameSound.Stop(); tile.AnimationStopped = true; tile.CurrentFrame = 1; if (_spitTimer.TimeElapsedInMilliSeconds > IdleTime) { _isSpittingFire = true; _spitTimer.Reset(); } } base.Update(tile); }
public override void Update(GameTime gameTime) { Sparks.CapacityLimit = 2048; Sparks.RemoveParticlesWhenCapacityReached = true; if (Game.IsActive) { if (KeyWasPressed(Keys.O)) { ShowOutlines = !ShowOutlines; } if (KeyWasPressed(Keys.L)) { ShowLightmap = !ShowLightmap; } var ms = Mouse.GetState(); Game.IsMouseVisible = true; LightmapScale = BaseLightmapScale + (ms.ScrollWheelValue / 200f); if (LightmapScale < 1f) { LightmapScale = 1f; } var mousePos = new Vector2(ms.X, ms.Y); if (ms.LeftButton == ButtonState.Pressed) { if (Dragging == null) { BackgroundEnvironment.Obstructions.Add(Dragging = new LightObstructionLine(mousePos, mousePos)); } else { Dragging.B = mousePos; } } else { if (Dragging != null) { Dragging.B = mousePos; Dragging = null; } } const int sparkSpawnCount = 24; var sparkSpawnPosition = mousePos; for (var i = 0; i < sparkSpawnCount; i++) { Sparks.Add(new Spark(Sparks, sparkSpawnPosition, 3)); } } Sparks.Update(); SparkLights.Update(); }
/// <summary> /// Creates a new pixel particle emitter inside a particle system. /// </summary> /// <param name="system">The system to add to.</param> protected ParticleEmitter(ParticleSystem system) : this() { if (system == null) { throw new ArgumentNullException("system"); } system.Add(this, true); }
private void SpawnSparkles(PickedUpArgs e) { for (int i = 0; i < 2; i++) { float randY = (float)(TMBAW_Game.Random.Next(-1, 0) * TMBAW_Game.Random.NextDouble()); float randX = (float)(TMBAW_Game.Random.Next(-1, 2) * TMBAW_Game.Random.NextDouble()); ParticleSystem.Add(ParticleType.Tiny, CalcHelper.GetRandXAndY(CollRectangle), new Vector2(randX, randY), Color.Yellow); } }
public override void SpawnExplosion(Vector2 position) { for (int i = 0; i < 50; i++) { ParticleSystem.Add(ParticleType.TilePiece, position, null, Color.White); } position -= new Vector2(24, 24); ParticleSystem.Add(ParticleType.BlueFireExplosion, position, null, Color.White); }
public override void Bind(Entity entity, Main main, bool creating = false) { if (ParticleSystem.Get(main, "Dust") == null) { ParticleSystem.Add(main, "Dust", new ParticleSystem.ParticleSettings { TextureName = "Particles\\default", EffectFile = "Effects\\ParticleSnow", MaxParticles = 20000, Duration = TimeSpan.FromSeconds(3.0f), MinHorizontalVelocity = -0.25f, MaxHorizontalVelocity = 0.25f, MinVerticalVelocity = -0.25f, MaxVerticalVelocity = 0.25f, Gravity = new Vector3(0.0f, 0.0f, 0.0f), MinRotateSpeed = 0.0f, MaxRotateSpeed = 0.0f, MinStartSize = 0.02f, MaxStartSize = 0.08f, MinEndSize = 0.0f, MaxEndSize = 0.0f, MinColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f), MaxColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f), EmitterVelocitySensitivity = 1.0f, BlendState = BlendState.Opaque, Material = new Components.Model.Material { SpecularIntensity = 0.0f, SpecularPower = 1.0f }, }); } entity.CannotSuspendByDistance = true; entity.GetOrCreate <Transform>("Transform"); ParticleEmitter emitter = entity.GetOrCreate <ParticleEmitter>("Emitter"); this.SetMain(entity, main); emitter.CalculateVelocity = false; emitter.ParticleType.Value = "Dust"; emitter.Jitter.Value = new Vector3(10, 10, 10); emitter.ParticlesPerSecond.Value = 300; emitter.Add(new Binding <Vector3>(emitter.Position, main.Camera.Position)); entity.Add("Velocity", emitter.VelocityOffset); entity.Add(new PostInitialization { delegate() { emitter.Prime(Vector3.Zero); } }); }
/// <summary> /// Creates and adds stomp particles where the entity is. /// </summary> /// <param name="count"></param> public void CreateStompParticles(int count) { if (_stompParticleTimer.TimeElapsedInMilliSeconds > 100) { for (int i = 0; i < count; i++) { ParticleSystem.Add(ParticleType.Smoke, new Vector2(CalcHelper.GetRandomX(GetCollRectangle()), GetCollRectangle().Bottom), new Vector2(TMBAW_Game.Random.Next(-5, 5) / 10f, -TMBAW_Game.Random.Next(1, 5) / 10f), Color.White); } _stompParticleTimer.Reset(); } }
public override void SpawnParticles() { if (_particleAlternatingVar) { ParticleSystem.Add(ParticleType.BlueFire, Center, null, Color.White); } else { ParticleSystem.Add(ParticleType.BlueFire, Center, null, new Color(0, 246, 255)); } _particleAlternatingVar = !_particleAlternatingVar; }
private void Entity_CollidedWithTileBelow(Entity entity, Tile tile) { if (isFalling) { for (int i = 0; i < 20; i++) { ParticleSystem.Add(ParticleType.Smoke, CalcHelper.GetRandXAndY(new Rectangle(entity.GetCollRectangle().X, entity.GetCollRectangle().Bottom, entity.GetCollRectangle().Width, 1)), CalcHelper.GetRandXAndY(new Rectangle(-40, -5, 80, 10)) / 10f, Color.White); } } isFalling = false; }
private void SpawnEnemyParticles() { foreach (var en in enemiesBeingSpawned) { for (int i = 0; i < 5; i++) { ParticleSystem.Add(ParticleType.Round_Common, CalcHelper.GetRandXAndY(en.GetDrawRectangle()), CalcHelper.GetRandXAndY(new Rectangle(-1, -1, 2, 2)), Color.Yellow); } en.SetVelX(0); en.SetVelY(0); } }
public override void Update(Tile tile) { heatTimer.Increment(); bubblingSound.PlayIfStopped(); if (heatTimer.TimeElapsedInMilliSeconds > 500) { heatTimer.Reset(); if (GameWorld.GetTileAbove(tile.TileIndex).Id == TMBAW_Game.TileType.Air) { ParticleSystem.Add(ParticleType.HeatEffect, new Vector2(tile.GetDrawRectangle().Center.X, tile.GetDrawRectangle().Top), CalcHelper.GetRandXAndY(new Rectangle(-10, -10, 10, 0)) / 10, Color.White); } } }
private void SpawnSpellParticles() { for (int i = 0; i < 5; i++) { Vector2 offset; if (Entity.IsFacingRight) { offset = new Vector2(40, 3); } else { offset = new Vector2(20, 3); } ParticleSystem.Add(ParticleType.Tiny, new Vector2(Entity.GetDrawRectangle().X, Entity.GetDrawRectangle().Y) + offset, CalcHelper.GetRandXAndY(new Rectangle(-10, -10, 20, 20)) / 10, Color.Yellow); } }
public void CreateMovingParticles() { if (!IsJumping) { if (Math.Abs(Velocity.X) < 3f) { return; } if (_movementParticlesTimer.TimeElapsedInMilliSeconds > 500 / (Math.Abs(Velocity.X))) { _movementParticlesTimer.Reset(); ParticleSystem.Add(ParticleType.Smoke, new Vector2(CollRectangle.Center.X, CollRectangle.Bottom), new Vector2(0, (float)(TMBAW_Game.Random.Next(-5, 5) / 10f)), Color.White); } } }
public void OnJumpAction(Player player) { float jumpAcc = JumpAcc; if (player.IsDucking) { player.WantsToMoveDownPlatform = true; player.AddAnimationToQueue("fall"); return; } if (player.IsClimbing) { if (player.IsInteractPressed()) { return; } player.IsClimbing = false; player.IsJumping = false; if (player.IsMoveDownPressed()) { jumpAcc = 0; } } if (!player.IsJumping) { player.Sounds.GetSoundRef("jump").Play(); player.IsJumping = true; player.SetVelY(jumpAcc); player.ChangePosBy(0, -1); player.AddAnimationToQueue("jump"); player.CollidedWithTileBelow += OnTouchGround; _lastJumpTimer.Reset(); player.GravityStrength = TMBAW_Game.Gravity * .5f; if (jumpAcc != 0) { for (int i = 0; i < 10; i++) { ParticleSystem.Add(ParticleType.Smoke, new Vector2(CalcHelper.GetRandomX(player.GetCollRectangle()), player.GetCollRectangle().Bottom), new Vector2(TMBAW_Game.Random.Next((int)player.GetVelocity().X - 1, (int)player.GetVelocity().X + 1) / 10f, -TMBAW_Game.Random.Next(1, 10) / 10f), Color.White); } } } }
/// <summary> /// Deals a certain amount of damage to the entity. /// </summary> /// <param name="damageDealer">Can be null.</param> /// <param name="damage"></param> public virtual void TakeDamage(Entity damageDealer, int damage) { if (TMBAW_Game.CurrentGameMode == GameMode.Play) { if (IsTakingDamage || IsPlayingDeathAnimation || !CanTakeDamage) { return; } if (this is Player) { Overlay.ColoredCorners.FlashColor(Color.Red); } // Main.TimeFreeze.AddFrozenTime(damage*3); IsTakingDamage = true; Health -= damage; ParticleSystem.Add("-" + damage, Center, new Vector2(TMBAW_Game.Random.Next(0, 2) * -2 + 1, -15), new Color(255, 108, 108)); _hitRecentlyTimer.ResetAndWaitFor(500); _hitRecentlyTimer.SetTimeReached += HitByPlayerTimer_SetTimeReached; //Creates damage particles. int particleCount = damage / 2; if (particleCount > 100) { particleCount = 100; } for (int i = 0; i < particleCount; i++) { ParticleSystem.Add(ParticleType.Round_Common, CalcHelper.GetRandXAndY(CollRectangle), null, Color.Red); } //if (damageDealer == null) // return; //Velocity.Y = -8f; //Velocity.X = (Weight / 2f); //if (!damageDealer.IsFacingRight) // Velocity.X *= -1; HasTakenDamage?.Invoke(); } }
private void LevitateEntities() { const float maxVel = 20f; float velocity = maxVel / 2 * -(float)Math.Cos(2 * Math.PI * levitateEntitiesTimer.TimeElapsedInMilliSeconds / 10000) + maxVel / 2 + 1; foreach (Player player in GameWorld.GetPlayers()) { player.SetVelY(-velocity); ParticleSystem.Add(ParticleType.Tiny, CalcHelper.GetRandXAndY(player.GetCollRectangle()), Vector2.Zero, new Color(196, 148, 255)); } foreach (var entity in GameWorld.Entities) { if (entity != Entity) { entity.SetVelY(-velocity); ParticleSystem.Add(ParticleType.Tiny, CalcHelper.GetRandXAndY(entity.GetCollRectangle()), Vector2.Zero, new Color(196, 148, 255)); } } }
/// <summary> /// Creates death particles and calls event to do other death related things. /// </summary> private void DeathAnimationEnded() { _deathAnimationTimer.SetTimeReached -= DeathAnimationEnded; IsDead = true; for (int i = 0; i < 20; i++) { ParticleSystem.Add(ParticleType.Smoke, CalcHelper.GetRandXAndY(CollRectangle), new Vector2(0, -TMBAW_Game.Random.Next(1, 5) / 10f), Color.White); } //Rectangle[] desinRectangles; //GetDisintegratedRectangles(out desinRectangles); //foreach (Rectangle rect in desinRectangles) //{ // EntityTextureParticle par = new EntityTextureParticle(CalcHelper.GetRandomX(CollRectangle), CalcHelper.GetRandomY(CollRectangle), rect, new Vector2(Main.Random.Next(-5, 5) / 10f, -Main.Random.Next(-5, 5) / 10f), this); // GameWorld.ParticleSystem.Add(par); //} HasFinishedDying?.Invoke(this); }
protected virtual void initExplosion() { _ptclSystem = new ParticleSystem(); _ptclEmitter = new ParticleSurfaceEmitter(_explosionColoredSurfaces == null ? _explosionSurfaces : _explosionColoredSurfaces); _ptclEmitter.Frequency = 300; _ptclEmitter.LifeFullMin = 20; _ptclEmitter.LifeFullMax = 40; _ptclEmitter.LifeMin = 10; _ptclEmitter.LifeMax = 30; _ptclEmitter.DirectionMin = -(float)Math.PI; _ptclEmitter.DirectionMax = (float)Math.PI; _ptclEmitter.SpeedMin = 5; _ptclEmitter.SpeedMax = 6; _ptclSystem.Add(_ptclEmitter); _ptclSystem.Manipulators.Add(new ParticleGravity(0.2f)); // Gravity _ptclSystem.Manipulators.Add(new ParticleFriction(0.3f)); // Slow down _ptclSystem //_ptclSystem.Manipulators.Add(new ParticleVortex(1f, 200f)); // A particle vortex fixed on the mouse _ptclSystem.Manipulators.Add(new ParticleBoundary(new Size(Constants.StageViewWidth, Constants.StageViewHeight))); // fix _ptclSystem on screen. }
public void AddParticle(Particle p) { System.Add(p); }
public override void Awake() { base.Awake(); this.EnabledInEditMode = true; this.EnabledWhenPaused = false; float sum = 0.0f; for (int x = 0; x < KernelSize; x++) { for (int y = 0; y < KernelSize; y++) { float cell = (KernelSize / 2) - new Vector2(x - (KernelSize / 2), y - (KernelSize / 2)).Length(); audioKernel[x, y] = cell; sum += cell; } } for (int x = 0; x < KernelSize; x++) { for (int y = 0; y < KernelSize; y++) { audioKernel[x, y] /= sum; } } this.Jitter.Value = new Vector3(KernelSpacing * KernelSize * 0.5f, 0.0f, KernelSpacing * KernelSize * 0.5f); if (ParticleSystem.Get(main, "Rain") == null) { ParticleSystem.Add(main, "Rain", new ParticleSystem.ParticleSettings { TextureName = "Particles\\default", EffectFile = "Effects\\ParticleRain", MaxParticles = 25000, Duration = TimeSpan.FromSeconds(MaxLifetime), MinHorizontalVelocity = 0.0f, MaxHorizontalVelocity = 0.0f, MinVerticalVelocity = -VerticalSpeed, MaxVerticalVelocity = -VerticalSpeed, Gravity = new Vector3(0.0f, 0.0f, 0.0f), MinRotateSpeed = 0.0f, MaxRotateSpeed = 0.0f, MinStartSize = 0.3f, MaxStartSize = 0.3f, MinEndSize = 0.3f, MaxEndSize = 0.3f, BlendState = BlendState.Opaque, MinColor = new Vector4(0.6f, 0.6f, 0.6f, 1.0f), MaxColor = new Vector4(0.6f, 0.6f, 0.6f, 1.0f), Material = new Components.Model.Material { SpecularIntensity = 0.0f, SpecularPower = 1.0f }, }); } if (this.main.EditorEnabled) { this.Add(new Binding <Vector3>(this.CurrentLightningColor, this.LightningColor)); } this.thunderTimer = this.ThunderIntervalMin + ((float)random.NextDouble() * (this.ThunderIntervalMax - this.ThunderIntervalMin)); }
public override void SpawnRibbon(int x) { Vector2 position = new Vector2(x, Center.Y - 8); ParticleSystem.Add(ParticleType.BlueFireRibbon, position, null, Color.White); }
public override void Bind(Entity entity, Main main, bool creating = false) { if (ParticleSystem.Get(main, "SnakeSparks") == null) { ParticleSystem.Add(main, "SnakeSparks", new ParticleSystem.ParticleSettings { TextureName = "Particles\\splash", MaxParticles = 1000, Duration = TimeSpan.FromSeconds(1.0f), MinHorizontalVelocity = -7.0f, MaxHorizontalVelocity = 7.0f, MinVerticalVelocity = 0.0f, MaxVerticalVelocity = 7.0f, Gravity = new Vector3(0.0f, -10.0f, 0.0f), MinRotateSpeed = -2.0f, MaxRotateSpeed = 2.0f, MinStartSize = 0.3f, MaxStartSize = 0.7f, MinEndSize = 0.0f, MaxEndSize = 0.0f, BlendState = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend, MinColor = new Vector4(2.0f, 2.0f, 2.0f, 1.0f), MaxColor = new Vector4(2.0f, 2.0f, 2.0f, 1.0f), }); ParticleSystem.Add(main, "SnakeSparksRed", new ParticleSystem.ParticleSettings { TextureName = "Particles\\splash", MaxParticles = 1000, Duration = TimeSpan.FromSeconds(1.0f), MinHorizontalVelocity = -7.0f, MaxHorizontalVelocity = 7.0f, MinVerticalVelocity = 0.0f, MaxVerticalVelocity = 7.0f, Gravity = new Vector3(0.0f, -10.0f, 0.0f), MinRotateSpeed = -2.0f, MaxRotateSpeed = 2.0f, MinStartSize = 0.3f, MaxStartSize = 0.7f, MinEndSize = 0.0f, MaxEndSize = 0.0f, BlendState = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend, MinColor = new Vector4(1.4f, 0.8f, 0.7f, 1.0f), MaxColor = new Vector4(1.4f, 0.8f, 0.7f, 1.0f), }); ParticleSystem.Add(main, "SnakeSparksYellow", new ParticleSystem.ParticleSettings { TextureName = "Particles\\splash", MaxParticles = 1000, Duration = TimeSpan.FromSeconds(1.0f), MinHorizontalVelocity = -7.0f, MaxHorizontalVelocity = 7.0f, MinVerticalVelocity = 0.0f, MaxVerticalVelocity = 7.0f, Gravity = new Vector3(0.0f, -10.0f, 0.0f), MinRotateSpeed = -2.0f, MaxRotateSpeed = 2.0f, MinStartSize = 0.3f, MaxStartSize = 0.7f, MinEndSize = 0.0f, MaxEndSize = 0.0f, BlendState = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend, MinColor = new Vector4(1.4f, 1.4f, 0.7f, 1.0f), MaxColor = new Vector4(1.4f, 1.4f, 0.7f, 1.0f), }); } Snake snake = entity.GetOrCreate <Snake>("Snake"); entity.CannotSuspendByDistance = true; Transform transform = entity.GetOrCreate <Transform>("Transform"); AI ai = entity.GetOrCreate <AI>("AI"); Agent agent = entity.GetOrCreate <Agent>("Agent"); const float defaultSpeed = 5.0f; const float chaseSpeed = 18.0f; const float closeChaseSpeed = 12.0f; const float crushSpeed = 125.0f; VoxelChaseAI chase = entity.GetOrCreate <VoxelChaseAI>("VoxelChaseAI"); chase.Add(new TwoWayBinding <Vector3>(transform.Position, chase.Position)); chase.Speed.Value = defaultSpeed; chase.EnablePathfinding.Value = ai.CurrentState.Value == "Chase"; chase.Filter = delegate(Voxel.State state) { if (state == Voxel.States.Infected || state == Voxel.States.Neutral || state == Voxel.States.Hard || state == Voxel.States.HardInfected) { return(true); } return(false); }; entity.Add(new CommandBinding(chase.Delete, entity.Delete)); PointLight positionLight = null; if (!main.EditorEnabled) { positionLight = new PointLight(); positionLight.Serialize = false; positionLight.Color.Value = new Vector3(1.5f, 0.5f, 0.5f); positionLight.Attenuation.Value = 20.0f; positionLight.Add(new Binding <bool, string>(positionLight.Enabled, x => x != "Suspended", ai.CurrentState)); positionLight.Add(new Binding <Vector3, string>(positionLight.Color, delegate(string state) { switch (state) { case "Chase": case "Crush": return(new Vector3(1.5f, 0.5f, 0.5f)); case "Alert": return(new Vector3(1.5f, 1.5f, 0.5f)); default: return(new Vector3(1.0f, 1.0f, 1.0f)); } }, ai.CurrentState)); entity.Add("PositionLight", positionLight); ParticleEmitter emitter = entity.GetOrCreate <ParticleEmitter>("Particles"); emitter.Serialize = false; emitter.ParticlesPerSecond.Value = 100; emitter.Add(new Binding <string>(emitter.ParticleType, delegate(string state) { switch (state) { case "Chase": case "Crush": return("SnakeSparksRed"); case "Alert": return("SnakeSparksYellow"); default: return("SnakeSparks"); } }, ai.CurrentState)); emitter.Add(new Binding <Vector3>(emitter.Position, transform.Position)); emitter.Add(new Binding <bool, string>(emitter.Enabled, x => x != "Suspended", ai.CurrentState)); positionLight.Add(new Binding <Vector3>(positionLight.Position, transform.Position)); emitter.Add(new Binding <Vector3>(emitter.Position, transform.Position)); agent.Add(new Binding <Vector3>(agent.Position, transform.Position)); Sound.AttachTracker(entity); } AI.Task checkMap = new AI.Task { Action = delegate() { if (chase.Voxel.Value.Target == null || !chase.Voxel.Value.Target.Active) { entity.Delete.Execute(); } }, }; AI.Task checkOperationalRadius = new AI.Task { Interval = 2.0f, Action = delegate() { bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < snake.OperationalRadius; if (shouldBeActive && ai.CurrentState == "Suspended") { ai.CurrentState.Value = "Idle"; } else if (!shouldBeActive && ai.CurrentState != "Suspended") { ai.CurrentState.Value = "Suspended"; } }, }; AI.Task checkTargetAgent = new AI.Task { Action = delegate() { Entity target = ai.TargetAgent.Value.Target; if (target == null || !target.Active) { ai.TargetAgent.Value = null; ai.CurrentState.Value = "Idle"; } }, }; Func <Voxel, Direction> randomValidDirection = delegate(Voxel m) { Voxel.Coord c = chase.Coord; Direction[] dirs = new Direction[6]; Array.Copy(DirectionExtensions.Directions, dirs, 6); // Shuffle directions int i = 5; while (i > 0) { int k = this.random.Next(i); Direction temp = dirs[i]; dirs[i] = dirs[k]; dirs[k] = temp; i--; } foreach (Direction dir in dirs) { if (chase.Filter(m[c.Move(dir)])) { return(dir); } } return(Direction.None); }; Direction currentDir = Direction.None; chase.Add(new CommandBinding <Voxel, Voxel.Coord>(chase.Moved, delegate(Voxel m, Voxel.Coord c) { if (chase.Active) { string currentState = ai.CurrentState.Value; Voxel.t id = m[c].ID; if (id == Voxel.t.Hard) { m.Empty(c); m.Fill(c, Voxel.States.HardInfected); m.Regenerate(); } else if (id == Voxel.t.Neutral) { m.Empty(c); m.Fill(c, Voxel.States.Infected); m.Regenerate(); } else if (id == Voxel.t.Empty) { m.Fill(c, Voxel.States.Infected); m.Regenerate(); } if (currentState == "Idle") { if (currentDir == Direction.None || !chase.Filter(m[chase.Coord.Value.Move(currentDir)]) || this.random.Next(8) == 0) { currentDir = randomValidDirection(m); } chase.Coord.Value = chase.Coord.Value.Move(currentDir); } else if (snake.Path.Length > 0) { chase.Coord.Value = snake.Path[0]; snake.Path.RemoveAt(0); } } })); Sound.AttachTracker(entity); SoundKiller.Add(entity, AK.EVENTS.STOP_SNAKE); ai.Add(new ChangeBinding <string>(ai.CurrentState, delegate(string old, string value) { if (value == "Suspended" || value == "Alert") { AkSoundEngine.PostEvent(AK.EVENTS.STOP_SNAKE, entity); } else if (old != "Idle" && old != "Chase" && old != "Crush") { AkSoundEngine.PostEvent(AK.EVENTS.PLAY_SNAKE, entity); } })); const float sightDistance = 50.0f; const float hearingDistance = 0.0f; ai.Setup ( new AI.AIState { Name = "Suspended", Tasks = new[] { checkOperationalRadius }, }, new AI.AIState { Name = "Idle", Enter = delegate(AI.AIState previous) { Entity voxelEntity = chase.Voxel.Value.Target; if (voxelEntity != null) { Voxel m = voxelEntity.Get <Voxel>(); if (currentDir == Direction.None || !chase.Filter(m[chase.Coord.Value.Move(currentDir)])) { currentDir = randomValidDirection(m); } chase.Coord.Value = chase.Coord.Value.Move(currentDir); } }, Tasks = new[] { checkMap, checkOperationalRadius, new AI.Task { Interval = 1.0f, Action = delegate() { Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player"); if (a != null) { ai.CurrentState.Value = "Alert"; } }, }, }, }, new AI.AIState { Name = "Alert", Enter = delegate(AI.AIState previous) { chase.EnableMovement.Value = false; }, Exit = delegate(AI.AIState next) { chase.EnableMovement.Value = true; }, Tasks = new[] { checkMap, checkOperationalRadius, new AI.Task { Interval = 0.4f, Action = delegate() { if (ai.TimeInCurrentState > 3.0f) { ai.CurrentState.Value = "Idle"; } else { Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player"); if (a != null) { ai.TargetAgent.Value = a.Entity; ai.CurrentState.Value = "Chase"; } } }, }, }, }, new AI.AIState { Name = "Chase", Enter = delegate(AI.AIState previousState) { chase.EnablePathfinding.Value = true; chase.Speed.Value = chaseSpeed; }, Exit = delegate(AI.AIState nextState) { chase.EnablePathfinding.Value = false; chase.Speed.Value = defaultSpeed; }, Tasks = new[] { checkMap, checkOperationalRadius, checkTargetAgent, new AI.Task { Interval = 0.07f, Action = delegate() { Vector3 targetPosition = ai.TargetAgent.Value.Target.Get <Agent>().Position; float targetDistance = (targetPosition - transform.Position).Length(); chase.Speed.Value = targetDistance < 15.0f ? closeChaseSpeed : chaseSpeed; if (targetDistance > 50.0f || ai.TimeInCurrentState > 30.0f) // He got away { ai.CurrentState.Value = "Alert"; } else if (targetDistance < 4.0f) // We got 'im { // First, make sure we're not near a reset block Voxel v = chase.Voxel.Value.Target.Get <Voxel>(); if (VoxelAStar.BroadphaseSearch(v, chase.Coord, 6, x => x.Type == Lemma.Components.Voxel.States.Reset) == null) { ai.CurrentState.Value = "Crush"; } } else { chase.Target.Value = targetPosition; } }, }, }, }, new AI.AIState { Name = "Crush", Enter = delegate(AI.AIState lastState) { // Set up cage Voxel.Coord center = chase.Voxel.Value.Target.Get <Voxel>().GetCoordinate(ai.TargetAgent.Value.Target.Get <Agent>().Position); int radius = 1; // Bottom for (int x = center.X - radius; x <= center.X + radius; x++) { for (int z = center.Z - radius; z <= center.Z + radius; z++) { snake.Path.Add(new Voxel.Coord { X = x, Y = center.Y - 4, Z = z }); } } // Outer shell radius = 2; for (int y = center.Y - 3; y <= center.Y + 3; y++) { // Left for (int z = center.Z - radius; z <= center.Z + radius; z++) { snake.Path.Add(new Voxel.Coord { X = center.X - radius, Y = y, Z = z }); } // Right for (int z = center.Z - radius; z <= center.Z + radius; z++) { snake.Path.Add(new Voxel.Coord { X = center.X + radius, Y = y, Z = z }); } // Backward for (int x = center.X - radius; x <= center.X + radius; x++) { snake.Path.Add(new Voxel.Coord { X = x, Y = y, Z = center.Z - radius }); } // Forward for (int x = center.X - radius; x <= center.X + radius; x++) { snake.Path.Add(new Voxel.Coord { X = x, Y = y, Z = center.Z + radius }); } } // Top for (int x = center.X - radius; x <= center.X + radius; x++) { for (int z = center.Z - radius; z <= center.Z + radius; z++) { snake.Path.Add(new Voxel.Coord { X = x, Y = center.Y + 3, Z = z }); } } chase.EnablePathfinding.Value = false; chase.Speed.Value = crushSpeed; snake.CrushCoordinate.Value = chase.Coord; }, Exit = delegate(AI.AIState nextState) { chase.Speed.Value = defaultSpeed; chase.Coord.Value = chase.LastCoord.Value = snake.CrushCoordinate; snake.Path.Clear(); }, Tasks = new[] { checkMap, checkOperationalRadius, checkTargetAgent, new AI.Task { Interval = 0.01f, Action = delegate() { Agent a = ai.TargetAgent.Value.Target.Get <Agent>(); a.Damage.Execute(0.01f / 1.5f); // seconds to kill if (!a.Active) { ai.CurrentState.Value = "Alert"; } else { if ((a.Position - transform.Position.Value).Length() > 5.0f) // They're getting away { ai.CurrentState.Value = "Chase"; } } } } }, } ); this.SetMain(entity, main); entity.Add("OperationalRadius", snake.OperationalRadius); }
private void Gem_OnPlayerPickUp(PickedUpArgs e) { e.Player.Score += GetValue(); ParticleSystem.Add("+" + GetValue() + "g", Center, new Vector2((float)TMBAW_Game.Random.NextDouble() * (TMBAW_Game.Random.Next(0, 2) - 1), -13), new Color(255, 233, 108)); //GameWorld.ParticleSystem.Add(new SplashNumber(this, GetValue(), Color.DarkGoldenrod)); }
public override void Bind(Entity entity, Main main, bool creating = false) { if (ParticleSystem.Get(main, "Snow") == null) { ParticleSystem.Add(main, "Snow", new ParticleSystem.ParticleSettings { TextureName = "Particles\\default", EffectFile = "Effects\\ParticleSnow", MaxParticles = 50000, Duration = TimeSpan.FromSeconds(SnowFactory.MaxLifetime), MinHorizontalVelocity = -1.0f, MaxHorizontalVelocity = 1.0f, MinVerticalVelocity = -1.0f, MaxVerticalVelocity = 1.0f, Gravity = new Vector3(0.0f, 0.0f, 0.0f), MinRotateSpeed = 0.0f, MaxRotateSpeed = 0.0f, MinStartSize = 0.05f, MaxStartSize = 0.15f, MinEndSize = 0.05f, MaxEndSize = 0.15f, MinColor = new Vector4(0.5f, 0.5f, 0.5f, 1.0f), MaxColor = new Vector4(0.5f, 0.5f, 0.5f, 1.0f), EmitterVelocitySensitivity = 1.0f, BlendState = BlendState.Opaque, Material = new Components.Model.Material { SpecularIntensity = 0.0f, SpecularPower = 1.0f }, }); ParticleSystem.Add(main, "Wind", new ParticleSystem.ParticleSettings { TextureName = "Particles\\wind", EffectFile = "Effects\\ParticleVolume", MaxParticles = 10000, Duration = TimeSpan.FromSeconds(SnowFactory.MaxWindLifetime), MinHorizontalVelocity = -1.0f, MaxHorizontalVelocity = 1.0f, MinVerticalVelocity = -1.0f, MaxVerticalVelocity = 1.0f, Gravity = new Vector3(0.0f, 0.0f, 0.0f), MinRotateSpeed = -1.0f, MaxRotateSpeed = 1.0f, MinStartSize = 20.0f, MaxStartSize = 30.0f, MinEndSize = 30.0f, MaxEndSize = 45.0f, MinColor = new Vector4(1.0f, 1.0f, 1.0f, 0.2f), MaxColor = new Vector4(1.0f, 1.0f, 1.0f, 0.2f), EmitterVelocitySensitivity = 1.0f, BlendState = BlendState.AlphaBlend, }); } entity.CannotSuspendByDistance = true; Transform transform = entity.GetOrCreate <Transform>("Transform"); ParticleWind wind = entity.GetOrCreate <ParticleWind>("Wind"); ParticleEmitter emitter = entity.GetOrCreate <ParticleEmitter>("Emitter"); emitter.Add(new Binding <Vector3>(emitter.Jitter, wind.Jitter)); ParticleEmitter windEmitter = entity.GetOrCreate <ParticleEmitter>("WindEmitter"); windEmitter.Add(new Binding <Vector3>(windEmitter.Jitter, wind.Jitter)); Property <Vector3> dir = new Property <Vector3>(); transform.Add(new Binding <Vector3, Quaternion>(dir, x => Vector3.Transform(Vector3.Down, x), transform.Quaternion)); wind.Add(new Binding <Quaternion>(wind.Orientation, transform.Quaternion)); emitter.Add(new Binding <Vector3, float>(emitter.Position, x => new Vector3(0, x * ParticleWind.StartHeightMultiplier, 0), wind.Speed)); windEmitter.Add(new Binding <Vector3, float>(windEmitter.Position, x => new Vector3(0, x * ParticleWind.StartHeightMultiplier * 2, 0), wind.Speed)); emitter.AddParticle = delegate(Vector3 position, Vector3 velocity, float prime) { Vector3 kernelCoord = (position + wind.Jitter) / wind.KernelSpacing; float distance = wind.RaycastDistances[Math.Max(0, Math.Min(ParticleWind.KernelSize - 1, (int)kernelCoord.X)), Math.Max(0, Math.Min(ParticleWind.KernelSize - 1, (int)kernelCoord.Z))]; if (distance > 0) { float lifetime = Math.Min(distance / wind.Speed, SnowFactory.MaxLifetime); if (lifetime > prime) { emitter.ParticleSystem.AddParticle(main.Camera.Position + Vector3.Transform(position, transform.Quaternion), dir.Value * wind.Speed.Value, lifetime, -1.0f, prime); } } }; windEmitter.AddParticle = delegate(Vector3 position, Vector3 velocity, float prime) { Vector3 kernelCoord = (position + wind.Jitter) / wind.KernelSpacing; float distance = wind.RaycastDistances[Math.Max(0, Math.Min(ParticleWind.KernelSize - 1, (int)kernelCoord.X)), Math.Max(0, Math.Min(ParticleWind.KernelSize - 1, (int)kernelCoord.Z))]; if (distance > 0) { float lifetime = Math.Min((distance + ParticleWind.StartHeightMultiplier) / wind.Speed, SnowFactory.MaxWindLifetime); if (lifetime > prime) { windEmitter.ParticleSystem.AddParticle(main.Camera.Position + Vector3.Transform(position, transform.Quaternion), dir.Value * wind.Speed.Value, lifetime, -1.0f, prime); } } }; this.SetMain(entity, main); emitter.ParticleType.Value = "Snow"; windEmitter.ParticleType.Value = "Wind"; entity.Add("KernelSpacing", wind.KernelSpacing); entity.Add("ParticlesPerSecond", emitter.ParticlesPerSecond); entity.Add("WindParticlesPerSecond", windEmitter.ParticlesPerSecond); entity.Add("Wind", wind.Speed); entity.Add(new PostInitialization { delegate() { wind.Update(); emitter.Prime(Vector3.Zero); windEmitter.Prime(Vector3.Zero); } }); }