Created
May 23, 2012 20:40
-
-
Save demonixis/2777665 to your computer and use it in GitHub Desktop.
New SpriteState implementation for YNA Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using Yna.Display; | |
using Yna.Input; | |
namespace Yna.Test | |
{ | |
public class SpriteState : YnState | |
{ | |
Sprite sonicSprite; // Sprite du joueur | |
YnText titleText; | |
public SpriteState () { } | |
public override void Initialize () | |
{ | |
// 1 - Création d'un Sprite à la position 50, 50 en utilisant la texture soniclg4 du dossier 2d | |
sonicSprite = new Sprite (new Vector2 (50, 50), "2d//soniclg4"); | |
// Indique que le Sprite est animé, renseigne la taille d'un Sprite sur la feuille de Sprite | |
sonicSprite.PrepareAnimation (50, 41); | |
// Ajoute des animations | |
// 1 - Nom de l'animation | |
// 2 - Tableau d'indices ciblant les frames | |
// 3 - Temps d'affichage d'une frame | |
// 4 - Indique si l'animation doit etre retournée | |
sonicSprite.AddAnimation ("down", new int[] { 0, 1, 2, 3 }, 150, false); | |
sonicSprite.AddAnimation ("left", new int[] { 4, 5, 6, 7 }, 150, false); | |
sonicSprite.AddAnimation ("right", new int[] { 8, 9, 10, 11 }, 150, false); | |
sonicSprite.AddAnimation ("up", new int[] { 12, 13, 14, 15 }, 150, false); | |
// Joue l'animation "up" | |
sonicSprite.Play ("up"); | |
// Modifie la taille du Sprite | |
// Tout est automatiquement mis à jour | |
sonicSprite.Scale = new Vector2 (1.5f, 1.5f); | |
// Force le sprite à rester sur l'écran | |
sonicSprite.ForceInsideScreen = true; | |
// Taux d'accéleration du personnage | |
sonicSprite.Acceleration = new Vector2(6.5f, 2.5f); | |
// Velocity Maximum | |
sonicSprite.VelocityMax = 0.85f; | |
// Ajoute le Sprite à la scène | |
Add (sonicSprite); | |
// 2 - Création d'un champ texte | |
string title = "Animated Sprite !"; | |
titleText = new YnText("Debug", Vector2.Zero, title); | |
Vector2 size = titleText.Font.MeasureString(title); | |
Vector2 zoom = new Vector2(1.5f, 1.5f); | |
titleText.Position = new Vector2(YnG.Width / 2 - ((size.X / 2) * zoom.X), 35); | |
titleText.Scale = zoom; | |
titleText.Color = Color.DeepSkyBlue; | |
Add(titleText); | |
} | |
public override void UnloadContent() | |
{ | |
sonicSprite.Die(); | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
base.Update(gameTime); // Les objets attachés sont mis à jour | |
// Déplacement du Sprite | |
if (YnG.Keys.Pressed(Keys.Up)) | |
{ | |
sonicSprite.Play("up"); | |
sonicSprite.VelocityY -= 0.5f; | |
} | |
else if (YnG.Keys.Pressed(Keys.Down)) | |
{ | |
sonicSprite.Play("down"); | |
sonicSprite.VelocityY += 0.5f; | |
} | |
else if (YnG.Keys.Pressed(Keys.Left)) | |
{ | |
sonicSprite.Play("left"); | |
sonicSprite.VelocityX -= 0.2f; | |
} | |
else if (YnG.Keys.Pressed(Keys.Right)) | |
{ | |
sonicSprite.Play("right"); | |
sonicSprite.VelocityX += 0.2f; | |
} | |
if (YnG.Keys.Pressed(Keys.Space)) | |
sonicSprite.Jump(); | |
if (YnG.Keys.JustPressed(Keys.Escape)) | |
YnG.SwitchState(new MenuState()); | |
} | |
public override void Draw (GameTime gameTime, SpriteBatch spriteBatch) | |
{ | |
YnG.GraphicsDevice.Clear(Color.AliceBlue); | |
base.Draw(gameTime, spriteBatch); // Les objets attachés sont dessinés | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment