ManaPlus
action.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "resources/action.h"
25 
27 
28 #include "utils/dtor.h"
29 #include "utils/foreach.h"
30 
31 #include "debug.h"
32 
33 Action::Action(const std::string &name) noexcept2 :
34  MemoryCounter(),
35  mAnimations(),
36  mCounterName(name),
37  mNumber(100)
38 {
39 }
40 
42 {
44 }
45 
47  const noexcept2
48 {
49  Animations::const_iterator i = mAnimations.find(direction);
50 
51  if (i == mAnimations.end())
52  {
53  if (direction == SpriteDirection::UPLEFT
54  || direction == SpriteDirection::UPRIGHT)
55  {
56  direction = SpriteDirection::UP;
57  }
58  else if (direction == SpriteDirection::DOWNLEFT
59  || direction == SpriteDirection::DOWNRIGHT)
60  {
61  direction = SpriteDirection::DOWN;
62  }
63  i = mAnimations.find(direction);
64 
65  // When the given direction is not available, return the first one.
66  // (either DEFAULT, or more usually DOWN).
67  if (i == mAnimations.end())
68  i = mAnimations.begin();
69  }
70 
71  return (i == mAnimations.end()) ? nullptr : i->second;
72 }
73 
75  Animation *const animation) noexcept2
76 {
77  mAnimations[direction] = animation;
78 }
79 
80 void Action::setLastFrameDelay(const int delay) noexcept2
81 {
82  FOR_EACH (AnimationIter, it, mAnimations)
83  {
84  Animation *const animation = (*it).second;
85  if (animation == nullptr)
86  continue;
87  animation->setLastFrameDelay(delay);
88  }
89 }
90 
92 {
93  return sizeof(Action);
94 }
95 
96 int Action::calcMemoryChilds(const int level) const
97 {
98  int sz = 0;
100  {
101  sz += sizeof(SpriteDirection::Type);
102  const Animation *const animation = (*it).second;
103  sz += animation->calcMemory(level + 1);
104  }
105  return sz;
106 }
void setLastFrameDelay(const int delay)
Definition: action.cpp:80
void setAnimation(const SpriteDirection::Type direction, Animation *const animation)
Definition: action.cpp:74
Action(const std::string &name)
Definition: action.cpp:33
Animations::const_iterator AnimationCIter
Definition: action.h:73
Animations mAnimations
Definition: action.h:75
~Action()
Definition: action.cpp:41
Animations::iterator AnimationIter
Definition: action.h:72
const Animation * getAnimation(SpriteDirection::Type direction) const
Definition: action.cpp:46
int calcMemoryChilds(const int level) const
Definition: action.cpp:96
int calcMemoryLocal() const
Definition: action.cpp:91
void setLastFrameDelay(const int delay)
Definition: animation.cpp:85
int calcMemory(const int level) const
void delete_all(Container &c)
Definition: dtor.h:56
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define noexcept2
Definition: localconsts.h:50
#define nullptr
Definition: localconsts.h:45