Otclient 1.0  14/8/2020
effect.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "effect.h"
25 #include "game.h"
26 #include "map.h"
27 
28 Effect::Effect() : m_timeToStartDrawing(0) {}
29 
30 void Effect::drawEffect(const Point& dest, float scaleFactor, int redrawFlag, LightView* lightView)
31 {
32  if(m_id == 0) return;
33 
34  // It only starts to draw when the first effect as it is about to end.
35  if(m_animationTimer.ticksElapsed() < m_timeToStartDrawing) return;
36 
37  int animationPhase;
38 
40  // This requires a separate getPhaseAt method as using getPhase would make all magic effects use the same phase regardless of their appearance time
41  animationPhase = rawGetThingType()->getAnimator()->getPhaseAt(m_animationTimer.ticksElapsed());
42  } else {
43  // hack to fix some animation phases duration, currently there is no better solution
44  int ticks = Otc::EFFECT_TICKS_PER_FRAME;
45  if(m_id == 33) {
46  ticks <<= 2;
47  }
48 
49  animationPhase = std::min<int>(static_cast<int>(m_animationTimer.ticksElapsed() / ticks), getAnimationPhases() - 1);
50  }
51 
52  const int xPattern = m_position.x % getNumPatternX();
53  const int yPattern = m_position.y % getNumPatternY();
54 
55  rawGetThingType()->draw(dest, scaleFactor, 0, xPattern, yPattern, 0, animationPhase, redrawFlag, lightView);
56 }
57 
59 {
60  m_animationTimer.restart();
61 
62  int ticksPerFrame;
64  m_duration = getThingType()->getAnimator()->getTotalDuration();
65 
66  ticksPerFrame = getThingType()->getAnimator()->getAverageDuration();
67  } else {
68  m_duration = Otc::EFFECT_TICKS_PER_FRAME;
69 
70  // hack to fix some animation phases duration, currently there is no better solution
71  if(m_id == 33) {
72  m_duration <<= 2;
73  }
74 
75  ticksPerFrame = m_duration;
76 
77  m_duration *= getAnimationPhases();
78  }
79 
80  startListenerPainter(ticksPerFrame);
81 
82  // schedule removal
83  const auto self = asEffect();
84  g_dispatcher.scheduleEvent([self]() { g_map.removeThing(self); }, m_duration);
85 }
86 
87 void Effect::waitFor(const EffectPtr& firstEffect)
88 {
89  m_timeToStartDrawing = (firstEffect->m_duration * .6) - firstEffect->m_animationTimer.ticksElapsed();
90 }
91 
93 {
95  id = 0;
96 
97  m_id = id;
98 }
99 
101 {
103 }
104 
106 {
108 }
ThingTypeManager::rawGetThingType
ThingType * rawGetThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:57
Thing::startListenerPainter
void startListenerPainter(const float duration)
Definition: thing.h:133
eventdispatcher.h
g_map
Map g_map
Definition: map.cpp:36
Timer::ticksElapsed
ticks_t ticksElapsed()
Definition: timer.cpp:33
Position::x
int x
Definition: position.h:265
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
Effect::setId
void setId(uint32 id) override
Definition: effect.cpp:92
Effect::Effect
Effect()
Definition: effect.cpp:28
uint32
uint32_t uint32
Definition: types.h:35
Effect::waitFor
void waitFor(const EffectPtr &firstEffect)
Definition: effect.cpp:87
Otc::GameEnhancedAnimations
@ GameEnhancedAnimations
Definition: const.h:424
Animator::getTotalDuration
ticks_t getTotalDuration()
Definition: animator.cpp:219
Thing::getAnimationPhases
int getAnimationPhases()
Definition: thing.h:78
g_game
Game g_game
Definition: game.cpp:37
Position::y
int y
Definition: position.h:266
ThingCategoryEffect
@ ThingCategoryEffect
Definition: thingtype.h:48
Effect::rawGetThingType
ThingType * rawGetThingType() override
Definition: effect.cpp:105
LightView
Definition: lightview.h:37
Thing::getNumPatternX
int getNumPatternX()
Definition: thing.h:75
Thing::m_position
Position m_position
Definition: thing.h:141
Animator::getPhaseAt
int getPhaseAt(ticks_t time)
Definition: animator.cpp:126
UIWidget::m_id
std::string m_id
Definition: uiwidget.h:61
Effect::getThingType
const ThingTypePtr & getThingType() override
Definition: effect.cpp:100
Thing::getNumPatternY
int getNumPatternY()
Definition: thing.h:76
map.h
Timer::restart
void restart()
Definition: timer.cpp:27
ThingTypeManager::getThingType
const ThingTypePtr & getThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.cpp:376
Game::getFeature
bool getFeature(Otc::GameFeature feature)
Definition: game.h:312
ThingType
Definition: thingtype.h:126
ThingType::getAnimator
AnimatorPtr getAnimator()
Definition: thingtype.h:154
Effect::drawEffect
void drawEffect(const Point &dest, float scaleFactor, int redrawFlag, LightView *lightView=nullptr)
Definition: effect.cpp:30
Animator::getAverageDuration
int getAverageDuration()
Definition: animator.h:57
Effect::onAppear
void onAppear() override
Definition: effect.cpp:58
effect.h
stdext::shared_object_ptr
Definition: shared_object.h:39
Effect::asEffect
EffectPtr asEffect()
Definition: effect.h:41
game.h
Otc::EFFECT_TICKS_PER_FRAME
@ EFFECT_TICKS_PER_FRAME
Definition: const.h:44
TPoint< int >
ThingType::draw
void draw(const Point &dest, float scaleFactor, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, int reDrawFlags=Otc::ReDrawThing, LightView *lightView=nullptr)
Definition: thingtype.cpp:428
EventDispatcher::scheduleEvent
ScheduledEventPtr scheduleEvent(const std::function< void()> &callback, int delay)
Definition: eventdispatcher.cpp:82
g_things
ThingTypeManager g_things
Definition: thingtypemanager.cpp:38
Map::removeThing
bool removeThing(const ThingPtr &thing)
Definition: map.cpp:198
ThingTypeManager::isValidDatId
bool isValidDatId(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:75