- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- using ShipGen;
- using System.Diagnostics;
- namespace SpaceSquadron
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class SpaceSquadronGame : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D backgroundTexture;
- Random random = new Random();
- SpriteFont menuFont;
- PlayerShip player1;
- List<Bullet> playerBullets = new List<Bullet>();
- EnemyGenerator enemyGenerator = new EnemyGenerator();
- public SpaceSquadronGame()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <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()
- {
- graphics.PreferredBackBufferWidth = 1280;
- graphics.PreferredBackBufferHeight = 720;
- graphics.ApplyChanges();
- graphics.SynchronizeWithVerticalRetrace = false;
- this.IsFixedTimeStep = false;
- Components.Add(new FrameRateCounter(this));
- //Components.Add(new GamerServicesComponent(this));
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- menuFont = Content.Load<SpriteFont>("Fonts\\MenuFont");
- backgroundTexture = new StaryBackground(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 2000);
- Bullet.Texture = Content.Load<Texture2D>("GFX\\Bullet");
- Bullet.Origin = new Vector2(Bullet.Texture.Width / 2, Bullet.Texture.Height / 2);
- player1 = new PlayerShip(
- PixelMaskGenerator.GetRandomImage(GraphicsDevice, 20, Color.Orange),
- PlayerIndex.One);
- player1.Position = new Vector2(100, 100);
- enemyGenerator.Initialize(GraphicsDevice);
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- if (player1.IsAlive && player1.HasHealth)
- player1.Update(gameTime, playerBullets, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
- enemyGenerator.Update(gameTime, null, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
- Rectangle screenRect = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
- List<Bullet> bulletsToRemove = new List<Bullet>();
- foreach (Bullet b in playerBullets)
- {
- b.Update();
- if (!screenRect.Intersects(b.Bounds))
- bulletsToRemove.Add(b);
- enemyGenerator.CheckCollisionWithBullets(b, bulletsToRemove);
- }
- foreach (Bullet b in bulletsToRemove)
- playerBullets.Remove(b);
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
- GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Point;
- GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Point;
- GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Point;
- spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
- player1.Draw(spriteBatch);
- foreach (Bullet b in playerBullets)
- b.Draw(spriteBatch);
- enemyGenerator.Draw(spriteBatch);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
