public void Hit() { if (!IsInvincible) { _lives--; _deadSound.Play(); var yellow = new Color(0.8f, 0.8f, 0.4f); for (int i = 0; i < 1200; i++) { float speed = 18f * (1f - 1 / GameRef.Rand.NextFloat(1f, 10f)); Color color = Color.Lerp(Color.White, yellow, GameRef.Rand.NextFloat(0, 1)); var state = new ParticleState() { Velocity = GameRef.Rand.NextVector2(speed, speed), Type = ParticleType.None, LengthMultiplier = 1 }; GameRef.ParticleManager.CreateParticle(GameRef.LineParticle, Position, color, 190, 1.5f, state); } _timeBeforeRespawn = Config.PlayerTimeBeforeRespawn; IsInvincible = true; CollisionBoxes.Add(_shieldCollisionCircle); } }
public virtual void Draw(GameTime gameTime) { if (Config.DisplayCollisionBoxes) { CollisionBoxes.Draw(GameRef.SpriteBatch); } }
protected override void LoadContent() { base.LoadContent(); Sprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/player"); _bulletSprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/player_bullet"); _hitboxSprite = GameRef.Content.Load <Texture2D>("Graphics/Pictures/player_hitbox"); CollisionBoxes.Add(new CollisionCircle(this, new Vector2(Sprite.Height / 6f, Sprite.Height / 6f), _hitboxRadius / 2f)); _shieldSprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/shield"); _shieldOrigin = new Vector2(_shieldSprite.Width / 2f, _shieldSprite.Height / 2f); _shieldCollisionCircle = new CollisionCircle(this, Vector2.Zero, _shieldSprite.Width / 2f); _lifeIcon = GameRef.Content.Load <Texture2D>("Graphics/Pictures/life_icon"); _bulletTimeBarLeft = GameRef.Content.Load <Texture2D>("Graphics/Pictures/gauge_left"); _bulletTimeBarContent = GameRef.Content.Load <Texture2D>("Graphics/Pictures/gauge_middle"); _bulletTimeBarRight = GameRef.Content.Load <Texture2D>("Graphics/Pictures/gauge_right"); if (_shootSound == null) { _shootSound = GameRef.Content.Load <SoundEffect>(@"Audio/SE/hit"); } if (_deadSound == null) { _deadSound = GameRef.Content.Load <SoundEffect>(@"Audio/SE/dead"); } }
protected override void LoadContent() { Sprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/boss_bullet_type3"); CollisionBoxes.Add(new CollisionCircle(this, Vector2.Zero, Sprite.Height / 2f)); Origin = new Vector2(Sprite.Height / 2f, Sprite.Height / 2f); _trail = GameRef.Content.Load <Texture2D>("Graphics/Pictures/trail"); base.LoadContent(); }
public Bullet(DnK gameRef, Texture2D sprite, Vector2 position, Vector2 direction, Vector2 velocity) : base(gameRef, sprite, position, direction, velocity) { Rotation = (float)Math.Atan2(direction.Y, direction.X) - MathHelper.PiOver2; _distance = 0; WaveMode = false; CollisionBoxes.Add(new CollisionCircle(this, Vector2.Zero, sprite.Width / 2f)); Power = Improvements.ShootPowerData[PlayerData.ShootPowerIndex].Key; }
public BulletView(Texture2D sprite, Vector2 position, Vector2 direction, Vector2 velocity) { Position = position; Sprite = sprite; Velocity = velocity; Direction = direction; Rotation = (float)Math.Atan2(direction.Y, direction.X) - MathHelper.PiOver2; _distance = 0; WaveMode = false; CollisionBoxes.Add(new CollisionCircle(this, Vector2.Zero, sprite.Width / 2f)); Power = Improvements.ShootPowerData[PlayerData.ShootPowerIndex].Key; }
public override void Draw(GameTime gameTime) { GameRef.SpriteBatch.Draw(Sprite, Position, null, Color.White, Direction, Origin, 1f, SpriteEffects.None, 0f); GameRef.SpriteBatch.Draw(_trail, Position, null, Color.White, Direction + (float)(Math.PI / 2f), Vector2.Zero, 1f, SpriteEffects.None, 0f); if (Config.DisplayCollisionBoxes) { CollisionBoxes.Draw(GameRef.SpriteBatch); } base.Draw(gameTime); }
protected override void InitializeLogic() { var logicSet = new LogicSet(this); logicSet.AddAction(new ChangePropertyLogicAction(this, "Shake", true)); logicSet.AddAction(new DelayLogicAction(1f)); logicSet.AddAction(new ChangePropertyLogicAction(this, "Shake", false)); logicSet.AddAction(new DelayLogicAction(1f)); var logicSet2 = new LogicSet(this); logicSet2.AddAction(new ChaseLogicAction(m_target, Vector2.Zero, Vector2.Zero, true, 1f)); var logicSet3 = new LogicSet(this); logicSet3.AddAction(new ChaseLogicAction(m_target, Vector2.Zero, Vector2.Zero, true, 1.75f)); ThrowAdvancedProjectiles(logicSet3, true); var logicSet4 = new LogicSet(this); logicSet4.AddAction(new ChaseLogicAction(m_target, Vector2.Zero, Vector2.Zero, true, 1.75f)); ThrowExpertProjectiles(logicSet4, true); var logicSet5 = new LogicSet(this); logicSet5.AddAction(new ChaseLogicAction(m_target, Vector2.Zero, Vector2.Zero, true, 1.25f)); ThrowProjectiles(logicSet5, true); m_generalBasicLB.AddLogicSet(logicSet, logicSet2); m_generalAdvancedLB.AddLogicSet(logicSet, logicSet3); m_generalExpertLB.AddLogicSet(logicSet, logicSet4); m_generalMiniBossLB.AddLogicSet(logicSet, logicSet5); m_generalCooldownLB.AddLogicSet(logicSet, logicSet2); logicBlocksToDispose.Add(m_generalBasicLB); logicBlocksToDispose.Add(m_generalAdvancedLB); logicBlocksToDispose.Add(m_generalExpertLB); logicBlocksToDispose.Add(m_generalMiniBossLB); logicBlocksToDispose.Add(m_generalCooldownLB); base.InitializeLogic(); CollisionBoxes.Clear(); CollisionBoxes.Add(new CollisionBox((int)(-18f * ScaleX), (int)(-24f * ScaleY), (int)(36f * ScaleX), (int)(48f * ScaleY), 2, this)); CollisionBoxes.Add(new CollisionBox((int)(-15f * ScaleX), (int)(-21f * ScaleY), (int)(31f * ScaleX), (int)(44f * ScaleY), 1, this)); if (Difficulty == GameTypes.EnemyDifficulty.MINIBOSS) { (GetChildAt(0) as SpriteObj).ChangeSprite("GiantPortrait_Sprite"); Scale = new Vector2(2f, 2f); AddChild(new SpriteObj("Portrait" + CDGMath.RandomInt(0, 7) + "_Sprite") { OverrideParentScale = true }); CollisionBoxes.Clear(); CollisionBoxes.Add(new CollisionBox(-124, -176, 250, 354, 2, this)); CollisionBoxes.Add(new CollisionBox(-124, -176, 250, 354, 1, this)); } }
protected override void LoadContent() { Sprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/boss_core"); _eyeOrbit = GameRef.Content.Load <Texture2D>("Graphics/Entities/eye_orbit"); // TODO: Upadte AnimatedSprite class to take a "rectangle" order var animation = new Animation(6, 42, 38, 0, 0, 2); Origin = new Vector2(21, 19); Scale = new Vector2(1.5f, 1.5f); _animatedSprite = new AnimatedSprite(Sprite, animation, Position) { IsAnimating = false }; CollisionBoxes.Add(new CollisionCircle(_parent, Vector2.Zero, 25)); base.LoadContent(); }
public virtual bool Intersects(Entity entity) { return(CollisionBoxes.Intersects(entity.CollisionBoxes)); }
protected override void LoadContent() { Sprite = GameRef.Content.Load <Texture2D>("Graphics/Entities/base_turret"); CollisionBoxes.Add(new CollisionCircle(this, Vector2.Zero, Sprite.Width / 2f)); }
/// <summary> /// Triggers Collision resolution for PlayerActor's Update() method, and adds Rectangle for /// handling to the CollisionBoxes list /// </summary> /// <param name="collisionRectangle">Rectangle specifying the exact space of intersecting BoundingBoxes</param> public void HasCollided(Rectangle collisionRectangle) { IsColliding = true; CollisionBoxes.Add(collisionRectangle); }
// Create multiple collision boxes from BossStructure private void ComputeCollisionBoxes() { CollisionBoxes.Clear(); _collisionBoxesHp.Clear(); // TODO: Handle the case where there is 2 vertices with Y = 0 var vertices = _structure.GetVertices(); var bottomVertices = new List <Vector2>(); // the lowest vertex for each step var topVertices = new List <Vector2>(); // the highest vertex for each step var currentStep = 0f; for (var i = 0; i < vertices.Length; i++) { // Bottom part if (vertices[i].Y >= 0) { if (vertices[i].X > currentStep) { bottomVertices.Add(vertices[i - 1]); bottomVertices.Add(vertices[i]); } } // Top part else { if (vertices[i].X < currentStep) { topVertices.Add(vertices[i - 1]); topVertices.Add(vertices[i]); } } currentStep = vertices[i].X; } if (bottomVertices.Count != topVertices.Count) { // Left part if (bottomVertices.First().Y.Equals(0f)) { topVertices.Add(vertices[vertices.Length - 1]); topVertices.Add(bottomVertices.First()); topVertices.Reverse(); } // Right part else if (bottomVertices.Last().Y.Equals(0f)) { topVertices.Reverse(); topVertices.Add(bottomVertices.Last()); } } else { topVertices.Reverse(); } if (bottomVertices.Count != topVertices.Count) { return; } for (var i = 1; i < bottomVertices.Count; i += 2) { var boxVertices = new List <Vector2> { bottomVertices[i], bottomVertices[i - 1], topVertices[i - 1], topVertices[i] }; var collisionBox = new CollisionConvexPolygon(this, Vector2.Zero, boxVertices); CollisionBoxes.Add(collisionBox); _collisionBoxesHp.Add(collisionBox, 100f); } }
private void Split(CollisionConvexPolygon box) { var newPolygonShape = _structure.Split(box); var center = box.GetCenterInWorldSpace(); // TODO: Part is dead? // A boss part is dead when its center is dead? // or when the number of sub-parts is less than a number? if (center.X > (Size.X / 2f - 2 * _step) + Position.X - Origin.X && center.X < (Size.X / 2f + 2 * _step) + Position.X - Origin.X) { TakeDamage(99999); } else { var boxLocalPosition = box.GetLocalPosition(); // Left (1) or right (-1) part? var factor = (boxLocalPosition.X > Origin.X) ? 1 : -1; // If the break out part is not large enough => we don't create another part if (newPolygonShape.Vertices != null && newPolygonShape.GetArea() > Config.MinBossPartArea) { var bossPart = new BossPart( GameRef, _bossRef, _players, _moverManager, null, _color, _health, 0, 25f, null, newPolygonShape ); bossPart.Initialize(); var bossPartSize = newPolygonShape.GetSize(); var boxWorldPosition = box.GetWorldPosition(); // Compute new boss part's world position var worldPosition = Vector2.Zero; worldPosition.Y = boxWorldPosition.Y - boxLocalPosition.Y; // Left part if (factor == 1) { worldPosition.X = boxWorldPosition.X - bossPartSize.X; } // Right part else if (factor == -1) { worldPosition.X = boxWorldPosition.X + _step; } // Update world position according to parent rotation bossPart.Scale = Scale; bossPart.Rotation = Rotation; var newLocalPosition = Vector2.Zero; newLocalPosition.X = (Position.X - Origin.X) + (boxLocalPosition.X); newLocalPosition.Y = (Position.Y - Origin.Y); if (factor == -1) { newLocalPosition.X -= bossPartSize.X; } else { newLocalPosition.X += _step; } var newOrigin = newPolygonShape.GetCenter(); newLocalPosition += newOrigin; var rotationOrigin = Position; newLocalPosition = Vector2.Transform(newLocalPosition - rotationOrigin, Matrix.CreateRotationZ(Rotation)) + rotationOrigin; bossPart.Origin = newOrigin; bossPart.Position = newLocalPosition; _bossRef.Parts.Add(bossPart); // Give to this new BossPart an impulsion to (pseudo) random direction due to explosion var random = (float)(GameRef.Rand.NextDouble() * (1f - 0.75f)) + 0.75f; bossPart.ApplyImpulse(new Vector2(factor, random * factor), new Vector2(random / 5f)); ApplyImpulse(new Vector2(-factor, random * -factor), new Vector2(random / 5f)); } // Remove destroyed bounding boxes if (factor == -1) { CollisionBoxes.RemoveAll(bb => bb.GetCenter().X < boxLocalPosition.X); } else { CollisionBoxes.RemoveAll(bb => bb.GetCenter().X > boxLocalPosition.X); } // This is the bounding that the player has destroyed, so we remove it from the list CollisionBoxes.Remove(box); } }
public void SetHitboxTypes(IEnumerable <string> hitboxTypes) { CollisionBoxes.Clear(); CollisionBoxes.AddRange(hitboxTypes); }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (_lives <= 0) { IsAlive = false; } if (IsInvincible) { if (_timeBeforeRespawn.TotalMilliseconds > 0) { _timeBeforeRespawn -= gameTime.ElapsedGameTime; if (_timeBeforeRespawn.TotalMilliseconds <= 0) { Position = _originPosition; } } else { _invincibleTime -= gameTime.ElapsedGameTime; if (_invincibleTime.TotalMilliseconds <= 0) { _invincibleTime = Config.PlayerInvicibleTime; IsInvincible = false; CollisionBoxes.Remove(_shieldCollisionCircle); } } } //else { float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; _direction = Vector2.Zero; if (_controller == Config.Controller.Keyboard) { // Keyboard if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Up"])) { _direction.Y = -1; } if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Right"])) { _direction.X = 1; } if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Down"])) { _direction.Y = 1; } if (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Left"])) { _direction.X = -1; } SlowMode = (PlayerData.SlowModeEnabled && (InputHandler.KeyDown(Config.PlayerKeyboardInputs["Slow"]))) ? true : false; BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.MouseState.RightButton == ButtonState.Pressed)) ? true : false; if (_direction != Vector2.Zero) { _velocitySlowMode = Config.PlayerMaxSlowVelocity / 2; _velocity = Config.PlayerMaxVelocity / 2; } else { _velocitySlowMode = Config.PlayerMaxSlowVelocity; _velocity = Config.PlayerMaxVelocity; } // Mouse _distance.X = (_viewport.Width / 2f) - InputHandler.MouseState.X; _distance.Y = (_viewport.Height / 2f) - InputHandler.MouseState.Y; Rotation = (float)Math.Atan2(_distance.Y, _distance.X) - MathHelper.PiOver2; // Debug if (InputHandler.KeyDown(Keys.R)) { Rotation = 0; } else if (InputHandler.KeyPressed(Keys.V)) { Hit(); } if (InputHandler.MouseState.LeftButton == ButtonState.Pressed) { Fire(gameTime); } if (InputHandler.MouseState.RightButton == ButtonState.Pressed) { _focusMode = true; SlowMode = true; } else if (_focusMode) { _focusMode = false; SlowMode = false; } } else if (_controller == Config.Controller.GamePad) { _direction.X = InputHandler.GamePadStates[0].ThumbSticks.Left.X; _direction.Y = (-1) * InputHandler.GamePadStates[0].ThumbSticks.Left.Y; SlowMode = (PlayerData.SlowModeEnabled && (InputHandler.ButtonDown(Config.PlayerGamepadInput[0], PlayerIndex.One))) ? true : false; BulletTime = (PlayerData.BulletTimeEnabled && (!_bulletTimeReloading && InputHandler.ButtonDown(Config.PlayerGamepadInput[1], PlayerIndex.One))) ? true : false; if (InputHandler.GamePadStates[0].ThumbSticks.Right.Length() > 0) { Rotation = (float) Math.Atan2(InputHandler.GamePadStates[0].ThumbSticks.Right.Y * (-1), InputHandler.GamePadStates[0].ThumbSticks.Right.X) + MathHelper.PiOver2; Fire(gameTime); } } if (BulletTime) { _bulletTimeTimer -= gameTime.ElapsedGameTime; if (_bulletTimeTimer <= TimeSpan.Zero) { _bulletTimeReloading = true; _bulletTimeTimer = TimeSpan.Zero; } } if (_bulletTimeReloading) { _bulletTimeTimer += gameTime.ElapsedGameTime; if (_bulletTimeTimer >= Config.DefaultBulletTimeTimer) { _bulletTimeReloading = false; _bulletTimeTimer = Config.DefaultBulletTimeTimer; } } UpdatePosition(dt); } // Update camera position _cameraPosition.X = MathHelper.Lerp( _cameraPosition.X, Position.X - Config.CameraDistanceFromPlayer.X * (float)Math.Cos(Rotation + MathHelper.PiOver2), Config.CameraMotionInterpolationAmount ); _cameraPosition.Y = MathHelper.Lerp( _cameraPosition.Y, Position.Y - Config.CameraDistanceFromPlayer.Y * (float)Math.Sin(Rotation + MathHelper.PiOver2), Config.CameraMotionInterpolationAmount ); _camera.Update(_cameraPosition); if (!Config.DisableCameraZoom) { // Update camera zoom according to mouse distance from player var mouseWorldPosition = new Vector2( _cameraPosition.X - GameRef.Graphics.GraphicsDevice.Viewport.Width / 2f + InputHandler.MouseState.X, _cameraPosition.Y - GameRef.Graphics.GraphicsDevice.Viewport.Height / 2f + InputHandler.MouseState.Y ); var mouseDistanceFromPlayer = (float) Math.Sqrt(Math.Pow(Position.X - mouseWorldPosition.X, 2) + Math.Pow(Position.Y - mouseWorldPosition.Y, 2)); var cameraZoom = GameRef.Graphics.GraphicsDevice.Viewport.Width / mouseDistanceFromPlayer; if (_focusMode) { cameraZoom = 1f; } else { cameraZoom = cameraZoom > Config.CameraZoomLimit ? 1f : MathHelper.Clamp(cameraZoom / Config.CameraZoomLimit, Config.CameraZoomMin, Config.CameraZoomMax); } _camera.Zoom = MathHelper.Lerp(_camera.Zoom, cameraZoom, Config.CameraZoomInterpolationAmount); } }
public void Update(GameTime gameTime) { HandleInput(); Vector2 _position = new Vector2(0); if (ControlKeys["UpKey"] == true) { if (IsAnimated == false) { IsAnimated = true; } if (IsColliding == true && IsCollisionAbove(SolidBoundingBox.Bottom) == true) { CurrentAnimation = "PushingUp"; } else { CurrentAnimation = "MoveUp"; } _position = _position + new Vector2(0, -MoveSpeed); Facing = PlayerFacing.Up; } if (ControlKeys["DownKey"] == true) { if (IsAnimated == false) { IsAnimated = true; } if (IsColliding == true && IsCollisionLeft(SolidBoundingBox.Top) == true) { CurrentAnimation = "PushingDown"; } else { CurrentAnimation = "MoveDown"; } _position = _position + new Vector2(0, MoveSpeed); Facing = PlayerFacing.Down; } if (ControlKeys["RightKey"] == true) { if (IsAnimated == false) { IsAnimated = true; } if (IsColliding == true && IsCollisionRight(SolidBoundingBox.Left) == true) { CurrentAnimation = "PushingRight"; } else { CurrentAnimation = "MoveRight"; } _position = _position + new Vector2(MoveSpeed, 0); Facing = PlayerFacing.Right; } if (ControlKeys["LeftKey"] == true) { if (IsAnimated == false) { IsAnimated = true; } if (IsColliding == true && IsCollisionLeft(SolidBoundingBox.Right) == true) { CurrentAnimation = "PushingLeft"; } else { CurrentAnimation = "MoveLeft"; } _position = _position + new Vector2(-MoveSpeed, 0); Facing = PlayerFacing.Left; } if (ControlKeys["NoMoveKeys"] == true) { IsAnimated = false; switch (Facing) { case PlayerFacing.Down: { CurrentAnimation = "MoveDown"; break; } case PlayerFacing.Up: { CurrentAnimation = "MoveUp"; break; } case PlayerFacing.Left: { CurrentAnimation = "MoveLeft"; break; } case PlayerFacing.Right: { CurrentAnimation = "MoveRight"; break; } } AnimationManager.Play(CurrentAnimation); } if (IsColliding == true) { foreach (Rectangle _collision in CollisionBoxes) { Uncollide(_collision); } for (int i = CollisionBoxes.Count - 1; i >= 0; i--) { CollisionBoxes.RemoveAt(i); } IsColliding = false; } UpdatePosition(_position); if (IsAnimated == true) { AnimationManager.Play(CurrentAnimation); AnimationManager.Update(gameTime); } }