missile.cpp

main 104 lines · 3.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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 "spritemanager.h"
28#include <framework/core/clock.h>
29#include <framework/core/eventdispatcher.h>
30
31void Missile::draw(const Point& dest, bool animate, LightView* lightView)
32{
33 if(m_id == 0 || !animate)
34 return;
35
36 int xPattern = 0, yPattern = 0;
37 if(m_direction == Otc::NorthWest) {
38 xPattern = 0;
39 yPattern = 0;
40 } else if(m_direction == Otc::North) {
41 xPattern = 1;
42 yPattern = 0;
43 } else if(m_direction == Otc::NorthEast) {
44 xPattern = 2;
45 yPattern = 0;
46 } else if(m_direction == Otc::East) {
47 xPattern = 2;
48 yPattern = 1;
49 } else if(m_direction == Otc::SouthEast) {
50 xPattern = 2;
51 yPattern = 2;
52 } else if(m_direction == Otc::South) {
53 xPattern = 1;
54 yPattern = 2;
55 } else if(m_direction == Otc::SouthWest) {
56 xPattern = 0;
57 yPattern = 2;
58 } else if(m_direction == Otc::West) {
59 xPattern = 0;
60 yPattern = 1;
61 } else {
62 xPattern = 1;
63 yPattern = 1;
64 }
65
66 float fraction = m_animationTimer.ticksElapsed() / m_duration;
67 rawGetThingType()->draw(dest + m_delta * fraction, 0, xPattern, yPattern, 0, 0, Color::white, lightView);
68}
69
70void Missile::setPath(const Position& fromPosition, const Position& toPosition)
71{
72 m_source = fromPosition;
73 m_destination = toPosition;
74
75 m_direction = fromPosition.getDirectionFromPosition(toPosition);
76
77 m_position = fromPosition;
78 m_delta = Point(toPosition.x - fromPosition.x, toPosition.y - fromPosition.y);
79 m_duration = 150 * std::sqrt(m_delta.length());
80 m_delta *= g_sprites.spriteSize();
81 m_animationTimer.restart();
82
83 // schedule removal
84 auto self = asMissile();
85 g_dispatcher.scheduleEvent([self]() { g_map.removeThing(self); }, m_duration);
86}
87
88void Missile::setId(uint32 id)
89{
90 if(!g_things.isValidDatId(id, ThingCategoryMissile))
91 id = 0;
92 m_id = id;
93}
94
95const ThingTypePtr& Missile::getThingType()
96{
97 return g_things.getThingType(m_id, ThingCategoryMissile);
98}
99
100ThingType* Missile::rawGetThingType()
101{
102 return g_things.rawGetThingType(m_id, ThingCategoryMissile);
103}
104
Top