Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- using System.Diagnostics;
- namespace GreatGamuxCollab.Source {
- class Sprite {
- public Texture2D texture;
- public Vector2 position;
- public int halfWidth;
- public int halfHeight;
- public Vector2 origin;
- /* Static Sprite; origin = center*/
- public Sprite(string textureName, Vector2 position) {
- this.position = position;
- NewTexture(textureName);
- }
- public void NewTexture(string textureName){
- texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
- halfHeight = texture.Height / 2;
- halfWidth = texture.Width / 2;
- origin = new Vector2(halfWidth, halfHeight);
- }
- public void Draw(SpriteBatch spriteBatch) {
- spriteBatch.Draw(texture, position, null, Color.White, 0f, origin, 1f, SpriteEffects.None, 1);
- }
- public void Clear() {
- texture = null;
- }
- }
- class SpriteSuper {
- public Texture2D texture;
- public Vector2 position;
- public int halfWidth;
- public int halfHeight;
- public Vector2 origin;
- public float scale;
- public float rotation;
- /* Sprite that can rotate and scale*/
- public SpriteSuper(string textureName, Vector2 position, float scale, float rotation) {
- this.position = position;
- this.scale = scale;
- this.rotation = rotation;
- NewTexture(textureName);
- }
- public void NewTexture(string textureName) {
- texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
- halfHeight = (int)(scale * texture.Height / 2);
- halfWidth = (int)(scale * texture.Width / 2);
- origin = new Vector2(texture.Height / 2, texture.Width / 2);
- }
- public void Draw(SpriteBatch spriteBatch) {
- spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, SpriteEffects.None, 1);
- }
- public void Clear() {
- texture = null;
- }
- }
- class SpriteAnimated {
- private Texture2D texture;
- public Vector2 position;
- private int frame;
- private float frameTime;
- public int frameWidth;
- public int frameHeight;
- public int halfWidth;
- public int halfHeight;
- public Vector2 origin;
- public float scale;
- public float rotation;
- private int nLines;
- private int nCollumns;
- private Rectangle sourceRect;
- private Animation currentAnimation;
- private Action callback;
- public struct Animation {
- public int firstFrame;
- public int lastFrame;
- public int fps;
- public bool loop;
- public string name;
- }
- //TODO: tentar usar enum para otimizar procura de Animation
- private List<Animation> animations;
- public SpriteAnimated(string textureName, Vector2 position, int frameWidth, int frameHeight) {
- this.position = position;
- this.frameWidth = frameWidth;
- this.frameHeight = frameHeight;
- scale = 1f;
- texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
- nCollumns = texture.Width / frameWidth;
- nLines = texture.Height / frameHeight;
- halfHeight = (int)(scale * frameHeight / 2);
- halfWidth = (int)(scale * frameWidth / 2);
- origin = new Vector2(frameWidth / 2, frameHeight / 2);
- rotation = 0f;
- SetFrame(1);
- frameTime = 0f;
- sourceRect = new Rectangle(0, 0, frameWidth, frameHeight);
- animations = new List<Animation>();
- }
- public void Draw(SpriteBatch spriteBatch, float secondsElapsed){
- if(currentAnimation.name != null){
- frameTime += secondsElapsed;
- if (frameTime >= 1 / currentAnimation.fps) {
- frameTime -= 1 / currentAnimation.fps;
- if (frame == currentAnimation.lastFrame) {
- if (currentAnimation.loop)
- frame = currentAnimation.firstFrame;
- else
- currentAnimation.name = null;
- if (callback != null) callback();
- } else if (currentAnimation.lastFrame > frame) {
- SetFrame(frame + 1);
- } else if (currentAnimation.lastFrame < frame) {
- SetFrame(frame - 1);
- }
- }
- }
- spriteBatch.Draw(texture, position, sourceRect, Color.White, rotation, origin, scale, SpriteEffects.None, 1);
- }
- /* Registra a animacao para usar depois*/
- public void RegisterAnimation(string name, int firstFrame, int lastFrame, int fps, bool loop) {
- Animation newAnimation = new Animation();
- newAnimation.name = name;
- newAnimation.firstFrame = firstFrame;
- newAnimation.lastFrame = lastFrame;
- newAnimation.fps = fps;
- newAnimation.loop = loop;
- animations.Add(newAnimation);
- }
- /* Reproduz uma animacao previamente registrada. Coloque callback como null caso não queira um.*/
- public void Play(string name, bool forceRestart, Action callback) {
- Animation animation = GetByName(name);
- this.callback = callback;
- if(currentAnimation.name != animation.name || forceRestart || frame == animation.lastFrame){
- currentAnimation = animation;
- frame = currentAnimation.firstFrame;
- }
- }
- /* Vai e para num frame desejado */
- public void GotoAndStop(int frame) {
- SetFrame(frame);
- currentAnimation.name = null;
- frameTime = 0f;
- }
- public void SetFrame(int value) {
- frame = value;
- sourceRect.X = frameWidth * ((frame - 1) % nCollumns);
- sourceRect.Y = frameHeight * ((frame - 1) / nCollumns);
- }
- public Animation GetByName(string name) {
- foreach (Animation anim in animations) {
- if (anim.name == name)
- return anim;
- }
- return animations[0];
- }
- /* Desfaz as referências */
- public void Clear() {
- texture = null;
- callback = null;
- }
- }
- }
Add Comment
Please, Sign In to add comment