1 module dgt.sprite; 2 3 import dgt.animation; 4 import dgt.color; 5 import dgt.texture; 6 7 /** 8 A drawable object attached to a transformation 9 10 Can be either static (Texture) or dynamic (Animation) 11 */ 12 struct Sprite 13 { 14 private: 15 union SpriteData 16 { 17 Animation anim; 18 Texture tex; 19 20 @nogc nothrow pure: 21 this(Animation a) { anim = a; } 22 this(Texture t) { tex = t; } 23 } 24 25 enum SpriteType 26 { 27 Static, Animated 28 } 29 30 SpriteData data; 31 SpriteType type; 32 33 public @nogc nothrow pure: 34 35 float x = 0, y = 0, width = 0, height = 0, 36 originX = 0, originY = 0, scaleX = 1, scaleY = 1, rotation = 0; 37 bool flipX = false, flipY = false; 38 Color color = Color.white; 39 40 ///Create a static sprite 41 this(in Texture tex) 42 { 43 data.tex = tex; 44 source = tex; 45 } 46 47 ///Create a dynamic sprite 48 this(scope Animation anim) 49 { 50 data.anim = anim; 51 source = anim; 52 } 53 54 ///Update the sprite (only affects dynamic sprites) 55 void update() 56 { 57 if(type == SpriteType.Animated) 58 data.anim.update(); 59 } 60 61 ///Get the current texture of the sprite 62 @property ref const(Texture) texture() const 63 { 64 if(type == SpriteType.Animated) 65 return data.anim.texture; 66 else 67 return data.tex; 68 } 69 70 ///Set the source of the sprite to a static texture 71 @property Texture source(in Texture tex) 72 { 73 data.tex = tex; 74 type = SpriteType.Static; 75 width = tex.size.width; 76 height = tex.size.height; 77 return tex; 78 } 79 80 ///Set the source of the sprite to a dynamic animation 81 @property Animation source(scope Animation anim) 82 { 83 data = SpriteData(anim); 84 type = SpriteType.Animated; 85 width = anim.texture.size.width; 86 height = anim.texture.size.height; 87 return anim; 88 } 89 }