public void Draw(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary) { if (!this._tank.IsExtant) { return; } this._rightTrackTime %= LengthOfTrackAnimation; if (this._rightTrackTime < 0) { this._rightTrackTime += LengthOfTrackAnimation; } var pctPositionInRightTrackAnimation = this._rightTrackTime / LengthOfTrackAnimation; DrawTrack(spriteBatch, spriteLibrary, 32, pctPositionInRightTrackAnimation); this._leftTrackTime %= LengthOfTrackAnimation; if (this._leftTrackTime < 0) { this._leftTrackTime += LengthOfTrackAnimation; } var pctPositionInLeftTrackAnimation = this._leftTrackTime / LengthOfTrackAnimation; DrawTrack(spriteBatch, spriteLibrary, 64, pctPositionInLeftTrackAnimation); DrawHull(spriteBatch, spriteLibrary); DrawTurret(spriteBatch, spriteLibrary); }
private DrawParameters SetDrawParametersForFacedDirection(ISpriteLibrary spriteLibrary) { DrawParameters drawParameters = default; switch (this._player.CurrentDirectionFaced) { case Direction.Left: drawParameters.Texture = spriteLibrary.GetSprite("Sprites/Player/PlayerLeftFacing"); drawParameters.Effects = SpriteEffects.None; break; case Direction.Right: drawParameters.Texture = spriteLibrary.GetSprite("Sprites/Player/PlayerLeftFacing"); drawParameters.Effects = SpriteEffects.FlipHorizontally; break; case Direction.Up: drawParameters.Texture = spriteLibrary.GetSprite("Sprites/Player/PlayerUpFacing"); drawParameters.Effects = SpriteEffects.None; break; case Direction.Down: drawParameters.Texture = spriteLibrary.GetSprite("Sprites/Player/PlayerDownFacing"); drawParameters.Effects = SpriteEffects.None; break; default: throw new InvalidOperationException(); } drawParameters.Position = this._player.Position; return(drawParameters); }
public void Draw(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary) { if (!this._player.IsAlive()) { return; } var drawParameters = SetDrawParametersForFacedDirection(spriteLibrary); var frameCount = (drawParameters.Texture.Width / Constants.TileLength); var position = this.TimeWithinAnimation / AnimationLength; int frameIndex = (int)Math.Floor(frameCount * position); var currentFoot = position >= 0.5; if (this._whichFootFlag != currentFoot) { this._player.PlaySound(currentFoot ? GameSound.PlayerMovesFirstFoot : GameSound.PlayerMovesSecondFoot); this._whichFootFlag = currentFoot; } // Calculate the source rectangle of the current frame. drawParameters.AreaWithinTexture = new Rectangle(frameIndex * Constants.TileLength, 0, Constants.TileLength, Constants.TileLength); // Draw the current frame. spriteBatch.DrawTexture(drawParameters); }
public void Draw(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary) { if (!this._gameObject.IsExtant) { return; } this._time %= this._lengthOfAnimation; var positionInAnimation = this._time / this._lengthOfAnimation; DrawParameters drawParameters = default; drawParameters.Texture = spriteLibrary.GetSprite(this._textureName); var frameCount = (drawParameters.Texture.Width / Constants.TileLength); int frameIndex = (int)Math.Floor(frameCount * positionInAnimation); // Calculate the source rectangle of the current frame. drawParameters.AreaWithinTexture = new Rectangle(frameIndex * Constants.TileLength, 0, Constants.TileLength, Constants.TileLength); drawParameters.Position = this._gameObject.Position; drawParameters.Rotation = this.Rotation; drawParameters.Effects = this.SpriteEffect; drawParameters.Opacity = this.Opacity; // Draw the current frame. spriteBatch.DrawTexture(drawParameters); }
private void DrawHull(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary) { DrawParameters drawParameters = default; drawParameters.Texture = spriteLibrary.GetSprite(TextureName); // Calculate the source rectangle of the current frame. drawParameters.AreaWithinTexture = new Rectangle(0, 0, Constants.TileLength, Constants.TileLength); drawParameters.Position = this._tank.Position; drawParameters.Rotation = this._tank.HullRotation; // Draw the current frame. spriteBatch.DrawTexture(drawParameters); }
private void DrawTrack(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary, int p2, double pctPositionInTrackAnimation) { DrawParameters drawParameters = default; drawParameters.Texture = spriteLibrary.GetSprite(TextureName); var frameCount = (drawParameters.Texture.Width / Constants.TileLength); int frameIndex = (int)Math.Floor(frameCount * pctPositionInTrackAnimation); // Calculate the source rectangle of the current frame. drawParameters.AreaWithinTexture = new Rectangle(frameIndex * Constants.TileLength, p2, Constants.TileLength, Constants.TileLength); drawParameters.Position = this._tank.Position; drawParameters.Rotation = this._tank.HullRotation; // Draw the current frame. spriteBatch.DrawTexture(drawParameters); }
private void DrawTurret(ISpriteBatch spriteBatch, ISpriteLibrary spriteLibrary) { Vector2 centreOfRotationForTurret = new Vector2(0, 3); double x = centreOfRotationForTurret.Y * Math.Sin(this._tank.HullRotation) + centreOfRotationForTurret.X * Math.Cos(this._tank.HullRotation); double y = centreOfRotationForTurret.Y * Math.Cos(this._tank.HullRotation) - centreOfRotationForTurret.X * Math.Sin(this._tank.HullRotation); Vector2 centreOfRotationForTurretAfterRotation = new Vector2((float)-x, (float)y); DrawParameters drawParameters = default; drawParameters.Texture = spriteLibrary.GetSprite(TextureName); // Calculate the source rectangle of the current frame. drawParameters.AreaWithinTexture = new Rectangle(32, 0, Constants.TileLength, Constants.TileLength); drawParameters.Position = this._tank.Position + centreOfRotationForTurretAfterRotation; drawParameters.Rotation = this._tank.TurretRotation; drawParameters.Centre = new Vector2(16, 19); // this is always the same - this is the point where we rotate the turret around in the source rectangle // Draw the current frame. spriteBatch.DrawTexture(drawParameters); }
public static void SetSpriteLibrary(ISpriteLibrary spriteLibrary) { SpriteLibrary = spriteLibrary; }
public WorldRenderer(TileRect viewOfWorld, [NotNull] ISpriteBatch spriteBatch, [NotNull] ISpriteLibrary spriteLibrary) { this._viewOfWorld = viewOfWorld; this._spriteBatch = spriteBatch ?? throw new ArgumentNullException(nameof(spriteBatch)); this._spriteLibrary = spriteLibrary ?? throw new ArgumentNullException(nameof(spriteLibrary)); }