Created
August 9, 2012 01:58
-
-
Save TinkerWorX/3300280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EntityPhysicsSystem : GameComponent | |
{ | |
private readonly HashSet<Entity> entities; | |
public EntityPhysicsSystem(Game game, HashSet<Entity> entities) | |
: base(game) | |
{ | |
this.entities = entities; | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
foreach (var entity in this.entities.ToArray()) | |
{ | |
PhysicsComponent physics; | |
if (!entity.TryGetComponent(out physics)) | |
continue; | |
physics.Position += physics.Velocity * (Single)gameTime.ElapsedGameTime.TotalSeconds; | |
if (physics.IsAffectedByDrag) | |
{ | |
physics.Velocity.X -= Math.Sign(physics.Velocity.X) * 1.00f; | |
if (Math.Abs(physics.Velocity.X) < 1.00f) | |
physics.Velocity.X = 0.00f; | |
physics.Velocity.Y -= Math.Sign(physics.Velocity.Y) * 1.00f; | |
if (Math.Abs(physics.Velocity.Y) < 1.00f) | |
physics.Velocity.Y = 0.00f; | |
} | |
} | |
} | |
} | |
public class EntityRenderSystem : DrawableGameComponent | |
{ | |
private readonly HashSet<Entity> entities; | |
private SpriteBatch spriteBatch; | |
public EntityRenderSystem(Game game, HashSet<Entity> entities) | |
: base(game) | |
{ | |
this.entities = entities; | |
} | |
public override void Initialize() | |
{ | |
this.spriteBatch = new SpriteBatch(this.Game.GraphicsDevice); | |
base.Initialize(); | |
} | |
public override void Draw(GameTime gameTime) | |
{ | |
this.spriteBatch.Begin(); | |
foreach (var entity in this.entities.ToArray()) | |
{ | |
SpriteComponent sprite; | |
if (!entity.TryGetComponent(out sprite)) | |
continue; | |
PhysicsComponent physics; | |
if (!entity.TryGetComponent(out physics)) | |
continue; | |
var texture = this.Game.Content.Load<Texture2D>(sprite.Source); | |
this.spriteBatch.Draw(texture, physics.Position, null, Color.White, physics.Facing, new Vector2(texture.Width / 2.00f, texture.Height / 2.00f), Vector2.One, SpriteEffects.None, 0.00f); | |
} | |
this.spriteBatch.End(); | |
} | |
} | |
public class EntityInputSystem : GameComponent | |
{ | |
private readonly HashSet<Entity> entities; | |
private readonly EntityFactory factory; | |
public EntityInputSystem(Game game, HashSet<Entity> entities) | |
: base(game) | |
{ | |
this.entities = entities; | |
this.factory = (EntityFactory)this.Game.Services.GetService(typeof(EntityFactory)); | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
foreach (var entity in this.entities.ToArray()) | |
{ | |
InputComponent input; | |
if (!entity.TryGetComponent(out input)) | |
continue; | |
PhysicsComponent physics; | |
if (!entity.TryGetComponent(out physics)) | |
continue; | |
var mouse = Mouse.GetState(); | |
var keyboard = Keyboard.GetState(); | |
const Single turn_rate = MathHelper.Pi; | |
if (keyboard.IsKeyDown(input.TurnLeft) && keyboard.IsKeyUp(input.TurnRight)) | |
{ | |
physics.Facing -= turn_rate * (Single)gameTime.ElapsedGameTime.TotalSeconds; | |
} | |
if (keyboard.IsKeyDown(input.TurnRight) && keyboard.IsKeyUp(input.TurnLeft)) | |
{ | |
physics.Facing += turn_rate * (Single)gameTime.ElapsedGameTime.TotalSeconds; | |
} | |
const Single acceleration = 10.00f; | |
if (keyboard.IsKeyDown(input.Forward) && keyboard.IsKeyUp(input.Reverse)) | |
{ | |
physics.Velocity.X += (Single)Math.Cos(physics.Facing) * acceleration; | |
physics.Velocity.Y += (Single)Math.Sin(physics.Facing) * acceleration; | |
} | |
if (keyboard.IsKeyDown(input.Reverse) && keyboard.IsKeyUp(input.Forward)) | |
{ | |
physics.Velocity.X -= (Single)Math.Cos(physics.Facing) * acceleration; | |
physics.Velocity.Y -= (Single)Math.Sin(physics.Facing) * acceleration; | |
} | |
if (keyboard.IsKeyDown(input.StrafeLeft) && keyboard.IsKeyUp(input.StrafeRight)) | |
{ | |
var direction = physics.Facing - MathHelper.PiOver2; | |
var vector = new Vector2((Single)Math.Cos(direction), (Single)Math.Sin(direction)); | |
physics.Velocity += vector * acceleration; | |
} | |
if (keyboard.IsKeyDown(input.StrafeRight) && keyboard.IsKeyUp(input.StrafeLeft)) | |
{ | |
var direction = physics.Facing + MathHelper.PiOver2; | |
var vector = new Vector2((Single)Math.Cos(direction), (Single)Math.Sin(direction)); | |
physics.Velocity += vector * acceleration; | |
} | |
physics.Direction = physics.Velocity; | |
physics.Direction.Normalize(); | |
const Single speed = 256.00f; | |
if (physics.Velocity.LengthSquared() > speed * speed) | |
{ | |
physics.Velocity = physics.Direction * speed; | |
} | |
CooldownComponent cooldown; | |
if (entity.TryGetComponent(out cooldown)) | |
{ | |
if (cooldown.Value > 0.00f) | |
cooldown.Value -= (Single)gameTime.ElapsedGameTime.TotalSeconds; | |
} | |
else | |
{ | |
entity.AddComponent(new CooldownComponent()); | |
} | |
if (keyboard.IsKeyDown(input.Shoot) && cooldown.Value <= 0.00f) | |
{ | |
var bullet = this.factory.Create("Bullet"); | |
PhysicsComponent bullet_physics; | |
if (bullet.TryGetComponent(out bullet_physics)) | |
{ | |
bullet_physics.Position = physics.Position; | |
bullet_physics.Facing = physics.Facing; | |
bullet_physics.Velocity.X = (Single)Math.Cos(physics.Facing) * 512; | |
bullet_physics.Velocity.Y = (Single)Math.Sin(physics.Facing) * 512; | |
bullet_physics.Direction = bullet_physics.Velocity; | |
bullet_physics.Direction.Normalize(); | |
this.entities.Add(bullet); | |
cooldown.Value = 0.20f; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment