bool RuntimeSpriteObject::SetDirection( float nb ) { if ( currentAnimation >= GetAnimationsCount() ) return false; if ( !animations[currentAnimation].Get().useMultipleDirections ) { currentAngle = nb; needUpdateCurrentSprite = true; return true; } else { if ( nb >= animations[currentAnimation].Get().GetDirectionsCount() || animations[currentAnimation].Get().GetDirection( nb ).HasNoSprites() ) return false; if ( nb == currentDirection ) return true; currentDirection = nb; currentSprite = 0; timeElapsedOnCurrentSprite = 0; needUpdateCurrentSprite = true; return true; } }
bool RuntimeSpriteObject::AnimationEnded() const { if (currentAnimation >= GetAnimationsCount()) return true; const gd::Direction & direction = animations[currentAnimation].Get().GetDirection( currentDirection ); return ( !direction.IsLooping() && currentSprite == direction.GetSpritesCount()-1 ); }
const Sprite * SpriteObject::GetInitialInstanceSprite(gd::InitialInstance & instance, gd::Project & project, gd::Layout & layout, bool * shouldNotRotate) const { if ( HasNoAnimations() ) return NULL; //Search the first sprite of the current animation/direction. std::size_t animationId = instance.floatInfos.find("animation") != instance.floatInfos.end() ? instance.floatInfos.find("animation")->second : 0; if ( animationId >= GetAnimationsCount() ) animationId = 0; const Animation & animation = GetAnimation(animationId); if ( animation.HasNoDirections() ) return NULL; std::size_t directionId = 0; if ( animation.useMultipleDirections ) { float normalizedAngle = static_cast<int>(instance.GetAngle())%360; if ( normalizedAngle < 0 ) normalizedAngle += 360; directionId = static_cast<int>(gd::Round(normalizedAngle/45.f))%8; } if ( directionId >= animation.GetDirectionsCount() ) directionId = 0; const Direction & direction = animation.GetDirection(directionId); if ( shouldNotRotate ) *shouldNotRotate = animation.useMultipleDirections; return direction.HasNoSprites() ? NULL : &direction.GetSprite(0); }
void RuntimeSpriteObject::UpdateTime(float elapsedTime) { if ( animationStopped || currentAnimation >= GetAnimationsCount() ) return; timeElapsedOnCurrentSprite += elapsedTime * animationSpeedScale; const gd::Direction & direction = animations[currentAnimation].Get().GetDirection( currentDirection ); float delay = direction.GetTimeBetweenFrames(); if ( timeElapsedOnCurrentSprite > delay ) { if ( delay != 0 ) { std::size_t frameCount = static_cast<std::size_t>( timeElapsedOnCurrentSprite / delay ); currentSprite += frameCount; } else currentSprite++; timeElapsedOnCurrentSprite = 0; } if ( currentSprite >= direction.GetSpritesCount() ) { if ( direction.IsLooping() ) currentSprite = 0; else currentSprite = direction.GetSpritesCount() - 1; } needUpdateCurrentSprite = true; }
/** * Update the time elpased on the current sprite, and change this latter if needed. */ void RuntimeSpriteObject::UpdateTime(float elapsedTime) { if ( animationStopped || currentAnimation >= GetAnimationsCount() ) return; timeElapsedOnCurrentSprite += elapsedTime; const gd::Direction & direction = animations[currentAnimation].Get().GetDirection( currentDirection ); float delay = direction.GetTimeBetweenFrames(); //On gère l'avancement du sprite actuel suivant le temps entre chaque sprite if ( timeElapsedOnCurrentSprite > delay ) { if ( delay != 0 ) { unsigned int frameCount = static_cast<unsigned int>( timeElapsedOnCurrentSprite / delay ); currentSprite += frameCount; } else currentSprite++; timeElapsedOnCurrentSprite = 0; } if ( currentSprite >= direction.GetSpritesCount() ) { if ( direction.IsLooping() ) currentSprite = 0; else currentSprite = direction.GetSpritesCount() - 1; } needUpdateCurrentSprite = true; }
bool SpriteObject::RemoveAnimation(std::size_t nb) { if ( nb >= GetAnimationsCount() ) return false; animations.erase( animations.begin() + nb ); return true; }
float RuntimeSpriteObject::GetCurrentDirectionOrAngle() const { if ( currentAnimation >= GetAnimationsCount() ) return 0; if ( animations[currentAnimation].Get().useMultipleDirections ) return GetCurrentDirection(); else return GetAngle(); }
void SpriteObject::ExposeResources(gd::ArbitraryResourceWorker & worker) { for ( unsigned int j = 0; j < GetAnimationsCount();j++ ) { for ( unsigned int k = 0;k < GetAnimation( j ).GetDirectionsCount();k++ ) { for ( unsigned int l = 0;l < GetAnimation( j ).GetDirection(k).GetSpritesCount();l++ ) worker.ExposeImage(GetAnimation( j ).GetDirection(k).GetSprite(l).GetImageName()); } } }
bool RuntimeSpriteObject::SetSprite( std::size_t nb ) { if ( currentAnimation >= GetAnimationsCount() || currentDirection >= animations[currentAnimation].Get().GetDirectionsCount() || nb >= animations[currentAnimation].Get().GetDirection( currentDirection ).GetSpritesCount() ) return false; currentSprite = nb; timeElapsedOnCurrentSprite = 0; needUpdateCurrentSprite = true; return true; }
bool RuntimeSpriteObject::SetCurrentAnimation( std::size_t nb ) { if ( nb >= GetAnimationsCount() ) return false; if ( nb == currentAnimation ) return true; currentAnimation = nb; currentSprite = 0; timeElapsedOnCurrentSprite = 0; needUpdateCurrentSprite = true; return true; }
bool RuntimeSpriteObject::ChangeProperty(std::size_t propertyNb, gd::String newValue) { if ( propertyNb == 0 ) { return SetCurrentAnimation(newValue.To<int>()); } else if ( propertyNb == 1 ) { if ( currentAnimation >= GetAnimationsCount() ) return false; return animations[currentAnimation].Get().useMultipleDirections ? SetDirection(newValue.To<std::size_t>()) : SetAngle(newValue.To<float>()); } else if ( propertyNb == 2 ) { return SetSprite(newValue.To<int>()); } else if ( propertyNb == 3 ) { SetOpacity(newValue.To<float>()); } else if ( propertyNb == 4 ) { SetBlendMode(newValue.To<int>()); } else if ( propertyNb == 5 ) {SetScaleX(newValue.To<float>());} else if ( propertyNb == 6 ) {SetScaleY(newValue.To<float>());} return true; }
bool RuntimeSpriteObject::ChangeProperty(unsigned int propertyNb, std::string newValue) { if ( propertyNb == 0 ) { return SetCurrentAnimation(ToInt(newValue)); } else if ( propertyNb == 1 ) { if ( currentAnimation >= GetAnimationsCount() ) return false; return animations[currentAnimation].Get().useMultipleDirections ? SetDirection(ToInt(newValue)) : SetAngle(ToFloat(newValue)); } else if ( propertyNb == 2 ) { return SetSprite(ToInt(newValue)); } else if ( propertyNb == 3 ) { SetOpacity(ToFloat(newValue)); } else if ( propertyNb == 4 ) { SetBlendMode(ToInt(newValue)); } else if ( propertyNb == 5 ) {SetScaleX(ToFloat(newValue));} else if ( propertyNb == 6 ) {SetScaleY(ToFloat(newValue));} return true; }
void SpriteObject::DoSerializeTo(gd::SerializerElement & element) const { //Animations gd::SerializerElement & animationsElement = element.AddChild("animations"); animationsElement.ConsiderAsArrayOf("animation"); for ( std::size_t k = 0;k < GetAnimationsCount();k++ ) { gd::SerializerElement & animationElement = animationsElement.AddChild("animation"); animationElement.SetAttribute( "useMultipleDirections", GetAnimation(k).useMultipleDirections); gd::SerializerElement & directionsElement = animationElement.AddChild("directions"); directionsElement.ConsiderAsArrayOf("direction"); for ( std::size_t l = 0;l < GetAnimation(k).GetDirectionsCount();l++ ) { GetAnimation(k).GetDirection(l).SerializeTo(directionsElement.AddChild("direction")); } } }
bool RuntimeSpriteObject::SetAngle(float newAngle) { if ( currentAnimation >= GetAnimationsCount() ) return false; if ( !animations[currentAnimation].Get().useMultipleDirections ) { currentAngle = newAngle; needUpdateCurrentSprite = true; } else { newAngle = static_cast<int>(newAngle)%360; if ( newAngle < 0 ) newAngle += 360; return SetDirection(static_cast<int>(GDRound(newAngle/45.f))%8); } return true; }