Exemplo n.º 1
0
        /// <summary>
        /// Test collisions for the given entity on the given axis.
        /// </summary>
        /// <param name="ent"> The entity to check collisions for.</param>
        /// <param name="axis"> The axis to check collisions on.</param>
        public void TestCollisions(Entity ent, CollisionTester.Axis axis)
        {
            if (_collidables.Count == 0)
            {
                // Concatenate all entities of the current frame.
                _collidables = _backgroundTiles.Concat(_entities).ToList();

                _collidables.RemoveAll(e => !e.Collidable);
            }

            // Get nearest collidables
            List <Entity> checkableColliders = _collidables.OrderBy(e => Vector2.Distance(ent.Position, e.Position)).Take(12).ToList();

            foreach (Entity c in checkableColliders)
            {
                if (c != ent && c.IsCollidable(ent, axis) && _collisions.TestAABB(ent, c))
                {
                    // Get test results
                    CollisionTestResult result = _collisions.Result;
                    Entity collider            = result.Collider;

                    // Resolve the collision occured
                    ent.OnCollision(collider, result.Penetration, result.Side, axis, collider.PreviousBounds);
                    collider.OnCollision(ent, result.Penetration, result.OppositeSide, axis, ent.PreviousBounds);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns if the Platform is collidable.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <returns>True if the platform is collidable.</returns>
        public override bool IsCollidable(Entity ent, CollisionTester.Axis axis)
        {
            Rectangle fromBounds = ent.PreviousBounds;

            return(
                Collidable && (                                 // Is the platform collidable in general?
                    axis == CollisionTester.Axis.Y &&           // Is the axis being checked Axis.Y?
                    (fromBounds.Bottom - Bounds.Top) <= 0) ||   // Does the player comes from above?

                (axis == CollisionTester.Axis.X &&              // Is the axis being checked Axis.X?
                 CollideLeft &&                                 // Should you collide on the Left side
                 (fromBounds.Right - Bounds.Left) <= 0 &&       // Does the player comes from left?
                 (fromBounds.Bottom - Bounds.Bottom) <= 0) ||   // Does the player comes from above?

                (axis == CollisionTester.Axis.X &&              // Is the axis being checked Axis.X?
                 CollideRight &&                                // Should you collide on the Right side
                 (fromBounds.Left - Bounds.Right) <= 0 &&       // Does the player comes from right?
                 (fromBounds.Bottom - Bounds.Bottom) <= 0)      // Does the player comes from above?
                );
        }
Exemplo n.º 3
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            if (ent is Player && _state == FinishState.OPEN)
            {
                _state = FinishState.CLOSED;

                Animations.Play((int)_state);
                OnClosed?.Invoke(this, new EventArgs());
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            if (!(ent is Enemy))
            {
                ResolveCollision(ent, penetration, side);

                if (ent.Solid && axis == CollisionTester.Axis.X)
                {
                    TurnAround();
                    State = PowerUpState.MOVING;
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Function gets called when a collision with this object occurs.
 /// </summary>
 /// <param name="ent"> Entity this object collides with.</param>
 /// <param name="penetration"> Amount of penetration.</param>
 /// <param name="side"> The side the objects collide on.</param>
 /// <param name="axis"> The axis that is being checked.</param>
 /// <param name="fromBounds"> Where the colliding entity came from.</param>
 public virtual void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
 {
     //
 }
Exemplo n.º 6
0
 /// <summary>
 /// Returns if the entity is collidable.
 /// </summary>
 /// <param name="ent"> Entity this object collides with.</param>
 /// <param name="axis"> The axis that is being checked.</param>
 /// <returns>True if the entity is collidable.</returns>
 public virtual bool IsCollidable(Entity ent, CollisionTester.Axis axis)
 {
     return(Collidable);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // Check if the entity colliding is player and came from the bottom.
            if (ent is Player && side == CollisionTester.CollisionSide.BOTTOM && axis == CollisionTester.Axis.Y)
            {
                // Check if there is a powerup inside.
                if (_item != null)
                {
                    // Get the powerup.
                    PowerUp item = _item;

                    // Set the position of the powerup.
                    item.Position = Position;

                    // Call the spawn of the powerup.
                    item.OnSpawn();

                    // Add the powerup to the level.
                    Parent.AddEntity(item);

                    // Play wood animation.
                    Animations.Play((int)GiftBlockAnimation.WOOD);

                    // Remove item.
                    _item = null;
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Function gets called when a collision with this object occurs.
 /// </summary>
 /// <param name="ent"> Entity this object collides with.</param>
 /// <param name="penetration"> Amount of penetration.</param>
 /// <param name="side"> The side the objects collide on.</param>
 /// <param name="axis"> The axis that is being checked.</param>
 /// <param name="fromBounds"> Where the colliding entity came from.</param>
 public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
 {
     // Dont allow player from side to kill it.
     if (ent is Player)
     {
         ((Player)ent).OnEnemyCollision();
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // Check if the collider is a player.
            if (ent is Player)
            {
                // Get the player.
                Player _ent = (Player)ent;

                // Disable collision for this mushroom.
                Collidable = false;
                // Make mushroom none solid.
                Solid = false;

                // Add score.
                Parent.Score.IncreaseScore(1000);

                // If player is small make the player grow.
                if (_ent.PowerState == Player.SizeState.SMALL)
                {
                    _ent.Grow();
                }
                // If the player is big.
                else
                {
                    // Add the mushroom to the HUD.
                    Parent.Display.AddPowerUp(new Mushroom(Vector2.Zero, Width, Height, _content, Parent));
                }

                Destroy();
            }

            // Call base collision.
            base.OnCollision(ent, penetration, side, axis, fromBounds);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Function gets called when a collision with this object occurs.
 /// </summary>
 /// <param name="ent"> Entity this object collides with.</param>
 /// <param name="penetration"> Amount of penetration.</param>
 /// <param name="side"> The side the objects collide on.</param>
 /// <param name="axis"> The axis that is being checked.</param>
 /// <param name="fromBounds"> Where the colliding entity came from.</param>
 public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
 {
     ResolveCollision(ent, penetration, side);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // If player clollides with coin, pick it up and destroy the coin.
            if (ent is Player)
            {
                OnPickUp();

                Destroy();
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Function gets called when a collision with this object occurs.
 /// </summary>
 /// <param name="ent"> Entity this object collides with.</param>
 /// <param name="penetration"> Amount of penetration.</param>
 /// <param name="side"> The side the objects collide on.</param>
 /// <param name="axis"> The axis that is being checked.</param>
 /// <param name="fromBounds"> Where the colliding entity came from.</param>
 public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
 {
     // Check if collider is player.
     if (ent is Player)
     {
         // Call on enemy collision function of player.
         ((Player)ent).OnEnemyCollision();
     }
 }
Exemplo n.º 13
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // Check if collider is player.
            if (ent is Player)
            {
                // Check if player comes from top.
                bool fromTop = (side == CollisionTester.CollisionSide.TOP && (fromBounds.Bottom - Bounds.Top) <= 0 && axis == CollisionTester.Axis.Y);

                // Get player.
                Player player = (Player)ent;

                // If player comes top and player is not invulnerable.
                if (fromTop && !player.Invulnerable)
                {
                    // If shell is moving make it idle, if it is idle make it moving.
                    AllowMovement = !AllowMovement;

                    // Start in opposite direction where it came from.
                    TurnAround();

                    // Play sound.
                    if (_startMovingSound != null)
                    {
                        _startMovingSound.Play();
                    }

                    // Make the palyer jump.
                    player.Jump();
                }
                // If player is not from top and shell is moving and player is not invurnelable.
                else if (Math.Abs(velocity.X) > 0 && !player.Invulnerable)
                {
                    // Call in player the on enemy collision function
                    ((Player)ent).OnEnemyCollision();
                }

                // If the player walks to it from the side and the shell is not moving.
                if ((int)velocity.X == 0 && axis == CollisionTester.Axis.X)
                {
                    // Make the shell move.
                    AllowMovement = true;

                    // Play sound.
                    if (_startMovingSound != null)
                    {
                        _startMovingSound.Play();
                    }

                    // Make shell move away from player.
                    if (player.Position.X < Position.X)
                    {
                        // If player is to the left make shell go to right.
                        FacingDirection = Facing.RIGHT;
                    }
                    else
                    {
                        // If player is to the right make shell go to left.
                        FacingDirection = Facing.LEFT;
                    }
                }
            }
            else
            {
                // Resolve collision.
                ResolveCollision(ent, penetration, side);
            }

            // Check if collision axis is X axis.
            if (axis == CollisionTester.Axis.X && ent.Solid)
            {
                // If ent is enemy and shell is moving.
                if (ent is Enemy && !(ent is EggEnemy) && AllowMovement)
                {
                    // Get enemy.
                    Enemy enemy = (Enemy)ent;

                    // If enemy is not invulnerable.
                    if (!enemy.Invulnerable)
                    {
                        // Kill enemy.
                        enemy.OnDeath(this);
                    }
                    else
                    {
                        TurnAround();
                    }
                }
                // If entity is not player turn around.
                else if (!(ent is Player))
                {
                    TurnAround();
                }
            }

            // Check if shell is moving.
            if (AllowMovement)
            {
                // Play moving animation.
                Animations.Play((int)EggEnemyAnimation.MOVING);
            }
            else
            {
                // Play idle animation.
                Animations.Play((int)EggEnemyAnimation.IDLE);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // Check if collision is a player and came from the bottom.
            if (ent is Player && side == CollisionTester.CollisionSide.BOTTOM && axis == CollisionTester.Axis.Y)
            {
                // Check if there are any coins left.
                if (_coins > 0)
                {
                    // Remove a coin.
                    _coins--;

                    int halfHeight = (Height / 2);

                    // Create a new coin.
                    Coin coin = new Coin(new Vector2(Position.X + 2, Position.Y - halfHeight), _content, Parent);

                    // Coin should perform on free action.
                    coin.OnFree();

                    // Add coin to the level.
                    Parent.AddEntity(coin);

                    // Set the state to BUMP_START.
                    State = TileState.BUMP_START;
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Function gets called when a collision with this object occurs.
        /// </summary>
        /// <param name="ent"> Entity this object collides with.</param>
        /// <param name="penetration"> Amount of penetration.</param>
        /// <param name="side"> The side the objects collide on.</param>
        /// <param name="axis"> The axis that is being checked.</param>
        /// <param name="fromBounds"> Where the colliding entity came from.</param>
        public override void OnCollision(Entity ent, int penetration, CollisionTester.CollisionSide side, CollisionTester.Axis axis, Rectangle fromBounds)
        {
            // Check if collider is player.
            if (ent is Player)
            {
                // Check if player came from top.
                bool fromTop = (side == CollisionTester.CollisionSide.TOP && (fromBounds.Bottom - Bounds.Top) <= 0 && axis == CollisionTester.Axis.Y);

                // Get player.
                Player player = (Player)ent;

                // If player came from top and player is not invulnerable.
                if (fromTop && !player.Invulnerable)
                {
                    // Call enemy on death.
                    OnDeath(player);

                    EnableDeathTimer();

                    // Make the player jump.
                    player.Jump();
                }
                else // Player is not comming from top.
                {
                    // Check if enemy is solid.
                    if (Solid)
                    {
                        // Call in player the on enemy collision function.
                        player.OnEnemyCollision();
                    }
                }
            }
            // If colliding with solid but not with player.
            else if (ent.Solid && axis == CollisionTester.Axis.X)
            {
                // Turn enemy around.
                TurnAround();
            }

            // If enemy is not colliding with power up resolve collision.
            if (!(ent is PowerUp))
            {
                ResolveCollision(ent, penetration, side);
            }
        }