Skip to content

Commit

Permalink
Make the enemy wander around the map
Browse files Browse the repository at this point in the history
  • Loading branch information
timotei committed Sep 16, 2011
1 parent 35f2929 commit 40d53eb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
40 changes: 40 additions & 0 deletions egp_story/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ but WITHOUT ANY WARRANTY.
*/


using System;
using Microsoft.Xna.Framework;
namespace egp_story
{
public class Enemy : GameActor
{
private int _lastTimeMoved = 0;
private static Random _random = new Random( );

public Enemy( Game game, CardinalDirection initialFacingDirection, AnimatedSprite[] attackAnims,
AnimatedSprite[] walkAnims, AnimatedSprite[] projectileAnims )
: base( game, initialFacingDirection, attackAnims, walkAnims, projectileAnims )
Expand Down Expand Up @@ -45,11 +49,47 @@ public Enemy( Enemy other ) :

public override void Update( LevelMap levelMap, GameTime gameTime )
{
if ( ( gameTime.TotalGameTime.TotalMilliseconds - _lastTimeMoved ) > 10 ) {
_lastTimeMoved = ( int ) gameTime.TotalGameTime.TotalMilliseconds;

// move randomly
int steps = 0; // bound the maximum times to retry
Vector2 displacement = new Vector2( );
while ( steps < 10 ) {
displacement.X = _random.Next( 5 ) - 2;
displacement.Y = _random.Next( 5 ) - 2;

Rectangle newBoundingBox = BoundingBox;
newBoundingBox.Offset( ( int ) displacement.X, ( int ) displacement.Y );
if ( IsNewPositionOK( levelMap, newBoundingBox ) ) {
Position += displacement;
break;
}
else {
// invert the move direction
displacement *= -2;

newBoundingBox.Offset( ( int ) displacement.X, ( int ) displacement.Y );
if ( IsNewPositionOK( levelMap, newBoundingBox ) ) {
Position += displacement;
break;
}
}
++steps;
}

}

if ( CurrentAnimation != null ) {
CurrentAnimation.Update( gameTime );
}
}

private bool IsNewPositionOK( LevelMap levelMap, Rectangle rectangle )
{
return ( levelMap.CheckRectangleBounds( rectangle ) &&
!levelMap.ThePlayer.BoundingBox.Intersects( rectangle ) &&
CheckHitAndRemove( levelMap, rectangle, false ) == null );
}
}
}
23 changes: 23 additions & 0 deletions egp_story/GameActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,28 @@ public virtual void Draw( SpriteBatch spriteBatch, GameTime gameTime )
}

#endregion



/// <summary>
/// Checks if the rectangle collides with an existing game object, and if so
/// removes it from the map returning the removed object. Returns null if
/// no collision exists.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public GameActor CheckHitAndRemove( LevelMap map, Rectangle rectangle, bool remove = true )
{
for ( int i = 0; i < map.ActorObjects.Count; ++i ) {
GameActor actor = map.ActorObjects[i];
if ( actor != this &&
actor.BoundingBox.Intersects( rectangle ) ) {
if ( remove )
map.ActorObjects.RemoveAt( i );
return actor;
}
}
return null;
}
}
}
21 changes: 2 additions & 19 deletions egp_story/LevelMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public bool CheckRectangleBounds( Rectangle rectangle )
if ( !Mask.Bounds.Contains( rectangle ) )
return false;

rectangle.Width--;
rectangle.Height--;
// upper left
Color texel = GetTexel( rectangle.Y, rectangle.X );
if ( texel == Color.White || texel == Color.Black ) {
Expand All @@ -117,25 +119,6 @@ public bool CheckRectangleBounds( Rectangle rectangle )
return false;
}

/// <summary>
/// Checks if the rectangle collides with an existing game object, and if so
/// removes it from the map returning the removed object. Returns null if
/// no collision exists.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public GameActor CheckHitAndRemove( Rectangle rectangle )
{
for ( int i = 0; i < ActorObjects.Count; ++i ) {
GameActor actor = ActorObjects[i];
if ( actor.BoundingBox.Intersects( rectangle ) ) {
ActorObjects.RemoveAt( i );
return actor;
}
}
return null;
}

#region IUpdateable Members

public void Update( GameTime gameTime )
Expand Down
2 changes: 1 addition & 1 deletion egp_story/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public override void Update( LevelMap levelMap, GameTime gameTime )
}
else {
// check collision with other objects
if ( levelMap.CheckHitAndRemove( projectileBox ) != null ) {
if ( CheckHitAndRemove( levelMap, projectileBox ) != null ) {
_projectilesShot.Dequeue( );
}
}
Expand Down

0 comments on commit 40d53eb

Please sign in to comment.