public void Update(LD44Game game, Level level, float delta) { PlayerMob playerMob = level.Mobs.OfType <PlayerMob>().First(); if (Vector2.Distance(playerMob.Body.Position, Body.Position) < 7f) { if (playerMob.Body.Position.X > Body.Position.X) { Sprite.Effects = SpriteEffects.FlipHorizontally; } else if (playerMob.Body.Position.X < Body.Position.X) { Sprite.Effects = SpriteEffects.None; } _shootCharger -= delta; if (_shootCharger <= 0f) { game.Content.Load <SoundEffect>("Sounds/projectile").Play(); var projectile = new ProjectileMob { Animation = new AnimationState <Sprite>(game.SpriteAnimations["projectile"], 0.2f) { IsLooping = true } }; projectile.Body.Position = Body.Position; projectile.Body.Velocity = Vector2.Normalize(playerMob.Body.Position - Body.Position) * 20f; level.FutureMobs.Add(projectile); _shootCharger = 3f; } } }
public void Update(LD44Game game, Level level, float delta) { if (_hasTarget) { _timer += delta; if (_timer > _speed) { _timer = _speed; } float p = _timer / _speed; p = 1f - (float)Math.Pow(1f - p, _smoothing); Body.Position = _startPosition * (1f - p) + _targetPosition * p - new Vector2(0f, _arc * (float)Math.Sin(p * (float)Math.PI)); } switch (_state) { case State.None: { _thinkTimer -= delta; if (_thinkTimer <= 0f) { int action = _random.Next(3); if (action == _lastAction) { action--; if (action < 0) { action = 2; } } _lastAction = action; switch (action) { case 0: { if (_random.Next(2) == 0) { _state = State.PreparingToChargeLeft; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(25.5f, 15.5f); _timer = 0f; _speed = 2f; _arc = 2f; _smoothing = 3f; _chargeNum = 0; } else { _state = State.PreparingToChargeRight; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(6.5f, 15.5f); _timer = 0f; _speed = 2f; _arc = 2f; _smoothing = 3f; _chargeNum = 0; break; } break; } case 1: { _state = State.Summoning; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(12f + 8f * (float)_random.NextDouble(), 13.5f); _timer = 0f; _speed = 2f; _arc = 1f; _smoothing = 2f; break; } case 2: { _state = State.PreparingToShoot; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(12f + 8f * (float)_random.NextDouble(), 9.5f); _timer = 0f; _speed = 2f; _arc = 2f; _smoothing = 3f; break; } } } break; } case State.PreparingToChargeLeft: { if (_timer >= _speed) { _startPosition = Body.Position; _targetPosition = new Vector2(5.5f, 15.5f); _timer = 0f; _speed = 1.75f - _chargeNum / 7f; _state = State.ChargingLeft; _arc = 0f; _smoothing = 1.5f; game.Content.Load <SoundEffect>("Sounds/dash").Play(); } break; } case State.PreparingToChargeRight: { if (_timer >= _speed) { _startPosition = Body.Position; _targetPosition = new Vector2(26.5f, 15.5f); _timer = 0f; _speed = 1.75f - _chargeNum / 7f; _state = State.ChargingRight; _arc = 0f; _smoothing = 1.5f; game.Content.Load <SoundEffect>("Sounds/dash").Play(); } break; } case State.ChargingLeft: { if (_timer >= _speed) { _chargeNum++; if (_chargeNum < 5) { if (_random.Next(3) == 0) { _startPosition = Body.Position; _targetPosition = new Vector2(26.5f, 15.5f); _timer = 0f; _speed = 1.75f - _chargeNum / 7f; _state = State.ChargingRight; _arc = 0f; _smoothing = 1.5f; game.Content.Load <SoundEffect>("Sounds/dash").Play(); } else { _state = State.PreparingToChargeLeft; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(25.5f, 15.5f); _timer = 0f; _speed = 2f - _chargeNum / 5f; _arc = 2f; _smoothing = 3f - _chargeNum / 5f; } } else { _state = State.None; _thinkTimer = 2f; } } break; } case State.ChargingRight: { if (_timer >= _speed) { _chargeNum++; if (_chargeNum < 5) { if (_random.Next(3) == 0) { _startPosition = Body.Position; _targetPosition = new Vector2(5.5f, 15.5f); _timer = 0f; _speed = 1.75f - _chargeNum / 7f; _state = State.ChargingLeft; _arc = 0f; _smoothing = 1.5f; game.Content.Load <SoundEffect>("Sounds/dash").Play(); } else { _state = State.PreparingToChargeRight; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(6.5f, 15.5f); _timer = 0f; _speed = 2f - _chargeNum / 5f; _arc = 2f; _smoothing = 3f - _chargeNum / 5f; } } else { _state = State.None; _thinkTimer = 2f; } } break; } case State.Summoning: { if (_timer > _speed * 3f / 4f) { Animation = new AnimationState <Sprite>(game.SpriteAnimations["valgox_aiming"], 1f); } if (_timer >= _speed) { var bat = new EliteBatMob { Animation = new AnimationState <Sprite>(game.SpriteAnimations["elite_bat_flying"], 0.5f) { IsLooping = true } }; bat.Body.Position = new Vector2(6.5f + 19f * (float)_random.NextDouble(), 12f); level.FutureMobs.Add(bat); _state = State.None; _thinkTimer = 2f; Animation = new AnimationState <Sprite>(game.SpriteAnimations["valgox_idle"], 0.5f) { IsLooping = true }; } break; } case State.PreparingToShoot: { if (_timer >= _speed) { _state = State.Shooting; _shootTimer = 0.5f; _shotNum = 0; } break; } case State.Shooting: { _shootTimer -= delta; if (_shootTimer < 0.2f) { Animation = new AnimationState <Sprite>(game.SpriteAnimations["valgox_aiming"], 1f); } else if (Animation.Animation != game.SpriteAnimations["valgox_idle"]) { Animation = new AnimationState <Sprite>(game.SpriteAnimations["valgox_idle"], 0.5f) { IsLooping = true }; } if (_shootTimer <= 0f) { game.Content.Load <SoundEffect>("Sounds/projectile").Play(); PlayerMob playerMob = level.Mobs.OfType <PlayerMob>().First(); var projectile = new ProjectileMob { Animation = new AnimationState <Sprite>(game.SpriteAnimations["projectile"], 0.2f) { IsLooping = true } }; projectile.Body.Position = Body.Position; projectile.Body.Velocity = Vector2.Normalize(playerMob.Body.Position - Body.Position) * 20f; level.FutureMobs.Add(projectile); _shotNum++; _shootTimer += 2f / _shotNum; _hasTarget = true; _startPosition = Body.Position; _targetPosition = new Vector2(12f + 8f * (float)_random.NextDouble(), 9f + 3f * (float)_random.NextDouble()); _timer = 0f; _speed = _shootTimer; _arc = 0f; _smoothing = 2f; } if (_shotNum >= 8) { _state = State.None; _thinkTimer = 2f; Animation = new AnimationState <Sprite>(game.SpriteAnimations["valgox_idle"], 0.5f) { IsLooping = true }; } break; } } }
public void Update(LD44Game game, Level level, float delta) { if (_charging) { _chargeTimer -= delta; if (_chargeTimer <= 0f) { _charging = false; } if (Body.Velocity != Vector2.Zero) { float p = _chargeTimer / 0.5f; float ip = 1f - p; var dir = Vector2.Normalize(Body.Velocity); Body.Velocity = dir * 10f * (1f - ip * ip * ip * ip); } return; } PlayerMob playerMob = level.Mobs.OfType <PlayerMob>().First(); Body.Velocity = Vector2.Zero; if (!_sighted) { if (Vector2.Distance(playerMob.Body.Position, Body.Position) < 10f) { _sighted = true; } } else if (playerMob.Body.Position != Body.Position) { float distance = Vector2.Distance(playerMob.Body.Position, Body.Position); if (_preparing) { if (distance > 5f) { _preparing = false; } else { _prepareTimer += delta; if (_prepareTimer > 2f) { var flash = new FlashMob(_random); flash.Body.Position = Body.Position; level.FutureMobs.Add(flash); _charging = true; _chargeTimer = 0.5f; Vector2 dir = (playerMob.Body.Position - Body.Position) / distance; Body.Velocity = dir * 10f; _prepareTimer = 0f; } } } else if (distance > 2f) { Vector2 dir = (playerMob.Body.Position - Body.Position) / distance; Body.Velocity = dir * 4f; if (_prepareTimer > 0f) { _prepareTimer -= delta; } } else { _preparing = true; } if (playerMob.Body.Position.X > Body.Position.X) { Sprite.Effects = SpriteEffects.FlipHorizontally; } else if (playerMob.Body.Position.X < Body.Position.X) { Sprite.Effects = SpriteEffects.None; } } if (Body.Velocity.X > 0f) { Sprite.Effects = SpriteEffects.FlipHorizontally; } else if (Body.Velocity.X < 0f) { Sprite.Effects = SpriteEffects.None; } }
public void Update(LD44Game game, Level level, float delta) { PlayerMob playerMob = level.Mobs.OfType <PlayerMob>().First(); if (!_sighted) { if (_sentryTimer <= 0f) { _sentryTimer = 3f + _random.Next(6); if (_random.Next(8) == 0) { _sentryTimer += 5f; } Sprite.Effects = Sprite.Effects == SpriteEffects.None ? SpriteEffects.FlipHorizontally : SpriteEffects.None; } else { _sentryTimer -= delta; } } var sight = new RectangleF(Body.Position.X, Body.Position.Y - 1.5f, 10f, 3f); if (!FacingRight) { sight.X -= sight.Width; } RectangleF playerRegion = playerMob.Body.Bounds; playerRegion.X = playerMob.Body.Position.X - playerMob.Body.Bounds.Width / 2f; playerRegion.Y = playerMob.Body.Position.Y - playerMob.Body.Bounds.Height / 2f; float distance = Vector2.Distance(Body.Position, playerMob.Body.Position); if (_sighted && distance > 10f) { _sighted = false; } if (!_sighted && (sight.Intersects(playerRegion) || distance < 1.5f)) { _sighted = true; _state = State.Waiting; _stateTimer = 1f; } if (_sighted) { switch (_state) { case State.Waiting: { Body.Velocity = new Vector2(0f, Body.Velocity.Y); if (playerMob.Body.Position.X > Body.Position.X) { Sprite.Effects = SpriteEffects.FlipHorizontally; } else if (playerMob.Body.Position.X < Body.Position.X) { Sprite.Effects = SpriteEffects.None; } _stateTimer -= delta; if (_stateTimer <= 0f) { if (playerMob.Body.Position.Y + 0.5f < Body.Position.Y) { _state = State.Leaping; } else { if (_random.Next(4) == 0) { _state = State.Leaping; } else { _state = State.Running; } } if (_state == State.Running) { _stateTimer = 3f; } else { if (playerMob.Body.Position.X > Body.Position.X) { Body.Velocity = new Vector2(8f, Body.Velocity.Y); } else if (playerMob.Body.Position.X < Body.Position.X) { Body.Velocity = new Vector2(-8f, Body.Velocity.Y); } Body.Velocity -= new Vector2(0f, 13f); _state = State.Leaping; } } break; } case State.Running: { if (playerMob.Body.Position.X - 0.75f > Body.Position.X) { Body.Velocity = new Vector2(6f, Body.Velocity.Y); Sprite.Effects = SpriteEffects.FlipHorizontally; } else if (playerMob.Body.Position.X + 0.75f < Body.Position.X) { Body.Velocity = new Vector2(-6f, Body.Velocity.Y); Sprite.Effects = SpriteEffects.None; } _stateTimer -= delta; if (_stateTimer <= 0f) { _state = State.Waiting; _stateTimer = 1f; } break; } case State.Leaping: { if (Body.Contact.Y > 0f) { _state = State.Waiting; _stateTimer = 1f; } break; } } } }