示例#1
0
        public static IBlock GetRandomBrickBlock(Vector2 pos, Random rand) {

            IBlock b;
            int random = rand.Next(Utility.RANDOM_VALUE_MAX);
            if (random < Utility.QUESTION_BLOCK_MUSHROOM_CHANCE) 
                b = new Block(new QuestionBlockState(new Mushroom(pos)), pos);
            else if(random < Utility.QUESTION_BLOCK_COIN_CHANCE)
                b = new Block(new QuestionBlockState(new Coin(pos, false)), pos);
            else if(random < Utility.QUESTION_BLOCK_LIFE_CHANCE)
                b = new Block(new QuestionBlockState(new LifeMushroom(pos)), pos);
            else if(random < Utility.QUESTION_BLOCK_STAR_CHANCE)
                b = new Block(new QuestionBlockState(new Star(pos)), pos);
            else
                b = new Block(new BrickBlockState(), pos);

            return b;
        }
示例#2
0
        public virtual void GenerateColumn(int column)
        {

            if (column > Utility.ChunkLength - Utility.HOLE_MAX_LENGTH) canGenerateHole = false;

            if (generateHole)
            {
                currentHoleLength++;
                if (currentHoleLength > currentHoleGoalLength)
                    generateHole = false;
            }
            if (generateBricks)
            {
                currentBrickLength++;
                if (currentBrickLength > currentBrickGoalLength)
                    generateBricks = false;
            }


            //Decides if it should generate a hole
            if (!generateBricks && !generateHole && canGenerateHole && r.Next(Utility.RANDOM_VALUE_MAX) < holeDensity)
            {
                generateHole = true;
                canGenerateHole = false;
                currentHoleLength = 1;
                currentHoleGoalLength = r.Next(Utility.HOLE_MIN_LENGTH, Utility.HOLE_MAX_LENGTH);
            }

            if (!generateHole && !generateBricks && canGenerateBricks && r.Next(Utility.RANDOM_VALUE_MAX) < brickDensity)
            {
                generateBricks = true;
                canGenerateBricks = false;
                currentBrickLength = 1;
                currentBrickGoalLength = r.Next(bricksMin, bricksMax);
                currentBrickHeight = r.Next(chunk.ChunkYValue - bricksHeightMax, chunk.ChunkYValue - bricksHeightMin);
            }


            for (int y = Utility.ZERO; y < Utility.WorldHeight; y++)
            {
                Vector2 pos = new Vector2(column * Utility.BLOCK_POSITION_SCALE + chunk.Start.X, y * Utility.BLOCK_POSITION_SCALE);
                IBlock b = new NullBlock();

                if (generateHole)
                {
                    //b = new NullBlock();
                }
                else
                {
                    if (!canGenerateHole) canGenerateHole = true;
                    if (generateBricks)
                    {
                        if (y == currentBrickHeight)
                        {
                            b = Chunk.GetRandomBrickBlock(pos, r);
                            //Spawns an enemy
                            if (r.Next(Utility.RANDOM_VALUE_MAX) < enemySpawnDensity)
                            {
                                Chunk.SpawnRandomEnemy(new Vector2(column * Utility.BLOCK_POSITION_SCALE + chunk.Start.X, y * Utility.BLOCK_POSITION_SCALE), r);
                            }
                        }
                    }
                    else canGenerateBricks = true;
                    if (y == Utility.KILL_BLOCK_HEIGHT)
                        b = new Block(new KillBlockState(), pos);
                    else
                        GenerateGroundBlocks(ref b, y, pos);
                }



                chunk.Blocks[column, y] = b;
                SprintFourGame.GetInstance().Components.Add(b);



            }

            //Spawns an enemy
            if (r.Next(Utility.RANDOM_VALUE_MAX) < enemySpawnDensity)
            {
                Chunk.SpawnRandomEnemy(new Vector2(column * Utility.BLOCK_POSITION_SCALE + chunk.Start.X, chunk.ChunkYValue * Utility.BLOCK_POSITION_SCALE), r);
            }
        }
示例#3
0
 public override void GenerateGroundBlocks(ref IBlock b, int y, Vector2 pos)
 {
     if (y >= chunk.ChunkYValue)
         b = new Block(new FloorBlockState(), pos);
     
 }
示例#4
0
 public BlockSprite(Block parentBlock, Tuple<int, int>[] frames)
     : base(frames, Utility.BLOCK_SPRITESHEET_ROWS, Utility.BLOCK_SPRITESHEET_COLUMNS, texture)
 {
     parent = parentBlock;
 }
        private static void ManageBlockCollisions(GameTime gameTime) {
            Block[] enemyCollisions = new Block[EnemyList.Count];
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            CheckXDirection(enemyCollisions, elapsed);

            CheckYDirection(enemyCollisions, elapsed);

            CheckEnemyBlockCollisions(enemyCollisions, elapsed);


            foreach (IBlock block in toRemoveB)
                blockList.Remove(block);
            toRemoveB.Clear();
        }
 private static Block GetGreatestCollision(Block[] blockCollisions, int collisionCount, Rectangle collider) {
     float greatestIntersection = float.MinValue;
     Block intersectedBlock = null;
     for (int j = Utility.ZERO; j < collisionCount; ++j)
     {
         Rectangle b = blockCollisions[j].Bounds;
         Rectangle intersect;
         Rectangle.Intersect(ref collider, ref b, out intersect);
         float area = intersect.Width * intersect.Height;
         if (!(area > greatestIntersection)) continue;
         greatestIntersection = area;
         intersectedBlock = blockCollisions[j];
     }
     return intersectedBlock;
 }
        private static void CheckYDirection(Block[] enemyCollisions, float elapsed)
        {
          
            Block[] marioCollisions = new Block[Utility.NUM_BLOCK_COLLISIONS];
            int marioIndex = Utility.ZERO ;
            Rectangle marioY = new Rectangle(mario.Bounds.X, mario.Bounds.Y - Utility.ONE, mario.Bounds.Width, mario.Bounds.Height + Utility.TWO);
            marioY.Offset(Utility.ZERO, (int)(mario.Velocity.Y * elapsed));
            foreach (Block block in blockList.Cast<Block>())
            {
                var enemyIndex = Utility.ZERO;
                foreach (IEnemy enemy in EnemyList)
                {
                    Rectangle enemyY = new Rectangle(enemy.Bounds.X, enemy.Bounds.Y - Utility.ONE, enemy.Bounds.Width, enemy.Bounds.Height + Utility.TWO);
                    enemyY.Offset(Utility.ZERO, (int)(enemy.Velocity.Y * elapsed));
                    if (block.Bounds.Intersects(enemyY))
                    {
                        enemyCollisions[enemyIndex] = block;
                    }
                    enemyIndex++;
                }

                if (!marioY.Intersects(block.Bounds) || (block.IsHidden && mario.Velocity.Y > Utility.ZERO)) continue;
                marioCollisions[marioIndex++] = block;
            }

            bool moveMario = true;
            if (marioIndex > Utility.ZERO)
            {
                Block intersectedBlock = GetGreatestCollision(marioCollisions, marioIndex, marioY);
                moveMario = !CollisionResponseManager.PlayerBlockCollision(mario, intersectedBlock, new Vector2(Utility.ZERO, -mario.Velocity.Y), marioY);
            }
            if (moveMario)
                mario.Position += new Vector2(Utility.ZERO, mario.Velocity.Y * elapsed);
        }
        private static void CheckXDirection(Block[] enemyCollisions, float elapsed)
        {

            Block[] marioCollisions = new Block[Utility.NUM_BLOCK_COLLISIONS];
            int marioIndex = Utility.ZERO;
            int enemyIndex;

            Rectangle marioX = new Rectangle(mario.Bounds.X - Utility.ONE, mario.Bounds.Y, mario.Bounds.Width + Utility.TWO, mario.Bounds.Height);
            marioX.Offset((int)(mario.Velocity.X * elapsed), Utility.ZERO);
            foreach (Block block in blockList.Cast<Block>())
            {
                //check against enemies, collectibles, and mario

                var block1 = block;
                enemyIndex = Utility.ZERO;
                foreach (IEnemy enemy in EnemyList)
                {
                    Rectangle enemyX = new Rectangle(enemy.Bounds.X - Utility.ONE, enemy.Bounds.Y, enemy.Bounds.Width + Utility.TWO, enemy.Bounds.Height);
                    enemyX.Offset((int)(enemy.Velocity.X * elapsed), Utility.ZERO);
                    if (enemyX.Intersects(block1.Bounds))
                        enemyCollisions[enemyIndex] = block;
                    enemyIndex++;

                }
 

                var bounds = block.Bounds;
                foreach (ICollectible item in itemList.Where(item => bounds.Intersects(item.Bounds)))
                    CollisionResponseManager.ItemBlockCollision(item, block);
                foreach (IProjectile proj in projList.Where(proj => bounds.Intersects(proj.Bounds)))
                    CollisionResponseManager.ProjectileBlockCollision(proj, block);

                Flagpole pole = block as Flagpole;
                if (pole != null && marioX.Intersects(pole.Bounds)) marioCollisions[marioIndex++] = pole;

                if (!marioX.Intersects(block.Bounds) || block.IsHidden) continue;
                marioCollisions[marioIndex++] = block;
            }

            bool moveMario = true;
            if (marioIndex > Utility.ZERO)
            {
                Block intersectedBlock = GetGreatestCollision(marioCollisions, marioIndex, marioX);
                moveMario = !CollisionResponseManager.PlayerBlockCollision(mario, intersectedBlock, new Vector2(mario.Velocity.X, Utility.ZERO), marioX);
            }
            if (moveMario)
                mario.Position += new Vector2(mario.Velocity.X * elapsed, Utility.ZERO);

            enemyIndex = Utility.ZERO;
            for (int i = Utility.ZERO; i < EnemyList.Count; i++)
            {
                IEnemy enemy = EnemyList[i];
                if (enemyIndex >= enemyCollisions.Count()) return; //This occurs when the game gets reset mid-function
                if (enemyCollisions[enemyIndex] == null)
                    enemy.Position += new Vector2(enemy.Velocity.X * elapsed, Utility.ZERO);
                else
                    CollisionResponseManager.EnemyBlockCollision(enemy, enemyCollisions[enemyIndex], new Vector2(enemy.Velocity.X, Utility.ZERO));
                enemyIndex++;

            }
        }
        private static void CheckEnemyBlockCollisions(Block [] enemyCollisions, float elapsed)
        {
            int enemyIndex = Utility.ZERO;

            foreach (IEnemy enemy in EnemyList)
            {
                if (enemyIndex >= enemyCollisions.Length || enemyIndex < Utility.ZERO) return;
                if (enemyCollisions[enemyIndex] == null)
                    enemy.Position += new Vector2(Utility.ZERO, enemy.Velocity.Y * elapsed);
                else
                    CollisionResponseManager.EnemyBlockCollision(enemy, enemyCollisions[enemyIndex], new Vector2(Utility.ZERO, -enemy.Velocity.Y));
                ++enemyIndex;
            }

        }
示例#10
0
 public void ChangeToWinning(Block flagpole)
 {
     SoundManager.Pause();
     CurrentGameState = GameState.Winning;           
     Mario.Velocity = Vector2.Zero;
     Mario.Position = new Vector2(flagpole.Bounds.X - Mario.Bounds.Width/Utility.TWO, Mario.Position.Y);
     Mario.CurrentState = new SlidingMarioState(true, Mario.CurrentState.Row,Mario);
     SoundManager.PlayAweSound();
     winTimer = 100;
 }
        public static void ProjectileBlockCollision(IProjectile proj, Block block) {

            Vector2 direction = GetCollisionDirection(proj.Bounds, block.Bounds);
            int penetration = GetPenetration(proj.Bounds, block.Bounds, direction);
            proj.Position += direction * penetration;

            proj.Bounce(direction);
        }
        public static void EnemyBlockCollision(IEnemy enemy, Block block, Vector2 direction)
        {
            if (block.BlockState is KillBlockState) {
                enemy.Remove();
                return;
            }

            direction.Normalize();

            Vector2 newPosition = new Vector2();
            if (direction.X < Utility.ZERO)
            {
                newPosition = new Vector2(block.Position.X + block.Bounds.Width + 1, enemy.Position.Y);
            }
            else if (direction.X > Utility.ZERO)
            {
                newPosition = new Vector2(block.Position.X - enemy.Bounds.Width - 1, enemy.Position.Y);
            }
            else if (direction.Y < Utility.ZERO)
            {
                newPosition = new Vector2(enemy.Position.X, block.Position.Y - enemy.Bounds.Height);
            }
            else if (direction.Y > Utility.ZERO)
            {
                newPosition = new Vector2(enemy.Position.X, block.Position.Y + block.Bounds.Height);
            }
            if (Vector2.Subtract(enemy.Position, newPosition).Length() > block.Bounds.Width / Utility.CAMERA_SCALE) return;
            enemy.Position = newPosition;

            //falling behavior
            if (direction.Y < Utility.ZERO)
            {
                enemy.OnGround = true;
                enemy.Velocity *= Vector2.UnitX;
            }

            //turnaround behavior
            if (Math.Abs(direction.X) > Utility.ZERO)
            {
                enemy.Velocity *= new Vector2(-Utility.ONE, Utility.ONE);
            }
        }
        public static void ItemBlockCollision(ICollectible item, Block block)
        {
            if (block.BlockState is KillBlockState)
            {
                item.Remove();
                return;
            }

            if (item.Spawning) return;
            Vector2 direction = GetCollisionDirection(item.Bounds, block.Bounds);
            int penetration = GetPenetration(item.Bounds, block.Bounds, direction);
            item.Position += direction * penetration;

            var star = item as Star;
            if (star != null)
                star.Bounce(direction);

            if (!(Math.Abs(direction.X) > Utility.DOUBLE_TOLERANCE)) return;
            var mushroom = item as Mushroom;
            if (mushroom != null)
            {
                mushroom.MoveRight = !mushroom.MoveRight;
            }
            var lifeMushroom = item as LifeMushroom;
            if (lifeMushroom != null)
            {
                lifeMushroom.MoveRight = !lifeMushroom.MoveRight;
            }
        }