diff --git a/Game Game.sln.DotSettings b/Game Game.sln.DotSettings new file mode 100644 index 0000000..fd32cec --- /dev/null +++ b/Game Game.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/Game/Amogus.cs b/Game/Amogus.cs deleted file mode 100644 index 23b3a42..0000000 --- a/Game/Amogus.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Sprack; -using Microsoft.Xna.Framework; -namespace Sprack -{ - public class Amogus - { - public double _x, _y; - private int _height, _width; - public Rectangle _drawRectangle; - public Rectangle _hitbox; - public enum Colour { grey, red} - public Colour colour; - Random random = new Random(); - public Amogus() - { - if (random.Next(0, 10) < 5) - { - _drawRectangle = new Rectangle(63, 0, 16, 18); - } - else - { - _drawRectangle = new Rectangle(79, 0, 16, 18); - } - } - public void Behaviour() - { - _hitbox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), _width, _height); - //Make him do some cool shit ¯\_(ツ)_/¯. - } - } - -} diff --git a/Game/Asteroid.cs b/Game/Asteroid.cs index b51663c..6b1a3f2 100644 --- a/Game/Asteroid.cs +++ b/Game/Asteroid.cs @@ -1,129 +1,127 @@ using System; -using System.Collections.Generic; -using System.Text; -using System.Diagnostics; -using Sprack; -using Microsoft.Xna.Framework.Input; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; -namespace Sprack +namespace Sprack.Game { - public class Asteroid + public class Asteroid : Sprite { - public double _x = 0; - public double _y = -100000; - public enum State {dead, alive, hit }; - public State _currentState; - private int _boxWidth = 64; - private int _boxHeight = 64; - public Rectangle _collisionBox; + public override Rectangle Shape => AstroidShape; + + private static readonly Rectangle AstroidShape = + new Rectangle(0, 8, 16, 16); + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), BoxHeight, BoxWidth); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(4, 4); + + public Asteroid() + { + YPosition = -1000; + } + + + public State CurrentState; + private const int BoxWidth = 64; + private const int BoxHeight = 64; private int _tickSinceLastTeleport; - public double _speed; + private double _speed; private Single _rotation; - public Single _spinning; //Used to create the effect of the asteroid spinning, in the draw method. - private float _spinningSpeed; - public Vector2 _rotationOrigin = new Vector2(4, 4 ); + private double _unitVectorX; private double _unitVectorY; - private Random randomGenerator = new Random(); - public Explosion _explosion = new Explosion(); - public bool _wasAlivePreviousFrame; + private readonly Random _randomGenerator = new Random(); + public Explosion Explosion = new Explosion(); + public bool WasAlivePreviousFrame; + public void Spawn(double shipX, double shipY) //To prevent the asteroids from spawning on top of or really close to the ship, the spawn method has to know where the ship is { //TODO: Solve the problem mentioned above. - _currentState = State.alive; - - if (randomGenerator.Next(0, 10) < 5) + CurrentState = State.alive; + + if (_randomGenerator.Next(0, 10) < 5) { - _x = shipX + randomGenerator.Next(100, 300); + XPosition = shipX + _randomGenerator.Next(100, 300); } - else + else { - _x = shipX - randomGenerator.Next(100, 300); + XPosition = shipX - _randomGenerator.Next(100, 300); } - _y = shipY + randomGenerator.Next(100, 300); - _rotation = Convert.ToSingle(randomGenerator.NextDouble()) + randomGenerator.Next(1, 6); - _speed = Convert.ToDouble(randomGenerator.NextDouble() + randomGenerator.Next(1, 2)); - double xVector; - double yVector; - double magnitude; - xVector = Math.Sin(_rotation); - yVector = Math.Cos(_rotation); - magnitude = Math.Sqrt(xVector * xVector + yVector * yVector); + YPosition = shipY + _randomGenerator.Next(100, 300); + _rotation = Convert.ToSingle(_randomGenerator.NextDouble()) + _randomGenerator.Next(1, 6); + _speed = Convert.ToDouble(_randomGenerator.NextDouble() + _randomGenerator.Next(1, 2)); + var xVector = Math.Sin(_rotation); + var yVector = Math.Cos(_rotation); + var magnitude = Math.Sqrt(xVector * xVector + yVector * yVector); _unitVectorX = xVector / magnitude; _unitVectorY = yVector / magnitude; - if (randomGenerator.Next(0, 10)>= 5) - { - _spinningSpeed = Convert.ToSingle((randomGenerator.NextDouble() / 100)*(-1)); - } - else - { - _spinningSpeed = Convert.ToSingle(randomGenerator.NextDouble() / 100); - } } - public void Behaviour(ref Bullet[] bullets) + + public void Behaviour(ref Bullet[] bullets) { - if (_wasAlivePreviousFrame == true && _currentState == State.dead) + if (WasAlivePreviousFrame && CurrentState == State.dead) { - _wasAlivePreviousFrame = false; + WasAlivePreviousFrame = false; } _tickSinceLastTeleport++; - _collisionBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), _boxHeight, _boxWidth); - _spinning += _spinningSpeed; - _x = _x + (_unitVectorX * _speed); - _y = _y - (_unitVectorY * _speed); - CheckCollision(ref bullets); - _explosion.Behaviour(); - } - private void Explode() + XPosition += _unitVectorX * _speed; + YPosition -= _unitVectorY * _speed; + CheckCollision(ref bullets); + Explosion.Behaviour(); + } + + private void Explode() { - _explosion.Start(_x, _y); + Explosion.Start(XPosition, YPosition); Reset(); } - public void Reset() + + public void Reset() { - _currentState = State.dead; - _wasAlivePreviousFrame = true; - _x = 0; - _y = -1000; + CurrentState = State.dead; + WasAlivePreviousFrame = true; + XPosition = 0; + YPosition = -1000; } - private void CheckCollision(ref Bullet[] bullets) + + private void CheckCollision(ref Bullet[] bullets) { foreach (Bullet bullet in bullets) { - if (_collisionBox.Intersects(bullet._bulletBox)) + if (HitBox.Intersects(bullet.HitBox)) { bullet.PutToSleep(); Explode(); } - } - if (_currentState == State.alive) + + if (CurrentState == State.alive) { if (_tickSinceLastTeleport > 60) { - if (_x < -46) + if (XPosition < -46) { - _x = 732; + XPosition = 732; _tickSinceLastTeleport = 0; } - else if (_x > 746) + else if (XPosition > 746) { - _x = -32; + XPosition = -32; _tickSinceLastTeleport = 0; } - if (_y < -46) + if (YPosition < -46) { - _y = 732; + YPosition = 732; _tickSinceLastTeleport = 0; } - else if (_y > 746) + else if (YPosition > 746) { - _y = -32; + YPosition = -32; _tickSinceLastTeleport = 0; } } - } - } + } + } } } diff --git a/Game/Bullet.cs b/Game/Bullet.cs index a21afd0..0191513 100644 --- a/Game/Bullet.cs +++ b/Game/Bullet.cs @@ -1,19 +1,23 @@ using System; -using System.Collections.Generic; -using System.Globalization; -using System.Text; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using SharpDX.Direct3D9; -using Sprack; -namespace Sprack +namespace Sprack.Game { //Bullets shot by the players spaceship. - public class Bullet - { - public double _x; - public double _y; + public class Bullet : Sprite + { + public override Rectangle Shape => BulletShape; + + private static readonly Rectangle BulletShape = + new Rectangle(9, 0, 1, 3); + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 5, 5); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(0, 1); + private double _xVector; private double _yVector; private double _magnitude; @@ -21,44 +25,41 @@ public class Bullet private double _unitVectorY; private readonly double _baseSpeed = 15.0f; private double _currentSpeed; - public Single _rotation; - public Vector2 _rotationOrigin = new Vector2(0, 1); - public Rectangle _bulletBox = new Rectangle(); - private enum State { sleep, woke } - State _Current = State.sleep; + + private State _current = State.dead; + public void Behaviour() { - if (_Current == State.woke) + if (_current == State.alive) { - _x = _x + (_unitVectorX * _currentSpeed); - _y = _y - (_unitVectorY * _currentSpeed); - _bulletBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 5, 5); + XPosition += _unitVectorX * _currentSpeed; + YPosition -= _unitVectorY * _currentSpeed; } - if (_x > 700 || _x < 0 && _y > 700 || _y < 0 && _Current != State.sleep) + if (XPosition > 700 || XPosition < 0 && YPosition > 700 || YPosition < 0 && _current != State.dead) { PutToSleep(); } } - public void Shoot(ref Single shipRotation, ref double shipSpeed, ref double shipX, ref double shipY) + + public void Shoot(Single shipRotation, ref double shipSpeed, double shipX, double shipY) { - _x = shipX; - _y = shipY; - _rotation = shipRotation; - _Current = State.woke; + XPosition = shipX; + YPosition = shipY; + Rotation = shipRotation; + _current = State.alive; _currentSpeed = shipSpeed + _baseSpeed; - _xVector = Math.Sin(_rotation); - _yVector = Math.Cos(_rotation); + _xVector = Math.Sin(Rotation); + _yVector = Math.Cos(Rotation); _magnitude = Math.Sqrt(_xVector * _xVector + _yVector * _yVector); _unitVectorX = _xVector / _magnitude; _unitVectorY = _yVector / _magnitude; } - public void PutToSleep() //This method is called from the asteroid class if the asteroid detects its own rectangle and the bullets rectangle intesecting. + + public void PutToSleep() //This method is called from the asteroid class if the asteroid detects its own rectangle and the bullets rectangle intersecting. { - _Current = State.sleep; - _x = -1000; - _y = 0; - _bulletBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 5, 5); + _current = State.dead; + XPosition = -1000; + YPosition = 0; } - } -} +} \ No newline at end of file diff --git a/Game/Explosion.cs b/Game/Explosion.cs index 2c3455f..8a083e6 100644 --- a/Game/Explosion.cs +++ b/Game/Explosion.cs @@ -1,55 +1,58 @@ using System; -using System.Collections.Generic; -using System.Text; -using Sprack; using Microsoft.Xna.Framework; -namespace Sprack +namespace Sprack.Game { - public class Explosion + public class Explosion : Sprite { - public bool _active = false; //Explosion only has two states, so i wont bother with making a state machine. - private double _x; - private double _y; - public Rectangle _rectangle; - public Rectangle _drawRectangle; - private const int _drawWidth = 32; - private const int _drawHeight = 32; + public override Rectangle Shape => + new Rectangle(_drawX, 31, DrawHeight, DrawWidth); + + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 128, 128); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(0, 0); + + public bool Active; //Explosion only has two states, so I will not bother with making a state machine. + + private const int DrawWidth = 32; + private const int DrawHeight = 32; private int _drawX; - private const int _drawY = 31; private int _frame; - public Vector2 _rotationOrigin = new Vector2(128, 128); - public Single _rotation = 0; - private Random randomGenerator = new Random(); - private void Animate() + private readonly Random _randomGenerator = new Random(); + + private void Animate() { _drawX = 32 * (_frame / 6); - if (_drawX > 192) + if (_drawX > 192) { _drawX = 0; End(); } - _drawRectangle = new Rectangle(_drawX, _drawY, _drawHeight, _drawWidth) ; - } + private void End() { - _x = -100000; - _active = false; + XPosition = -100000; + Active = false; } + public void Start(double newX, double newY) { - _active = true; + Active = true; _frame = 0; - _rotation = Convert.ToInt32(randomGenerator.NextDouble() + randomGenerator.Next(0, 3)); - _x = newX; - _y = newY; - _rectangle = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 128, 128); + Rotation = Convert.ToInt32(_randomGenerator.NextDouble() + _randomGenerator.Next(0, 3)); + XPosition = newX; + YPosition = newY; } + public void Behaviour() { _frame++; - Animate(); + Animate(); } } -} +} \ No newline at end of file diff --git a/Game/Game1.cs b/Game/Game1.cs index 1a67829..2f59689 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -1,138 +1,118 @@ -using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using System; -namespace Sprack +namespace Sprack.Game { //24 NOV 2020 - ??? - public class Game1 : Game + public class Game1 : Microsoft.Xna.Framework.Game { - private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; - public Vector2 SpawnPos; - private SpriteEffects effect; - private float depth = 0; - private Texture2D MainSpritesheet; - private SpriteFont Font; - private Rectangle shipRectangle = new Rectangle(0, 0, 8, 8); - private Rectangle shipBulletRectangle = new Rectangle(9, 0, 1, 3); - private Rectangle bulletRectangle = new Rectangle(9, 0, 1, 3); - private Rectangle thrustRectangle = new Rectangle(16, 0, 3, 3); - private Rectangle asteroidDrawRectangle = new Rectangle(0, 8, 16, 16); - private Rectangle titleDrawRectanlge; - private Rectangle titleRectangle = new Rectangle(94, 94, 521, 128); - private Rectangle _OrangeExpParticle = new Rectangle(19, 0, 1, 1); - private int _titleTime = 0; - public Asteroid[] _asteroids = new Asteroid[20]; - public Star[] _stars = new Star[100]; - GameController gameController = new GameController(); - private Random random = new Random(); + private Texture2D _mainSpritesheet; + private SpriteFont _font; + + private readonly Rectangle _titleRectangle = new Rectangle(94, 94, 521, 128); + + private int _titleTime; + private readonly Asteroid[] _asteroids = new Asteroid[20]; + private readonly Star[] _stars = new Star[100]; + private readonly GameController _gameController = new GameController(); + private readonly Random _random = new Random(); + private readonly List _sprites = new List(400); + public Game1() { - _graphics = new GraphicsDeviceManager(this); + var graphics = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; IsMouseVisible = false; - _graphics.PreferredBackBufferWidth = 700; - _graphics.PreferredBackBufferHeight = 700; - effect = SpriteEffects.None; - _graphics.ApplyChanges(); - for (int j = 0; j < 20; j++) - { + + graphics.PreferredBackBufferWidth = 700; + graphics.PreferredBackBufferHeight = 700; + + graphics.ApplyChanges(); + + for (var j = 0; j < _asteroids.Length; j++) + { _asteroids[j] = new Asteroid(); } - for (int j = 0; j < 100; j++) + for (var j = 0; j < _stars.Length; j++) { - _stars[j] = new Star(); - _stars[j]._y = j * 7 + random.Next(0, 10); + _stars[j] = new Star + { + YPosition = j * 7 + _random.Next(0, 10) + }; } + + _sprites.AddRange(_asteroids); + _sprites.AddRange(_asteroids.Select(a => a.Explosion)); + _sprites.AddRange(_stars); + _sprites.AddRange(_gameController.Ship.ThrustParticles); + _sprites.AddRange(_gameController.Ship.ShipBullets); + _sprites.AddRange(_gameController.Ufo.Rockets); + _sprites.Add(_gameController.Ship); } - protected override void Initialize() - { - base.Initialize(); - } + protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); - MainSpritesheet = Content.Load("Sprack Spritesheet"); - Font = Content.Load("Score"); + _mainSpritesheet = Content.Load("Sprack Spritesheet"); + _font = Content.Load("Score"); } + protected override void Update(GameTime gameTime) { - gameController.Update(ref gameController.ship, ref _asteroids, _stars); + _gameController.Update(ref _gameController.Ship, _asteroids, _stars); } + protected override void Draw(GameTime gameTime) { - _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise); GraphicsDevice.Clear(Color.Black); - foreach (Star star in _stars) - { - _spriteBatch.Draw(MainSpritesheet, star._hitBox, star.drawRectangle, Color.White, 0, new Vector2(0, 0), effect, depth); - } - foreach (ThrustParticle thrustParticle in gameController.ship._thrustParticles)//SHIPTHRUSTPARTICLES - { - _spriteBatch.Draw(MainSpritesheet, thrustParticle.RectangleBox, thrustRectangle, Color.White, thrustParticle._rotation, thrustParticle._rotationOrigin, effect, depth); - } - foreach (Bullet bullet in gameController.ship._shipBullets)//SHIPBULLETS - { - _spriteBatch.Draw(MainSpritesheet, bullet._bulletBox, bulletRectangle, Color.White, bullet._rotation, bullet._rotationOrigin, effect, depth); - } - foreach (Asteroid Asteroid in _asteroids)//ASTEROIDS - { - _spriteBatch.Draw(MainSpritesheet, Asteroid._collisionBox, asteroidDrawRectangle, Color.White, 0, Asteroid._rotationOrigin, effect, depth); - if (Asteroid._explosion._active == true) - { - _spriteBatch.Draw(MainSpritesheet, Asteroid._explosion._rectangle, Asteroid._explosion._drawRectangle, Color.White, Asteroid._explosion._rotation, Asteroid._rotationOrigin, effect, depth); - } - } - foreach (Rocket rocket in gameController.UFO.rockets) - { - _spriteBatch.Draw(MainSpritesheet, rocket._positionBox, rocket._drawRectangle, Color.White, rocket._rotation, rocket._rotaionOrigin, effect, depth); - } - _spriteBatch.Draw(MainSpritesheet, gameController.UFO._rectangle, gameController.UFO._drawRectangle, Color.White); - if (gameController.CurrentGameState == GameController.gameState.menu ) - { - _spriteBatch.DrawString(Font, "Highscore:" + gameController.Highscore._score, new Vector2(164, 125), Color.White); - _spriteBatch.DrawString(Font, "Your Score:" + gameController.CurrentScore._score, new Vector2(164, 164), Color.White); - _spriteBatch.DrawString(Font, "Start? Y/N", new Vector2(164, 300), Color.White); - } - else if (gameController.CurrentGameState == GameController.gameState.firstMenu) + + foreach (var sprite in _sprites) { - _spriteBatch.Draw(MainSpritesheet, titleRectangle, titleDrawRectanlge = AnimateTitleScreen(), Color.White); - _spriteBatch.DrawString(Font, "Author: Sivert L.V.", new Vector2 (0, 660), Color.White); - _spriteBatch.DrawString(Font, "Start? Y/N", new Vector2(175, 300), Color.White); + if(sprite is Explosion { Active: false }) + continue; + + _spriteBatch.Draw(_mainSpritesheet, sprite.HitBox, sprite.Shape, sprite.Color, sprite.Rotation, sprite.RotationOrigin, sprite.Effect, sprite.Depth); } - else if (gameController.CurrentGameState == GameController.gameState.inGame) + + switch (_gameController.CurrentGameState) { - _spriteBatch.DrawString(Font, "Score:" + gameController.CurrentScore._score, new Vector2(10, 10), Color.White); + case GameState.menu: + _spriteBatch.DrawString(_font, "High Score:" + _gameController.HighRecord.Score, new Vector2(164, 125), Color.White); + _spriteBatch.DrawString(_font, "Your Score:" + _gameController.CurrentRecord.Score, new Vector2(164, 164), Color.White); + _spriteBatch.DrawString(_font, "Start? Y/N", new Vector2(164, 300), Color.White); + break; + case GameState.firstMenu: + _spriteBatch.Draw(_mainSpritesheet, _titleRectangle, AnimateTitleScreen(), Color.White); + _spriteBatch.DrawString(_font, "Author: Sivert L.V.", new Vector2(0, 660), Color.White); + _spriteBatch.DrawString(_font, "Start? Y/N", new Vector2(175, 300), Color.White); + break; + case GameState.inGame: + _spriteBatch.DrawString(_font, "Score:" + _gameController.CurrentRecord.Score, new Vector2(10, 10), Color.White); + break; } - _spriteBatch.Draw(MainSpritesheet, gameController.ship._shipBox, shipRectangle, Color.White, gameController.ship._rotation, gameController.ship._rotationOrigin, effect, depth); //PLAYERSHIP - + base.Draw(gameTime); _spriteBatch.End(); } - private Rectangle AnimateTitleScreen() - { + private Rectangle AnimateTitleScreen() + { _titleTime++; - if (_titleTime < 30) + if (_titleTime < 30) { - if (_titleTime == 60) - { - _titleTime = 0; - } return new Rectangle(0, 71, 63, 16); } - else if (_titleTime > 30) - { - if (_titleTime == 60) - { - _titleTime = 0; - } - return new Rectangle(0, 87, 63, 16); - } - else + + if (_titleTime >= 60) { - return new Rectangle(0, 87, 63, 16); + _titleTime = 0; } + + return new Rectangle(0, 87, 63, 16); } } } diff --git a/Game/GameController.cs b/Game/GameController.cs index 0d8a2e1..d69016f 100644 --- a/Game/GameController.cs +++ b/Game/GameController.cs @@ -1,127 +1,130 @@ using System; -using System.Collections.Generic; -using System.Text; +using System.IO; using System.Text.Json; -using System.Diagnostics; -using SharpDX.Direct2D1; -using Sprack; using Microsoft.Xna.Framework; -using System.IO; using Microsoft.Xna.Framework.Input; -namespace Sprack +namespace Sprack.Game { public class GameController { + public GameController() + { + Ship = new Ship(); + Ufo = new Ufo(Ship); + } + private bool _asteroids = true; - private Random random = new Random(); - private int frame; - public Ship ship = new Ship(); - public UFO UFO = new UFO(); + private readonly Random _random = new Random(); + private int _frame; + + public Ship Ship; + public Ufo Ufo; + private string _savefileName; - public string _highScore; - private string _windowsUserName = Environment.UserName; - public Score CurrentScore = new Score(); - public Score Highscore = new Score(); - private readonly int[] _difficulties = new int[15] { 0, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 10, 10}; - private int _localDifficulty = 0; + private string _highScore; + private readonly string _windowsUserName = Environment.UserName; + public Record CurrentRecord = new Record(); + public Record HighRecord = new Record(); + private readonly int[] _difficulties = new int[] { 0, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 10, 10 }; + private int _localDifficulty; private readonly Rectangle _topRectangle = new Rectangle(-46, -146, 792, 100); private readonly Rectangle _bottomRectangle = new Rectangle(-46, 746, 792, 100); private readonly Rectangle _leftRectangle = new Rectangle(-146, -46, 100, 792); private readonly Rectangle _rightRectangle = new Rectangle(746, 46, 100, 792); - public enum gameState { menu, inGame, firstMenu } - public gameState CurrentGameState = gameState.firstMenu; - public void Update(ref Ship ship, ref Asteroid[] asteroids, Star[] _stars) + + public GameState CurrentGameState = GameState.firstMenu; + public void Update(ref Ship ship, Asteroid[] asteroids, Star[] stars) { - frame++; - if (CurrentGameState == gameState.inGame) - { - if (ship._shipState != Ship.state.dead) + _frame++; + if (CurrentGameState == GameState.inGame) + { + if (ship.ShipState != State.dead) { ship.CheckUserInput(asteroids, _topRectangle, _bottomRectangle, _leftRectangle, _rightRectangle); } - if (_asteroids) + if (_asteroids) { - UpdateAsteroids(asteroids, ship._shipBullets); + UpdateAsteroids(asteroids, ship.ShipBullets); } - if (Keyboard.GetState().IsKeyDown(Keys.P)) + if (Keyboard.GetState().IsKeyDown(Keys.P)) { _asteroids = false; KillAllAsteroids(asteroids); - } - foreach (Bullet bullet in ship._shipBullets) + } + foreach (Bullet bullet in ship.ShipBullets) { bullet.Behaviour(); } - foreach (ThrustParticle thrustParticle in ship._thrustParticles) + foreach (ThrustParticle thrustParticle in ship.ThrustParticles) { thrustParticle.Behaviour(); } foreach (Asteroid asteroid in asteroids) { - - if (asteroid._currentState == Asteroid.State.alive) + + if (asteroid.CurrentState == State.alive) { - asteroid.Behaviour(ref ship._shipBullets); - if (asteroid._wasAlivePreviousFrame) + asteroid.Behaviour(ref ship.ShipBullets); + if (asteroid.WasAlivePreviousFrame) { CalculateScore(); } } - else if (asteroid._currentState != Asteroid.State.alive) + else if (asteroid.CurrentState != State.alive) { - asteroid._explosion.Behaviour(); + asteroid.Explosion.Behaviour(); } } - ufoUpdate(); - - if (ship.CheckIfShipIsDead() == true) + UfoUpdate(); + + if (ship.CheckIfShipIsDead()) { - CurrentGameState = gameState.menu; + CurrentGameState = GameState.menu; } } - else if (CurrentGameState == gameState.menu) + else if (CurrentGameState == GameState.menu) { - foreach (Bullet bullet in ship._shipBullets) + foreach (Bullet bullet in ship.ShipBullets) { bullet.Behaviour(); } - ReadHighscore(); + ReadHighScore(); ManageScore(); if (Keyboard.GetState().IsKeyDown(Keys.Y)) { - + ship.Resurrect(); ResetAsteroids(asteroids); - CurrentScore._score = 0; - CurrentGameState = gameState.inGame; + CurrentRecord.Score = 0; + CurrentGameState = GameState.inGame; } else if (Keyboard.GetState().IsKeyDown(Keys.N)) - { - WriteHighscore(); + { + WriteHighScore(); Environment.Exit(1); } foreach (Asteroid asteroid in asteroids) { - if (asteroid._currentState == Asteroid.State.alive) + if (asteroid.CurrentState == State.alive) { - asteroid.Behaviour(ref ship._shipBullets); + asteroid.Behaviour(ref ship.ShipBullets); } - } + } } - else if (CurrentGameState == gameState.firstMenu) + else if (CurrentGameState == GameState.firstMenu) { - ReadHighscore(); - if (Keyboard.GetState().IsKeyDown(Keys.Y)) + ReadHighScore(); + if (Keyboard.GetState().IsKeyDown(Keys.Y)) { - CurrentGameState = gameState.inGame; + CurrentGameState = GameState.inGame; } else if (Keyboard.GetState().IsKeyDown(Keys.N)) { Environment.Exit(1); } } - foreach (Star star in _stars) + foreach (var star in stars) { star.Behaviour(); } @@ -130,10 +133,10 @@ public void Update(ref Ship ship, ref Asteroid[] asteroids, Star[] _stars) private void UpdateAsteroids(Asteroid[] asteroids, Bullet[] bullets) { int currentlyAlive = 0; - for (int i = 0; i < asteroids.Length; i++) + foreach (var asteroid in asteroids) { - asteroids[i].Behaviour(ref bullets); - if (asteroids[i]._currentState == Asteroid.State.alive) //Check how many live asteroids there are. + asteroid.Behaviour(ref bullets); + if (asteroid.CurrentState == State.alive) //Check how many live asteroids there are. { currentlyAlive++; } @@ -144,101 +147,93 @@ private void UpdateAsteroids(Asteroid[] asteroids, Bullet[] bullets) { _localDifficulty++; } - SpawnAsteroidWave(currentlyAlive, _localDifficulty, asteroids); + SpawnAsteroidWave(asteroids); } } private void ResetAsteroids(Asteroid[] asteroids) { _localDifficulty = 0; - foreach (Asteroid asteroid in asteroids) + foreach (Asteroid asteroid in asteroids) { asteroid.Reset(); } } - private void SpawnAsteroidWave(int currentlyAliveBig, int _localdifficulty, Asteroid[] asteroids) + private void SpawnAsteroidWave(Asteroid[] asteroids) { - if (_asteroids) + if (_asteroids) { for (int i = 0; i < _difficulties[_localDifficulty]; i++) { - asteroids[i].Spawn(ship._x, ship._y); + asteroids[i].Spawn(Ship.XPosition, Ship.YPosition); } } } - private void KillAllAsteroids(Asteroid[] asteroids) + private void KillAllAsteroids(Asteroid[] asteroids) { - foreach (Asteroid asteroid in asteroids) + foreach (Asteroid asteroid in asteroids) { - asteroid._currentState = Asteroid.State.dead; + asteroid.CurrentState = State.dead; asteroid.Reset(); } } - private void ufoUpdate() + private void UfoUpdate() { - UFO.Behaviour(ship._shipBullets, ship._x, ship._y); - if (Keyboard.GetState().IsKeyDown(Keys.U)) + Ufo.Behaviour(Ship.ShipBullets, Ship.XPosition, Ship.YPosition); + if (Keyboard.GetState().IsKeyDown(Keys.U)) { - UFO.Spawn(); - if (_localDifficulty >= 0 && UFO.CurrentState == UFO.State.dead) + Ufo.Spawn(); + if (_localDifficulty >= 0 && Ufo.CurrentState == State.dead) { - if (random.Next(0, 10) == 10) + if (_random.Next(0, 10) == 10) { - UFO.Spawn(); + Ufo.Spawn(); } } } } - private void RocketsUpdate() + private void RocketsUpdate() { - if (frame == 1000) + if (_frame == 1000) { - UFO.rockets[0].Spawn(UFO._x, UFO._y, ship._x, ship._y); - } - foreach (Rocket rocket in UFO.rockets) - { - if (rocket._alive) - { - rocket.Behaviour(); - } + Ufo.Rockets[0].Spawn(Ufo.XPosition, Ufo.YPosition, Ship.XPosition, Ship.YPosition); } } - private void CalculateScore() + private void CalculateScore() { - CurrentScore._score++; + CurrentRecord.Score++; } - private void WriteHighscore() + private void WriteHighScore() { - string _fileName = "C:\\Users\\" + _windowsUserName + "\\Documents\\save.json"; - _highScore = JsonSerializer.Serialize(Highscore); - File.WriteAllText(_fileName, _highScore); + string fileName = "C:\\Users\\" + _windowsUserName + "\\Documents\\save.json"; + _highScore = JsonSerializer.Serialize(HighRecord); + File.WriteAllText(fileName, _highScore); } - private void ReadHighscore() + private void ReadHighScore() { - string _windowsUserName = Environment.UserName; _savefileName = "C:\\Users\\" + this._windowsUserName + "\\Documents\\save.json"; - if (File.Exists(_savefileName) == false) + if (!File.Exists(_savefileName)) { CreateSaveFile(); - } + } _highScore = File.ReadAllText(_savefileName); - Highscore = JsonSerializer.Deserialize(_highScore); + HighRecord = JsonSerializer.Deserialize(_highScore); } - private void ManageScore() + private void ManageScore() { - if (CurrentScore._score > Highscore._score) + if (CurrentRecord.Score > HighRecord.Score) { - Highscore._score = CurrentScore._score; - WriteHighscore(); + HighRecord.Score = CurrentRecord.Score; + WriteHighScore(); } - else if (CurrentScore._score < Highscore._score) - { } + else if (CurrentRecord.Score < HighRecord.Score) + { } } - private void CreateSaveFile() + private void CreateSaveFile() { - FileStream myFile = File.Create(_savefileName); //Makes and "opens" savefile in the event that it dosent exist on the computer that is running this program. + FileStream myFile = File.Create(_savefileName); //Makes and "opens" savefile in the event that it does not exist on the computer that is running this program. myFile.Close(); //The file then has to be closed otherwise an error is thrown by the second next line since it cant write to the file if it is open. - _highScore = JsonSerializer.Serialize(Highscore); + _highScore = JsonSerializer.Serialize(HighRecord); File.WriteAllText(_savefileName, _highScore); } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/Game/GameState.cs b/Game/GameState.cs new file mode 100644 index 0000000..e2728fe --- /dev/null +++ b/Game/GameState.cs @@ -0,0 +1,9 @@ +namespace Sprack.Game +{ + public enum GameState + { + menu, + inGame, + firstMenu + } +} \ No newline at end of file diff --git a/Game/Particle.cs b/Game/Particle.cs index baf9855..08fe6e2 100644 --- a/Game/Particle.cs +++ b/Game/Particle.cs @@ -1,45 +1,41 @@ using System; -using System.Collections.Generic; -using System.Text; using Microsoft.Xna.Framework; -namespace Sprack +namespace Sprack.Game { - public class Particle + public abstract class Particle : Sprite { - public double _x; - public double _y; - public int _lifeSpan; //The max amount of ticks the particle is allowed to stay on screen for. - private int _currentLifeSpan; //In order for the particle to dissapear it keeps track of the amount of ticks when it is on the screen. - public bool _spin; - public Single _rotationSpeed; - public Single _rotation; - public Vector2 _rotationOrigin; - public bool _travel; + public override Rectangle Shape => ParticleShape; - public void Spawn(double x, double y) + private static readonly Rectangle ParticleShape = + new Rectangle(16, 0, 3, 3); + + public int LifeSpan; //The max amount of ticks the particle is allowed to stay on screen for. + private int _currentLifeSpan; //In order for the particle to disappear it keeps track of the amount of ticks when it is on the screen. + public bool Spin; + public Single RotationSpeed; + + public void Spawn(double x, double y) { - _x = x; - _y = y; + XPosition = x; + YPosition = y; } - public void Behaviour() + public void Behaviour() { _currentLifeSpan++; - if (_spin) + if (Spin) { - _rotation += _rotationSpeed; + Rotation += RotationSpeed; } - if (_currentLifeSpan == _lifeSpan) + if (_currentLifeSpan == LifeSpan) { Reset(); } } - private void Reset() + private void Reset() { - _x = 0; - _y = -1000; + XPosition = 0; + YPosition = -1000; } - - } -} +} \ No newline at end of file diff --git a/Game/Record.cs b/Game/Record.cs new file mode 100644 index 0000000..fe806d1 --- /dev/null +++ b/Game/Record.cs @@ -0,0 +1,7 @@ +namespace Sprack.Game +{ + public class Record + { + public int Score { get; set; } + } +} diff --git a/Game/Rocket.cs b/Game/Rocket.cs index b9c7b62..ed151dd 100644 --- a/Game/Rocket.cs +++ b/Game/Rocket.cs @@ -1,53 +1,38 @@ using System; -using System.Collections.Generic; -using System.Text; -using Sprack; using Microsoft.Xna.Framework; -namespace Sprack +namespace Sprack.Game { - public class Rocket + public class Rocket : Sprite { - public ThrustParticle[] _thrustParticles = new ThrustParticle[20]; - private int _nextParticle = 0; - public Single _rotation; - private double xVector, yVector; - private double _x = -100, _y; - private double _speed = 0; - public Rectangle _positionBox; - public Rectangle _collisionBox; //Since the rocket is long and thin, the hitbox should be smaller than the size of the rocket so the player wont die when the ship and rocket sprites dont look like thet are colliding. - public Vector2 _rotaionOrigin = new Vector2(4, 2); - public Rectangle _drawRectangle; - public bool _alive = false; - public Rocket() - { - for (int j = 0; j < 20; j++) - { - ThrustParticle thrustParticle = new ThrustParticle() {_lifeSpan = 4, _rotationSpeed = 2.0f }; - _thrustParticles[j] = thrustParticle; - } - _drawRectangle = new Rectangle(37, 0, 8, 3); - } + public override Rectangle Shape => RocketShape; + + private static readonly Rectangle RocketShape = + new Rectangle(37, 0, 8, 3); + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 24, 15); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(4, 2); - public void Spawn(double ufoX, double ufoY, double shipX, double shipY) + public bool Alive; + + public Rocket() { - _alive = true; - _x = ufoX; - _y = ufoY; - double xDiff, yDiff, hyp; - xDiff = ufoX - shipX; - yDiff = ufoY - ufoY; - hyp = (yDiff*yDiff) + (xDiff*xDiff); - _rotation = Convert.ToSingle(Math.Acos(hyp / xDiff)); - } - public void Behaviour() - { - _positionBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 24, 15); - _collisionBox = new Rectangle(Convert.ToInt32(_x)+10, Convert.ToInt32(_y)-10, 12, 12); + XPosition = -100; } - public void Die() + + public void Spawn(double ufoX, double ufoY, double shipX, double shipY) { - + Alive = true; + XPosition = ufoX; + YPosition = ufoY; + var xDiff = ufoX - shipX; + var yDiff = ufoY - ufoY; + var hyp = yDiff * yDiff + xDiff * xDiff; + Rotation = Convert.ToSingle(Math.Acos(hyp / xDiff)); } } } diff --git a/Game/Score.cs b/Game/Score.cs deleted file mode 100644 index 5fe57e5..0000000 --- a/Game/Score.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Sprack; - -namespace Sprack -{ - public class Score - { - public int _score { get; set; } - - - } -} diff --git a/Game/Ship.cs b/Game/Ship.cs index f0493f0..cf97f55 100644 --- a/Game/Ship.cs +++ b/Game/Ship.cs @@ -1,206 +1,197 @@ -using Microsoft.Xna.Framework; -using System; +using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Text; -using Sprack; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; -using System.Diagnostics; -using Microsoft.Xna.Framework.Graphics; -using System.Diagnostics.Tracing; -using Microsoft.VisualBasic.FileIO; -namespace Sprack +namespace Sprack.Game { //The Players ship. - public class Ship + public class Ship : Sprite { + public override Rectangle Shape => ShipShape; + + private static readonly Rectangle ShipShape = + new Rectangle(0, 0, 8, 8); + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 32, 32); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(4, 4); + public Ship() { - _shipState = state.alive; + XPosition = 350; + YPosition = 350; + ShipState = State.alive; for (int i = 0; i < 30; i++) { Bullet bullet = new Bullet(); - _shipBullets[i] = bullet; - } - this._rotationOrigin = new Vector2(4, 4); + ShipBullets[i] = bullet; + } } - public double _x = 350; - public double _y = 350; - public Bullet[] _shipBullets = new Bullet[30]; - public List _thrustParticles = new List(); - private int _nextBulletIndex = 0; //Keeps track of what bullet to fire from the shipBullets array + + public Bullet[] ShipBullets = new Bullet[30]; + public List ThrustParticles = new List(); + private int _nextBulletIndex; //Keeps track of what bullet to fire from the shipBullets array private int _localTick; private int _bulletTick; private int _tickSinceLastTeleport; - public enum state {dead, alive, waiting}; - public state _shipState; - public Single _rotation = 0.0f; - private Single _oldRotation = 0.0f; - public Vector2 _rotationOrigin; - public Rectangle _shipBox; - private double xVector; - private double yVector; + + public State ShipState; + private Single _oldRotation; + private double _xVector; + private double _yVector; private double _speed; - private double _maxSpeed = 10.0f; - private double _spaceResistance = 0.02f; - public Explosion _explosion = new Explosion(); - public void CheckUserInput(Asteroid[] asteroids, Rectangle _topRectangle, Rectangle _bottomRectangle, Rectangle _leftRectangle, Rectangle _rightRectangle) + private const double MaxSpeed = 10.0f; + private const double SpaceResistance = 0.02f; + + public void CheckUserInput(Asteroid[] asteroids, Rectangle topRectangle, Rectangle bottomRectangle, Rectangle leftRectangle, Rectangle rightRectangle) { _localTick++; - if (_rotation >= 6.3f) + if (Rotation >= 6.3f) { - _rotation = 0.0f; + Rotation = 0.0f; } - if (Keyboard.GetState().IsKeyDown(Keys.D) && Keyboard.GetState().IsKeyDown(Keys.W) == false) - { - _rotation += 0.07f; - + if (Keyboard.GetState().IsKeyDown(Keys.D) && !Keyboard.GetState().IsKeyDown(Keys.W)) + { + Rotation += 0.07f; } - else if (Keyboard.GetState().IsKeyDown(Keys.A) && Keyboard.GetState().IsKeyDown(Keys.W) == false) - { - _rotation -= 0.07f; - + else if (Keyboard.GetState().IsKeyDown(Keys.A) && !Keyboard.GetState().IsKeyDown(Keys.W)) + { + Rotation -= 0.07f; } - + if (Keyboard.GetState().IsKeyDown(Keys.W)) { - if (_oldRotation != _rotation) + if (Math.Abs(_oldRotation - Rotation) > 0.01) { _speed = 0; } - _oldRotation = _rotation; - if (_speed < _maxSpeed) + _oldRotation = Rotation; + if (_speed < MaxSpeed) { _speed += 0.15f; - } + } Move(); ThrustParticle thrustParticle = new ThrustParticle(); - _thrustParticles.Add(thrustParticle); - thrustParticle.Spawn(_x, _y); + ThrustParticles.Add(thrustParticle); + thrustParticle.Spawn(XPosition, YPosition); } - else if (Keyboard.GetState().IsKeyDown(Keys.W) == false) + else if (!Keyboard.GetState().IsKeyDown(Keys.W)) { Drift(); - if (_speed >= 0) - { - _speed -= _spaceResistance; - } + if (_speed >= 0) + { + _speed -= SpaceResistance; + } else if (_speed < 1.2f && _speed != 0) { - _speed -= _spaceResistance * _speed; + _speed -= SpaceResistance * _speed; } } - if (Keyboard.GetState().IsKeyDown(Keys.Space) && _bulletTick + 25 < _localTick) + if (Keyboard.GetState().IsKeyDown(Keys.Space) && _bulletTick + 25 < _localTick) { _bulletTick = _localTick; Shoot(); } CheckCollision(asteroids); - Teleport(_topRectangle, _bottomRectangle, _leftRectangle, _rightRectangle); + Teleport(); _tickSinceLastTeleport++; } + public void CheckCollision(Asteroid[] asteroids) { - _shipBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 32, 32); - foreach (Asteroid asteroid in asteroids) + foreach (var asteroid in asteroids) { - if (this._shipBox.Intersects(asteroid._collisionBox)) + if (HitBox.Intersects(asteroid.HitBox)) { ShipDie(); } - } + } } - private void Drift() + + private void Drift() { - xVector = Math.Sin(_oldRotation); - yVector = Math.Cos(_oldRotation); - _x = _x + (xVector * _speed); - _y = _y - (yVector * _speed); - + _xVector = Math.Sin(_oldRotation); + _yVector = Math.Cos(_oldRotation); + XPosition += _xVector * _speed; + YPosition -= _yVector * _speed; + } - private void Move() + + private void Move() { - xVector = Math.Sin(_rotation); - yVector = Math.Cos(_rotation); - _x = _x + (xVector * _speed); - _y = _y - (yVector * _speed); - if (_thrustParticles.Count == 50) + _xVector = Math.Sin(Rotation); + _yVector = Math.Cos(Rotation); + XPosition += _xVector * _speed; + YPosition -= _yVector * _speed; + if (ThrustParticles.Count == 50) { - _thrustParticles.RemoveAt(0); + ThrustParticles.RemoveAt(0); } - - } - private void Shoot() + + private void Shoot() { - if (_nextBulletIndex == _shipBullets.Length) + if (_nextBulletIndex == ShipBullets.Length) { _nextBulletIndex = 0; } - _shipBullets[_nextBulletIndex].Shoot(ref _rotation, ref _speed, ref _x, ref _y); + ShipBullets[_nextBulletIndex].Shoot(Rotation, ref _speed, XPosition, YPosition); _nextBulletIndex++; } - private void ShipDie() + + private void ShipDie() { - _shipState = state.dead; - _x = -1000; - _y = -1000; - _shipBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 32, 32); - + ShipState = State.dead; + XPosition = -1000; + YPosition = -1000; } - private void Teleport(Rectangle _topRectangle, Rectangle _bottomRectangle, Rectangle _leftRectangle, Rectangle _rightRectangle) + + private void Teleport() { if (_tickSinceLastTeleport > 60) { - if (_x < -23) + if (XPosition < -23) { - _x = 723; + XPosition = 723; _tickSinceLastTeleport = 0; } - else if (_x > 723) + else if (XPosition > 723) { - _x = -23; + XPosition = -23; _tickSinceLastTeleport = 0; } - if (_y < -23) + if (YPosition < -23) { - _y = 723; + YPosition = 723; _tickSinceLastTeleport = 0; } - else if (_y > 723) + else if (YPosition > 723) { - _y = -23; + YPosition = -23; _tickSinceLastTeleport = 0; } } } - public void Resurrect() + + public void Resurrect() { - _shipState = state.alive; - _x = 350; - _y = 350; + ShipState = State.alive; + XPosition = 350; + YPosition = 350; _speed = 0; - _shipBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 32, 32); } - public bool CheckIfShipIsDead() + + public bool CheckIfShipIsDead() { - if (_shipState == state.alive) - { - return false; - } - else if (_shipState == state.dead) + if (ShipState == State.dead) { return true; } - else - { - return false; - } - } - private void Dash() - { - //Dash to the side when you dont have time to rotate and accelerate + return false; } - } -} + } +} \ No newline at end of file diff --git a/Game/SmallAsteroid.cs b/Game/SmallAsteroid.cs deleted file mode 100644 index 82b3e07..0000000 --- a/Game/SmallAsteroid.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.Xna.Framework; -using Sprack; - -namespace Sprack -{ - class SmallAsteroid - { - private double _x; - private double _y; - private double _speed; - private Single _rotation; - public Vector2 _rotationOrigin = new Vector2(4, 4); - public Single _spinning; - private float _spinningSpeed; - private int _width; - private int _height; - public Rectangle _collisionBox; - private double _unitVectorX; - private double _unitVectorY; - private Random randomGenerator = new Random(); - public void Spawn(double x, double y) - { - _x = x; - _y = y; - _rotation = Convert.ToSingle(randomGenerator.NextDouble()) + randomGenerator.Next(1, 6); - _speed = Convert.ToDouble(randomGenerator.NextDouble() + randomGenerator.Next(2, 6)); - double xVector; - double yVector; - double magnitude; - xVector = Math.Sin(_rotation); - yVector = Math.Cos(_rotation); - magnitude = Math.Sqrt(xVector * xVector + yVector * yVector); - _unitVectorX = xVector / magnitude; - _unitVectorY = yVector / magnitude; - if (randomGenerator.Next(0, 10) >= 5) - { - _spinningSpeed = Convert.ToSingle((randomGenerator.NextDouble() / 100) * (-1)); - } - else - { - _spinningSpeed = Convert.ToSingle(randomGenerator.NextDouble() / 100); - } - } - public void Behaviour(ref Bullet[] bullets) - { - _collisionBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), _width, _height); - - } - private void CheckCollision(ref Bullet[] bullets) - { - foreach (Bullet bullet in bullets) - { - if (_collisionBox.Intersects(bullet._bulletBox)) - { - bullet.PutToSleep(); - Explode(); - } - } - } - private void Explode() - { - //Teleport in snow particles that spin and travel in random directions before disappearing. - //And teleport this asteroid of screen and add to the players score. - } - - } -} diff --git a/Game/Sprite.cs b/Game/Sprite.cs new file mode 100644 index 0000000..4cdfc91 --- /dev/null +++ b/Game/Sprite.cs @@ -0,0 +1,34 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace Sprack.Game +{ + public abstract class Sprite + { + public double XPosition { get; set; } + public double YPosition { get; set; } + + public abstract Rectangle Shape { get; } + + public abstract Rectangle HitBox { get; } + + public Color Color => DefaultColor; + + private static readonly Color DefaultColor = + Color.White; + + public SpriteEffects Effect => DefaultEffect; + + private static readonly SpriteEffects DefaultEffect = + SpriteEffects.None; + + public float Depth => DefaultDepth; + + private static readonly float DefaultDepth = 0; + + public float Rotation { get; set; } + + public abstract Vector2 RotationOrigin { get; } + + } +} \ No newline at end of file diff --git a/Game/Star.cs b/Game/Star.cs index 1a5dee5..154c954 100644 --- a/Game/Star.cs +++ b/Game/Star.cs @@ -1,34 +1,39 @@ using System; -using System.Collections.Generic; -using System.Text; using Microsoft.Xna.Framework; -using Sprack; -namespace Sprack +namespace Sprack.Game { - public class Star + public class Star : Sprite { - public Rectangle _hitBox; - public Rectangle drawRectangle = new Rectangle(23, 5, 1, 1); - public double _x; - public double _y; - private double _speed; - private Random random = new Random(); - public Star() + public override Rectangle Shape => StarShape; + + private static readonly Rectangle StarShape = + new Rectangle(23, 5, 1, 1); + + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 4, 4); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(0, 0); + + private readonly double _speed; + private readonly Random _random = new Random(); + + public Star() { - _speed = random.NextDouble() + random.Next(1, 3); - _x = random.Next(0, 700); - + _speed = _random.NextDouble() + _random.Next(1, 3); + XPosition = _random.Next(0, 700); } - public void Behaviour() + + public void Behaviour() { - _x += _speed; - if (_x > 700) + XPosition += _speed; + if (XPosition > 700) { - _x = -2; + XPosition = -2; } - _hitBox = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 4, 4); } - } } diff --git a/Game/State.cs b/Game/State.cs new file mode 100644 index 0000000..69b7435 --- /dev/null +++ b/Game/State.cs @@ -0,0 +1,8 @@ +namespace Sprack.Game +{ + public enum State + { + dead, + alive + } +} \ No newline at end of file diff --git a/Game/ThrustParticle.cs b/Game/ThrustParticle.cs index bab5604..fb3c2ec 100644 --- a/Game/ThrustParticle.cs +++ b/Game/ThrustParticle.cs @@ -1,29 +1,24 @@ using System; -using System.Collections.Generic; -using System.Text; using Microsoft.Xna.Framework; -using Sprack; -namespace Sprack +namespace Sprack.Game { //Particles shot out of the ships thrusters. public class ThrustParticle : Particle { - public Rectangle RectangleBox - { - get - { - return new Rectangle(Convert.ToInt32(base._x), Convert.ToInt32(base._y), 16, 16); - } - } + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 16, 16); + + public override Vector2 RotationOrigin => RotationOriginSpin; + private static readonly Vector2 RotationOriginSpin = + new Vector2(1, 1); - public ThrustParticle() + public ThrustParticle() { - base._spin = true; - base._rotationOrigin = new Vector2(1, 1); - base._lifeSpan = 15; - base._rotationSpeed = 0.1f; + Spin = true; + LifeSpan = 15; + RotationSpeed = 0.1f; } } } diff --git a/Game/UFO.cs b/Game/UFO.cs index 149941c..756f12c 100644 --- a/Game/UFO.cs +++ b/Game/UFO.cs @@ -1,115 +1,112 @@ using System; -using System.Collections.Generic; -using System.Text; -using System.Diagnostics; using Microsoft.Xna.Framework; -namespace Sprack +namespace Sprack.Game { - public class UFO + public class Ufo : Sprite { - public UFO() + private readonly Sprite _ship; + public override Rectangle Shape => + Draw(_ship.XPosition, _ship.YPosition); + public override Rectangle HitBox => + new Rectangle(Convert.ToInt32(XPosition), Convert.ToInt32(YPosition), 64, 64); + + public override Vector2 RotationOrigin => RotationOriginSpin; + + private static readonly Vector2 RotationOriginSpin = + new Vector2(0, 4); + + public Ufo(Sprite ship) { - for (int i = 0; i < 4; i++) + XPosition = -100; + YPosition = -100; + _ship = ship; + for (int i = 0; i < Rockets.Length; i++) { Rocket rocket = new Rocket(); - rockets[i] = rocket; + Rockets[i] = rocket; } } - public Rocket[] rockets = new Rocket[4]; - Random random = new Random(); - public double _x = -100, _y = -100; - private double _xAccelerationAmount = 0.05f; + + public Rocket[] Rockets = new Rocket[4]; + private readonly Random _random = new Random(); + private const double XAccelerationAmount = 0.05f; private double _xSpeed; - public Rectangle _rectangle; - private int _spriteWidth = 16, _spriteHeight = 16; - public Rectangle _drawRectangle; - public enum State { aliveLeft, aliveRight, dead} + private const int SpriteWidth = 16; + private const int SpriteHeight = 16; + + public State CurrentState = State.dead; - private int frame = 0; - private int ticksSinceTeleport; - public void Spawn() + + private int _ticksSinceTeleport; + + public void Spawn() { - _y = random.Next(75, 625); - _x = -64; - CurrentState = State.aliveLeft; + YPosition = _random.Next(75, 625); + XPosition = -64; + CurrentState = State.alive; } - public void Behaviour(Bullet[] bullets, double shipX, double shipY) + + public void Behaviour(Bullet[] bullets, double shipX, double shipY) { - frame++; Movement(shipX); - Draw(shipX, shipY); - _rectangle = new Rectangle(Convert.ToInt32(_x), Convert.ToInt32(_y), 64, 64); - } - private void Shoot(double shipX, double shipY) + + private Rectangle Draw(double shipX, double shipY) { - Single rocketRotation = (Convert.ToSingle(_x - shipX / _y - shipY)); - } - private void Die() - { - CurrentState = State.dead; - } - private void Draw(double shipX, double shipY) - { - if (shipX > _x + 8) + if (shipX > XPosition + 8) { - if (shipY < _y + 8 ) - { - _drawRectangle = new Rectangle(32, 105, _spriteWidth, _spriteHeight ); - } - else + if (shipY < YPosition + 8) { - _drawRectangle = new Rectangle(0, 105, _spriteWidth, _spriteHeight); + return new Rectangle(32, 105, SpriteWidth, SpriteHeight); } + + return new Rectangle(0, 105, SpriteWidth, SpriteHeight); } - else + + if (shipY < YPosition + 8) { - if (shipY < _y + 8) - { - _drawRectangle = new Rectangle(48, 105, _spriteWidth, _spriteHeight); - } - else - { - _drawRectangle = new Rectangle(16, 105, _spriteWidth, _spriteHeight); - } - } + return new Rectangle(48, 105, SpriteWidth, SpriteHeight); + } + + return new Rectangle(16, 105, SpriteWidth, SpriteHeight); } - private void Movement(double shipX) + + private void Movement(double shipX) { - if (shipX > _x) + if (shipX > XPosition) { - if (_xSpeed < 3.5f) + if (_xSpeed < 3.5f) { - _xSpeed += _xAccelerationAmount; + _xSpeed += XAccelerationAmount; } } - else if (shipX < _x) + else if (shipX < XPosition) { - if (_xSpeed > -3.5f) + if (_xSpeed > -3.5f) { - _xSpeed -= _xAccelerationAmount; + _xSpeed -= XAccelerationAmount; } } - _x += _xSpeed; + XPosition += _xSpeed; Teleport(); - ticksSinceTeleport++; + _ticksSinceTeleport++; } - private void Teleport() + + private void Teleport() { - if (ticksSinceTeleport > 60) + if (_ticksSinceTeleport > 60) { - if (_x > 700) + if (XPosition > 700) { - _x = -64; - ticksSinceTeleport = 0; + XPosition = -64; + _ticksSinceTeleport = 0; } - else if (_x + 64 < 0) + else if (XPosition + 64 < 0) { - _x = 700; - ticksSinceTeleport = 0; + XPosition = 700; + _ticksSinceTeleport = 0; } - } } } diff --git a/Program.cs b/Program.cs index 68a7b3f..6af2866 100644 --- a/Program.cs +++ b/Program.cs @@ -1,14 +1,15 @@ using System; +using Sprack.Game; -namespace Game_Game +namespace Sprack { public static class Program { [STAThread] static void Main() { - using (var game = new Sprack.Game1()) - game.Run(); + using var game = new Game1(); + game.Run(); } } -} +} \ No newline at end of file