コード例 #1
0
ファイル: Game1.cs プロジェクト: GavinMNeises/GameProject5
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
#if VISUAL_DEBUG
            VisualDebugging.LoadContent(Content);
#endif
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            var t = Content.Load <Texture2D>("spritesheet");
            sheet = new SpriteSheet(t, 21, 21, 3, 2);

            // Load the tilemap
            tilemap = Content.Load <Tilemap>("level2");

            SoundEffect tokenSound = Content.Load <SoundEffect>("PickupCoin");

            SoundEffect playerJump = Content.Load <SoundEffect>("Jump");
            SoundEffect playerHurt = Content.Load <SoundEffect>("Hurt");

            //Gets the texture for the particle systems and initializes them
            Texture2D texture = Content.Load <Texture2D>("Particle");
            playerDeathParticles = new ParticleSystem(GraphicsDevice, 1000, texture);
            playerDeathParticles.SpawnPerFrame = 20;

            ParticleSystem coinCollectParticles = new ParticleSystem(GraphicsDevice, 1000, texture);
            coinCollectParticles.SpawnPerFrame = 20;

            //Set the SpawnParticle method for the systems
            playerDeathParticles.SpawnParticle = (ref Particle particle) =>
            {
                Random random = new Random();
                particle.Position = new Vector2(player.Position.X, player.Position.Y - 20);
                particle.Velocity = new Vector2(
                    MathHelper.Lerp(-75, 75, (float)random.NextDouble()), //X between -75 and 75
                    MathHelper.Lerp(0, -150, (float)random.NextDouble())  //Y between 0 and -150
                    );
                particle.Acceleration = 0.1f * new Vector2(0, (float)random.NextDouble());
                particle.Color        = Color.Red;
                particle.Scale        = 1f;
                particle.Life         = 1.0f;
            };

            coinCollectParticles.SpawnParticle = (ref Particle particle) =>
            {
                Random random = new Random();
                particle.Position = coinCollectParticles.Emitter;
                particle.Velocity = new Vector2(
                    MathHelper.Lerp(-180, 180, (float)random.NextDouble()), //X between -180 and 180
                    MathHelper.Lerp(-180, 180, (float)random.NextDouble())  //Y between 180 and 180
                    );
                particle.Acceleration = new Vector2(0, 0);
                particle.Color        = Color.Gold;
                particle.Scale        = 0.5f;
                particle.Life         = 0.25f;
            };

            //Set the UpdateParticle method for the systems
            playerDeathParticles.UpdateParticle = (float deltaT, ref Particle particle) =>
            {
                particle.Velocity += deltaT * particle.Acceleration;
                particle.Position += deltaT * particle.Velocity;
                particle.Scale    -= deltaT;
                particle.Life     -= deltaT;
            };

            coinCollectParticles.UpdateParticle = playerDeathParticles.UpdateParticle;

            // Use the object groups to load in the tokens, platforms, and player spawn point
            foreach (ObjectGroup objectGroup in tilemap.ObjectGroups)
            {
                if (objectGroup.Name == "Coin Layer")
                {
                    foreach (GroupObject groupObject in objectGroup.Objects)
                    {
                        BoundingRectangle bounds = new BoundingRectangle(
                            groupObject.X,
                            groupObject.Y,
                            groupObject.Width,
                            groupObject.Height
                            );
                        tokens.Add(new Token(bounds, sheet[groupObject.SheetIndex - 1], tokenSound));
                    }
                    tokenAxis = new AxisList();
                    foreach (Token token in tokens)
                    {
                        tokenAxis.AddGameObject(token);
                    }
                }
                else if (objectGroup.Name == "Platform Layer")
                {
                    foreach (GroupObject groupObject in objectGroup.Objects)
                    {
                        BoundingRectangle bounds = new BoundingRectangle(
                            groupObject.X,
                            groupObject.Y,
                            groupObject.Width,
                            groupObject.Height
                            );
                        platforms.Add(new Platform(bounds, sheet[groupObject.SheetIndex - 1]));
                    }

                    platformAxis = new AxisList();
                    foreach (Platform platform in platforms)
                    {
                        platformAxis.AddGameObject(platform);
                    }
                }
                else if (objectGroup.Name == "Spawn Layer")
                {
                    GroupObject groupObject = objectGroup.Objects[0];
                    // Create the player with the corresponding frames from the spritesheet
                    var           playerFrames     = from index in Enumerable.Range(19, 11) select sheet[index];
                    List <Sprite> playerFramesList = playerFrames.ToList();
                    playerFramesList.Add(sheet[112]);
                    player = new Player(playerFramesList, groupObject.X, groupObject.Y, playerJump, playerHurt, coinCollectParticles);
                }
            }

            score = 0;

            // Loads the background texture, makes its Sprite, and creates its parallax layer
            var backgroundTexture = Content.Load <Texture2D>("background");
            var backgroundSprite  = new StaticSprite(backgroundTexture);
            var backgroundLayer   = new ParallaxLayer(this, player, 0.1f);
            backgroundLayer.Sprites.Add(backgroundSprite);
            backgroundLayer.DrawOrder = 0;
            Components.Add(backgroundLayer);

            // Loads the midground textures
            var midgroundTextures = new Texture2D[]
            {
                Content.Load <Texture2D>("midground1"),
                Content.Load <Texture2D>("midground2")
            };

            // Creates the midground sprites
            var midgroundSprites = new StaticSprite[]
            {
                new StaticSprite(midgroundTextures[0]),
                new StaticSprite(midgroundTextures[1], new Vector2(3500, 0))
            };

            // Creates the midground layer and initializes its controller
            var midgroundLayer = new ParallaxLayer(this, player, 0.4f);
            midgroundLayer.Sprites.AddRange(midgroundSprites);
            midgroundLayer.DrawOrder = 1;
            Components.Add(midgroundLayer);

            var birdTexture  = Content.Load <Texture2D>("birds");
            var birdsSprites = new StaticSprite(birdTexture);
            var birdsLayer   = new ParallaxLayer(this, 80f);
            birdsLayer.Sprites.Add(birdsSprites);
            birdsLayer.DrawOrder = 2;
            Components.Add(birdsLayer);

            gameText.LoadContent(Content);
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: cchawley/platformer-example
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
#if VISUAL_DEBUG
            VisualDebugging.LoadContent(Content);
#endif
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //load in spritefont
            spriteFont = Content.Load <SpriteFont>("File");

            // TODO: use this.Content to load your game content here
            var t = Content.Load <Texture2D>("spritesheet");
            sheet = new SpriteSheet(t, 21, 21, 1, 2);


            // Load the level
            tilemap = Content.Load <Tilemap>("level5");

            foreach (ObjectGroup objectGroup in tilemap.ObjectGroups)
            {
                if (objectGroup.Name == "Platforms") //create all of the tiles from the tile objects
                {
                    foreach (GroupObject groupObject in objectGroup.Objects)
                    {
                        BoundingRectangle bounds = new BoundingRectangle(
                            groupObject.X,
                            groupObject.Y,
                            groupObject.Width,
                            groupObject.Height
                            );
                        platforms.Add(new Platform(bounds, sheet[groupObject.SheetIndex - 1]));
                    }

                    world = new AxisList();
                    foreach (Platform platform in platforms)
                    {
                        world.AddGameObject(platform);
                    }
                }
                else if (objectGroup.Name == "Spawn")// give the player the starting point from the object
                {
                    GroupObject groupObject = objectGroup.Objects[0];
                    // Create the player with the corresponding frames from the spritesheet
                    var playerFrames = from index in Enumerable.Range(139, 150) select sheet[index];
                    //List<Sprite> playerFramesList = playerFrames.ToList();
                    //playerFramesList.Add(sheet[112]);
                    player = new Player(playerFrames, groupObject.X, groupObject.Y);
                }
                else if (objectGroup.Name == "Ghost")
                {
                    GroupObject groupObject = objectGroup.Objects[0];
                    var         GhostFrames = from index in Enumerable.Range(445, 449) select sheet[index];
                    ghost = new GhostEnemy(GhostFrames, player, groupObject.X, groupObject.Y);
                }
                else if (objectGroup.Name == "End") //get the ending location which will end the game
                {
                    GroupObject groupObject = objectGroup.Objects[0];
                    endX = groupObject.X;
                    endY = groupObject.Y;
                }
            }


            // Add the platforms to the axis list
            world = new AxisList();
            foreach (Platform platform in platforms)
            {
                world.AddGameObject(platform);
            }

            tileset = Content.Load <Tileset>("BetterSet2");

            // particle information for when player reaches the door

            NormalParticle              = Content.Load <Texture2D>("particle");
            DoorParticles               = new ParticleSystem(GraphicsDevice, 1000, NormalParticle);
            DoorParticles.Emitter       = new Vector2(100, 100);
            DoorParticles.SpawnPerFrame = 4;

            // Set the SpawnParticle method
            DoorParticles.SpawnParticle = (ref Particle particle) =>
            {
                particle.Position = new Vector2(player.Position.X, player.Position.Y);
                particle.Velocity = new Vector2(
                    MathHelper.Lerp(-200, 200, (float)random.NextDouble()),
                    MathHelper.Lerp(-200, 200, (float)random.NextDouble())
                    );
                particle.Acceleration = 2.0f * new Vector2(0, (float)-random.NextDouble());
                particle.Color        = Color.Gold;
                particle.Scale        = 1.5f;
                particle.Life         = 8.0f;
            };

            // Set the UpdateParticle method
            DoorParticles.UpdateParticle = (float deltaT, ref Particle particle) =>
            {
                particle.Velocity += deltaT * particle.Acceleration;
                particle.Position += deltaT * particle.Velocity;
                particle.Scale    -= deltaT;
                particle.Life     -= deltaT;
            };


            // particle info for rain
            RainParticle       = Content.Load <Texture2D>("rain drop");
            Rain               = new ParticleSystem(GraphicsDevice, 5000, RainParticle);
            Rain.Emitter       = new Vector2(100, 100);
            Rain.SpawnPerFrame = 4;

            // Set the SpawnParticle method
            Rain.SpawnParticle = (ref Particle particle) =>
            {
                particle.Position = new Vector2(
                    MathHelper.Lerp(0, 2100, (float)random.NextDouble()),
                    MathHelper.Lerp(0, 1, (float)random.NextDouble())
                    );
                particle.Velocity = new Vector2(
                    MathHelper.Lerp(0, 0, (float)random.NextDouble()),
                    MathHelper.Lerp(-100, -25, (float)random.NextDouble())
                    );
                particle.Acceleration = 0.0f * new Vector2(0, (float)-random.NextDouble());
                particle.Color        = Color.OrangeRed;
                particle.Scale        = 0.5f;
                particle.Life         = 200.0f;
            };

            // Set the UpdateParticle method
            Rain.UpdateParticle = (float deltaT, ref Particle particle) =>
            {
                particle.Velocity += deltaT * particle.Acceleration;
                particle.Position -= deltaT * particle.Velocity;
                //particle.Scale = particle.Scale;
                particle.Life -= deltaT;
            };


            // particle info for ghost
            FireParticle                 = Content.Load <Texture2D>("Fire");
            GhostParticles               = new ParticleSystem(GraphicsDevice, 1000, FireParticle);
            GhostParticles.Emitter       = new Vector2(100, 100);
            GhostParticles.SpawnPerFrame = 4;

            // Set the SpawnParticle method
            GhostParticles.SpawnParticle = (ref Particle particle) =>
            {
                particle.Position = new Vector2(
                    MathHelper.Lerp(ghost.Position.X - 42, ghost.Position.X, (float)random.NextDouble()),
                    MathHelper.Lerp(ghost.Position.Y - 42, ghost.Position.Y, (float)random.NextDouble())
                    );
                particle.Velocity = new Vector2(
                    MathHelper.Lerp(-50, 50, (float)random.NextDouble()),
                    MathHelper.Lerp(-50, 50, (float)random.NextDouble())
                    );
                particle.Acceleration = 1.0f * new Vector2(0, (float)-random.NextDouble());
                particle.Color        = Color.GhostWhite;
                particle.Scale        = 0.5f;
                particle.Life         = 0.5f;
            };

            // Set the UpdateParticle method
            GhostParticles.UpdateParticle = (float deltaT, ref Particle particle) =>
            {
                particle.Velocity += deltaT * particle.Acceleration;
                particle.Position += deltaT * particle.Velocity;
                particle.Scale    -= deltaT;
                particle.Life     -= deltaT;
            };

            // put player and ghost drawings on layer 2

            /*
             * var playerLayer = new ParallaxLayer(this);
             * playerLayer.Sprites.Add(player);
             * playerLayer.Sprites.Add(ghost);
             * playerLayer.DrawOrder = 3;
             * Components.Add(playerLayer);
             */

            //fireballs background drawing
            var FireTextures = new Texture2D[]
            {
                Content.Load <Texture2D>("FireBalls2")
            };

            var FireSprites = new StaticSprite[]
            {
                new StaticSprite(FireTextures[0]),
                new StaticSprite(FireTextures[0], new Vector2(500, 0)),
                new StaticSprite(FireTextures[0], new Vector2(1000, 0)),
                new StaticSprite(FireTextures[0], new Vector2(1500, 0)),
                new StaticSprite(FireTextures[0], new Vector2(2000, 0))
            };
            var FireLayer = new ParallaxLayer(this);
            FireLayer.Sprites.AddRange(FireSprites);
            FireLayer.DrawOrder = 2;
            Components.Add(FireLayer);


            //Cloud layer background Drawing
            var CloudTextures = new Texture2D[]
            {
                Content.Load <Texture2D>("Clouds2")
            };

            var CloudSprites = new StaticSprite[]
            {
                new StaticSprite(CloudTextures[0]),
                new StaticSprite(CloudTextures[0], new Vector2(500, 0)),
                new StaticSprite(CloudTextures[0], new Vector2(1000, 0)),
                new StaticSprite(CloudTextures[0], new Vector2(1500, 0)),
                new StaticSprite(CloudTextures[0], new Vector2(2000, 0))
            };

            var CloudLayer = new ParallaxLayer(this);
            CloudLayer.Sprites.AddRange(CloudSprites);
            CloudLayer.DrawOrder = 1;
            Components.Add(CloudLayer);

            //Mountain layer background Drawing
            var FlourTextures = new Texture2D[]
            {
                Content.Load <Texture2D>("Flours")
            };

            var FlourSprites = new StaticSprite[]
            {
                new StaticSprite(FlourTextures[0]),
                new StaticSprite(FlourTextures[0], new Vector2(500, 0)),
                new StaticSprite(FlourTextures[0], new Vector2(1000, 0)),
                new StaticSprite(FlourTextures[0], new Vector2(1500, 0)),
                new StaticSprite(FlourTextures[0], new Vector2(2000, 0))
            };

            var FlourLayer = new ParallaxLayer(this);
            FlourLayer.Sprites.AddRange(FlourSprites);
            FlourLayer.DrawOrder = 0;
            Components.Add(FlourLayer);


            //speeds of each background and character layer
            FlourLayer.ScrollController = new PlayerTrackingScrollController(player, 0.1f);
            CloudLayer.ScrollController = new PlayerTrackingScrollController(player, 0.4f);
            //playerLayer.ScrollController = new PlayerTrackingScrollController(player, 1.0f);
            FireLayer.ScrollController = new PlayerTrackingScrollController(player, 1.0f);
        }