Otclient  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"
24 #include "map.h"
25 #include "game.h"
27 
28 void Effect::drawEffect(const Point& dest, float scaleFactor, bool animate, int offsetX, int offsetY, LightView *lightView)
29 {
30  if(m_id == 0)
31  return;
32 
33  int animationPhase = 0;
34  if(animate) {
36  // This requires a separate getPhaseAt method as using getPhase would make all magic effects use the same phase regardless of their appearance time
37  animationPhase = rawGetThingType()->getAnimator()->getPhaseAt(m_animationTimer.ticksElapsed());
38  } else {
39  // hack to fix some animation phases duration, currently there is no better solution
40  int ticks = EFFECT_TICKS_PER_FRAME;
41  if (m_id == 33) {
42  ticks <<= 2;
43  }
44 
45  animationPhase = std::min<int>((int)(m_animationTimer.ticksElapsed() / ticks), getAnimationPhases() - 1);
46  }
47  }
48 
49  int xPattern = offsetX % getNumPatternX();
50  if(xPattern < 0)
51  xPattern += getNumPatternX();
52 
53  int yPattern = offsetY % getNumPatternY();
54  if(yPattern < 0)
55  yPattern += getNumPatternY();
56 
57  rawGetThingType()->draw(dest, scaleFactor, 0, xPattern, yPattern, 0, animationPhase, lightView);
58 }
59 
61 {
62  m_animationTimer.restart();
63 
64  int duration = 0;
66  duration = getThingType()->getAnimator()->getTotalDuration();
67  } else {
68  duration = 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  duration <<= 2;
73  }
74 
75  duration *= getAnimationPhases();
76  }
77 
78  // schedule removal
79  auto self = asEffect();
80  g_dispatcher.scheduleEvent([self]() { g_map.removeThing(self); }, duration);
81 }
82 
84 {
86  id = 0;
87  m_id = id;
88 }
89 
91 {
93 }
94 
96 {
98 }
ThingTypeManager::rawGetThingType
ThingType * rawGetThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:57
eventdispatcher.h
g_map
Map g_map
Definition: map.cpp:36
Timer::ticksElapsed
ticks_t ticksElapsed()
Definition: timer.cpp:33
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
uint32
uint32_t uint32
Definition: types.h:35
Otc::GameEnhancedAnimations
@ GameEnhancedAnimations
Definition: const.h:402
Animator::getTotalDuration
ticks_t getTotalDuration()
Definition: animator.cpp:219
Thing::getAnimationPhases
int getAnimationPhases()
Definition: thing.h:76
g_game
Game g_game
Definition: game.cpp:37
ThingCategoryEffect
@ ThingCategoryEffect
Definition: thingtype.h:45
LightView
Definition: lightview.h:37
Thing::getNumPatternX
int getNumPatternX()
Definition: thing.h:73
Effect::onAppear
void onAppear()
Definition: effect.cpp:60
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()
Definition: effect.cpp:90
Effect::drawEffect
void drawEffect(const Point &dest, float scaleFactor, bool animate, int offsetX=0, int offsetY=0, LightView *lightView=nullptr)
Definition: effect.cpp:28
Thing::getNumPatternY
int getNumPatternY()
Definition: thing.h:74
Effect::rawGetThingType
ThingType * rawGetThingType()
Definition: effect.cpp:95
map.h
Timer::restart
void restart()
Definition: timer.cpp:27
ThingTypeManager::getThingType
const ThingTypePtr & getThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.cpp:377
Game::getFeature
bool getFeature(Otc::GameFeature feature)
Definition: game.h:310
Effect::setId
void setId(uint32 id)
Definition: effect.cpp:83
ThingType
Definition: thingtype.h:123
ThingType::getAnimator
AnimatorPtr getAnimator()
Definition: thingtype.h:151
effect.h
stdext::shared_object_ptr< ThingType >
ThingType::draw
void draw(const Point &dest, float scaleFactor, int layer, int xPattern, int yPattern, int zPattern, int animationPhase, LightView *lightView=nullptr)
Definition: thingtype.cpp:374
Effect::asEffect
EffectPtr asEffect()
Definition: effect.h:43
game.h
TPoint< int >
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:177
ThingTypeManager::isValidDatId
bool isValidDatId(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:75