1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;
namespace Gamecodeur_JAM_1
{
public enum SheetOrientation
{
VERTICAL,
HORIZONTAL
}
public enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
}
public class AnimatedSprite : Sprite
{
// VARIABLE
public Rectangle sourceRectangle;
private SheetOrientation sheetOrientation;
private Direction direction;
private int spriteWidth, spriteHeight, currentIndex;
// SETTER & GETTER
public void SetIndex(int index)
{
this.currentIndex = index;
}
// CONSTRUCTOR
public AnimatedSprite(string imgPath, ContentManager content, int spriteWidth, int spriteHeight, int index, SheetOrientation sheetOrientation)
: base(imgPath, content)
{
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
this.currentIndex = index;
this.sheetOrientation = sheetOrientation;
this.imgPath = imgPath;
this.destinationRectangle = new Rectangle(this.x, this.y, this.spriteWidth, this.spriteHeight);
//if (this.sheetOrientation == SheetOrientation.HORIZONTAL)
//{
this.sourceRectangle = new Rectangle(this.currentIndex, 0, this.spriteWidth, this.spriteHeight);
//}
//else
//{
//this.sourceRectangle = new Rectangle(0, this.currentIndex, this.spriteWidth, this.spriteHeight);
//}
}
// UPDATE & DRAW
public override void Update(int x, int y)
{
this.destinationRectangle.X = x;
this.destinationRectangle.Y = y;
this.destinationRectangle.Width = this.spriteWidth;
this.destinationRectangle.Height = this.spriteHeight;
//if (this.sheetOrientation == SheetOrientation.HORIZONTAL)
//{
//this.sourceRectangle = new Rectangle(this.currentIndex, 3, this.spriteWidth, this.spriteHeight);
//}
//else
//{
this.sourceRectangle = new Rectangle(0, this.currentIndex, this.spriteWidth, this.spriteHeight);
//}
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(this.texture, this.destinationRectangle, this.sourceRectangle, this.color);
}
// FUNCTION
public void Move(GameTime gameTime, Direction direction)
{
if (direction == Direction.UP)
{
this.destinationRectangle.Y -= (int)Information.PLAYER_SPEED;
this.sourceRectangle.X = 0;
}
else if (direction == Direction.DOWN)
{
this.destinationRectangle.Y += (int)Information.PLAYER_SPEED;
this.sourceRectangle.X = 0;
}
if (direction == Direction.LEFT)
{
this.destinationRectangle.X -= (int)Information.PLAYER_SPEED;
this.sourceRectangle.X = 0;
}
else if (direction == Direction.RIGHT)
{
this.destinationRectangle.X += (int)Information.PLAYER_SPEED;
this.sourceRectangle.X = 0;
}
}
}
} |
Partager