Skip to content

Commit

Permalink
Some code fix and reorganizing
Browse files Browse the repository at this point in the history
  • Loading branch information
timotei committed Sep 15, 2011
1 parent bba7479 commit 32e692d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
10 changes: 5 additions & 5 deletions egp_story/AnimatedSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public void Draw( SpriteBatch spriteBatch, Vector2 position, GameTime gameTime,
spriteBatch.Draw( _texture, position, _sourceRectangle, Color.White, 0f, Vector2.Zero, 1f,
effects, 0 );

#if DEBUG
Rectangle boundingRect = FrameBoundingBox;
boundingRect.Offset( ( int ) position.X, ( int ) position.Y );
boundingRect.Draw( spriteBatch );
#endif
//#if DEBUG
// Rectangle boundingRect = FrameBoundingBox;
// boundingRect.Offset( ( int ) position.X, ( int ) position.Y );
// boundingRect.Draw( spriteBatch );
//#endif
}

/// <summary>
Expand Down
34 changes: 34 additions & 0 deletions egp_story/LevelMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,39 @@ public void Draw( SpriteBatch spriteBatch, GameTime gameTime )
}

#endregion

/// <summary>
/// Returns true if the rectangle's bounds are on valid position (walkable)
/// </summary>
/// <param name="rectangle">The rectangle to check the bounds for</param>
/// <returns></returns>
public bool CheckRectangleBounds( Rectangle rectangle )
{
if ( !Mask.Bounds.Contains( rectangle ) )
return false;

// upper left
Color texel = GetTexel( rectangle.Y, rectangle.X );
if ( texel == Color.White || texel == Color.Black ) {

// upper right
texel = GetTexel( rectangle.Y, rectangle.Right );
if ( texel == Color.White || texel == Color.Black ) {

// bottom right
texel = GetTexel( rectangle.Bottom, rectangle.Right );
if ( texel == Color.White || texel == Color.Black ) {

// bottom left
texel = GetTexel( rectangle.Bottom, rectangle.X );
if ( texel == Color.White || texel == Color.Black ) {
return true;
}
}
}
}

return false;
}
}
}
30 changes: 3 additions & 27 deletions egp_story/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,8 @@ public void Update( LevelMap levelMap, GameTime gameTime )
Rectangle newBoundingBox = CurrentAnimation.FrameBoundingBox;
newBoundingBox.Offset( ( int ) newPosition.X, ( int ) newPosition.Y );

if ( levelMap.Mask.Bounds.Contains( newBoundingBox ) ) {
bool canMove = false;
// upper left
Color texel = levelMap.GetTexel( newPosition.Y, newPosition.X );
if ( texel == Color.White || texel == Color.Black ) {

// upper right
texel = levelMap.GetTexel( newPosition.Y, newBoundingBox.Right );
if ( texel == Color.White || texel == Color.Black ) {

// bottom right
texel = levelMap.GetTexel( newBoundingBox.Bottom, newBoundingBox.Right );
if ( texel == Color.White || texel == Color.Black ) {

// bottom left
texel = levelMap.GetTexel( newBoundingBox.Bottom, newPosition.X );
if ( texel == Color.White || texel == Color.Black ) {
canMove = true;
}
}
}
}

if ( canMove ) {
Position += FacingDirection.ToVelocity( ) * 2;
}
if ( levelMap.CheckRectangleBounds( newBoundingBox ) ) {
Position = newPosition;
}
}
}
Expand All @@ -218,7 +194,7 @@ public void Update( LevelMap levelMap, GameTime gameTime )

projectile.Position += projectile.Velocity * 5;

if ( !_graphics.GraphicsDevice.Viewport.Bounds.Contains( ref projectile.Position ) ) {
if ( !levelMap.Mask.Bounds.Contains( ref projectile.Position ) ) {
// remove this
_projectilesShot.Dequeue( );
}
Expand Down

0 comments on commit 32e692d

Please sign in to comment.