SpriteController.cpp
Go to the documentation of this file.00001 //*** SpriteController.cpp ** 00002 00003 #include "SpriteController.h" 00004 #include "Debug.h" 00005 #include "SpriteAction.h" 00006 #include "Sprite.h" 00007 00008 //*** Constructor *** 00009 00010 SpriteController::SpriteController(Sprite* sprite): 00011 sprite_(sprite), 00012 referenceCount_(0), 00013 velocityX_(0), 00014 velocityY_(0), 00015 offsetX_(0), 00016 offsetY_(0), 00017 previousOffsetX_(0), 00018 previousOffsetY_(0) 00019 { 00020 } 00021 00022 00023 //*** Destructor *** 00024 00025 SpriteController::~SpriteController() 00026 { 00027 for (int i=0; i<currentActions_.GetItemCount(); i++) 00028 { 00029 ActionEntry* entry=currentActions_.Get(i); 00030 delete entry->action; 00031 delete entry; 00032 } 00033 00034 for (int i=0; i<queuedActions_.GetItemCount(); i++) 00035 { 00036 ActionEntry* entry=queuedActions_.Get(i); 00037 delete entry->action; 00038 delete entry; 00039 } 00040 } 00041 00042 00043 //*** Update *** 00044 00045 void SpriteController::Update(float deltaTime) 00046 { 00047 // Reset offsets and velocities 00048 offsetX_=0; 00049 offsetY_=0; 00050 velocityX_=0; 00051 velocityY_=0; 00052 00053 // Get sprite position 00054 float x=sprite_->GetX()-previousOffsetX_; 00055 float y=sprite_->GetY()-previousOffsetY_; 00056 00057 // Reset sprite position 00058 sprite_->SetPosition(x,y); 00059 00060 // Update all actions 00061 for (int i=0; i<currentActions_.GetItemCount(); i++) 00062 { 00063 ActionEntry* entry=currentActions_.Get(i); 00064 if (entry->timeLeftToStart<=0) 00065 { 00066 entry->action->Update(this,GetSprite(),deltaTime); 00067 } 00068 else 00069 { 00070 entry->timeLeftToStart-=deltaTime; 00071 if (entry->timeLeftToStart<=0) 00072 { 00073 entry->action->Setup(this,GetSprite()); 00074 } 00075 } 00076 } 00077 00078 00079 // Do physics 00080 x+=velocityX_*deltaTime; 00081 y+=velocityY_*deltaTime; 00082 00083 // Apply offsets 00084 sprite_->SetPosition((x+offsetX_), (y+offsetY_)); 00085 previousOffsetX_=offsetX_; 00086 previousOffsetY_=offsetY_; 00087 00088 00089 00090 // Trigger queued actions 00091 for (int i=0; i<currentActions_.GetItemCount(); i++) 00092 { 00093 ActionEntry* entry=currentActions_.Get(i); 00094 if (entry->action->IsActionCompleted() && entry->endTrigger!=0) 00095 { 00096 for (int j=queuedActions_.GetItemCount()-1; j>=0; j--) 00097 { 00098 ActionEntry* queuedEntry=queuedActions_.Get(j); 00099 if (queuedEntry->startTrigger==entry->endTrigger) 00100 { 00101 queuedEntry->action->Setup(this,GetSprite()); 00102 queuedActions_.Remove(j); 00103 currentActions_.Add(queuedEntry); 00104 } 00105 00106 } 00107 } 00108 } 00109 00110 // Remove completed actions 00111 for (int i=currentActions_.GetItemCount()-1; i>=0; i--) 00112 { 00113 ActionEntry* entry=currentActions_.Get(i); 00114 if (entry->action->IsActionCompleted()) 00115 { 00116 currentActions_.Remove(i); 00117 delete entry->action; 00118 delete entry; 00119 DecreaseReferenceCount(); 00120 } 00121 } 00122 } 00123 00124 00125 //*** GetSprite *** 00126 00127 Sprite* SpriteController::GetSprite() const 00128 { 00129 return sprite_; 00130 } 00131 00132 00133 00134 //*** AddAction *** 00135 00136 void SpriteController::AddAction(SpriteAction* action, float delay, StringId startTrigger, StringId endTrigger, StringId actionId) 00137 { 00138 Assert(action,"Invalid action"); 00139 if (!action) 00140 { 00141 return; 00142 } 00143 00144 ActionEntry* entry=new ActionEntry(); 00145 entry->action=action; 00146 entry->delay=delay; 00147 entry->startTrigger=startTrigger; 00148 entry->endTrigger=endTrigger; 00149 entry->actionId=actionId; 00150 entry->timeLeftToStart=delay; 00151 00152 00153 // If this action has no start trigger, add it directly to the list of current actions 00154 if (startTrigger==0) 00155 { 00156 currentActions_.Add(entry); 00157 if (delay==0) 00158 { 00159 action->Setup(this,GetSprite()); 00160 } 00161 } 00162 else 00163 { 00164 queuedActions_.Add(entry); 00165 } 00166 00167 IncreaseReferenceCount(); 00168 } 00169 00170 00171 //*** RemoveAction *** 00172 00173 void SpriteController::RemoveAction(SpriteAction* action) 00174 { 00175 for (int i=currentActions_.GetItemCount()-1; i>=0; i--) 00176 { 00177 ActionEntry* entry=currentActions_.Get(i); 00178 if (entry->action==action) 00179 { 00180 currentActions_.Remove(i); 00181 delete entry->action; 00182 delete entry; 00183 DecreaseReferenceCount(); 00184 } 00185 } 00186 } 00187 00188 00189 //*** RemoveAction *** 00190 00191 void SpriteController::RemoveAction(StringId actionId) 00192 { 00193 for (int i=currentActions_.GetItemCount()-1; i>=0; i--) 00194 { 00195 ActionEntry* entry=currentActions_.Get(i); 00196 if (entry->actionId==actionId) 00197 { 00198 currentActions_.Remove(i); 00199 delete entry->action; 00200 delete entry; 00201 DecreaseReferenceCount(); 00202 } 00203 } 00204 } 00205 00206 00207 //*** AddOffset *** 00208 00209 void SpriteController::AddOffset(float x, float y) 00210 { 00211 offsetX_+=x; 00212 offsetY_+=y; 00213 } 00214 00215 00216 //*** AddVelocity *** 00217 00218 void SpriteController::AddVelocity(float x, float y) 00219 { 00220 velocityX_+=x; 00221 velocityY_+=y; 00222 } 00223 00224 00225 //*** GetReferenceCount *** 00226 00227 int SpriteController::GetReferenceCount() const 00228 { 00229 return referenceCount_; 00230 } 00231 00232 00233 //*** IncreaseReferenceCount *** 00234 00235 void SpriteController::IncreaseReferenceCount() 00236 { 00237 referenceCount_++; 00238 } 00239 00240 00241 //*** DecreaseReferenceCount *** 00242 00243 void SpriteController::DecreaseReferenceCount() 00244 { 00245 Assert(referenceCount_>0,"Trying to decrease reference count to below 0"); 00246 if (referenceCount_<=0) 00247 { 00248 return; 00249 } 00250 00251 referenceCount_--; 00252 } 00253 00254 00255 //*** GetActionCount *** 00256 00257 int SpriteController::GetActionCount() const 00258 { 00259 return currentActions_.GetItemCount()+queuedActions_.GetItemCount(); 00260 } 00261
Reproduction/republishing of any material on this site without permission is strictly prohibited.
