/GameLogics/World.cpp
C++ | 186 lines | 152 code | 28 blank | 6 comment | 30 complexity | 57f2d9c1739e4dfa8d94bb5394389273 MD5 | raw file
- /*
- * World.cpp
- *
- * Created on: Dec 27, 2011
- * Author: maarten
- */
- #include "World.h"
- #include "Factory.h"
- #include "Entity.h"
- #include <list>
- #include <iostream>
- #include <stdlib.h>
- using namespace Tyrian;
- using namespace std;
- World::World( Factory* fact)
- : thisFactory(fact),thisKeyboard(thisFactory->CreateKeyboard()), thisStopwatch(thisFactory->CreateStopwatch()), lastFrame(thisStopwatch->GetTime())
- {
- srand( this->lastFrame );
- thisFactory->AddWorld(this);
- this->ObjectList.push_back( thisFactory->CreatePlayer());
- }
- void World::AddToList( Entity* src)
- {
- this->ObjectList.push_back( src );
- }
- World::~World()
- {
- std::list<Entity*>::iterator it = this->ObjectList.begin();
- while ( it != this->ObjectList.end() )
- {
- (*it)->~Entity();
- this->ObjectList.erase(it);
- }
- }
- unsigned long World::GetFrameDelay()
- {
- unsigned long passedTime;
- unsigned long temp = thisStopwatch->GetTime();
- passedTime = temp - lastFrame;
- lastFrame = temp;
- cout << "Last frame: " << lastFrame << endl;
- cout << "Passed time: " << passedTime << endl;
- return passedTime;
- }
- void World::Run()
- {
- while ( !this->thisKeyboard->KeyEscapePressed() )
- {
- this->Collision();
- this->Move();
- this->CreateEnemy();
- }
- }
- void World::Move()
- {
- list<Entity*>::iterator it = this->ObjectList.begin();
- unsigned long frameDelay = (this->GetFrameDelay() );
- while ( it != this->ObjectList.end() )
- {
- frameDelay += (this->GetFrameDelay() );
- (*it)->Move( frameDelay );
- it++;
- }
- }
- void World::OutOfBounds()
- {
- list<Entity*>::iterator it = this->ObjectList.begin();
- while( it != this->ObjectList.end())
- {
- if( ((*it)->GetCoord()->GetX()) > (XAXIS/2 + (*it)->GetXSize()))
- {
- delete *it;
- it = this->ObjectList.erase(it);
- }
- else if( ((*it)->GetCoord()->GetX()) < -(XAXIS/2 + (*it)->GetXSize()))
- {
- delete *it;
- it = this->ObjectList.erase(it);
- }
- else if( ((*it)->GetCoord()->GetY()) > (YAXIS/2 + (*it)->GetYSize()))
- {
- delete *it;
- it = this->ObjectList.erase(it);
- }
- else if( ((*it)->GetCoord()->GetY()) < -(YAXIS/2 + (*it)->GetYSize()))
- {
- delete *it;
- it = this->ObjectList.erase(it);
- }
- else
- {
- it++;
- }
- }
- cout << "List size: " << this->ObjectList.size() << endl;
- }
- void World::Collision()
- {
- this->OutOfBounds();
- cout << "Got to Collision" << endl;
- list<Entity*>::iterator it = this->ObjectList.begin();
- while ( it != this->ObjectList.end())
- {
- list<Entity*>::iterator it2 = this->ObjectList.begin();
- while ( it2 != this->ObjectList.end())
- {
- if (((*it2)->GetCoord()->GetX() <= ((*it)->GetCoord()->GetX() + (*it)->GetXSize())
- && ((*it2)->GetCoord()->GetX() >= ((*it)->GetCoord()->GetX()))
- && ((*it2)->GetCoord()->GetY() <= ((*it)->GetCoord()->GetY() + (*it)->GetYSize()))
- && ((*it2)->GetCoord()->GetY() >= ((*it)->GetCoord()->GetY())))
- || (((*it)->GetCoord()->GetX() <= ((*it2)->GetCoord()->GetX() + (*it2)->GetXSize())
- && ((*it)->GetCoord()->GetX() >= ((*it2)->GetCoord()->GetX()))
- && ((*it)->GetCoord()->GetY() <= ((*it2)->GetCoord()->GetY() + (*it2)->GetYSize()))
- && ((*it)->GetCoord()->GetY() >= ((*it2)->GetCoord()->GetY())))))
- {
- cout << "Collided" << endl;
- if((*it)->CollidesWith((*it2)))
- {
- if ((*it2)->CollidesWith((*it)))
- {
- delete *it2;
- delete *it;
- this->ObjectList.erase(it2);
- it = this->ObjectList.erase(it);
- it2 = this->ObjectList.end();
- }
- else
- {
- delete *it;
- it = this->ObjectList.erase(it);
- it2 = this->ObjectList.end();
- }
- }
- else if ((*it2)->CollidesWith((*it)))
- {
- delete *it2;
- it2 = this->ObjectList.erase(it2);
- }
- else
- {
- it2++;
- }
- }
- else
- {
- it2++;
- }
- }
- it++;
- }
- }
- void World::CreateEnemy()
- {
- if ( ((rand() % 100)) > SPAWNCHANCE )
- {
- this->AddToList(this->thisFactory->CreateEnemy( (rand() % XAXIS) - (XAXIS/2) ));
- }
- }