summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-04-13 03:36:00 +0300
committerAndrei Karas <akaras@inbox.ru>2011-04-13 03:36:00 +0300
commitc978b7f0d9de1bb6bd84cd299ed80c27c5147927 (patch)
tree8f1edc2be0816693761ba58aa7c7c554040a7db6 /src
parentda1b2d8311ca78eccd3e9875598562ce6bf05cab (diff)
downloadmanaplus-c978b7f0d9de1bb6bd84cd299ed80c27c5147927.tar.gz
manaplus-c978b7f0d9de1bb6bd84cd299ed80c27c5147927.tar.bz2
manaplus-c978b7f0d9de1bb6bd84cd299ed80c27c5147927.tar.xz
manaplus-c978b7f0d9de1bb6bd84cd299ed80c27c5147927.zip
Impliment new tags in sprites animations and random condition.
Tags: <label>, <goto>
Diffstat (limited to 'src')
-rw-r--r--src/actorsprite.cpp5
-rw-r--r--src/animatedsprite.cpp69
-rw-r--r--src/gui/widgets/progressindicator.cpp2
-rw-r--r--src/particleemitter.cpp16
-rw-r--r--src/resources/animation.cpp27
-rw-r--r--src/resources/animation.h13
-rw-r--r--src/resources/mapreader.cpp3
-rw-r--r--src/resources/spritedef.cpp23
-rw-r--r--src/simpleanimation.cpp7
9 files changed, 127 insertions, 38 deletions
diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp
index 686c871e7..d87156285 100644
--- a/src/actorsprite.cpp
+++ b/src/actorsprite.cpp
@@ -488,8 +488,9 @@ void ActorSprite::loadTargetCursor(const std::string &filename,
{
// anim->addFrame(currentImageSet->get(i), 0,
anim->addFrame(currentImageSet->get(i), 75,
- (16 - (currentImageSet->getWidth() / 2)),
- (16 - (currentImageSet->getHeight() / 2)));
+ (16 - (currentImageSet->getWidth() / 2)),
+ (16 - (currentImageSet->getHeight() / 2)),
+ 100);
}
SimpleAnimation *currentCursor = new SimpleAnimation(anim);
diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp
index b9946fb1d..1069bcf47 100644
--- a/src/animatedsprite.cpp
+++ b/src/animatedsprite.cpp
@@ -142,30 +142,77 @@ bool AnimatedSprite::updateCurrentAnimation(unsigned int time)
mFrameTime += time;
- while (mFrameTime > static_cast<unsigned>(mFrame->delay)
- && mFrame->delay > 0)
+ while ((mFrameTime > static_cast<unsigned>(mFrame->delay)
+ && mFrame->delay > 0) || mFrame->type != Frame::ANIMATION)
{
+ bool fail(true);
mFrameTime -= static_cast<unsigned>(mFrame->delay);
mFrameIndex++;
- if (mFrameIndex == mAnimation->getLength())
+ if (mFrameIndex >= mAnimation->getLength())
mFrameIndex = 0;
mFrame = mAnimation->getFrame(mFrameIndex);
- if (Animation::isTerminator(*mFrame))
+ if (mFrame->type == Frame::LABEL && !mFrame->nextAction.empty())
{
- mAnimation = 0;
- mFrame = 0;
- return false;
+ fail = true;
+ }
+ else if (mFrame->type == Frame::GOTO && !mFrame->nextAction.empty())
+ {
+ if (mFrame->rand == 100 || rand() % 100 <= mFrame->rand)
+ {
+ for (int i = 0; i < mAnimation->getLength(); i ++)
+ {
+ Frame *frame = mAnimation->getFrame(i);
+ if (frame->type == Frame::LABEL
+ && mFrame->nextAction == frame->nextAction)
+ {
+ mFrameTime = 0;
+ mFrameIndex = i;
+ if (mFrameIndex >= mAnimation->getLength())
+ mFrameIndex = 0;
+
+ mFrame = mAnimation->getFrame(mFrameIndex);
+
+ fail = true;
+ break;
+ }
+ }
+ }
+ }
+ else if (mFrame->type == Frame::JUMP && !mFrame->nextAction.empty())
+ {
+ if (mFrame->rand == 100 || rand() % 100 <= mFrame->rand)
+ {
+ play(mFrame->nextAction);
+ return true;
+ }
+ }
+ else if (Animation::isTerminator(*mFrame))
+ {
+ if (mFrame->rand == 100 || rand() % 100 <= mFrame->rand)
+ {
+ mAnimation = 0;
+ mFrame = 0;
+ return false;
+ }
}
- if (mFrame->type == Frame::JUMP && !mFrame->nextAction.empty())
+ else
{
- play(mFrame->nextAction);
- return true;
+ if (mFrame->rand == 100 || mFrameIndex >= mAnimation->getLength())
+ {
+ fail = false;
+ }
+ else
+ {
+ if (rand() % 100 <= mFrame->rand)
+ fail = false;
+ }
}
+ if (fail)
+ mFrameTime = mFrame->delay + 1;
}
-
return true;
}
diff --git a/src/gui/widgets/progressindicator.cpp b/src/gui/widgets/progressindicator.cpp
index 936e36ada..af48d0649 100644
--- a/src/gui/widgets/progressindicator.cpp
+++ b/src/gui/widgets/progressindicator.cpp
@@ -41,7 +41,7 @@ ProgressIndicator::ProgressIndicator()
if (images)
{
for (ImageSet::size_type i = 0; i < images->size(); ++i)
- anim->addFrame(images->get(i), 100, 0, 0);
+ anim->addFrame(images->get(i), 100, 0, 0, 100);
mIndicator = new SimpleAnimation(anim);
diff --git a/src/particleemitter.cpp b/src/particleemitter.cpp
index b3f8f40d7..1c3e63f30 100644
--- a/src/particleemitter.cpp
+++ b/src/particleemitter.cpp
@@ -215,6 +215,7 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
int delay = XML::getProperty(frameNode, "delay", 0);
int offsetX = XML::getProperty(frameNode, "offsetX", 0);
int offsetY = XML::getProperty(frameNode, "offsetY", 0);
+ int rand = XML::getProperty(frameNode, "rand", 100);
if (!imageset)
{
logger->log1("Error: no valid imageset");
@@ -242,7 +243,8 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
continue;
}
- mParticleRotation.addFrame(img, delay, offsetX, offsetY);
+ mParticleRotation.addFrame(img, delay,
+ offsetX, offsetY, rand);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
{
@@ -266,13 +268,13 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
}
mParticleRotation.addFrame(img, delay,
- offsetX, offsetY);
+ offsetX, offsetY, rand);
start++;
}
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
{
- mParticleRotation.addTerminator();
+ mParticleRotation.addTerminator(rand);
}
} // for frameNode
}
@@ -296,6 +298,7 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
int delay = XML::getProperty(frameNode, "delay", 0);
int offsetX = XML::getProperty(frameNode, "offsetX", 0);
int offsetY = XML::getProperty(frameNode, "offsetY", 0);
+ int rand = XML::getProperty(frameNode, "rand", 100);
offsetY -= imageset->getHeight() - 32;
offsetX -= imageset->getWidth() / 2 - 16;
@@ -317,7 +320,8 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
continue;
}
- mParticleAnimation.addFrame(img, delay, offsetX, offsetY);
+ mParticleAnimation.addFrame(img, delay,
+ offsetX, offsetY, rand);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
{
@@ -341,13 +345,13 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target,
}
mParticleAnimation.addFrame(img, delay,
- offsetX, offsetY);
+ offsetX, offsetY, rand);
start++;
}
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
{
- mParticleAnimation.addTerminator();
+ mParticleAnimation.addTerminator(rand);
}
} // for frameNode
}
diff --git a/src/resources/animation.cpp b/src/resources/animation.cpp
index 98ea19f2a..1154d9c9d 100644
--- a/src/resources/animation.cpp
+++ b/src/resources/animation.cpp
@@ -29,25 +29,38 @@ Animation::Animation():
{
}
-void Animation::addFrame(Image *image, int delay, int offsetX, int offsetY)
+void Animation::addFrame(Image *image, int delay, int offsetX, int offsetY,
+ int rand)
{
- Frame frame = { image, delay, offsetX, offsetY, Frame::ANIMATION, "" };
+ Frame frame = { image, delay, offsetX, offsetY, rand, Frame::ANIMATION, "" };
mFrames.push_back(frame);
mDuration += delay;
}
-void Animation::addTerminator()
+void Animation::addTerminator(int rand)
{
- addFrame(NULL, 0, 0, 0);
+ addFrame(NULL, 0, 0, 0, rand);
}
bool Animation::isTerminator(const Frame &candidate)
{
- return (candidate.image == NULL);
+ return (candidate.image == NULL && candidate.type == Frame::ANIMATION);
}
-void Animation::addJump(std::string name)
+void Animation::addJump(std::string name, int rand)
{
- Frame frame = { 0, 0, 0, 0, Frame::JUMP, name };
+ Frame frame = { 0, 0, 0, 0, rand, Frame::JUMP, name };
+ mFrames.push_back(frame);
+}
+
+void Animation::addLabel(std::string name)
+{
+ Frame frame = { 0, 0, 0, 0, 100, Frame::LABEL, name };
+ mFrames.push_back(frame);
+}
+
+void Animation::addGoto(std::string name, int rand)
+{
+ Frame frame = { 0, 0, 0, 0, rand, Frame::GOTO, name };
mFrames.push_back(frame);
}
diff --git a/src/resources/animation.h b/src/resources/animation.h
index 5130bc8dd..0c6f45681 100644
--- a/src/resources/animation.h
+++ b/src/resources/animation.h
@@ -39,12 +39,14 @@ struct Frame
{
ANIMATION = 0,
JUMP,
+ GOTO,
LABEL
};
Image *image;
int delay;
int offsetX;
int offsetY;
+ int rand;
FrameType type;
std::string nextAction;
};
@@ -61,13 +63,14 @@ class Animation
/**
* Appends a new animation at the end of the sequence.
*/
- void addFrame(Image *image, int delay, int offsetX, int offsetY);
+ void addFrame(Image *image, int delay, int offsetX, int offsetY,
+ int rand);
/**
* Appends an animation terminator that states that the animation
* should not loop.
*/
- void addTerminator();
+ void addTerminator(int rand);
/**
* Returns the frame at the specified index.
@@ -87,7 +90,11 @@ class Animation
int getDuration() const
{ return mDuration; }
- void addJump(std::string name);
+ void addJump(std::string name, int rand);
+
+ void addLabel(std::string name);
+
+ void addGoto(std::string name, int rand);
/**
* Determines whether the given animation frame is a terminator.
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp
index d2bcde06d..3bd77315a 100644
--- a/src/resources/mapreader.cpp
+++ b/src/resources/mapreader.cpp
@@ -705,11 +705,12 @@ Tileset *MapReader::readTileset(xmlNodePtr node, const std::string &path,
"animation-frame" + toString(i));
iDelay = tileProperties.find(
"animation-delay" + toString(i));
+ // possible need add random attribute?
if (iFrame != tileProperties.end()
&& iDelay != tileProperties.end())
{
ani->addFrame(set->get(iFrame->second),
- iDelay->second, 0, 0);
+ iDelay->second, 0, 0, 100);
}
else
{
diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp
index e6a65926e..1eea6547d 100644
--- a/src/resources/spritedef.cpp
+++ b/src/resources/spritedef.cpp
@@ -225,6 +225,8 @@ void SpriteDef::loadAnimation(xmlNodePtr animationNode,
imageSet->getOffsetX();
int offsetY = XML::getProperty(frameNode, "offsetY", 0) +
imageSet->getOffsetY();
+ int rand = XML::getProperty(frameNode, "rand", 100);
+
offsetY -= imageSet->getHeight() - 32;
offsetX -= imageSet->getWidth() / 2 - 16;
@@ -246,7 +248,7 @@ void SpriteDef::loadAnimation(xmlNodePtr animationNode,
continue;
}
- animation->addFrame(img, delay, offsetX, offsetY);
+ animation->addFrame(img, delay, offsetX, offsetY, rand);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
{
@@ -282,7 +284,7 @@ void SpriteDef::loadAnimation(xmlNodePtr animationNode,
continue;
}
- animation->addFrame(img, delay, offsetX, offsetY);
+ animation->addFrame(img, delay, offsetX, offsetY, rand);
pos ++;
}
repeat --;
@@ -290,11 +292,24 @@ void SpriteDef::loadAnimation(xmlNodePtr animationNode,
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
{
- animation->addTerminator();
+ animation->addTerminator(rand);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "jump"))
{
- animation->addJump(XML::getProperty(frameNode, "action", ""));
+ animation->addJump(XML::getProperty(
+ frameNode, "action", ""), rand);
+ }
+ else if (xmlStrEqual(frameNode->name, BAD_CAST "label"))
+ {
+ std::string name = XML::getProperty(frameNode, "name", "");
+ if (!name.empty())
+ animation->addLabel(name);
+ }
+ else if (xmlStrEqual(frameNode->name, BAD_CAST "goto"))
+ {
+ std::string name = XML::getProperty(frameNode, "label", "");
+ if (!name.empty())
+ animation->addGoto(name, rand);
}
} // for frameNode
}
diff --git a/src/simpleanimation.cpp b/src/simpleanimation.cpp
index 9931f491c..8d86583eb 100644
--- a/src/simpleanimation.cpp
+++ b/src/simpleanimation.cpp
@@ -155,6 +155,7 @@ void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode)
int delay = XML::getProperty(frameNode, "delay", 0);
int offsetX = XML::getProperty(frameNode, "offsetX", 0);
int offsetY = XML::getProperty(frameNode, "offsetY", 0);
+ int rand = XML::getProperty(frameNode, "rand", 100);
offsetY -= imageset->getHeight() - 32;
offsetX -= imageset->getWidth() / 2 - 16;
@@ -177,7 +178,7 @@ void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode)
}
if (mAnimation)
- mAnimation->addFrame(img, delay, offsetX, offsetY);
+ mAnimation->addFrame(img, delay, offsetX, offsetY, rand);
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
{
@@ -201,14 +202,14 @@ void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode)
}
if (mAnimation)
- mAnimation->addFrame(img, delay, offsetX, offsetY);
+ mAnimation->addFrame(img, delay, offsetX, offsetY, rand);
start++;
}
}
else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
{
if (mAnimation)
- mAnimation->addTerminator();
+ mAnimation->addTerminator(rand);
}
}