Click here to Skip to main content
15,886,362 members
Articles / Game Development

Journey of Windows 8 Game (SkyWar) for Intel App Up Using MonoGame Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Jan 2013CPOL15 min read 23.2K   226   12  
Journey of Windows 8 game SkyWar for Intel App Up competition
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SkyWar
{
    class AliensComponent
    {
        GameplayScreen _gameScreen;
        GameData _gameData;
        BossAlien bossAlien = null;

        Alien[] _aliens = new Alien[40];        
        int _currentAlienIndex;
        Random _rnd = new Random();
        TimeSpan _elapsedTime;
        int _currentLiveAliens, _totalKilledAliens;
        private TimeSpan _delayCount, _delay;
        public TimeSpan Delay
        {
            get { return _delay; }
            set
            {
                _delay = value;
                _delayCount = TimeSpan.Zero;
            }
        }
        internal IEnumerable<Alien> Aliens { get { return _aliens; } }
        public BossAlien BossAlien { get { return bossAlien; } }
        // alien bullets
        Sprite[] _bullets = new Sprite[40];
        //SoundEffectInstance[] _bulletSoundInst = new SoundEffectInstance[24];
        //SoundEffect _bulletSound;
        Texture2D _texture;
        int _totalBullets, _currentBullet;

        public AliensComponent(GameplayScreen gpScreen, ContentManager content)
        {
            _gameScreen = gpScreen;
            _gameData = GameData.GetGameDataInstance();
            _texture = content.Load<Texture2D>("images/laser1");
        }

        public void Update(GameTime gameTime)
        {
            // get current level data
            LevelData data = _gameScreen.GetCurrentLevelData();

            if (data == null)
            {
                _gameData.IsGameCompleted = true;
                return;
            }
            foreach (var bullet in _bullets)
            {
                if (bullet != null && bullet.Active)
                {
                    bullet.Update(gameTime);
                    if (bullet.Position.Y > _gameData.GameScreenHeight + 30)
                    {
                        bullet.Active = false;
                        _totalBullets--;
                    }
                    else if (bullet.Collide(_gameScreen.SpaceShipComponent.BoundingBox))
                    {
                        _gameScreen.SpaceShipComponent.PlayerHit();
                        bullet.Active = false;
                        _totalBullets--;
                    }
                }
            }

            if (Delay > TimeSpan.Zero)
            {
                if ((_delayCount += gameTime.ElapsedGameTime) >= Delay)
                    Delay = TimeSpan.Zero;
            }
            
            if (_delay == TimeSpan.Zero && (_elapsedTime += gameTime.ElapsedGameTime) > data.AlienGenerationTime)
            {
                _elapsedTime = TimeSpan.Zero;
                var alien = CreateAlien(data);
                if (alien != null)
                {
                    _currentLiveAliens++;
                    while (_aliens[_currentAlienIndex] != null && _aliens[_currentAlienIndex].Active)
                        _currentAlienIndex = (_currentAlienIndex + 1) % _aliens.Length;
                    _aliens[_currentAlienIndex] = alien;
                }
            }

            foreach (var alien in _aliens)
                if (alien != null && alien.Active)
                {
                    alien.Update(gameTime);
                    if (!alien.IsDead && _gameScreen.SpaceShipComponent.IsSpaceShipActive && alien.Collide(_gameScreen.SpaceShipComponent.BoundingBox))
                    {
                        alien.Hit();
                        _gameScreen.SpaceShipComponent.PlayerHit();
                        if (_totalKilledAliens < data.TotalAliensToFinish)
                            _totalKilledAliens = 0;
                    }
                    if (_delay == TimeSpan.Zero && _totalBullets < data.MaxAlienBullets && _rnd.Next(100) < data.FireChance)
                        CreateBullet(alien);
                }

            if (bossAlien != null && bossAlien.Active && (!bossAlien.IsDead))
            {
                bossAlien.Update(gameTime);
                if (!bossAlien.IsDead && _gameScreen.SpaceShipComponent.IsSpaceShipActive && bossAlien.Collide(_gameScreen.SpaceShipComponent.BoundingBox))
                {
                    bossAlien.Hit();
                    _gameScreen.SpaceShipComponent.PlayerHit();
                }
                if (_delay == TimeSpan.Zero && _totalBullets < data.MaxAlienBullets && _rnd.Next(100) < data.FireChance)
                    CreateBullet(bossAlien);
            }
        }

        private Alien CreateAlien(LevelData data)
        {
            if (_currentLiveAliens > data.MaxActiveAliens) return null;

            if (_totalKilledAliens == data.TotalAliensToFinish)
            {
                _totalKilledAliens++;
                bossAlien = CreateBoss(data);
                bossAlien.Killed += delegate
                {
                    _currentLiveAliens--;
                    _gameScreen.InitLevel(_gameScreen.Level + 1);
                    _totalKilledAliens = 0;
                };
                return null;
            }

            int chance = 0;
            int value = _rnd.Next(100);
            foreach (var sd in data.SelectionData)
            {
                if (value < sd.Chance + chance)
                {
                    Alien alien = new Alien(sd.Alien, false);
                    alien.Position = new Vector2((float)(_rnd.NextDouble() * _gameData.GameScreenWidth), -50);
                    alien.Velocity = new Vector2((float)(_rnd.NextDouble() * alien.Type.MaxXSpeed * 2 - alien.Type.MaxXSpeed), (float)(_rnd.NextDouble() * alien.Type.MaxYSpeed + 2));
                    alien.Scale = 0.7f;
                    _aliens[_currentAlienIndex] = alien;
                    alien.OutOfBounds += a =>
                    {
                        _currentLiveAliens--;
                    };
                    alien.Killed += a =>
                    {
                        _currentLiveAliens--;
                        _totalKilledAliens++;
                    };
                    return alien;
                }
                chance += sd.Chance;
            }
            return null;
        }
        private BossAlien CreateBoss(LevelData data)
        {
            BossAlien alien = new BossAlien(data.Boss);
            alien.Position = new Vector2((float)(_rnd.NextDouble() * _gameData.GameScreenWidth), 10);
            alien.Velocity = new Vector2((float)(_rnd.NextDouble() * alien.Type.MaxXSpeed * 4 - alien.Type.MaxXSpeed), 0.0f);
            alien.Scale = 2.3f;
            return alien;
        }

        private void CreateBullet(Sprite alien)
        {
            while (_bullets[_currentBullet] != null && _bullets[_currentBullet].Active)
                _currentBullet = (_currentBullet + 1) % _bullets.Length;
            Sprite bullet = _bullets[_currentBullet];
            if (bullet == null)
            {
                bullet = new Sprite(_texture, new Rectangle(0, 0, 16, 46));
                bullet.Scale = 0.5f;
                bullet.ZLayer = .5f;
                //bullet.Color = Color.White;
                _bullets[_currentBullet] = bullet;
                //_bulletSoundInst[_currentBullet] = _bulletSound.CreateInstance();
                //_bulletSoundInst[_currentBullet].Volume = .7f;
            }
            bullet.Position = alien.Position + new Vector2(-2, alien.ActualHeight / 2);
            bullet.Velocity.Y = alien.Velocity.Y > 0 ? 1.8f * alien.Velocity.Y : _rnd.Next(10) + 3;
            bullet.Active = true;
            //_bulletSoundInst[_currentBullet].Play();
            _totalBullets++;
        }

        public void Draw(GameTime gameTime, SpriteBatch spbatch)
        {
            foreach (var alien in _aliens)
                if (alien != null && alien.Active)
                    alien.Draw(gameTime, spbatch);

            foreach (var bullet in _bullets)
                if (bullet != null)
                    bullet.Draw(gameTime, spbatch);

            if (bossAlien != null && bossAlien.Active && (!bossAlien.IsDead))
                bossAlien.Draw(gameTime, spbatch);
        }
    }
}

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
Software Developer eMids Technologies Pvt Ltd
India India
2+ year of experience in Information Technology with the extensive exposure of Treasury & Capital, Health Care and ERP domain.

Area of interest - Multithreading, Files, WPF, WCF, Jquery, Mvc, Sql Server, Perceptual programming

Heavily Worked on desktop application ,Web application and WCF Services.

Also worked on small games application for learning purpose.

Latest interest - Windows 8 , Perceptual Programming, Windows Phone, WCF etc

Comments and Discussions