#include "dxstdafx.h" #include ".\sprite.h" #include "Scene.h" #include "GameManager.h" #include "ResourceManager.h" Sprite::Sprite(void) : Node() { this->firstTime = true; this->lookAtCamera = true; this->rightVec.setXYZ(1, 0, 0); this->upVec.setXYZ(0, 1, 0); this->activeFrame = 0; this->nbFrames = 1; this->animationDuration = 0; this->maxDuration = 0; this->nbRows = 1; this->nbColumns = 1; this->invRows = 1; this->invColumns = 1; this->loopAnimation = true; this->invFps = 1; this->fps = 1; this->sphereSizeFactor = 1; for(int i=0;i<3;i++) { for(int j=0;j<7;j++) { this->keyFrame[i][j] = 1; currentValues[j] = 1; } } this->halfWidth = 1; this->halfHeight = 1; this->keyFrame[Sprite::KEY_START][KF_TIME] = 0; this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME] = 0.5; this->firstRendering = true; this->nodeType |= Scene::NODE_SPRITE; this->animatedSprite = false; this->textureName = ""; this->alpha = 1; this->firstRendering = true; this->texture = NULL; } Sprite::~Sprite(void) { } void Sprite::setDimension(float _width, float _height) { this->halfWidth = _width/2; this->halfHeight = _height/2; for(int i=0;i<3;i++) { this->keyFrame[i][Sprite::KF_HWIDTH] = this->halfWidth; this->keyFrame[i][Sprite::KF_HHEIGHT] = this->halfHeight; } this->minAABBox.x = -this->halfWidth; this->maxAABBox.x = this->halfWidth; this->minAABBox.y = -this->halfWidth; this->maxAABBox.y = this->halfWidth; this->minAABBox.z = -0.5; this->maxAABBox.z = 0.5; } void Sprite::update(float dt) { Node::update(dt); if(!this->firstRendering) { this->calcCurrentValues(this->maxAge - this->timeToLive); if(this->animatedSprite) { //Calculate activeFrame this->animationDuration+=dt; if(this->loopAnimation) { if(this->animationDuration>this->maxDuration) this->animationDuration-=this->maxDuration; } else { this->animationDuration = this->maxDuration; } this->activeFrame = (int) (this->animationDuration*this->fps); } else { this->maxDuration = (float) this->nbFrames*this->invFps; this->maxDuration++; } } } void Sprite::generatePhysicMesh(int type) { sphereDesc.radius = (this->halfHeight>this->halfWidth) ? this->halfHeight*this->sphereSizeFactor : this->halfWidth*this->sphereSizeFactor; this->pActorDesc.shapes.pushBack(&sphereDesc); } Node* Sprite::clone() { Sprite* obj = (Sprite*) this->myScene->createNode(Scene::NODE_SPRITE,*this->father, true, false); this->cloneChildren(obj); obj->lookAtCamera = this->lookAtCamera; obj->halfWidth = this->halfWidth; obj->halfHeight = this->halfHeight; obj->rightVec = this->rightVec; obj->upVec = this->upVec; obj->setAlpha(this->alpha); obj->setRowColumnCount(this->nbRows, this->nbColumns); obj->setFPS(this->fps); obj->setFrameCount(this->nbFrames); obj->setTexture(this->textureName); obj->setDimension(2*this->halfWidth, 2*this->halfHeight); obj->setTimeToLive(this->timeToLive); obj->userData = this->userData; for(int i=0;i<3;i++) { for(int j=0;j<7;j++) { obj->keyFrame[i][j] = this->keyFrame[i][j]; } } obj->animatedSprite = this->animatedSprite; obj->pActorDesc = this->pActorDesc; obj->pBodyDesc = this->pBodyDesc; obj->pActorDesc.body = &obj->pBodyDesc; obj->pActorDesc.shapes.clear(); obj->sphereDesc.radius = this->sphereDesc.radius; obj->pActorDesc.shapes.pushBack(&obj->sphereDesc); return obj; } void Sprite::setLookAtCamera(bool _lookAtCamera) { this->lookAtCamera = _lookAtCamera; } void Sprite::calcPhysicWorldMatrix(D3DXMATRIX &pMatWorld) { if(this->lookAtCamera) { if(this->pActor!=NULL) { NxVec3 vec = this->pActor->getGlobalPosition(); D3DXMatrixTranslation(&this->myWorldMatrix, vec.x, vec.y, vec.z); } } else { Node::calcPhysicWorldMatrix(pMatWorld); } } void Sprite::setTexture(std::string _textureName) { this->textureName = _textureName; this->texture = this->myScene->manager->resManager.loadTexture(this->textureName); } void Sprite::setAlpha(float _alpha) { this->alpha = _alpha; if(this->hasRenderer() && this->alpha!=1) { SimpleMeshRenderer* r = (SimpleMeshRenderer*) this->myRenderer.get(); r->enableAlphaBlending(true); } } float Sprite::getMinDim() { return min(this->halfWidth, this->halfHeight); } void Sprite::setFPS(int _fps) { this->animatedSprite = true; this->invFps = (float) 1.0f/_fps; this->fps = _fps; } void Sprite::setFrameCount(int _frameCount) { this->animatedSprite = true; this->nbFrames = _frameCount; } void Sprite::setRowColumnCount(int _nbRows, int _nbColumns) { this->animatedSprite = true; this->nbRows = _nbRows; this->nbColumns = _nbColumns; this->invRows = (float) 1/this->nbRows; this->invColumns = (float) 1/this->nbColumns; } void Sprite::setLoopAnimation(bool _loopAnimation) { this->animatedSprite = true; this->loopAnimation = _loopAnimation; } void Sprite::setSphereSizeFactor(float _sphereSizeFactor) { this->sphereSizeFactor = _sphereSizeFactor; } void Sprite::setKeyFrame(int _keyFrame, float alpha, float red, float green, float blue, float width, float height, float time) { this->keyFrame[_keyFrame][Sprite::KF_ALPHA] = alpha; this->keyFrame[_keyFrame][Sprite::KF_RED] = red; this->keyFrame[_keyFrame][Sprite::KF_GREEN] = green; this->keyFrame[_keyFrame][Sprite::KF_BLUE] = blue; this->keyFrame[_keyFrame][Sprite::KF_HWIDTH] = width/2; this->keyFrame[_keyFrame][Sprite::KF_HHEIGHT] = height/2; this->keyFrame[_keyFrame][Sprite::KF_TIME] = time; } void Sprite::calcCurrentValues(float age) { int sid; int eid; float fadeFactor = age/this->maxAge; if(fadeFactor < this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME]) { //interpolate between start - middle sid = KEY_START; eid = KEY_MIDDLE; fadeFactor = fadeFactor/this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME]; } else { //interpolate between middle - end sid = KEY_MIDDLE; eid = KEY_END; fadeFactor = (fadeFactor-this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME])/(1-this->keyFrame[Sprite::KEY_MIDDLE][KF_TIME]); } fadeFactor = (fadeFactor>1) ? 1 : fadeFactor; for(int i=0;i<7;i++) { this->currentValues[i] = (1-fadeFactor)*this->keyFrame[sid][i] + fadeFactor*this->keyFrame[eid][i]; } this->currentValues[Sprite::KF_ALPHA] = max(0.0f, this->currentValues[Sprite::KF_ALPHA]); this->halfWidth = this->currentValues[Sprite::KF_HWIDTH]; this->halfHeight = this->currentValues[Sprite::KF_HHEIGHT]; }