tibia-client/src/otclient/missile.cpp

100 lines
3.2 KiB
C++
Raw Normal View History

2011-11-08 02:44:30 +01:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-11-08 02:44:30 +01:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "missile.h"
2012-06-21 19:54:20 +02:00
#include "thingtypemanager.h"
2011-11-08 02:44:30 +01:00
#include "map.h"
#include "tile.h"
#include <framework/core/clock.h>
2011-11-08 02:44:30 +01:00
#include <framework/core/eventdispatcher.h>
2012-02-02 21:54:49 +01:00
void Missile::draw(const Point& dest, float scaleFactor, bool animate)
2011-11-08 02:44:30 +01:00
{
2012-02-02 21:54:49 +01:00
if(m_id == 0 || !animate)
return;
2011-11-08 02:44:30 +01:00
int xPattern = 0, yPattern = 0;
2012-02-02 21:54:49 +01:00
if(m_direction == Otc::NorthWest) {
xPattern = 0;
yPattern = 0;
2012-02-02 21:54:49 +01:00
} else if(m_direction == Otc::North) {
xPattern = 1;
yPattern = 0;
} else if(m_direction == Otc::NorthEast) {
xPattern = 2;
yPattern = 0;
} else if(m_direction == Otc::East) {
xPattern = 2;
yPattern = 1;
} else if(m_direction == Otc::SouthEast) {
xPattern = 2;
yPattern = 2;
} else if(m_direction == Otc::South) {
xPattern = 1;
yPattern = 2;
} else if(m_direction == Otc::SouthWest) {
xPattern = 0;
yPattern = 2;
} else if(m_direction == Otc::West) {
xPattern = 0;
yPattern = 1;
} else {
xPattern = 1;
yPattern = 1;
2011-11-08 02:44:30 +01:00
}
2012-02-02 21:54:49 +01:00
float fraction = m_animationTimer.ticksElapsed() / m_duration;
2012-07-19 23:22:52 +02:00
rawGetThingType()->draw(dest + m_delta * fraction * scaleFactor, scaleFactor, 0, xPattern, yPattern, 0, 0);
}
void Missile::setPath(const Position& fromPosition, const Position& toPosition)
{
m_direction = fromPosition.getDirectionFromPosition(toPosition);
2012-01-30 01:00:12 +01:00
m_position = fromPosition;
2012-02-02 21:54:49 +01:00
m_delta = Point(toPosition.x - fromPosition.x, toPosition.y - fromPosition.y);
m_duration = 150 * std::sqrt(m_delta.length());
m_delta *= Otc::TILE_PIXELS;
m_animationTimer.restart();
2011-11-08 02:44:30 +01:00
// schedule removal
auto self = asMissile();
2012-06-26 00:13:30 +02:00
g_dispatcher.scheduleEvent([self]() { g_map.removeThing(self); }, m_duration);
2011-11-08 02:44:30 +01:00
}
void Missile::setId(uint32 id)
2011-11-08 02:44:30 +01:00
{
if(!g_things.isValidDatId(id, ThingCategoryMissile))
id = 0;
m_id = id;
}
2012-07-19 23:22:52 +02:00
const ThingTypePtr& Missile::getThingType()
{
return g_things.getThingType(m_id, ThingCategoryMissile);
}
2012-07-19 23:22:52 +02:00
ThingType* Missile::rawGetThingType()
{
return g_things.rawGetThingType(m_id, ThingCategoryMissile);
2011-11-08 02:44:30 +01:00
}