Esempio n. 1
0
 /// <summary>
 /// Allows the game to perform any initialization it needs to before starting to run.
 /// This is where it can query for any required services and load any non-graphic
 /// related content.  Calling base.Initialize will enumerate through any components
 /// and initialize them as well.
 /// </summary>
 protected override void Initialize()
 {
     // TODO: Add your initialization logic here
     //Initialize all your objects here
     player1 = new Player(400, -32, 50, 50);
     physics = new Physics();
     controls = new Controls();
     base.Initialize();
 }
Esempio n. 2
0
        private void Shoot(Controls controls, Level level, GameTime gameTime)
        {
            index = rand.Next(4);
            // New shots
            if (controls.onPress(Keys.D, Buttons.RightShoulder) && !powerShotCharging)
            {
                shooting = true;
                shotPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds);
            }
            else if (controls.onRelease(Keys.D, Buttons.RightShoulder))
            {
                shooting = false;
            }

            if (powerup["charged"])
            {
                if (controls.onPress(Keys.X, Buttons.RightTrigger))
                {
                    if (hydration >= powerShotCost)
                        powerShotCharging = true;
                    powerShotPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                    powerShotRelease = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                    tryShotHydration = 0;
                }
                else if (controls.isPressed(Keys.X, Buttons.RightTrigger) && powerShotCharging)
                {
                    if (tryShotHydration < powerShotCost)
                    {
                        tryShotHydration += 1;
                        hydration -= 1;
                    }
                }
                else if (controls.onRelease(Keys.X, Buttons.RightTrigger) && powerShotCharging)
                {
                    powerShotRelease = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                    if (((powerShotRelease - powerShotPoint) >= powerShotDelay) && tryShotHydration == powerShotCost)
                    {
                        powerShotCharging = false;
                        tryShotHydration = 0;
                        string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none";
                        PowerShot s = new PowerShot(this, dir);
                        if (index == 0)
                        {
                            soundList["Sounds/Shot1.wav"].Play();
                        }
                        else if (index == 1)
                        {
                            soundList["Sounds/Shot2.wav"].Play();
                        }
                        else if (index == 2)
                        {
                            soundList["Sounds/Shot3.wav"].Play();
                        }
                        else
                        {
                            soundList["Sounds/Shot4.wav"].Play();
                        }
                        level.projectiles.Add(s);
                    }
                    else
                    {
                        powerShotCharging = false;
                        hydration += tryShotHydration;
                        tryShotHydration = 0;
                    }
                }
            }

            // Deal with shot creation and delay
            if (!frozen)
            {
                // Generate regular shots
                int currentTime1 = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                if (((currentTime1 - shotPoint) >= shotDelay || (currentTime1 - shotPoint) == 0)
                    && shooting && hydration >= shotCost)
                {
                    shotPoint = currentTime1;
                    string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none";
                    Shot s = new Shot(this, dir);
                    if (index == 0)
                    {
                        soundList["Sounds/Shot1.wav"].Play();
                    }
                    else if (index == 1)
                    {
                        soundList["Sounds/Shot2.wav"].Play();
                    }
                    else if (index == 2)
                    {
                        soundList["Sounds/Shot3.wav"].Play();
                    }
                    else
                    {
                        soundList["Sounds/Shot4.wav"].Play();
                    }
                    level.projectiles.Add(s);
                    hydration -= shotCost;
                }

                // Jetpack (Midair jump and downward shots)
                int currentTime2 = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                if ((currentTime2 - jumpPoint) >= jumpDelay && yVel > 3 && powerup["jetpack"] &&
                    hydration >= jetpackCost && !grounded && controls.isPressed(Keys.S, Buttons.A))
                {
                    // New shot
                    jumpPoint = currentTime2;
                    Shot s = new Shot(this, "down");
                    if (index == 0)
                    {
                        soundList["Sounds/Shot1.wav"].Play();
                    }
                    else if (index == 1)
                    {
                        soundList["Sounds/Shot2.wav"].Play();
                    }
                    else if (index == 2)
                    {
                        soundList["Sounds/Shot3.wav"].Play();
                    }
                    else
                    {
                        soundList["Sounds/Shot4.wav"].Play();
                    }
                    level.projectiles.Add(s);
                    hydration -= jetpackCost;

                    // Slight upward boost
                    spriteY--;
                    movedY--;
                    yVel = -4.5;
                }
            }
        }
Esempio n. 3
0
        private void Puddle(Controls controls)
        {
            if (controls.isPressed(Keys.Down, Buttons.DPadDown) &&
                !frozen && grounded && powerup["puddle"])
            {
                puddled = true;
            }

            if (puddled)
            {
                if (hydration >= puddleCost)
                    hydration -= puddleCost;
                else
                {
                    puddled = false;
                    piped = false;
                }
            }
        }
Esempio n. 4
0
        private void Move(Controls controls, Level level)
        {
            // Sideways Acceleration
            if (controls.onPress(Keys.Right, Buttons.DPadRight))
                xAccel += speed;
            else if (controls.onRelease(Keys.Right, Buttons.DPadRight))
                xAccel -= speed;
            if (controls.onPress(Keys.Left, Buttons.DPadLeft))
                xAccel -= speed;
            else if (controls.onRelease(Keys.Left, Buttons.DPadLeft))
                xAccel += speed;

            // Sideways Movement
            double playerFriction = pushing ? (friction * 3) : friction;
            xVel = xVel * (1 - playerFriction)
                + (frozen ? 0 : xAccel * .10);
            movedX = Convert.ToInt32(xVel + rollerVel);
            spriteX += movedX;

            pushing = false;
            rollerVel = 0;

            // Check left/right collisions
            checkXCollisions(level);

            // Gravity
            yVel += level.gravity;
            if (yVel > level.maxFallSpeed)
                yVel = level.maxFallSpeed;
            // Round up to force movement every step
            movedY = Convert.ToInt32(Math.Ceiling(yVel));
            spriteY += movedY;
            grounded = false;

            // Check up/down collisions
            checkYCollisions(level);

            // Determine direction
            if (xVel > 0.1)
                faceLeft = false;
            else if (xVel < -0.1)
                faceLeft = true;
        }
Esempio n. 5
0
        private void Jump(Controls controls, Level level, GameTime gameTime)
        {
            // Jump on button press
            if (controls.onPress(Keys.S, Buttons.A) && !frozen && grounded)
            {
                instance = soundList["Sounds/Jump.wav"].CreateInstance();
                instance.Volume = 0.4f;
                instance.Play();
                yVel = -jumpHeight;
                jumpPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds);
            }

            // Cut jump short on button release
            else if (controls.onRelease(Keys.S, Buttons.A) && yVel < 0)
            {
                yVel /= 2;
            }
        }
Esempio n. 6
0
        private void Animate(Controls controls, Level level, GameTime gameTime)
        {
            // Determine type of movement
            if (!frozen)
            {
                // Jumping
                if (!grounded)
                {
                    // Initialize sprite
                    if (image != images["jump"])
                    {
                        image = images["jump"];
                        frameIndexX = 0;
                    }
                    // Animate
                    else if (frameIndexX < 2 * 32 && level.count % 6 == 0)
                        frameIndexX += 32;
                }
                // Grounded, not Moving
                else if (Math.Abs(xVel) < 1)
                {
                    // Initialize sprite. No animation.
                    if (image != images["stand"])
                    {
                        image = images["stand"];
                        frameIndexX = 0;
                    }
                }
                // Grounded, yes moving
                else
                {
                    // Initialize sprite
                    if (image != images["walk"])
                        image = images["walk"];
                    // Animate
                    //frameIndex = (level.count / 8 % 4) * 32;
                    frameIndexX = ((int)(gameTime.TotalGameTime.TotalMilliseconds)/128 % 4)*32;
                }
            }

            // Puddle, if puddling
            if (puddled)
            {
                // Initialize sprite
                if (image != images["puddle"])
                {
                    image = images["puddle"];
                    frameIndexX = 0;
                }
                // Animate downward if puddling
                else if (controls.isPressed(Keys.Down, Buttons.DPadDown))
                {
                    if (frameIndexX < 5 * 32)
                        frameIndexX += 32;
                }
                // Animate upward if unpuddling
                else
                {
                    frameIndexX -= 32;
                    if (frameIndexX <= 0)
                    {
                        puddled = false;
                        piped = false;
                    }
                }
            }
        }
Esempio n. 7
0
        public void Update(Controls controls, Level level, GameTime gameTime)
        {
            pauseScreen = String.Format("Slides/pause{0}", numPowers);

            if (hydration + hydrationRegen <= maxHydration && !powerShotCharging)
                hydration += hydrationRegen;

            Move(controls, level);

            Puddle(controls);

            Shoot(controls, level, gameTime);

            Jump(controls, level, gameTime);

            CheckCollisions(level);

            HandleCollisions(level);

            Animate(controls, level, gameTime);
        }
Esempio n. 8
0
        private void Move(Controls controls, Level level)
        {
            // Sideways Acceleration
            if (controls.onPress(Keys.Right, Buttons.DPadRight))
                x_accel += speed;
            else if (controls.onRelease(Keys.Right, Buttons.DPadRight))
                x_accel -= speed;
            if (controls.onPress(Keys.Left, Buttons.DPadLeft))
                x_accel -= speed;
            else if (controls.onRelease(Keys.Left, Buttons.DPadLeft))
                x_accel += speed;

            // Sideways Movement
            double playerFriction = pushing ? (friction * 3) : friction;
            x_vel = x_vel * (1 - playerFriction)
                + (frozen ? 0 : x_accel * .10);
            spriteX += Convert.ToInt32(x_vel);

            pushing = false;

            // Check left/right collisions
            foreach (Sprite s in level.items)
            {
                if (s.isSolid && Intersects(s))
                {
                    // Collision with right block
                    if (bottomWall > s.topWall &&
                        rightWall - Convert.ToInt32(x_vel) < s.leftWall &&
                        x_vel > 0)
                    {
                        // Push
                        if (s is Block && ((Block)s).rightPushable && grounded)
                        {
                            ((Block)s).x_vel = x_vel;
                            pushing = true;
                        }

                        // Hit the wall
                        else
                        {
                            while (rightWall >= s.leftWall)
                                spriteX--;
                        }
                    }

                    // Push to the left
                    else if (bottomWall > s.topWall &&
                        leftWall - Convert.ToInt32(x_vel) > s.rightWall &&
                        x_vel < 0)
                    {
                        // Push
                        if (s is Block && ((Block)s).leftPushable && grounded)
                        {
                            ((Block)s).x_vel = x_vel;
                            pushing = true;
                        }

                        // Hit the wall
                        else
                        {
                            while (leftWall <= s.rightWall)
                                spriteX++;
                        }
                    }
                }
            }

            // Gravity
            if (!grounded)
            {
                y_vel += level.gravity;
                if (y_vel > level.maxFallSpeed)
                    y_vel = level.maxFallSpeed;
                spriteY += Convert.ToInt32(y_vel);
            }
            else
            {
                y_vel = 1;
            }

            grounded = false;

            // Check up/down collisions
            foreach (Sprite s in level.items)
            {
                if (s.isSolid && Intersects(s))
                {
                    // Up collision
                    if (topWall - Convert.ToInt32(y_vel) > s.bottomWall)
                    {
                        y_vel = 0;
                        while (topWall < s.bottomWall)
                            spriteY++;
                    }

                    // Down collision
                    else if (!grounded &&
                        (bottomWall - Convert.ToInt32(y_vel)) < s.topWall)
                    {
                        grounded = true;
                        while (bottomWall > s.topWall)
                            spriteY--;
                    }
                }
            }

            // Determine direction
            if (x_vel > 0.1)
                faceLeft = false;
            else if (x_vel < -0.1)
                faceLeft = true;
        }
Esempio n. 9
0
        private void Jump(Controls controls, Level level, GameTime gameTime)
        {
            SoundEffectInstance instance = soundList["Sounds/Jump.wav"].CreateInstance();
            instance.Volume = 0.1f;
            // Jump on button press
            if (controls.isPressed(Keys.S, Buttons.A) && !frozen && grounded)
            {
                if(instance.State != SoundState.Playing)
                    instance.Play();
                y_vel = -11;
                jumpPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds);
                grounded = false;
            }

            // Cut jump short on button release
            else if (controls.onRelease(Keys.S, Buttons.A) && y_vel < 0)
            {
                y_vel /= 2;
            }
        }
Esempio n. 10
0
        public void Update(Controls controls, Level level, 
            ContentManager content, GameTime gameTime)
        {
            if (hydration + hydrationRegen <= maxHydration && !powerShotCharging)
                hydration += hydrationRegen;

            Move(controls, level);

            Puddle(controls);

            Shoot(controls, level, content ,gameTime);

            Jump(controls, level, gameTime);

            CheckCollisions(level);

            HandleCollisions(level);

            Animate(controls, level, gameTime);
        }
Esempio n. 11
0
        protected override void Initialize()
        {
            string initialLevel = String.Format("Content/Level{0}.tmx", 1);
            map = new TmxMap(initialLevel);

            // Read Level Size From Map
            graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
            graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;

            paused = false;
            // Create built-in objects
            player1 = new Player(
                Convert.ToInt32(map.Properties["startX"]),
                Convert.ToInt32(map.Properties["startY"]),
                32,
                32
            );
            level = new Level(player1);
            controls = new Controls();
            newMapLoad = true;
            newMapTimer = LOAD_SCREEN_TIME;
            player1.newMap = initialLevel;

            song = Content.Load<SoundEffect>("Sounds/InGame.wav");
            instance = song.CreateInstance();
            instance.IsLooped = true;
            if (instance.State == SoundState.Stopped)
                instance.Play();

            base.Initialize();
        }
Esempio n. 12
0
        protected override void Initialize()
        {
            string initialLevel = "Menu";
            map = new TmxMap(String.Format(LEVEL_PATH, initialLevel));

            // Read Level Size From Map
            graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
            graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;

            paused = false;
            intro = false;
            introImage = Content.Load<Texture2D>("Slides/Slide1.png");
            slideCount = 1;
            previousMap = "";

            // Create built-in objects
            player1 = new Player(
                Convert.ToInt32(map.Properties["startX"]),
                Convert.ToInt32(map.Properties["startY"])
            );
            level = new Level(player1, "menu", this.Content);
            levelSelect = null;
            controls = new Controls();
            pauseControls = new Controls();
            loadingMap = true;
            newMapTimer = LOAD_SCREEN_TIME;
            introScreenTimer = INTRO_SCREEN_TIME;
            player1.newMap = initialLevel;

            song = Content.Load<SoundEffect>("Sounds/InGame.wav");
            instance = song.CreateInstance();
            instance.IsLooped = true;

            bossSong = Content.Load<SoundEffect>("Sounds/Boss.wav");
            bossInstance = bossSong.CreateInstance();
            bossInstance.IsLooped = true;

            menuSong = Content.Load<SoundEffect>("Sounds/Menu.wav");
            menuInstance = menuSong.CreateInstance();
            menuInstance.IsLooped = true;
            menuInstance.Play();

            pauseScreens = new List<Texture2D>();
            for (int i=0; i < 4; i++)
            {
                pauseScreens.Add(Content.Load<Texture2D>(String.Format("Slides/pause{0}.png", i)));
            }
            base.Initialize();
        }
Esempio n. 13
0
        public void Update(Controls controls, Physics physics)
        {
            // Move
            Move(physics);

            // Update sprite
            frameIndex = ((physics.count + seed) / 8 % 4) * 32;

            // Be killed if necessary
            destroyed = (health <= 0);
        }
Esempio n. 14
0
        private void Animate(Controls controls, Physics physics, 
            ContentManager content)
        {
            // Check for standing still
            if (!frozen())
            {
                // Jumping
                if (!grounded)
                {
                    if (this.imageFile != "PC/jump.png")
                    {
                        frameIndex = 0;
                        this.imageFile = "PC/jump.png";
                        LoadContent(content);
                    }
                }
                // Not Moving
                else if (Math.Abs(x_vel) < .5)
                {
                    if (this.imageFile != "PC/stand.png")
                    {
                        frameIndex = 0;
                        this.imageFile = "PC/stand.png";
                        LoadContent(content);
                    }
                }

                // Yes moving
                else if (this.imageFile != "PC/sheet.png")
                {
                    frameIndex = 0;
                    this.imageFile = "PC/sheet.png";
                    LoadContent(content);
                }
            }

            // Puddle sprite logic
            if (this.imageFile == "PC/collapse.png")
            {
                if (controls.isPressed(Keys.Down, Buttons.DPadDown))
                {
                    if (frameIndex < 5 * 32)
                        frameIndex += 32;
                }
                else
                {
                    frameIndex -= 32;
                    if (frameIndex <= 0)
                        puddled = false;
                }
            }

            // Walking sprite frames
            else if (this.imageFile == "PC/sheet.png")
            {
                frameIndex = (physics.count / 8 % 4) * 32;
            }

            // Jumping sprite frames
            else if (this.imageFile == "PC/jump.png")
            {
                if (frameIndex < 2 * 32)
                    frameIndex += 32;
            }

            // Standing sprite frames
            else
            {
                frameIndex = 0;
            }
        }
Esempio n. 15
0
        public void Update(Controls controls, Physics physics, 
            ContentManager content)
        {
            //  Acceleration
            if (controls.onPress(Keys.Right, Buttons.DPadRight))
                x_accel += speed;
            else if (controls.onRelease(Keys.Right, Buttons.DPadRight))
                x_accel -= speed;
            if (controls.onPress(Keys.Left, Buttons.DPadLeft))
                x_accel -= speed;
            else if (controls.onRelease(Keys.Left, Buttons.DPadLeft))
                x_accel += speed;

            // Movement
            x_vel = x_vel * (1 - friction)
                + (frozen() ? 0 : x_accel * .10);
            spriteX += Convert.ToInt32(x_vel);

            // Direction
            if (x_vel > 0.1)
                left = false;
            else if (x_vel < -0.1)
                left = true;

            // Puddle
            if (!frozen() && grounded && powerup["puddle"] &&
                controls.isPressed(Keys.Down, Buttons.DPadDown))
            {
                puddled = true;
                imageFile = "PC/collapse.png";
                LoadContent(content);
                if (controls.onPress(Keys.Down, Buttons.DPadDown))
                    puddle_point = physics.count;
            }

            // New shots
            if (controls.onPress(Keys.D, Buttons.RightShoulder))
            {
                shooting = true;
                shot_point = physics.count;
            }
            else if (controls.onRelease(Keys.D, Buttons.RightShoulder))
            {
                shooting = false;
            }

            if (controls.onPress(Keys.S, Buttons.A) && grounded)
            {
                jump_point = physics.count;
            }
            else if (controls.onRelease(Keys.S, Buttons.A))
            {
                // Cut jump short
                if (y_vel < 0)
                    y_vel /= 2;
            }

            // Fall (with grace!)
            if (spriteY < physics.ground)
            {
                spriteY += y_vel;
                y_vel += physics.gravity;
            }
            else
            {
                spriteY = physics.ground;
                y_vel = 0;
                grounded = true;
            }

            // Deal with shot creation and delay
            if (!frozen())
            {
                // Generate regular shots
                if ((physics.count - shot_point) % shot_delay == 0 && shooting)
                {
                    string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none";
                    Shot s = new Shot(this, dir);
                    s.LoadContent(content);
                    physics.shots.Add(s);
                }

                // Generate downward shots
                if ((physics.count - jump_point) % jump_delay == 0 && powerup["jetpack"] &&
                    !grounded && controls.isPressed(Keys.S, Buttons.A))
                {
                    // New shot
                    Shot s = new Shot(this, "down");
                    s.LoadContent(content);
                    physics.shots.Add(s);

                    // Upwards
                    spriteY -= 1;
                    y_vel = -8;
                }
            }

            // Jump logic
            if (controls.isPressed(Keys.S, Buttons.A) && !frozen() && grounded)
            {
                spriteY -= 1;
                y_vel = -15;
                grounded = false;
            }

            Animate(controls, physics, content);

            CheckCollisions(physics);
        }