From 32e692dfbb0332c6544f98824b4926994097e294 Mon Sep 17 00:00:00 2001 From: timotei Date: Thu, 15 Sep 2011 21:20:48 +0300 Subject: [PATCH] Some code fix and reorganizing --- egp_story/AnimatedSprite.cs | 10 +++++----- egp_story/LevelMap.cs | 34 ++++++++++++++++++++++++++++++++++ egp_story/Player.cs | 30 +++--------------------------- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/egp_story/AnimatedSprite.cs b/egp_story/AnimatedSprite.cs index 88dd711..dec4e94 100644 --- a/egp_story/AnimatedSprite.cs +++ b/egp_story/AnimatedSprite.cs @@ -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 } /// diff --git a/egp_story/LevelMap.cs b/egp_story/LevelMap.cs index 642b9c5..e4e5f38 100644 --- a/egp_story/LevelMap.cs +++ b/egp_story/LevelMap.cs @@ -62,5 +62,39 @@ public void Draw( SpriteBatch spriteBatch, GameTime gameTime ) } #endregion + + /// + /// Returns true if the rectangle's bounds are on valid position (walkable) + /// + /// The rectangle to check the bounds for + /// + 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; + } } } diff --git a/egp_story/Player.cs b/egp_story/Player.cs index b7417cf..cfb9fff 100644 --- a/egp_story/Player.cs +++ b/egp_story/Player.cs @@ -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; } } } @@ -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( ); }