Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mcgrue/demon_door
Conflicts: DemonDoor/DemonDoor/Game1.cs
- Loading branch information
Showing
21 changed files
with
418 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
| ||
using FarseerPhysics.Collision.Shapes; | ||
using FarseerPhysics.Dynamics; | ||
using FarseerPhysics.Dynamics.Contacts; | ||
using Microsoft.Xna.Framework; | ||
|
||
using XNAVERGE; | ||
using System; | ||
|
||
namespace DemonDoor | ||
{ | ||
|
||
class CopController : IDrawableThing, ICollidable, IBrainyThing | ||
{ | ||
CopSprite copSprite = null; | ||
Vector2 screen; | ||
|
||
private Body _fsBody; | ||
private Shape _fsShape; | ||
private Fixture _fsFixture; | ||
private World _world; | ||
|
||
private enum BehaviorState | ||
{ | ||
Flying, Walking, Dead, Aiming, Shooting | ||
} | ||
private BehaviorState behaviorState = BehaviorState.Flying; | ||
|
||
public CopController( World w, Vector2 r0, CopSprite sprite ) | ||
{ | ||
_world = w; | ||
|
||
_fsBody = w.NewBody(); | ||
_fsBody.BodyType = BodyType.Dynamic; | ||
_fsBody.Position = r0; | ||
|
||
copSprite = sprite; | ||
|
||
MakeLivingFixture(); | ||
} | ||
|
||
private void MakeLivingFixture() | ||
{ | ||
if (_fsFixture != null) | ||
{ | ||
_fsBody.DestroyFixture(_fsFixture); | ||
} | ||
|
||
_fsShape = new CircleShape(1f, 1.0f); | ||
_fsFixture = _fsBody.CreateFixture(_fsShape, this); | ||
_fsFixture.Restitution = 0.2f; | ||
_fsFixture.OnCollision += BehaviorCollided; | ||
} | ||
|
||
private void MakeDeadFixture() | ||
{ | ||
if (_fsFixture != null) | ||
{ | ||
_fsBody.DestroyFixture(_fsFixture); | ||
} | ||
|
||
PolygonShape shape = new PolygonShape(1.0f); | ||
shape.SetAsBox(1.0f, 1.0f); | ||
_fsFixture = _fsBody.CreateFixture(_fsShape, this); | ||
_fsFixture.Restitution = 0.2f; | ||
_fsFixture.OnCollision += BehaviorCollided; | ||
} | ||
|
||
public int GetX() { | ||
return (int)_fsBody.Position.X; | ||
} | ||
public int GetY() { | ||
return (int)_fsBody.Position.Y; | ||
} | ||
|
||
RenderDelegate _myDrawDelegate; | ||
|
||
public RenderDelegate GetDrawDelegate() { | ||
if( _myDrawDelegate != null ) return _myDrawDelegate; | ||
|
||
_myDrawDelegate = ( int x, int y ) => { | ||
this.screen = Coords.Physics2Screen( new Vector2 { X = Position.X, Y = Position.Y } ); | ||
// maybe update the screen here? | ||
copSprite.Sprite.x = (int)screen.X - 8; | ||
copSprite.Sprite.y = (int)screen.Y - 8; | ||
copSprite.Sprite.Draw(); | ||
}; | ||
|
||
return _myDrawDelegate; | ||
} | ||
|
||
private bool BehaviorCollided(Fixture f1, Fixture f2, Contact contact) | ||
{ | ||
Fixture self = null, other = null; | ||
|
||
if (f1 == _fsFixture) | ||
{ | ||
self = f1; | ||
other = f2; | ||
} | ||
else if (f2 == _fsFixture) | ||
{ | ||
self = f2; | ||
other = f1; | ||
} | ||
|
||
if (other.UserData is ICollidable) | ||
{ | ||
this.Collided(other.UserData as ICollidable); | ||
(other.UserData as ICollidable).Collided(this); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void Collided(ICollidable other) | ||
{ | ||
if (other == _world) | ||
{ | ||
//Console.WriteLine("Velocity " + _fsBody.LinearVelocity.Y); | ||
if (Math.Abs(_fsBody.LinearVelocity.Y) < 1 && behaviorState == BehaviorState.Flying) | ||
{ | ||
this.behaviorState = BehaviorState.Walking; | ||
} | ||
else if (_fsBody.LinearVelocity.Y < -20 && behaviorState == BehaviorState.Flying) | ||
{ | ||
this.behaviorState = BehaviorState.Dead; | ||
copSprite.SetAnimationState(CopSprite.AnimationState.Dead); | ||
} | ||
} | ||
|
||
if (other is CivvieController) | ||
{ | ||
var otherCivvie = other as CivvieController; | ||
if (otherCivvie._fsBody.LinearVelocity.Length() > 50) | ||
{ | ||
otherCivvie.Die(); | ||
this.Die(); | ||
} | ||
} | ||
} | ||
|
||
public Vector2 Position | ||
{ | ||
get | ||
{ | ||
return _fsBody.Position; | ||
} | ||
} | ||
|
||
public float Theta | ||
{ | ||
get | ||
{ | ||
return _fsBody.Rotation; | ||
} | ||
} | ||
|
||
public void ProcessBehavior(GameTime time) | ||
{ | ||
if (Math.Abs(_fsBody.LinearVelocity.Y) > 1 && behaviorState != BehaviorState.Dead) { | ||
behaviorState = BehaviorState.Flying; | ||
copSprite.SetAnimationState(CopSprite.AnimationState.Flying); | ||
} | ||
if (behaviorState == BehaviorState.Walking) | ||
{ | ||
copSprite.SetAnimationState(CopSprite.AnimationState.WalkingLeft); | ||
_fsBody.LinearVelocity = new Vector2(-20, _fsBody.LinearVelocity.Y); | ||
_fsBody.Rotation = 0; | ||
} | ||
} | ||
|
||
private void Die() | ||
{ | ||
this.behaviorState = BehaviorState.Dead; | ||
copSprite.SetAnimationState(CopSprite.AnimationState.Dead); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using XNAVERGE; | ||
using Microsoft.Xna.Framework; | ||
|
||
namespace DemonDoor | ||
{ | ||
class CopSpawner : IDrawableThing, IBrainyThing | ||
{ | ||
TimeSpan spawnRate; | ||
private TimeSpan spawnTimeAccumulator; | ||
private McgLayer layer; | ||
private World world; | ||
private Vector2 location; | ||
private SpriteBasis copSpriteBasis; | ||
private int spawnRateFuzzMillis; | ||
|
||
public CopSpawner(World world, McgLayer layer, Vector2 location, TimeSpan spawnRate, SpriteBasis copSpriteBasis, int spawnRateFuzzMillis = 0) | ||
{ | ||
this.layer = layer; | ||
this.world = world; | ||
this.spawnRate = spawnRate; | ||
this.location = location; | ||
this.copSpriteBasis = copSpriteBasis; | ||
this.spawnRateFuzzMillis = spawnRateFuzzMillis; | ||
} | ||
|
||
public int GetX() | ||
{ | ||
return (int)location.X; | ||
} | ||
|
||
public int GetY() | ||
{ | ||
return (int)location.Y; | ||
} | ||
|
||
public RenderDelegate GetDrawDelegate() | ||
{ | ||
return (x, y) => { }; | ||
} | ||
|
||
public void ProcessBehavior(Microsoft.Xna.Framework.GameTime time) | ||
{ | ||
spawnTimeAccumulator += time.ElapsedGameTime; | ||
if (spawnTimeAccumulator > spawnRate) | ||
{ | ||
layer.AddNode(new McgNode(new CopController(world, new Vector2(location.X, location.Y), new CopSprite(copSpriteBasis)), layer, | ||
(int)location.X, (int)location.Y)); | ||
spawnTimeAccumulator = TimeSpan.FromMilliseconds(VERGEGame.rand.Next(-spawnRateFuzzMillis, 0)); | ||
} | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.