Alvaro Cassinelli / Mbed 2 deprecated skinGames_forktest

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

blobConfig.cpp

Committer:
mbedalvaro
Date:
2012-09-21
Revision:
30:d8af03f01cd4
Parent:
28:44b7b6e35548
Child:
31:5f039cbddee8

File content as of revision 30:d8af03f01cd4:


#include "blobConfig.h"

blobConfig::blobConfig(): numBlobs(0) {
    //blobArray.clear();// there is no need to do this, the vector does not contains anything here.
}

blobConfig::~blobConfig() {
    clearConfig();
}

// =========================================== STANDARD CONFIGURATIONS =============================================================================



void blobConfig::clearConfig() {
    for (int i=0; i<blobArray.size(); i++) delete blobArray[i]; // we must delete the pointer created with new, so the memory for the object is liberated (calls its destructor)
    blobArray.clear();
    numBlobs=0;// this is just equal to blobArray.size()
}

void blobConfig::initConfig(configType cfType,  unsigned char numblobs) {
    myConfigType=cfType;
    numBlobs=numblobs;
    switch(myConfigType) {
        case ONE_ELASTIC_FOLLOWING:
            // computeBoundingBox();
            clearConfig();
            addOneElasticContourFollowing();
        break;
        case ONE_ELASTIC_MOUTH:
            // computeBoundingBox();
            clearConfig();
            addOneElasticLoopContractCentral();
        break;
        case ONE_ELASTIC_MOUTH_SMALL:
            // computeBoundingBox();
            clearConfig();
            addOneElasticLoopContractCentralFast();
        break;
        case BOUNCING_SPOTS:
            // computeBoundingBox();
           clearConfig();
           for (int i=0; i<numBlobs ; i++) addOneRigidLoopBouncing();
        break;
        case LORENTZ_SPOTS:
         // computeBoundingBox();
            clearConfig();
            for (int i=0; i<numBlobs ; i++) addOneRigidLoopLorentz();
        break;
        case FOLLOWING_SPOTS:
         // computeBoundingBox();
            clearConfig();
            for (int i=0; i<numBlobs ; i++) addOneRigidLoopFollowing();
        break;
        case AIR_HOCKEY_GAME: 
            // computeBoundingBox();
            clearConfig();
            for (int i=0; i<numBlobs ; i++) addOneRigidLoopAirHockey();
        break;
        case CIRCULAR_PONG_GAME:
            // computeBoundingBox();
            clearConfig();
            // (1) One SPOT_TRACK to track the background. It will be the number 0 in the config. 
            addOneRigidTrackingSpot();

            // (2) Add bouncing spots:
            for (int i=0; i<numBlobs ; i++) addOneRigidLoopBouncing();
        break;
        case VERTICAL_PINBALL_GAME:
         clearConfig();
         // (1) One SPOT_TRACK to track the background - It will be the number 0 in the config. 
          addOneRigidTrackingSpot();

        break; 
        case PAC_MAN_GAME:
            clearConfig();
            //(1) add one very slowly slidind-bouncing spot, the PACMAN (number 0 in the config)
            addOneRigidLoopPacman(vector2Df(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_Y), vector2Df(2,0));
     
            // (2) Add some initial SPOT_FOLLOWING or SPOT_GHOST spots (the ghosts):
            //for (int i=0; i<numBlobs; i++) 
            addOneRigidLoopFollowing(vector2Df(CENTER_AD_MIRROR_X-200, CENTER_AD_MIRROR_Y-200), vector2Df(2,0));
            addOneRigidLoopGhost(vector2Df(CENTER_AD_MIRROR_X+200, CENTER_AD_MIRROR_Y-200), vector2Df(2,0));
        break;
        default:
        break;
    }
}

 // ==================== Template spots from which to create multi-spot configurations: =====================

void blobConfig::addOneElasticLoopRelax(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), RELAX, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();

}


void blobConfig::addOneElasticLoopContract(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), CONTRACT, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneElasticLoopContractCentral(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), CONTRACT_CENTRAL, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneElasticLoopContractCentralFast(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), CONTRACT_CENTRAL_FAST, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneElasticContourFollowing(vector2Df initpos, vector2Df initspeed) {

    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), CONTOUR_FOLLOWING, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}


void blobConfig:: addOneElasticContourFollowingFAST(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), CONTOUR_FOLLOWING_FAST,  initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneElasticBouncing(vector2Df initpos, vector2Df initspeed) {
    elasticLoop* pBlob= new elasticLoop();
    pBlob->createBlob(blobArray.size(), BOUNCING,  initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidLoopBouncing(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_BOUNCING, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidLoopPacman(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_PACMAN, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}
void blobConfig::addOneRigidLoopGhost(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_GHOST, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}


void blobConfig::addOneRigidLoopLorentz(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_LORENTZ_FORCE, initpos, initspeed);
    // add this loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidLoopAirHockey(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_AIR_HOCKEY,  initpos, initspeed);
    // add this loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidLoopFollowing(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_FOLLOWING, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidLoopTest(vector2Df initpos, vector2Df initspeed) {
    rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_TEST, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

void blobConfig::addOneRigidTrackingSpot(vector2Df initpos, vector2Df initspeed) {
   rigidLoop* pBlob= new rigidLoop();
    pBlob->createBlob(blobArray.size(), SPOT_TRACK, initpos, initspeed);
    // add this relaxing loop to the present config:
    blobArray.push_back(pBlob);

    // update auxiliary variable numBlobs (just for easy reference):
    numBlobs=blobArray.size();
}

// ==================================================================================================================================================


void blobConfig::allKill() { // this put all the blobs in "dead" mode, meaning that neither rendering nor update is done (but they are not deleted).
    for (int i=0; i<blobArray.size(); i++) {
        blobArray[i]->render = false;
        blobArray[i]->standByMode = false;
    }
}
void blobConfig::allAlive() {
    for (int i=0; i<blobArray.size(); i++) {
        blobArray[i]->render = true;
        blobArray[i]->standByMode = true;
    }
}

void blobConfig::allStandBy() {
    for (int i=0; i<blobArray.size(); i++) blobArray[i]->standByMode = true;
}

void blobConfig::allResume() {
    for (int i=0; i<blobArray.size(); i++) blobArray[i]->standByMode = false;
}

void blobConfig::allVisible() {
    for (int i=0; i<blobArray.size(); i++) blobArray[i]->render = true;
}

void blobConfig::allInvisible() { // note that they may continue to evolve
    for (int i=0; i<blobArray.size(); i++) blobArray[i]->render = false;
}

void blobConfig::allSetColor(unsigned char c) {
    for (int i=0; i<blobArray.size(); i++) blobArray[i]->setColor(c);
 }

void blobConfig::update() { // update dynamics of the blob configuration:

    // (1) First, update dynamics of each blob independently:
    for (int i=0; i<blobArray.size(); i++) {
        if (blobArray[i]->standByMode==false) blobArray[i]->update();
    }
    // (2) Then, depending on the config type, perform some special test and updates: 
   switch(myConfigType) {
        // simple behaviours:
        case ONE_ELASTIC_FOLLOWING:
        break;
        case ONE_ELASTIC_MOUTH:
        break;
        case ONE_ELASTIC_MOUTH_SMALL:
        break;
        case BOUNCING_SPOTS:
        break;
        case LORENTZ_SPOTS:
        break;
        case FOLLOWING_SPOTS:
        break;
        // more game-like:
        case AIR_HOCKEY_GAME: 
        break;
        case CIRCULAR_PONG_GAME:
        break;
        case VERTICAL_PINBALL_GAME:
        break; 
        case PAC_MAN_GAME:
        break;
        default:
        break;
    }
     
    
}

void blobConfig::computeBoundingBox() {
    for (int i=0; i<blobArray.size(); i++) {
        blobArray[i]->computeBoundingBox();
    }
}

void blobConfig::draw() { // draw uses the opengl like renderer (if any), and save projected trajectory in the LaserSensingTrajectory object of each blob
    for (int i=0; i<blobArray.size(); i++) {
        if (blobArray[i]->render==true) blobArray[i]->draw();
    }
}

void blobConfig::sendConfData() {
// For the time being, only "per blob" data sending:
// (b) Per-spot sending of data (note: both are NOT exclusive; so if we want just packaged data, we need to make all the spot STOP sending data.
    for (int i=0; i<blobArray.size(); i++) { 
        if (blobArray[i]->render==true) blobArray[i]->sendData(); // a blob that is in stand-by mode may send data (good for testing with a fixed loop)
    }
}