Click here to Skip to main content
15,896,278 members
Articles / Artificial Intelligence

Invasion Game in XNA for Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.92/5 (31 votes)
6 Jan 2012CPOL12 min read 111K   6.6K   59  
This is a port of Invasion (originally posted on CodeProject by Mauricio Ritter) to Windows Phone 7 (with some enhancements).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using ImproviSoft.System;

using Invasion.Sprites;

namespace Invasion.SpriteManagers {


    public class ExtrasManager : DrawableGameComponent {

        static internal Texture2D ExtrasTex = null;

        private SoundEffect gotExtraSoundEffect = null;

        List<Extra> extrasList = new List<Extra>(5);

        public static ExtrasManager Instance = null;



        public ExtrasManager(Game game)
            : base(game) {
            Instance = this;
        }


        public override void Initialize() {
            
            base.Initialize();
        }
        

        public void Reset() {
            extrasList.Clear();
        }


        protected override void LoadContent() {

            ExtrasTex = Game.Content.Load<Texture2D>("Sprites/extras");

            gotExtraSoundEffect = Game.Content.Load<SoundEffect>("Sounds/getextra");

            base.LoadContent();
        }


        public override void Update(GameTime gameTime) {

            if (Invasion.CurrentScreen != GameScreen.GameplayScreen && 
                Invasion.CurrentScreen != GameScreen.HelpScreen)
                return;

            foreach (Extra extra in extrasList)
                extra.Update(gameTime);

            // remove extras that go beyond the bottom of the screen
            for (int iExtra = extrasList.Count-1; iExtra >= 0; iExtra--) {
                Extra extra = (Extra)extrasList[iExtra];

                if (extra.Position.Y > InvasionGame.SectorHeight)
                    extrasList.Remove(extra);
            }

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime) {

            foreach (Extra extra in extrasList)
                extra.Draw(gameTime);

            base.Draw(gameTime);
        }


        public void Add(Extra extra) {
            extrasList.Add(extra);
        }


        public void CheckForShipAward(StarShip starShip) {

            if (extrasList.Count == 0)
                return;

            Extra.ExtraType extra = CheckForHitExtra(starShip);

            switch (extra) {
                case Extra.ExtraType.PhotonAmmo: //A
                    starShip.PhotonAmmo += 25;
                    break;

                case Extra.ExtraType.Weapons: //W
                    if (starShip.MaxWeapon < Weapon.Photon3)
                        starShip.MaxWeapon++;

                    //starShip.Weapon = starShip.MaxWeapon; //now using StarShip.AutoSelectMaxWeapon()
                    break;

                case Extra.ExtraType.Hundred: //100
                    InvasionGame.Scoreboard.Score += 100 * InvasionGame.Scoreboard.Wave;
                    break;

                case Extra.ExtraType.BlasterAmmo: //B
                    starShip.LaserAmmo += 40;
                    break;

                case Extra.ExtraType.Shield: //S
                    starShip.Shield += 10;
                    break;
            }

            if (extra != Extra.ExtraType.NotSet)
                SoundManager.Instance.Play(gotExtraSoundEffect);
        }



        /// <summary>
        /// Check if the ship got an extra
        /// </summary>
        /// <returns>the value of the extra that was hit</returns>
        Extra.ExtraType CheckForHitExtra(StarShip starShip) {
            Extra.ExtraType hitExtra = Extra.ExtraType.NotSet;

            Rectangle shipBoundingBox = starShip.BoundingBox;

            for (int iExtra = extrasList.Count-1; iExtra >= 0; iExtra--) {
                Extra extra = (Extra)extrasList[iExtra];
                Rectangle extraBoundingBox = extra.BoundingBox;

                if (extraBoundingBox.Intersects(shipBoundingBox)) {
                    hitExtra = extra.Type;
                    extrasList.Remove(extra);
                    return hitExtra;
                }
            }
            return hitExtra;
        }


        public int Count {
            get { return extrasList.Count; }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President ImproviSoft LLC
United States United States
Dan is the Founder and President of ImproviSoft LLC (mobile software) and AdStreamer, Inc. (mobile advertising) - both Microsoft BizSpark Plus Startups.

Dan holds a B.S. in Computer Science from Clarkson University and M.S. degrees in Computer Science and Computer Engineering from Syracuse University. He is an ASQ Certified Software Quality Engineer (CSQE) and was a 2012 Microsoft XNA/DirectX MVP.

Prior experience includes Software Engineering, Project Management, and Functional Management in the Aerospace & Defense, Medical Devices, Automotive Engineering, and e-Commerce industries.

Dan's dev-blog is The ImproviSoft Blog.

Comments and Discussions