1 /*
2  *  Copyright (C) 2011-2016  OpenDungeons Team
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "creatureeffect/CreatureEffectSpeedChange.h"
19 
20 #include "creatureeffect/CreatureEffectManager.h"
21 #include "entities/Creature.h"
22 #include "utils/LogManager.h"
23 
24 static const std::string CreatureEffectSpeedChangeName = "SpeedChange";
25 
26 namespace
27 {
28 class CreatureEffectSpeedChangeFactory : public CreatureEffectFactory
29 {
createCreatureEffect() const30     CreatureEffect* createCreatureEffect() const override
31     { return new CreatureEffectSpeedChange; }
32 
getCreatureEffectName() const33     const std::string& getCreatureEffectName() const override
34     {
35         return CreatureEffectSpeedChangeName;
36     }
37 };
38 
39 // Register the factory
40 static CreatureEffectRegister reg(new CreatureEffectSpeedChangeFactory);
41 }
42 
getEffectName() const43 const std::string& CreatureEffectSpeedChange::getEffectName() const
44 {
45     return CreatureEffectSpeedChangeName;
46 }
47 
applyEffect(Creature & creature)48 void CreatureEffectSpeedChange::applyEffect(Creature& creature)
49 {
50     if(!creature.isAlive())
51         return;
52 
53     if(mEffectValue == 1.0)
54         return;
55 
56     creature.setMoveSpeedModifier(mEffectValue);
57     mEffectValue = 1.0;
58 }
59 
releaseEffect(Creature & creature)60 void CreatureEffectSpeedChange::releaseEffect(Creature& creature)
61 {
62     if(!creature.isAlive())
63         return;
64 
65     creature.clearMoveSpeedModifier();
66     mEffectValue = 1.0;
67 
68 }
69 
load(std::istream & is)70 CreatureEffectSpeedChange* CreatureEffectSpeedChange::load(std::istream& is)
71 {
72     CreatureEffectSpeedChange* effect = new CreatureEffectSpeedChange;
73     effect->importFromStream(is);
74     return effect;
75 }
76 
exportToStream(std::ostream & os) const77 void CreatureEffectSpeedChange::exportToStream(std::ostream& os) const
78 {
79     CreatureEffect::exportToStream(os);
80     os << "\t" << mEffectValue;
81 }
82 
importFromStream(std::istream & is)83 bool CreatureEffectSpeedChange::importFromStream(std::istream& is)
84 {
85     if(!CreatureEffect::importFromStream(is))
86         return false;
87     if(!(is >> mEffectValue))
88         return false;
89 
90     return true;
91 }
92