Click here to Skip to main content
15,896,512 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.Diagnostics;
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 BulletsManager : DrawableGameComponent {

        internal Texture2D photonTex = null;
        internal Texture2D ufoPhotonTex = null;
        internal Texture2D laserTex = null;

        List<Bullet> bulletList = new List<Bullet>(8);

        static internal Texture2D[] BulletTex = new Texture2D[3];

        public static BulletsManager Instance = null;


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


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


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


        protected override void LoadContent() {

            laserTex = Game.Content.Load<Texture2D>("Sprites/shoot2");
            photonTex = Game.Content.Load<Texture2D>("Sprites/shoot");
            ufoPhotonTex = Game.Content.Load<Texture2D>("Sprites/shootufo");

            BulletTex[0] = photonTex;
            BulletTex[1] = ufoPhotonTex;
            BulletTex[2] = laserTex;
            
            base.LoadContent();
        }


        public override void Update(GameTime gameTime) {

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

            foreach (Bullet bullet in bulletList)
                bullet.Update(gameTime);

            if (bulletList.Count > 0) {
                for (int x = bulletList.Count-1; x >= 0; x--) {

                    Bullet bullet = bulletList[x];

                    if ((bullet.Position.Y <= -bullet.Height) || (bullet.Position.Y >= InvasionGame.SectorHeight)) {
                        bulletList.Remove(bullet);
                    }
                }
            }

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime) {

            foreach (Bullet bullet in bulletList)
                bullet.Draw(gameTime);

            base.Draw(gameTime);
        }


        public void Add(Bullet bullet) {
            bulletList.Add(bullet);
        }


        public bool CheckForHitUFO(UFO ufo) {

            if (ufo.State == ShipState.Destroyed)
                return false;

            Rectangle ufoBoundingBox = ufo.BoundingBox;

            foreach (Bullet bullet in bulletList) {
                if (bullet.BulletSource != BulletSource.UfoPhotonBlaster) {
                    if (bullet.BoundingBox.Intersects(ufoBoundingBox)) {
                        ufo.Damaged(bullet.Power);
                        bulletList.Remove(bullet);
                        return true;
                    }
                }
            }
            return false;
        }



        /// <summary>
        /// check if the ship was hit by a bullet
        /// </summary>
        /// <returns>true if the ship was hit by a bullet
        /// else false</returns>
        public bool CheckForHitShip(StarShip starShip) {
            if (starShip.ShipState != ShipState.OK)
                return false;

            if (bulletList.Count > 0) {
                Rectangle shipBoundingBox = starShip.BoundingBox;

                for (int iBullet = bulletList.Count-1; iBullet >= 0; iBullet--) {
                    Bullet bullet = (Bullet)bulletList[iBullet];
                    Rectangle bulletBoundingBox = bullet.BoundingBox;

                    if (bulletBoundingBox.Intersects(shipBoundingBox) &&
						bullet.BulletSource == BulletSource.UfoPhotonBlaster)
                    {
                        bulletList.Remove(bullet);
                        return true;
                    }
                }
            }
            return false;
        }

    }
}

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