The Game Engine  1
ParticleSystem.cpp
Go to the documentation of this file.
1 #include "ParticleSystem.h"
2 #include "Actor.h"
3 #include "Game.h"
4 #include "fns.h"
5 #include "Collision.h"
6 
8  :Parent(NULL),
9  Position(),
10  Velocity(),
11  Size(1.0f),
12  Color(Color::WHITE),
13  Life(0)
14 {}
15 
16 ParticleSystem::ParticleSystem(string name, Point2D position, Texture tex, vector<CollisionObject*> collisionbounds)
17  : WorldObject(name, position),
18  ParticleInit(NULL),
19  ParticleUpdate(NULL),
20  mTexture(tex),
21  CollisionEnabled(false),
22  mCollisionData(collisionbounds),
23  mParticles()
24 {
25  Game::game()->addObject(this);
26 }
27 
29 {
30 }
31 
32 void ParticleSystem::SetTexture(Actor actor, int animation, int frame)
33 {
34  mTexture = actor.Animations[animation]->Frames[frame]->tex;
35 }
36 
38 {
39  mTexture = frame->tex;
40 }
41 
42 void ParticleSystem::SpawnParticles(int numparticles)
43 {
44  Game* g = Game::game();
45  g->CurrentParticleSystem = this;
46  for (int i = 0; i < numparticles; ++i)
47  {
48  Particle* newparticle = new Particle();
49  newparticle->Parent = this;
50  g->CurrentParticle = newparticle;
51  if (ParticleInit != NULL)
52  ParticleInit();
53  mParticles.push_back(newparticle);
54  }
55  g->CurrentParticle = NULL;
56  g->CurrentParticleSystem = NULL;
57 }
58 
60  {
61  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
62  //glTranslated(-frame->animationPeg.x, -frame->animationPeg.y, 0);//place animation peg on origin
63  glBindTexture(GL_TEXTURE_2D, mTexture.image);
64  foritr(pPtr, mParticles){
65  Particle* p = *pPtr;
66  glColor4f(p->Color.getRed(), p->Color.getGreen(), p->Color.getBlue(), p->Color.getAlpha());
67  glLoadIdentity();
68  //cam pos
69  Point2D movePos=mPos+p->Position-Game::game()->mCamera.pos;
70  glTranslated(movePos.x, movePos.y, 0);//move to position
71  //glTranslated(frame->animationPeg.x, frame->animationPeg.y, 0);//move back to where it was \todo fox for scaling
72  glRotatef(mAngle,0.0f,0.0f,1.0f);//rotate shape
73  glScaled(mScale, mScale, mScale);
74  glBegin( GL_QUADS );
75 
76  // Top-left vertex (corner)
77  glTexCoord2f( 0, 0 );
78  glVertex3d( 0, 0, 0 );
79 
80  // Bottom-left vertex (corner)
81  glTexCoord2f( 1, 0 );
82  glVertex3d( p->Size, 0, 0 );
83 
84  // Bottom-right vertex (corner)
85  glTexCoord2f( 1, 1 );
86  glVertex3d( p->Size, p->Size, 0 );
87 
88  // Top-right vertex (corner)
89  glTexCoord2f( 0, 1 );
90  glVertex3d( 0, p->Size, 0 );
91  glEnd();
92  }
93  glLoadIdentity();
94  glFinish();
95  }
96 
98  {
99  if (mParticles.size() == 0)
100  return;
102 
104  }
105 
107  if(mUnused) return;
110  list<Particle*>::iterator n = mParticles.begin();
111  while( n!=mParticles.end() )
112  {
113  (*n)->Life--;
114  if ((*n)->Life <= 0)
115  {
116  mParticles.erase(n++);
117  }
118  else
119  {
121  (*n)->Position.x += (*n)->Velocity.x;
122  (*n)->Position.y += (*n)->Velocity.y;
123 
125  double pleft = (*n)->Position.x - (*n)->Size / 2.0f;
126  double pright = (*n)->Position.x + (*n)->Size / 2.0f;
127  double ptop = (*n)->Position.y - (*n)->Size / 2.0f;
128  double pbottom = (*n)->Position.y + (*n)->Size / 2.0f;
129  if (n == mParticles.begin())
130  {
131  mBoundingBox.mPos.x = pleft;
132  mBoundingBox.mPos.y = ptop;
133  mBoundingBox.width = pright - pleft;
134  mBoundingBox.height = pbottom - ptop;
135  }
136  if (pleft < mBoundingBox.getLeft())
137  {
138  mBoundingBox.width += mBoundingBox.getLeft() - pleft;
139  mBoundingBox.mPos.x = pleft;
140  }
141  if (ptop < mBoundingBox.getTop())
142  {
144  mBoundingBox.mPos.y = ptop;
145  }
146  if (pright > mBoundingBox.getRight())
147  {
148  mBoundingBox.width += pright - mBoundingBox.getRight();
149  }
150  if (pbottom > mBoundingBox.getBottom())
151  {
152  mBoundingBox.height += pbottom - mBoundingBox.getBottom();
153  }
154 
155  ++n;
156  }
157  }
158 }
159 
161 {
162  Game* g = Game::game();
163  g->CurrentParticleSystem = this;
165  if(ParticleUpdate)
166  {
167  foritr(particle,mParticles){
168  g->CurrentParticle=*particle;
169  ParticleUpdate();
170  }
171  g->CurrentParticle=NULL;
172  }
173  g->CurrentParticleSystem=NULL;
174 }
175 
176 vector<CollisionObject*>& ParticleSystem::getCollisionData() {
177  return mCollisionData;
178 }
179 
180 vector<Sprite*> ParticleSystem::collisionWithSprites(string name, int count) {
181  vector<Sprite*> retvals = vector<Sprite*>();
182 
183  //get the first sprite with this name
184  vector<Sprite*> ss = Game::game()->getCurrentLevel()->findSprites(name,count);
185  if(ss.size()==0)
186  return retvals;
188  for(unsigned int i=0; i < mCollisionData.size(); i++){
189  foritr(s, ss)
190  {
191  auto sprite = *s;
192  //if there is a collision
193  if(col->collided(this, sprite)){
194  retvals.push_back(sprite);
195  }
196  else{
197  auto val = mCollisionData[i]->checkCollisions(sprite->getCollisionData(), sprite->getPosition() + sprite->getAnimation()->animationPeg, mPos);
199  if(val){
200  //col->markCollision(this, frame->collisionData[i], sprite, );
201  retvals.push_back(sprite);
202  }
203  }
204  }
205  }
206 
207  return retvals;//if there aren't collisions
208 }
209 
212 }
213 
216 }
217