2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02: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.
|
|
|
|
*/
|
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "item.h"
|
2011-08-31 17:03:33 +02:00
|
|
|
#include "thingstype.h"
|
2011-08-15 21:15:49 +02:00
|
|
|
#include "spritemanager.h"
|
2011-08-15 16:11:24 +02:00
|
|
|
#include "thing.h"
|
2012-02-02 17:55:42 +01:00
|
|
|
#include "tile.h"
|
2011-12-01 23:25:32 +01:00
|
|
|
#include <framework/core/clock.h>
|
2011-12-27 03:18:15 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
2012-01-30 01:00:12 +01:00
|
|
|
#include <framework/graphics/graphics.h>
|
|
|
|
#include <framework/graphics/paintershaderprogram.h>
|
2012-04-19 01:03:43 +02:00
|
|
|
#include <framework/graphics/painterogl2_shadersources.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-31 22:22:57 +02:00
|
|
|
Item::Item() : Thing()
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
m_id = 0;
|
2012-04-28 02:44:55 +02:00
|
|
|
m_countOrSubType = 1;
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2012-01-09 06:23:39 +01:00
|
|
|
ItemPtr Item::create(int id)
|
|
|
|
{
|
|
|
|
ItemPtr item = ItemPtr(new Item);
|
2012-02-09 04:45:19 +01:00
|
|
|
if(id < g_thingsType.getFirstItemId() || id > g_thingsType.getMaxItemid())
|
|
|
|
logTraceError("invalid item id ", id);
|
2012-04-11 05:37:46 +02:00
|
|
|
else {
|
2012-02-09 04:45:19 +01:00
|
|
|
item->setId(id);
|
2012-04-11 05:37:46 +02:00
|
|
|
}
|
2012-01-09 06:23:39 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
/*
|
2012-01-30 01:00:12 +01:00
|
|
|
PainterShaderProgramPtr itemProgram;
|
2012-04-07 05:37:29 +02:00
|
|
|
int ITEM_ID_UNIFORM = 10;
|
2012-04-19 01:03:43 +02:00
|
|
|
*/
|
2012-02-02 17:37:52 +01:00
|
|
|
void Item::draw(const Point& dest, float scaleFactor, bool animate)
|
2011-08-28 21:51:34 +02:00
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
if(m_id == 0)
|
|
|
|
return;
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
// determine animation phase
|
|
|
|
int animationPhase = 0;
|
|
|
|
if(getAnimationPhases() > 1) {
|
|
|
|
if(animate)
|
|
|
|
animationPhase = (g_clock.ticks() % (Otc::ITEM_TICKS_PER_FRAME * getAnimationPhases())) / Otc::ITEM_TICKS_PER_FRAME;
|
|
|
|
else
|
|
|
|
animationPhase = getAnimationPhases()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine x,y,z patterns
|
|
|
|
int xPattern = 0, yPattern = 0, zPattern = 0;
|
|
|
|
if(isGround()) {
|
|
|
|
xPattern = m_position.x % getNumPatternsX();
|
|
|
|
yPattern = m_position.y % getNumPatternsY();
|
|
|
|
zPattern = m_position.z % getNumPatternsZ();
|
|
|
|
} else if(isStackable() && getNumPatternsX() == 4 && getNumPatternsY() == 2) {
|
|
|
|
if(m_countOrSubType < 5) {
|
|
|
|
xPattern = m_countOrSubType-1;
|
|
|
|
yPattern = 0;
|
|
|
|
} else if(m_countOrSubType < 10) {
|
|
|
|
xPattern = 0;
|
|
|
|
yPattern = 1;
|
|
|
|
} else if(m_countOrSubType < 25) {
|
|
|
|
xPattern = 1;
|
|
|
|
yPattern = 1;
|
|
|
|
} else if(m_countOrSubType < 50) {
|
|
|
|
xPattern = 2;
|
|
|
|
yPattern = 1;
|
|
|
|
} else if(m_countOrSubType <= 100) {
|
|
|
|
xPattern = 3;
|
|
|
|
yPattern = 1;
|
|
|
|
}
|
|
|
|
} else if(isHangable()) {
|
2012-02-02 17:55:42 +01:00
|
|
|
const TilePtr& tile = getTile();
|
|
|
|
if(tile) {
|
|
|
|
if(tile->mustHookSouth())
|
|
|
|
xPattern = getNumPatternsX() >= 2 ? 1 : 0;
|
2012-04-05 05:20:40 +02:00
|
|
|
else if(tile->mustHookEast())
|
2012-02-02 17:55:42 +01:00
|
|
|
xPattern = getNumPatternsX() >= 3 ? 2 : 0;
|
|
|
|
}
|
2012-02-02 17:37:52 +01:00
|
|
|
} else if(isFluid() || isFluidContainer()) {
|
|
|
|
int color = Otc::FluidTransparent;
|
|
|
|
switch(m_countOrSubType) {
|
|
|
|
case Otc::FluidNone:
|
|
|
|
color = Otc::FluidTransparent;
|
|
|
|
break;
|
|
|
|
case Otc::FluidWater:
|
|
|
|
color = Otc::FluidBlue;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMana:
|
|
|
|
color = Otc::FluidPurple;
|
|
|
|
break;
|
|
|
|
case Otc::FluidBeer:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidOil:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidBlood:
|
|
|
|
color = Otc::FluidRed;
|
|
|
|
break;
|
|
|
|
case Otc::FluidSlime:
|
|
|
|
color = Otc::FluidGreen;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMud:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidLemonade:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMilk:
|
|
|
|
color = Otc::FluidWhite;
|
|
|
|
break;
|
|
|
|
case Otc::FluidWine:
|
|
|
|
color = Otc::FluidPurple;
|
|
|
|
break;
|
|
|
|
case Otc::FluidHealth:
|
|
|
|
color = Otc::FluidRed;
|
|
|
|
break;
|
|
|
|
case Otc::FluidUrine:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidRum:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidFruidJuice:
|
|
|
|
color = Otc::FluidYellow;
|
|
|
|
break;
|
|
|
|
case Otc::FluidCoconutMilk:
|
|
|
|
color = Otc::FluidWhite;
|
|
|
|
break;
|
|
|
|
case Otc::FluidTea:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
case Otc::FluidMead:
|
|
|
|
color = Otc::FluidBrown;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = Otc::FluidTransparent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xPattern = (color % 4) % getNumPatternsX();
|
|
|
|
yPattern = (color / 4) % getNumPatternsY();
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup item drawing shader
|
2012-04-19 01:03:43 +02:00
|
|
|
/*
|
2012-01-30 01:00:12 +01:00
|
|
|
if(!itemProgram) {
|
|
|
|
itemProgram = PainterShaderProgramPtr(new PainterShaderProgram);
|
|
|
|
itemProgram->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
|
|
|
|
itemProgram->addShaderFromSourceFile(Shader::Fragment, "/game_shaders/item.frag");
|
2012-02-20 03:27:08 +01:00
|
|
|
itemProgram->link();
|
2012-04-07 05:37:29 +02:00
|
|
|
itemProgram->bindUniformLocation(ITEM_ID_UNIFORM, "itemId");
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setShaderProgram(itemProgram);
|
2012-04-08 21:28:03 +02:00
|
|
|
//itemProgram->bind();
|
|
|
|
//itemProgram->setUniformValue(ITEM_ID_UNIFORM, (int)m_id);
|
2012-04-19 01:03:43 +02:00
|
|
|
*/
|
2011-08-28 21:51:34 +02:00
|
|
|
|
2012-02-02 21:10:14 +01:00
|
|
|
// now we can draw the item
|
|
|
|
internalDraw(dest, scaleFactor, xPattern, yPattern, zPattern, animationPhase);
|
2012-02-02 17:37:52 +01:00
|
|
|
|
|
|
|
// release draw shader
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->resetShaderProgram();
|
2011-08-28 21:51:34 +02:00
|
|
|
}
|
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
void Item::setId(uint32 id)
|
2011-08-28 21:47:23 +02:00
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
if(id < g_thingsType.getFirstItemId() || id > g_thingsType.getMaxItemid()) {
|
|
|
|
logTraceError("invalid item id ", id);
|
|
|
|
return;
|
2011-08-25 15:30:46 +02:00
|
|
|
}
|
2012-02-02 17:37:52 +01:00
|
|
|
m_id = id;
|
|
|
|
m_type = g_thingsType.getThingType(m_id, ThingsType::Item);
|
2011-08-28 21:47:23 +02:00
|
|
|
}
|