#include "cWorld.h"

cWorld::cWorld(cParticleSystem* ps) : 
    staticObjects_(NULL), 
    particleSystem_(ps),
    camera_(&ball_) 
{
    ball_.ps_ = particleSystem_;
    loadedLevel_ = false;
    
}
    

void cWorld::loadLevel(string filename, int level)
{
    if(staticObjects_ == NULL)
    {
		particleSystem_->level_ = -1;
        cout << "loading " << filename << endl;
        loadedLevel_ = true;
        ball_.randomize(level);
        ball_.level_ = level;
        camera_.setBall(&ball_);
        staticObjects_ = new cStaticTriangleMesh(filename, level);
        //particleSystem_ = new cParticleSystem(staticObjects_, level);
        ball_.ps_ = particleSystem_;
    }
    else
        cout << "error, unload file first" << endl;
}

bool cWorld::levelIsLoaded() { return loadedLevel_; }


void cWorld::unloadLevel()
{
    cout << "unloading level" << endl;
    loadedLevel_ = false;
    if(staticObjects_ != NULL)
        delete staticObjects_;
    //delete particleSystem_;
    staticObjects_ = NULL;
}

cWorld::~cWorld()
{
    delete particleSystem_;
    if(staticObjects_!= NULL)
        delete staticObjects_;
}

void cWorld::update(float dt)
{
    if(staticObjects_ == NULL) return;
    camera_.update(dt);
    
    
    ball_.update(dt , *staticObjects_); 
}

void cWorld::draw()
{
    if(staticObjects_ == NULL) return;
    camera_.look();
    ball_.draw();
    
    staticObjects_->draw();
    
    ball_.drawLaunchArrow();
    
}
