Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Game Game.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sprack/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
35 changes: 0 additions & 35 deletions Game/Amogus.cs

This file was deleted.

152 changes: 75 additions & 77 deletions Game/Asteroid.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}
}
}
}
71 changes: 36 additions & 35 deletions Game/Bullet.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
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;
private double _unitVectorX;
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;
}

}
}
}
Loading