Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if projectile is still alive
        /// </summary>
        /// <param name="gameTime"></param>
        /// <returns>True if projectile is still alive</returns>
        public bool Update(GameTime gameTime, Map map, ParticleTools particleTools)
        {
            offset += (float)gameTime.ElapsedGameTime.TotalMilliseconds * ProjectileSpeed;

            TileInfo.SideType    direction      = TileInfo.FromFacing(horizontalDirection, verticalDirection);
            Rectangle            rectangle      = CollisionRectangle;
            List <CollisionTile> collidingTiles = map.GetCollidingTiles(rectangle, direction);

            for (int i = 0; i < collidingTiles.Count; i++)
            {
                TileInfo.SideType side = TileInfo.OppositeSide(direction);
                GameUnit          perpendicularPosition = TileInfo.Vertical(side) ?
                                                          rectangle.Center.X : rectangle.Center.Y;
                GameUnit          leadingPosition  = rectangle.Side(direction);
                bool              shouldTestSlopes = true;
                TestCollisionInfo testInfo         = collidingTiles[i].TestCollision(side, perpendicularPosition,
                                                                                     leadingPosition, shouldTestSlopes);
                if (testInfo.isColliding)
                {
                    GameUnit collisionX;
                    if (TileInfo.Vertical(side))
                    {
                        collisionX = perpendicularPosition;
                    }
                    else
                    {
                        collisionX = testInfo.position;
                    }
                    GameUnit collisionY;
                    if (TileInfo.Vertical(side))
                    {
                        collisionY = testInfo.position;
                    }
                    else
                    {
                        collisionY = perpendicularPosition;
                    }
                    particleTools.FrontSystem.AddNewParticle(ProjectileWallParticle.Create(particleTools.Content,
                                                                                           collisionX - Units.HalfTile, collisionY - Units.HalfTile));
                    return(false);
                }
            }
            if (!alive)
            {
                return(false);
            }
            else if (offset >= ProjectileMaxOffsets[gunLevel - 1])
            {
                particleTools.FrontSystem.AddNewParticle(ProjectileStarParticle.Create(particleTools.Content, X, Y));
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="side">The side of the tile that is being collided with</param>
        /// <param name="perpendicularPosition">The position on the tile on the opposite axis of side</param>
        /// <param name="leadingPosition">Position of the leading edge of the colliding entity</param>
        /// <param name="shouldTestSlopes">whether slopes should be considered for collision</param>
        /// <returns>
        /// isColliding is true if there was a collision
        /// Returns the position of the collision on the same axis of side
        /// </returns>
        public TestCollisionInfo TestCollision(TileInfo.SideType side, GameUnit perpendicularPosition,
                                               GameUnit leadingPosition, bool shouldTestSlopes)
        {
            TestCollisionInfo info = new TestCollisionInfo(false, leadingPosition);

            if (tileType[(int)TileInfo.TileFlag.Wall])
            {
                info.isColliding = true;
                if (side == TileInfo.SideType.TopSide)
                {
                    info.position = Units.TileToGame(row);
                }
                else if (side == TileInfo.SideType.BottomSide)
                {
                    info.position = Units.TileToGame(row + 1);
                }
                else if (side == TileInfo.SideType.LeftSide)
                {
                    info.position = Units.TileToGame(col);
                }
                else
                {
                    info.position = Units.TileToGame(col + 1);
                }
            }
            else if (shouldTestSlopes && tileType[(int)TileInfo.TileFlag.Slope] &&
                     !tileType[(int)TileInfo.SlopeFlagFromSide(side)])
            {
                GameUnit row                = Units.TileToGame(this.row);
                GameUnit col                = Units.TileToGame(this.col);
                float    slope              = GetSlope(tileType);
                GameUnit offset             = GetOffset(tileType);
                GameUnit calculatedPosition = TileInfo.Vertical(side) ?
                                              slope * (perpendicularPosition - col) + offset + row :
                                              (perpendicularPosition - row - offset) / slope + col;

                bool isColliding = TileInfo.IsMax(side) ?
                                   leadingPosition <= calculatedPosition :
                                   leadingPosition >= calculatedPosition;

                info.isColliding = isColliding;
                info.position    = calculatedPosition;
            }
            return(info);
        }
Ejemplo n.º 3
0
        CollisionInfo?TestMapStickyCollision(Map map, Rectangle rectangle,
                                             TileInfo.SideType direction, BitArray maybeGroundTile)
        {
            List <CollisionTile> tiles = map.GetCollidingTiles(rectangle, direction);

            for (int i = 0; i < tiles.Count; i++)
            {
                TileInfo.SideType side = TileInfo.OppositeSide(direction);
                GameUnit          perpendicularPosition;
                if (TileInfo.Vertical(side))
                {
                    perpendicularPosition = rectangle.Center.X;
                }
                else
                {
                    perpendicularPosition = rectangle.Center.Y;
                }
                GameUnit          leadingPosition  = rectangle.Side(direction);
                bool              shouldTestSlopes = TileInfo.Vertical(side);
                TestCollisionInfo testInfo         = tiles[i].TestCollision(side, perpendicularPosition,
                                                                            leadingPosition, shouldTestSlopes);
                if (testInfo.isColliding)
                {
                    CollisionInfo info = new CollisionInfo(testInfo.position, tiles[i].Position, tiles[i].TileType);
                    return(info);
                }
                else if (maybeGroundTile != null && direction == TileInfo.SideType.BottomSide)
                {
                    BitArray tallSlope = TileInfo.CreateTileType();
                    tallSlope.Set((int)TileInfo.TileFlag.Slope, true);
                    tallSlope.Set((int)TileInfo.TileFlag.TallSlope, true);
                    if ((maybeGroundTile[(int)TileInfo.TileFlag.Slope] && tiles[i].TileType[(int)TileInfo.TileFlag.Slope]) ||
                        (maybeGroundTile[(int)TileInfo.TileFlag.Wall] &&
                         (tallSlope.And(tiles[i].TileType).Equals(tallSlope))))
                    {
                        CollisionInfo info = new CollisionInfo(testInfo.position, tiles[i].Position, tiles[i].TileType);
                        return(info);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 4
0
        CollisionInfo?TestMapBouncingCollision(Map map, Rectangle rectangle,
                                               TileInfo.SideType direction, BitArray maybeGroundTile)
        {
            List <CollisionTile> tiles  = map.GetCollidingTiles(rectangle, direction);
            CollisionInfo?       result = null;

            for (int i = 0; i < tiles.Count; i++)
            {
                TileInfo.SideType side = TileInfo.OppositeSide(direction);
                GameUnit          perpendicularPosition;
                if (TileInfo.Vertical(side))
                {
                    perpendicularPosition = rectangle.Center.X;
                }
                else
                {
                    perpendicularPosition = rectangle.Center.Y;
                }
                GameUnit          leadingPosition  = rectangle.Side(direction);
                bool              shouldTestSlopes = TileInfo.Vertical(side);
                TestCollisionInfo testInfo         = tiles[i].TestCollision(side, perpendicularPosition,
                                                                            leadingPosition, shouldTestSlopes);
                if (testInfo.isColliding)
                {
                    bool shouldReplaceResult = true;
                    if (result.HasValue)
                    {
                        shouldReplaceResult = TileInfo.IsMin(side) ?
                                              testInfo.position <result.Value.position :
                                                                 testInfo.position> result.Value.position;
                    }
                    if (shouldReplaceResult)
                    {
                        CollisionInfo info = new CollisionInfo(testInfo.position, tiles[i].Position, tiles[i].TileType);
                        result = info;
                    }
                }
            }
            return(result);
        }