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"
|
2011-12-01 23:25:32 +01:00
|
|
|
#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)
|
2012-02-02 17:37:52 +01:00
|
|
|
return;
|
2011-11-08 02:44:30 +01:00
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
int xPattern = 0, yPattern = 0;
|
2012-02-02 21:54:49 +01:00
|
|
|
if(m_direction == Otc::NorthWest) {
|
2012-02-02 17:37:52 +01:00
|
|
|
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);
|
2012-02-02 17:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-02-02 17:37:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
void Missile::setId(uint32 id)
|
2011-11-08 02:44:30 +01:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
if(!g_things.isValidDatId(id, ThingCategoryMissile))
|
2012-07-18 01:49:21 +02:00
|
|
|
id = 0;
|
2012-02-02 17:37:52 +01:00
|
|
|
m_id = id;
|
2012-07-15 15:23:13 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 23:22:52 +02:00
|
|
|
const ThingTypePtr& Missile::getThingType()
|
2012-07-15 15:23:13 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
return g_things.getThingType(m_id, ThingCategoryMissile);
|
2012-07-15 15:23:13 +02:00
|
|
|
}
|
|
|
|
|
2012-07-19 23:22:52 +02:00
|
|
|
ThingType* Missile::rawGetThingType()
|
2012-07-15 15:23:13 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
return g_things.rawGetThingType(m_id, ThingCategoryMissile);
|
2011-11-08 02:44:30 +01:00
|
|
|
}
|