Ejemplo n.º 1
0
Tree::Tree(Genus *r, Planet *pl, const vec2 &p, const vec2 &d, float lenBallance,
				float l_up,   float lengthFactor_up,  float lengthFactorDiv_up,    float angleFactor_up,   float angleFactorDiv_up,
				float l_down, float lengthFactor_down, float lengthFactorDiv_down, float angleFactor_down, float angleFactorDiv_down, 
				Deformer *def): 
	genus(r), planet(pl), lengthBallance(lenBallance), accumulator(0), lastStep(0),
	coma(p, d, l_up, lengthFactor_up, lengthFactorDiv_up, angleFactor_up, angleFactorDiv_up, 0),
	root(p, -d, l_down, lengthFactor_down, lengthFactorDiv_down, angleFactor_down, angleFactorDiv_down, def)
{
	seed = rand() % 1000 + 1;
	growUp();
	calcVars();
}
void TableBehaviorSnake::doLoop()
{
   paintAll(CRGB::Black, false);

    int newPosX = -1;
    int newPosY = -1;

    int randomTryCount = 0;

    while(newPosX == -1 && randomTryCount < 500){


        switch (mDirection) {
        case 0:
            if(isPositionEmpty(mSnake[0]->x - 1, mSnake[0]->y) == 1){
                newPosX = mSnake[0]->x - 1;
                newPosY = mSnake[0]->y;
            }
            break;

        case 1:
            if(isPositionEmpty(mSnake[0]->x + 1, mSnake[0]->y) == 1){
                newPosX = mSnake[0]->x + 1;
                newPosY = mSnake[0]->y;
            }
            break;
        case 2:
            if(isPositionEmpty(mSnake[0]->x, mSnake[0]->y - 1) == 1){
                newPosX = mSnake[0]->x;
                newPosY = mSnake[0]->y - 1;
            }
            break;
        case 3:
            if(isPositionEmpty(mSnake[0]->x, mSnake[0]->y + 1) == 1){
                newPosX = mSnake[0]->x;
                newPosY = mSnake[0]->y + 1;
            }
            break;
        default:
            break;
        }

        if (newPosX == -1){
            mDirection = random(4);
            randomTryCount++;
        }
    }

    // Gameover ?
    if (newPosX == -1 || newPosY == -1){

        delay(1000);
        mSnakeSize = 3;
        mSnake[0]->set(9, 4);
        mSnake[1]->set(9, 3);
        mSnake[2]->set(9, 2);

    }else{
        // move the snake
        SnakeUnit previousValues;
        SnakeUnit newValues;
        for (int i = 0; i < mSnakeSize; ++i) {

            int tmpX = mSnake[i]->x;
            int tmpY = mSnake[i]->y;


            // if 'head' else 'body'
            if (i == 0){
                newValues.x = newPosX;
                newValues.y = newPosY;

            }else{
                newValues.x = previousValues.x;
                newValues.y = previousValues.y;
            }

            // Apply new values
            mSnake[i]->x = newValues.x;
            mSnake[i]->y = newValues.y;

            // Store old values
            previousValues.x = tmpX;
            previousValues.y = tmpY;

            setCorrectColor(mSnake[i]->x,  mSnake[i]->y, mSnakeColor);
        }
    }

    // Sometimes change the direction
    if (random(100) > 80){
        mDirection = random(4);
    }

    // Sometimes growup
    if (random(100) > 95){
        growUp();
    }

    FastLED.show();

    delay(50);

    // read any changes on Potentiometer and Button B
    readInputs();
}
void TableBehaviorSnake::onClickButtonB()
{
    //mDirection = random(4);
    growUp();
}