public void UpdateTexture() { Rectangle source = SourceRectangle; PowerStateType power = PowerStateEnum; if (PowerStateEnum == PowerStateType.Star) { power = PowerState.Previous; } MarioSpriteFactory.SetSourceRectangle(ref source, power, ActionStateEnum); SourceRectangle = source; }
private void HandleRunningUpdate(GameTime gameTime) { if (lastChange > 16) { if (CurrentFrame == 2) { dir = -1; } else if (CurrentFrame == 0) { dir = 1; } CurrentFrame += dir; Rectangle source = SourceRectangle; //to handle invincibility while walking PowerStateType power = PowerStateEnum; if (Invincible) { if (useSource)//useSource was switched in invincable { power = PowerState.Previous; useSource = true; } } if (PowerStateEnum == PowerStateType.Star) { power = PowerState.Previous; } if (ActionStateEnum == ActionStateType.Decaying && source.X >= MarioSpriteFactory.RightFacingStart) { MarioSpriteFactory.Running(ref source, CurrentFrame, power, ActionStateType.WalkingRight); } else { MarioSpriteFactory.Running(ref source, CurrentFrame, power, ActionStateEnum); } SourceRectangle = source; lastChange = 0; } else { lastChange += (int)gameTime.ElapsedGameTime.TotalMilliseconds; } }
//private methods for update private void HandleInvincibilityUpdate(GameTime gameTime) { if (invincibleCurrentTime >= InvincibleTime) { Invincible = false; useSource = true; Rectangle sourceInv = SourceRectangle; PowerStateType power = PowerStateEnum; if (PowerStateEnum == PowerStateType.Star) { power = PowerState.Previous; } MarioSpriteFactory.SetSourceRectangle(ref sourceInv, power, ActionStateEnum); SourceRectangle = sourceInv; invincibleCurrentTime = 0; sourceInvincibleChange = 0; } else { invincibleCurrentTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (sourceInvincibleChange >= SourceChangeTime) { PowerStateType power; if (useSource) { if (PowerStateEnum != PowerStateType.Star) { power = PowerStateEnum; } else { power = PowerState.Previous; } useSource = false; } else { if (PowerStateEnum != PowerStateType.Star) { power = PowerState.Previous; } else { power = ((MarioPowerStateStar)PowerState).previousState.Previous; } useSource = true; } Rectangle sourceInv = SourceRectangle; MarioSpriteFactory.SetSourceRectangle(ref sourceInv, power, ActionStateEnum); if (ActionStateEnum == ActionStateType.WalkingLeft || ActionStateEnum == ActionStateType.WalkingRight)//need to handle walking as this will update faster than animation frame { MarioSpriteFactory.Walking(ref sourceInv, CurrentFrame, power, ActionStateEnum); } SourceRectangle = sourceInv; sourceInvincibleChange = 0; } else { sourceInvincibleChange += (int)gameTime.ElapsedGameTime.TotalMilliseconds; } } }
public override void Update(GameTime gameTime) { base.Update(gameTime); OldVelocity = Velocity; if (Velocity.Y > 0 && !Falling) { Falling = true; if (!(ActionState is MarioActionStateJump)) { ActionState = new MarioActionStateJump(this, ActionState); ActionStateEnum = ActionStateType.Jumping; ActionState.Enter(); } SourceRectangle = MarioSpriteFactory.FallingJump(SourceRectangle, ActionStateEnum); } if (ActionStateEnum == ActionStateType.Decaying || (ActionStateEnum == ActionStateType.Jumping && ActionState.Previous is MarioActionStateDecaying)) { Velocity = new Vector2(decay * Velocity.X, Velocity.Y); if (Velocity.X < .1 && Velocity.X > -.1) { Velocity = new Vector2(0, Velocity.Y); if (ActionStateEnum == ActionStateType.Jumping) { ActionState.SetPrevious(new MarioActionStateIdle(this, null)); } else { ActionState.Leave(new MarioActionStateIdle(this, null)); } } } //invulernability if (Invincible) { HandleInvincibilityUpdate(gameTime); } //handle walking if (ActionStateEnum == ActionStateType.WalkingLeft || ActionStateEnum == ActionStateType.WalkingRight || ActionStateEnum == ActionStateType.Decaying) { if (fireChange >= FireTime || !ShotFireBall) { if (ActionState is MarioActionStateMovingLeft || ActionState is MarioActionStateMovingRight || ActionState is MarioActionStateDecaying) { HandleWalkingUpdate(gameTime); } else { HandleRunningUpdate(gameTime); } } } else { CurrentFrame = 0; } //powerstate handles the texture changes for mario if (PowerStateEnum == PowerStateType.Star) { HandleStarUpdate(gameTime); } if (ShotFireBall) { if (fireChange >= FireTime) { Rectangle source = SourceRectangle; MarioSpriteFactory.SetSourceRectangle(ref source, PowerStateEnum, ActionStateEnum); SourceRectangle = source; if (fireChange >= 4 * FireTime) { ShotFireBall = false; fireChange = 0; } else { fireChange += (int)gameTime.ElapsedGameTime.TotalMilliseconds; } } else { fireChange += (int)gameTime.ElapsedGameTime.TotalMilliseconds; } } //koopa shell holding if (ActionStateEnum == ActionStateType.HoldingShell) { ((MarioHoldingShellState)ActionState).UpdateState(); } }