Skip to content

Commit

Permalink
Make the attack animation to be triggered
Browse files Browse the repository at this point in the history
only with Space
  • Loading branch information
timotei committed Sep 13, 2011
1 parent a66c10e commit f8d96f4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
8 changes: 8 additions & 0 deletions egp_story/AnimatedSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AnimatedSprite
private float _fps;
private int _increment;
private float _totalElapsed;
public bool Finished { get; set; }

public AnimatedSprite( Texture2D texture, int spriteCount, int speed )
{
Expand All @@ -31,6 +32,11 @@ public void Update( GameTime gameTime )
_sourceRectangle.X = ( _sourceRectangle.X + _increment ) % ( _texture.Width );

_totalElapsed -= _fps;

// restarted.
if ( _sourceRectangle.X == 0 ) {
Finished = true;
}
}
}

Expand All @@ -52,6 +58,8 @@ public void Reset( )
{
_totalElapsed = 0;
_sourceRectangle.X = 0;

Finished = false;
}
}
}
8 changes: 8 additions & 0 deletions egp_story/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class Assets
public static Texture2D SilverboltShootNorth { get; private set; }
public static Texture2D SilverboltShootEast { get; private set; }

public static Texture2D SilverboltWalkSouth { get; private set; }
public static Texture2D SilverboltWalkNorth { get; private set; }
public static Texture2D SilverboltWalkEast { get; private set; }

public static void LoadAssets( ContentManager content )
{
MainFont = content.Load<SpriteFont>( "Arial" );
Expand All @@ -35,6 +39,10 @@ public static void LoadAssets( ContentManager content )
SilverboltShootSouth = content.Load<Texture2D>( "gfx/silverbolt_shoot_s" );
SilverboltShootNorth = content.Load<Texture2D>( "gfx/silverbolt_shoot_n" );
SilverboltShootEast = content.Load<Texture2D>( "gfx/silverbolt_shoot_e" );

SilverboltWalkSouth = content.Load<Texture2D>( "gfx/silverbolt_walk_s" );
SilverboltWalkNorth = content.Load<Texture2D>( "gfx/silverbolt_walk_n" );
SilverboltWalkEast = content.Load<Texture2D>( "gfx/silverbolt_walk_e" );
}
}
}
6 changes: 5 additions & 1 deletion egp_story/Levels/DarkvilleFarmsLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public class DarkvilleFarmsLevel : IStoryLevel

public DarkvilleFarmsLevel( )
{
_player = new Player( );
_player = new Player( CardinalDirection.EAST );

_player.AttackEastAnim = new AnimatedSprite( Assets.SilverboltShootEast, 10, 10 );
_player.AttackSouthAnim = new AnimatedSprite( Assets.SilverboltShootSouth, 10, 10 );
_player.AttackNorthAnim = new AnimatedSprite( Assets.SilverboltShootNorth, 10, 10 );

_player.WalkEastAnim = new AnimatedSprite( Assets.SilverboltWalkEast, 2, 10 );
_player.WalkSouthAnim = new AnimatedSprite( Assets.SilverboltWalkSouth, 2, 10 );
_player.WalkNorthAnim = new AnimatedSprite( Assets.SilverboltWalkNorth, 2, 10 );
}

public void Update( GameTime gameTime )
Expand Down
96 changes: 65 additions & 31 deletions egp_story/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,38 @@ public class Player : IUpdateable, IDrawable
public AnimatedSprite AttackSouthAnim { get; set; }
public AnimatedSprite AttackEastAnim { get; set; }

public AnimatedSprite WalkNorthAnim { get; set; }
public AnimatedSprite WalkSouthAnim { get; set; }
public AnimatedSprite WalkEastAnim { get; set; }

public AnimatedSprite CurrentAnimation { get; set; }

public Player( CardinalDirection initialFacingDirection )
{
}

private void ReplaceCurrentAnimation( )
{
StopAnimation( );

switch ( FacingDirection ) {
case CardinalDirection.EAST:
CurrentAnimation = _attacking ? AttackEastAnim : WalkEastAnim;
break;
case CardinalDirection.WEST:
CurrentAnimation = _attacking ? AttackEastAnim : WalkEastAnim;
break;
case CardinalDirection.SOUTH:
CurrentAnimation = _attacking ? AttackSouthAnim : WalkSouthAnim;
break;
case CardinalDirection.NORTH:
CurrentAnimation = _attacking ? AttackNorthAnim : WalkNorthAnim;
break;
}

CurrentAnimation.Playing = true;
}

#region IDrawable Members

public void Draw( SpriteBatch spriteBatch, GameTime gameTime )
Expand All @@ -44,47 +74,51 @@ public void Draw( SpriteBatch spriteBatch, GameTime gameTime )

public void Update( GameTime gameTime )
{
// for the moment
_attacking = true;

KeyboardState keys = Keyboard.GetState( );
Vector2 deplacement = Vector2.Zero;
if ( keys.IsKeyDown2( Keys.Left ) ) {
StopAnimation( );

FacingDirection = CardinalDirection.WEST;
CurrentAnimation = _attacking ? AttackEastAnim : null;
deplacement.X = -10;
if ( !_attacking && keys.IsKeyDown2( Keys.Space ) ) {
_attacking = true;
ReplaceCurrentAnimation( );
}
else if ( keys.IsKeyDown2( Keys.Right ) ) {
StopAnimation( );

FacingDirection = CardinalDirection.EAST;
CurrentAnimation = _attacking ? AttackEastAnim : null;
deplacement.X = 10;
else if ( _attacking && CurrentAnimation.Finished ) {
_attacking = false;
ReplaceCurrentAnimation( );
}
else if ( keys.IsKeyDown2( Keys.Down ) ) {
StopAnimation( );

FacingDirection = CardinalDirection.SOUTH;
CurrentAnimation = _attacking ? AttackSouthAnim : null;
deplacement.Y = 10;
}
else if ( keys.IsKeyDown2( Keys.Up ) ) {
StopAnimation( );

FacingDirection = CardinalDirection.NORTH;
CurrentAnimation = _attacking ? AttackNorthAnim : null;
deplacement.Y = -10;
// cannot move while attacking
if ( !_attacking ) {
Vector2 deplacement = Vector2.Zero;
if ( keys.IsKeyDown( Keys.Left ) ) {
FacingDirection = CardinalDirection.WEST;
deplacement.X = -3;

ReplaceCurrentAnimation( );
}
else if ( keys.IsKeyDown( Keys.Right ) ) {
FacingDirection = CardinalDirection.EAST;
deplacement.X = 3;

ReplaceCurrentAnimation( );
}
else if ( keys.IsKeyDown( Keys.Down ) ) {
FacingDirection = CardinalDirection.SOUTH;
deplacement.Y = 3;

ReplaceCurrentAnimation( );
}
else if ( keys.IsKeyDown( Keys.Up ) ) {
FacingDirection = CardinalDirection.NORTH;
deplacement.Y = -3;

ReplaceCurrentAnimation( );
}

Position += deplacement;
}

if ( CurrentAnimation != null ) {
CurrentAnimation.Playing = true;

CurrentAnimation.Update( gameTime );
}

Position += deplacement;
}

private void StopAnimation( )
Expand Down

0 comments on commit f8d96f4

Please sign in to comment.