mirror of
https://github.com/Memorix101/UnityXNA/
synced 2024-12-30 15:25:35 +01:00
The XNA 4.0 PlatformerGame sample is successfully running inside Unity3D 3.5. Implemented a basic game loop, game timing, content loading for Texture2D, SoundEffect and Song. Emulated SpriteBatch drawing for sprites and strings (note SpriteFont is not yet supported to all strings are rendered using the default GUI label font). Songs can be played using an AudioSource attached to the XNATest game object which acts as an emulator for MediaPlayer. Playing a SoundEffect creates a game object with an AudioSource attached which is automatically deleted when the sound finishes. Implemented keyboard input with a limited set of XNA Keys mapping to Unity3D KeyCodes.
100 lines
3.2 KiB
C#
100 lines
3.2 KiB
C#
#region File Description
|
|
//-----------------------------------------------------------------------------
|
|
// AnimationPlayer.cs
|
|
//
|
|
// Microsoft XNA Community Game Platform
|
|
// Copyright (C) Microsoft Corporation. All rights reserved.
|
|
//-----------------------------------------------------------------------------
|
|
#endregion
|
|
|
|
using System;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
namespace Platformer
|
|
{
|
|
/// <summary>
|
|
/// Controls playback of an Animation.
|
|
/// </summary>
|
|
struct AnimationPlayer
|
|
{
|
|
/// <summary>
|
|
/// Gets the animation which is currently playing.
|
|
/// </summary>
|
|
public Animation Animation
|
|
{
|
|
get { return animation; }
|
|
}
|
|
Animation animation;
|
|
|
|
/// <summary>
|
|
/// Gets the index of the current frame in the animation.
|
|
/// </summary>
|
|
public int FrameIndex
|
|
{
|
|
get { return frameIndex; }
|
|
}
|
|
int frameIndex;
|
|
|
|
/// <summary>
|
|
/// The amount of time in seconds that the current frame has been shown for.
|
|
/// </summary>
|
|
private float time;
|
|
|
|
/// <summary>
|
|
/// Gets a texture origin at the bottom center of each frame.
|
|
/// </summary>
|
|
public Vector2 Origin
|
|
{
|
|
get { return new Vector2(Animation.FrameWidth / 2.0f, Animation.FrameHeight); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Begins or continues playback of an animation.
|
|
/// </summary>
|
|
public void PlayAnimation(Animation animation)
|
|
{
|
|
// If this animation is already running, do not restart it.
|
|
if (Animation == animation)
|
|
return;
|
|
|
|
// Start the new animation.
|
|
this.animation = animation;
|
|
this.frameIndex = 0;
|
|
this.time = 0.0f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Advances the time position and draws the current frame of the animation.
|
|
/// </summary>
|
|
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects)
|
|
{
|
|
if (Animation == null)
|
|
throw new NotSupportedException("No animation is currently playing.");
|
|
|
|
// Process passing time.
|
|
time += (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
while (time > Animation.FrameTime)
|
|
{
|
|
time -= Animation.FrameTime;
|
|
|
|
// Advance the frame index; looping or clamping as appropriate.
|
|
if (Animation.IsLooping)
|
|
{
|
|
frameIndex = (frameIndex + 1) % Animation.FrameCount;
|
|
}
|
|
else
|
|
{
|
|
frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1);
|
|
}
|
|
}
|
|
|
|
// Calculate the source rectangle of the current frame.
|
|
Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);
|
|
|
|
// Draw the current frame.
|
|
spriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0f, Origin, 1.0f, spriteEffects, 0.0f);
|
|
}
|
|
}
|
|
}
|