YAPOG  0.0.1
Yet Another Pokemon Online Game
SpriteSet.hxx
Go to the documentation of this file.
1 #ifndef YAPOG_SPRITESET_HXX
2 # define YAPOG_SPRITESET_HXX
3 
4 namespace yap
5 {
6  template <typename K>
8  : BaseSprite ()
9  , currentKey_ ()
10  , defaultKey_ ()
11  , currentSprite_ (nullptr)
12  , sprites_ ()
13  {
14  }
15 
16  template <typename K>
17  inline SpriteSet<K>::SpriteSet (const SpriteSet& copy)
18  : BaseSprite (copy)
19  , currentKey_ ()
20  , defaultKey_ ()
21  , currentSprite_ (nullptr)
22  , sprites_ ()
23  {
24  for (const auto& it : copy.sprites_)
25  AddSprite (it.first, it.second->Clone ());
26 
29  }
30 
31  template <typename K>
33  {
34  for (const auto& it : sprites_)
35  delete it.second;
36  }
37 
38  template <typename K>
40  {
41  return new SpriteSet<K> (*this);
42  }
43 
44  template <typename K>
45  inline void SpriteSet<K>::AddSprite (const KeyType& key, ISprite* sprite)
46  {
47  bool isEmpty = sprites_.IsEmpty ();
48 
49  sprites_.Add (key, sprite);
50 
51  if (!isEmpty)
52  return;
53 
54  SetDefaultKey (key);
55  SetDefaultSprite ();
56 
57  }
58 
59  template <typename K>
60  inline void SpriteSet<K>::RemoveSprite (const KeyType& key)
61  {
62  if (currentKey_ == key)
63  SetDefaultSprite ();
64 
65  sprites_.Remove (key);
66  }
67 
68  template <typename K>
69  inline void SpriteSet<K>::SetCurrentSprite (const KeyType& key)
70  {
71  if (!sprites_.Contains (key))
72  return;
73 
74  currentKey_ = key;
75  currentSprite_ = sprites_[currentKey_];
76  }
77 
78  template <typename K>
79  inline void SpriteSet<K>::SetDefaultKey (const KeyType& key)
80  {
81  defaultKey_ = key;
82  }
83 
84  template <typename K>
86  {
87  SetCurrentSprite (defaultKey_);
88  }
89 
90  template <typename K>
92  {
93  return currentSprite_->GetSize ();
94  }
95 
96  template <typename K>
97  inline void SpriteSet<K>::HandleMove (const Vector2& offset)
98  {
99  for (auto& it : sprites_)
100  it.second->Move (offset);
101  }
102 
103  template <typename K>
104  inline void SpriteSet<K>::HandleScale (const Vector2& factor)
105  {
106  for (auto& it : sprites_)
107  it.second->Scale (factor);
108  }
109 
110  template <typename K>
112  {
113  currentSprite_->Draw (context);
114  }
115 
116  template <typename K>
117  inline void SpriteSet<K>::HandleShow (bool isVisible)
118  {
119  }
120 
121  template <typename K>
122  inline void SpriteSet<K>::HandleChangeColor (const sf::Color& color)
123  {
124  for (auto& it : sprites_)
125  it.second->ChangeColor (color);
126  }
127 
128  template <typename K>
129  inline void SpriteSet<K>::HandleUpdate (const Time& dt)
130  {
131  currentSprite_->Update (dt);
132  }
133 } // namesapce yap
134 
135 #endif // YAPOG_SPRITESET_HXX