Otclient  14/8/2020
missile.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 "missile.h"
24 #include "thingtypemanager.h"
25 #include "map.h"
26 #include "tile.h"
27 #include <framework/core/clock.h>
29 
30 void Missile::draw(const Point& dest, float scaleFactor, bool animate, LightView *lightView)
31 {
32  if(m_id == 0 || !animate)
33  return;
34 
35  int xPattern = 0, yPattern = 0;
36  if(m_direction == Otc::NorthWest) {
37  xPattern = 0;
38  yPattern = 0;
39  } else if(m_direction == Otc::North) {
40  xPattern = 1;
41  yPattern = 0;
42  } else if(m_direction == Otc::NorthEast) {
43  xPattern = 2;
44  yPattern = 0;
45  } else if(m_direction == Otc::East) {
46  xPattern = 2;
47  yPattern = 1;
48  } else if(m_direction == Otc::SouthEast) {
49  xPattern = 2;
50  yPattern = 2;
51  } else if(m_direction == Otc::South) {
52  xPattern = 1;
53  yPattern = 2;
54  } else if(m_direction == Otc::SouthWest) {
55  xPattern = 0;
56  yPattern = 2;
57  } else if(m_direction == Otc::West) {
58  xPattern = 0;
59  yPattern = 1;
60  } else {
61  xPattern = 1;
62  yPattern = 1;
63  }
64 
65  float fraction = m_animationTimer.ticksElapsed() / m_duration;
66  rawGetThingType()->draw(dest + m_delta * fraction * scaleFactor, scaleFactor, 0, xPattern, yPattern, 0, 0, lightView);
67 }
68 
69 void Missile::setPath(const Position& fromPosition, const Position& toPosition)
70 {
71  m_direction = fromPosition.getDirectionFromPosition(toPosition);
72 
73  m_position = fromPosition;
74  m_delta = Point(toPosition.x - fromPosition.x, toPosition.y - fromPosition.y);
75  m_duration = 150 * std::sqrt(m_delta.length());
76  m_delta *= Otc::TILE_PIXELS;
77  m_animationTimer.restart();
78 
79  // schedule removal
80  auto self = asMissile();
81  g_dispatcher.scheduleEvent([self]() { g_map.removeThing(self); }, m_duration);
82 }
83 
85 {
87  id = 0;
88  m_id = id;
89 }
90 
92 {
94 }
95 
97 {
99 }
ThingTypeManager::rawGetThingType
ThingType * rawGetThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:57
thingtypemanager.h
Otc::TILE_PIXELS
@ TILE_PIXELS
Definition: const.h:29
eventdispatcher.h
Missile::draw
void draw(const Point &dest, float scaleFactor, bool animate, LightView *lightView=nullptr)
Definition: missile.cpp:30
g_map
Map g_map
Definition: map.cpp:36
Missile::rawGetThingType
ThingType * rawGetThingType()
Definition: missile.cpp:96
ThingCategoryMissile
@ ThingCategoryMissile
Definition: thingtype.h:46
Timer::ticksElapsed
ticks_t ticksElapsed()
Definition: timer.cpp:33
Otc::North
@ North
Definition: const.h:162
Position::x
int x
Definition: position.h:243
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
uint32
uint32_t uint32
Definition: types.h:35
Point
TPoint< int > Point
Definition: point.h:86
Position::y
int y
Definition: position.h:244
LightView
Definition: lightview.h:37
clock.h
Thing::m_position
Position m_position
Definition: thing.h:131
Otc::NorthEast
@ NorthEast
Definition: const.h:166
Missile::getThingType
const ThingTypePtr & getThingType()
Definition: missile.cpp:91
Otc::West
@ West
Definition: const.h:165
Position
Definition: position.h:33
missile.h
map.h
Timer::restart
void restart()
Definition: timer.cpp:27
ThingTypeManager::getThingType
const ThingTypePtr & getThingType(uint16 id, ThingCategory category)
Definition: thingtypemanager.cpp:377
ThingType
Definition: thingtype.h:123
Otc::SouthEast
@ SouthEast
Definition: const.h:167
Missile::asMissile
MissilePtr asMissile()
Definition: missile.h:45
Position::getDirectionFromPosition
Otc::Direction getDirectionFromPosition(const Position &position) const
Definition: position.h:177
Otc::South
@ South
Definition: const.h:164
TPoint::length
float length() const
Definition: point.h:76
Otc::NorthWest
@ NorthWest
Definition: const.h:169
Missile::setPath
void setPath(const Position &fromPosition, const Position &toPosition)
Definition: missile.cpp:69
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
Otc::East
@ East
Definition: const.h:163
TPoint< int >
EventDispatcher::scheduleEvent
ScheduledEventPtr scheduleEvent(const std::function< void()> &callback, int delay)
Definition: eventdispatcher.cpp:82
tile.h
g_things
ThingTypeManager g_things
Definition: thingtypemanager.cpp:38
Map::removeThing
bool removeThing(const ThingPtr &thing)
Definition: map.cpp:177
Otc::SouthWest
@ SouthWest
Definition: const.h:168
Missile::setId
void setId(uint32 id)
Definition: missile.cpp:84
ThingTypeManager::isValidDatId
bool isValidDatId(uint16 id, ThingCategory category)
Definition: thingtypemanager.h:75