started a monogame extended space game demo

This commit is contained in:
Dylan Wilson
2015-12-30 21:41:08 +10:00
parent f78b8aec9a
commit 56b70daed3
33 changed files with 1004 additions and 6 deletions
+34
View File
@@ -25,31 +25,65 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Extended.Tests", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Extended.Content.Pipeline.Tests", "MonoGame.Extended.Content.Pipeline.Tests\MonoGame.Extended.Content.Pipeline.Tests.csproj", "{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceGame", "SpaceGame\SpaceGame.csproj", "{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}"
ProjectSection(ProjectDependencies) = postProject
{28CD24BD-432C-4987-9E9D-65CFCF120EA0} = {28CD24BD-432C-4987-9E9D-65CFCF120EA0}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Debug|x86.ActiveCfg = Debug|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Debug|x86.Build.0 = Debug|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Release|Any CPU.Build.0 = Release|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Release|x86.ActiveCfg = Release|Any CPU
{41724C52-3D50-45BB-81EB-3C8A247EAFD1}.Release|x86.Build.0 = Release|Any CPU
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Debug|Any CPU.ActiveCfg = Debug|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Debug|Any CPU.Build.0 = Debug|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Debug|x86.ActiveCfg = Debug|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Debug|x86.Build.0 = Debug|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Release|Any CPU.ActiveCfg = Release|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Release|x86.ActiveCfg = Release|x86
{B7965D5F-83D2-4C04-B9FE-7D5FBF365F93}.Release|x86.Build.0 = Release|x86
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Debug|x86.ActiveCfg = Debug|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Debug|x86.Build.0 = Debug|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Release|Any CPU.Build.0 = Release|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Release|x86.ActiveCfg = Release|Any CPU
{28CD24BD-432C-4987-9E9D-65CFCF120EA0}.Release|x86.Build.0 = Release|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Debug|x86.ActiveCfg = Debug|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Debug|x86.Build.0 = Debug|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Release|Any CPU.Build.0 = Release|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Release|x86.ActiveCfg = Release|Any CPU
{CEF2144F-7E98-45A5-BE08-E6DBC307A5C1}.Release|x86.Build.0 = Release|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Debug|x86.ActiveCfg = Debug|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Debug|x86.Build.0 = Debug|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Release|Any CPU.Build.0 = Release|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Release|x86.ActiveCfg = Release|Any CPU
{75D76507-9CB0-4A60-BE46-696DF6E7AA6C}.Release|x86.Build.0 = Release|Any CPU
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Debug|Any CPU.ActiveCfg = Debug|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Debug|Any CPU.Build.0 = Debug|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Debug|x86.ActiveCfg = Debug|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Debug|x86.Build.0 = Debug|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Release|Any CPU.ActiveCfg = Release|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Release|x86.ActiveCfg = Release|x86
{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -24,8 +24,8 @@ namespace MonoGame.Extended.Sprites
public static void Draw(this SpriteBatch spriteBatch, SpriteSheetAnimator animator)
{
if (animator == null) throw new ArgumentNullException("animator");
if (animator.Sprite != null)
if (animator.IsPlaying && animator.Sprite != null)
Draw(spriteBatch, animator.Sprite);
}
@@ -144,12 +144,13 @@ namespace MonoGame.Extended.Sprites
public void PlayAnimation(string name, Action onCompleteAction = null)
{
if(_currentAnimation != null && _currentAnimation.Name == name)
if(_currentAnimation != null && _currentAnimation.Name == name && IsPlaying)
return;
_currentAnimation = _animations[name];
_frameIndex = 0;
_onCompleteAction = onCompleteAction;
IsPlaying = true;
}
public void Update(GameTime gameTime)
@@ -166,9 +167,9 @@ namespace MonoGame.Extended.Sprites
if (_frameIndex >= _currentAnimation.FrameIndicies.Length)
{
if (IsLooping)
_frameIndex = 0;
else
_frameIndex = 0;
if (!IsLooping)
IsPlaying = false;
var onCompleteAction = _onCompleteAction;
+34
View File
@@ -0,0 +1,34 @@
using Microsoft.Xna.Framework;
using MonoGame.Extended.TextureAtlases;
using SpaceGame.Entities;
namespace SpaceGame
{
public interface IBulletFactory
{
void Create(Vector2 position, Vector2 direction, float rotation, float speed);
}
public class BulletFactory : IBulletFactory
{
private readonly IEntityManager _entityManager;
private readonly TextureRegion2D _bulletRegion;
public BulletFactory(IEntityManager entityManager, TextureRegion2D bulletRegion)
{
_entityManager = entityManager;
_bulletRegion = bulletRegion;
}
public void Create(Vector2 position, Vector2 direction, float rotation, float speed)
{
var velocity = direction * speed;
var laser = new Laser(_bulletRegion, velocity)
{
Position = position,
Rotation = rotation
};
_entityManager.AddEntity(laser);
}
}
}
+194
View File
@@ -0,0 +1,194 @@
#----------------------------- Global Properties ----------------------------#
/outputDir:bin
/intermediateDir:obj
/platform:Windows
/config:
/profile:Reach
/compress:False
#-------------------------------- References --------------------------------#
/reference:..\..\MonoGame.Extended.Content.Pipeline\bin\Debug\MonoGame.Extended.Content.Pipeline.dll
#---------------------------------- Content ---------------------------------#
#begin playerShip1_blue.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:playerShip1_blue.png
#begin meteorBrown_big1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_big1.png
#begin laserBlue03.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:laserBlue03.png
#begin meteorBrown_big2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_big2.png
#begin meteorBrown_big3.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_big3.png
#begin meteorBrown_big4.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_big4.png
#begin meteorBrown_med1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_med1.png
#begin meteorBrown_med3.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_med3.png
#begin meteorBrown_small1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_small1.png
#begin meteorBrown_small2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_small2.png
#begin meteorBrown_tiny1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_tiny1.png
#begin meteorBrown_tiny2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:meteorBrown_tiny2.png
#begin black.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:black.png
#begin explosion-animations.aa
/importer:AstridAnimatorImporter
/processor:AstridAnimatorProcessor
/build:explosion-animations.aa
#begin regular-explosion.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:regular-explosion.png
#begin regular-explosion-sheet.json
/importer:TexturePackerJsonImporter
/processor:TexturePackerProcessor
/build:regular-explosion-sheet.json
Binary file not shown.

After

Width:  |  Height:  |  Size: 610 B

@@ -0,0 +1,20 @@
{
"TextureAtlas": "regular-explosion-sheet.json",
"Animations": [
{
"Name": "explode",
"FramesPerSecond": 8,
"Frames": [
"regularExplosion00",
"regularExplosion01",
"regularExplosion02",
"regularExplosion03",
"regularExplosion04",
"regularExplosion05",
"regularExplosion06",
"regularExplosion07",
"regularExplosion08"
]
}
]
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 982 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 881 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 692 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 461 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 371 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

@@ -0,0 +1,93 @@
{"frames": [
{
"filename": "regularExplosion00.png",
"frame": {"x":2,"y":2,"w":192,"h":192},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":192,"h":192},
"sourceSize": {"w":192,"h":192},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion01.png",
"frame": {"x":196,"y":2,"w":152,"h":150},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":152,"h":150},
"sourceSize": {"w":152,"h":150},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion02.png",
"frame": {"x":350,"y":2,"w":82,"h":91},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":82,"h":91},
"sourceSize": {"w":82,"h":91},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion03.png",
"frame": {"x":2,"y":196,"w":92,"h":102},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":92,"h":102},
"sourceSize": {"w":92,"h":102},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion04.png",
"frame": {"x":96,"y":196,"w":120,"h":124},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":120,"h":124},
"sourceSize": {"w":120,"h":124},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion05.png",
"frame": {"x":218,"y":196,"w":133,"h":134},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":133,"h":134},
"sourceSize": {"w":133,"h":134},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion06.png",
"frame": {"x":2,"y":332,"w":138,"h":140},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":138,"h":140},
"sourceSize": {"w":138,"h":140},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion07.png",
"frame": {"x":142,"y":332,"w":143,"h":144},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":143,"h":144},
"sourceSize": {"w":143,"h":144},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "regularExplosion08.png",
"frame": {"x":287,"y":332,"w":149,"h":151},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":149,"h":151},
"sourceSize": {"w":149,"h":151},
"pivot": {"x":0.5,"y":0.5}
}],
"meta": {
"app": "http://www.codeandweb.com/texturepacker",
"version": "1.0",
"image": "regular-explosion.png",
"format": "RGBA8888",
"size": {"w":438,"h":485},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:98ae692ca0a173e85675618865346a23:050419c81994b49a0c48dc01e2d1b84c:0d3d2cb5875b2c23d78b2722385f0925$"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

+23
View File
@@ -0,0 +1,23 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace SpaceGame.Entities
{
public abstract class Entity
{
protected Entity()
{
IsDestroyed = false;
}
public abstract void Update(GameTime gameTime);
public abstract void Draw(SpriteBatch spriteBatch);
public bool IsDestroyed { get; private set; }
public virtual void Destroy()
{
IsDestroyed = true;
}
}
}
+34
View File
@@ -0,0 +1,34 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Sprites;
namespace SpaceGame.Entities
{
public class Explosion : Entity
{
private readonly SpriteSheetAnimator _animator;
public Explosion(SpriteSheetAnimator animator, Vector2 position)
{
_animator = animator;
_animator.Sprite.Position = position;
_animator.IsLooping = false;
_animator.PlayAnimation("explode", Destroy);
}
public override void Destroy()
{
base.Destroy();
}
public override void Update(GameTime gameTime)
{
_animator.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_animator);
}
}
}
+54
View File
@@ -0,0 +1,54 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
namespace SpaceGame.Entities
{
public class Laser : Entity
{
public Laser(TextureRegion2D textureRegion, Vector2 velocity)
{
_timeToLive = 1.0f;
_sprite = new Sprite(textureRegion)
{
Scale = Vector2.One*0.5f
};
Velocity = velocity;
}
private readonly Sprite _sprite;
private float _timeToLive;
public Vector2 Position
{
get { return _sprite.Position; }
set { _sprite.Position = value; }
}
public float Rotation
{
get { return _sprite.Rotation; }
set { _sprite.Rotation = value; }
}
public Vector2 Velocity { get; set; }
public override void Update(GameTime gameTime)
{
var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
Position += Velocity * deltaTime;
_timeToLive -= deltaTime;
if (_timeToLive <= 0)
Destroy();
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_sprite);
}
}
}
+55
View File
@@ -0,0 +1,55 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Shapes;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
namespace SpaceGame.Entities
{
public class Meteor : Entity
{
public Meteor(TextureRegion2D textureRegion, Vector2 velocity)
{
_sprite = new Sprite(textureRegion);
Shape = new CircleF(_sprite.Position, _radius);
Velocity = velocity;
HealthPoints = 10;
}
private const float _radius = 55f;
private readonly Sprite _sprite;
public CircleF Shape { get; private set; }
public int HealthPoints { get; private set; }
public Vector2 Velocity { get; set; }
public Vector2 Position
{
get { return _sprite.Position; }
set
{
_sprite.Position = value;
Shape = new CircleF(_sprite.Position, _radius);
}
}
public void Damage(int damage)
{
HealthPoints -= damage;
if (HealthPoints <= 0)
Destroy();
}
public override void Update(GameTime gameTime)
{
var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
Position += Velocity * deltaTime;
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_sprite);
}
}
}
+83
View File
@@ -0,0 +1,83 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
namespace SpaceGame.Entities
{
public class Spaceship : Entity
{
private readonly IBulletFactory _bulletFactory;
public Spaceship(TextureRegion2D textureRegion, IBulletFactory bulletFactory)
{
_bulletFactory = bulletFactory;
_sprite = new Sprite(textureRegion)
{
Scale = Vector2.One * 0.5f,
Position = new Vector2(400, 240)
};
}
private readonly Sprite _sprite;
public Vector2 Velocity { get; set; }
public Vector2 Position
{
get { return _sprite.Position; }
set { _sprite.Position = value; }
}
public float Rotation
{
get { return _sprite.Rotation - MathHelper.ToRadians(90); }
set { _sprite.Rotation = value + MathHelper.ToRadians(90); }
}
public Vector2 Direction => Vector2.UnitX.Rotate(Rotation);
private float _fireCooldown;
public override void Update(GameTime gameTime)
{
var deltaTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
Position += Velocity * deltaTime;
Velocity *= 0.98f;
if (_fireCooldown > 0)
_fireCooldown -= deltaTime;
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_sprite);
}
public void Accelerate(float acceleration)
{
Velocity += Direction * acceleration;
}
public void LookAt(Vector2 point)
{
Rotation = (float) Math.Atan2(point.Y - Position.Y, point.X - Position.X);
}
public void Fire()
{
if (_fireCooldown > 0)
return;
var angle = Rotation + MathHelper.ToRadians(90);
var muzzle1Position = Position + new Vector2(14, 0).Rotate(angle);
var muzzle2Position = Position + new Vector2(-14, 0).Rotate(angle);
_bulletFactory.Create(muzzle1Position, Direction, angle, 550f);
_bulletFactory.Create(muzzle2Position, Direction, angle, 550f);
_fireCooldown = 0.2f;
}
}
}
+45
View File
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpaceGame.Entities;
namespace SpaceGame
{
public interface IEntityManager
{
T AddEntity<T>(T entity) where T : Entity;
}
public class EntityManager : IEntityManager
{
public EntityManager()
{
_entities = new List<Entity>();
}
private readonly List<Entity> _entities;
public IEnumerable<Entity> Entities => _entities;
public T AddEntity<T>(T entity) where T : Entity
{
_entities.Add(entity);
return entity;
}
public void Update(GameTime gameTime)
{
foreach (var entity in _entities.Where(e => !e.IsDestroyed))
entity.Update(gameTime);
_entities.RemoveAll(e => e.IsDestroyed);
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (var entity in _entities.Where(e => !e.IsDestroyed))
entity.Draw(spriteBatch);
}
}
}
+153
View File
@@ -0,0 +1,153 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.Sprites;
using MonoGame.Extended.TextureAtlases;
using MonoGame.Extended.ViewportAdapters;
using SpaceGame.Entities;
namespace SpaceGame
{
public class GameMain : Game
{
// ReSharper disable once NotAccessedField.Local
private GraphicsDeviceManager _graphicsDeviceManager;
private SpriteBatch _spriteBatch;
private Texture2D _backgroundTexture;
private Spaceship _player;
private readonly Random _random;
private readonly EntityManager _entityManager;
private Camera2D _camera;
public GameMain()
{
_random = new Random();
_entityManager = new EntityManager();
_graphicsDeviceManager = new GraphicsDeviceManager(this);
//{
//IsFullScreen = true
//};
//_graphicsDeviceManager.ApplyChanges();
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
IsMouseVisible = true;
}
protected override void LoadContent()
{
_viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480);
_camera = new Camera2D(_viewportAdapter);
_spriteBatch = new SpriteBatch(GraphicsDevice);
var workingFolder = Directory.GetCurrentDirectory();
_backgroundTexture = Content.Load<Texture2D>("black");
var bulletTexture = Content.Load<Texture2D>("laserBlue03");
var bulletRegion = new TextureRegion2D(bulletTexture);
var bulletFactory = new BulletFactory(_entityManager, bulletRegion);
var spaceshipTexture = Content.Load<Texture2D>("playerShip1_blue");
var spaceshipRegion = new TextureRegion2D(spaceshipTexture);
_player = _entityManager.AddEntity(new Spaceship(spaceshipRegion, bulletFactory));
var meteorTexture = Content.Load<Texture2D>("meteorBrown_big1");
var meteorRegion = new TextureRegion2D(meteorTexture);
for (var i = 0; i < 5; i++)
{
var x = _random.Next(2) == 0 ? 1 : -1;
var y = _random.Next(2) == 0 ? 1 : -1;
var velocity = new Vector2(x, y) * 10f;
var meteor = new Meteor(meteorRegion, velocity) {Position = new Vector2(100, 100) };
_entityManager.AddEntity(meteor);
}
}
protected override void UnloadContent()
{
}
private MouseState _previousMouseState;
private ViewportAdapter _viewportAdapter;
protected override void Update(GameTime gameTime)
{
var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
var keyboardState = Keyboard.GetState();
var mouseState = Mouse.GetState();
if (keyboardState.IsKeyDown(Keys.Escape))
Exit();
const float acceleration = 5f;
if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up))
_player.Accelerate(acceleration);
if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down))
_player.Accelerate(-acceleration);
if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left))
_player.Rotation -= deltaTime * 3f;
if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right))
_player.Rotation += deltaTime * 3f;
if (keyboardState.IsKeyDown(Keys.Space) || mouseState.LeftButton == ButtonState.Pressed)
_player.Fire();
if(_previousMouseState.X != mouseState.X || _previousMouseState.Y != mouseState.Y)
_player.LookAt(_camera.ScreenToWorld(new Vector2(mouseState.X, mouseState.Y)));
_entityManager.Update(gameTime);
foreach (var laser in _entityManager.Entities.Where(e => e is Laser).Cast<Laser>().ToArray())
{
foreach (var meteor in _entityManager.Entities.Where(e => e is Meteor).Cast<Meteor>().ToArray())
{
if (meteor.Shape.Contains(laser.Position))
{
meteor.Damage(1);
laser.Destroy();
var animator = Content.Load<SpriteSheetAnimator>("explosion-animations");
_entityManager.AddEntity(new Explosion(animator, meteor.Position));
return;
}
}
}
_previousMouseState = mouseState;
_camera.LookAt(_player.Position + _player.Velocity * 0.2f);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
var sourceRectangle = new Rectangle(0, 0, _viewportAdapter.VirtualWidth, _viewportAdapter.VirtualHeight);
sourceRectangle.Offset(_camera.Position);
_spriteBatch.Begin(samplerState: SamplerState.LinearWrap, transformMatrix: _camera.GetViewMatrix());
_spriteBatch.Draw(_backgroundTexture, _camera.Position, sourceRectangle, Color.White);
_spriteBatch.End();
_spriteBatch.Begin(samplerState: SamplerState.LinearClamp, transformMatrix: _camera.GetViewMatrix());
_entityManager.Draw(_spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

+22
View File
@@ -0,0 +1,22 @@
using System;
namespace SpaceGame
{
#if WINDOWS || LINUX
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (var game = new GameMain())
game.Run();
}
}
#endif
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SpaceGame")]
[assembly: AssemblyProduct("SpaceGame")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8d113802-8e43-4c89-a06e-a7d0625f70e6")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.5.0.629")]
[assembly: AssemblyFileVersion("3.5.0.629")]
+113
View File
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D5314E38-8F5A-48A5-A092-BD8BBEACB5DD}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SpaceGame</RootNamespace>
<AssemblyName>SpaceGame</AssemblyName>
<FileAlignment>512</FileAlignment>
<MonoGamePlatform>WindowsGL</MonoGamePlatform>
<MonoGameContentBuilderExe>
</MonoGameContentBuilderExe>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\WindowsGL\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;WINDOWS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\WindowsGL\Release\</OutputPath>
<DefineConstants>TRACE;WINDOWS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Compile Include="BulletFactory.cs" />
<Compile Include="Entities\Entity.cs" />
<Compile Include="Entities\Explosion.cs" />
<Compile Include="Entities\Laser.cs" />
<Compile Include="Entities\Meteor.cs" />
<Compile Include="Entities\Spaceship.cs" />
<Compile Include="EntityManager.cs" />
<Compile Include="GameMain.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGame.Framework, Version=3.4.0.459, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Framework.WindowsGL.3.4.0.459\lib\net40\MonoGame.Framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NVorbis">
<HintPath>$(MSBuildProgramFiles32)\MonoGame\v3.0\Assemblies\WindowsGL\NVorbis.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\MonoGame.Framework.WindowsGL.3.4.0.459\lib\net40\OpenTK.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\black.png" />
<Content Include="Content\laserBlue03.png" />
<Content Include="Content\meteorBrown_big1.png" />
<Content Include="Content\meteorBrown_big2.png" />
<Content Include="Content\meteorBrown_big3.png" />
<Content Include="Content\meteorBrown_big4.png" />
<Content Include="Content\meteorBrown_med1.png" />
<Content Include="Content\meteorBrown_med3.png" />
<Content Include="Content\meteorBrown_small1.png" />
<Content Include="Content\meteorBrown_small2.png" />
<Content Include="Content\meteorBrown_tiny1.png" />
<Content Include="Content\meteorBrown_tiny2.png" />
<Content Include="Content\playerShip1_blue.png" />
<Content Include="Content\regular-explosion.png" />
<Content Include="Icon.ico" />
</ItemGroup>
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\explosion-animations.aa" />
<Content Include="Content\regular-explosion-sheet.json" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonoGame.Extended\MonoGame.Extended.csproj">
<Project>{41724c52-3d50-45bb-81eb-3c8a247eafd1}</Project>
<Name>MonoGame.Extended</Name>
</ProjectReference>
</ItemGroup>
<!-- Specify the search path explicitly so the ResolveAssemblyReferences task picks the right ones -->
<Target Name="BeforeResolveReferences">
<CreateProperty Value="$(MSBuildProgramFiles32)\MonoGame\v3.0\Assemblies\Windows;$(AssemblySearchPaths)">
<Output TaskParameter="Value" PropertyName="AssemblySearchPaths" />
</CreateProperty>
</Target>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MonoGame.Framework.WindowsGL" version="3.4.0.459" targetFramework="net40" />
</packages>