Exemplo n.º 1
0
void initNiveau(Partie *p, Snake *s) {
    /*Reinitialisation du serpent*/
    resetSnake(s, *p);

    /*Creation de la cible initiale*/
    creerCible(*s, p);
}
Exemplo n.º 2
0
/* May require the use of a ticker.attach in order to update the snake every
 * x seconds. <http://mbed.org/handbook/Ticker>
 */
void moveSnake(void) {
/*
 * NOTE: This is part of an ISR called by the Ticker
 * So no printf, limited use of malloc.
 * If issues occur, split up function
 */
 /* TODO: test mbed ISR, determine how much memory allocation,
  * max time for ISR, etc
  * REMOVE PRINTF ONCE MIGRATION OCCURS
  */
    LLRoot * master = &Snake;
    LLNode * tmp;
    char sHead_x, sHead_y, sHead_z;
    char apple_x, apple_y, apple_z;
    signed char XDIR, YDIR, ZDIR;
    char count;
    
    if(master == NULL) return;
    
    moveFlag = 1;
    errcode = 0;
    
    /* Careful usage required, stale data can occur with these variables */
    sHead_x = master->head->x;
    sHead_y = master->head->y;
    sHead_z = master->head->z;
    apple_x = master->fruit_x;
    apple_y = master->fruit_y;
    apple_z = master->fruit_z;
    
    XDIR = YDIR = ZDIR = 0;
    switch(master->direction) {
        case XPOS:
            XDIR = 1;
            break;
        case XNEG:
            XDIR = -1;
            break;
        case YPOS:
            YDIR = 1;
            break;
        case YNEG:
            YDIR = -1;
            break;
        case ZPOS:
            ZDIR = 1;
            break;
        case ZNEG:
            ZDIR = -1;
            break;
            
        default:
            break;
    }
    
    /* Update snake nodes */
    __disable_irq();
    addToHeadLL(master, sHead_x + XDIR, sHead_y + YDIR, sHead_z + ZDIR);
    __enable_irq();
    // BEAMMEUPSCOTTY
    myCube.plotPoint(sHead_x + XDIR, sHead_y + YDIR, sHead_z + ZDIR);
    if(LLDEBUG) printf("debug: adding [%d %d %d]\r\n", sHead_x + XDIR, sHead_y + YDIR, sHead_z + ZDIR);
    
    /* Boundary/Self checking */
    count = 0;
    tmp = master->head;
    while(tmp != NULL) {
        ++count;
        
        /* Check head coordinates against all other nodes */
        if(count > 1) {
            if((master->head->x == tmp->x) && (master->head->y == tmp->y) && (master->head->z == tmp->z)) {
                if(LLDEBUG) printf("debug: conflict with snake head: [%d %d %d] ... resetting game\r\n", master->head->x, master->head->y, master->head->z);
                resetSnake(master);
                reset = 1;
                errcode = 1;
                break;
            }
        }
        
        // TODO: flash cube face on wall hit
        // in if statements
        
        if(tmp->x > BOUNDARY) {
            resetSnake(master);
            reset = 1; 
            errcode = 3;
            // TODO: light right wall
            break;
        }
        if(tmp->x < 0) {
            resetSnake(master);
            reset = 1; 
            errcode = 3;
            // TODO: light left wall
            break;
        }
        if(tmp->y > BOUNDARY) {
            resetSnake(master);
            reset = 1;
            errcode = 3;
            // TODO: light back wall
            break;
        }
        if(tmp->y < 0) {
            resetSnake(master);
            reset = 1;
            errcode = 3;
            // TODO: light front wall
            break;
        }
        if(tmp->z > BOUNDARY) {
            resetSnake(master);
            reset = 1;
            errcode = 3;
            // TODO: light top wall
            break;
        }
        if(tmp->z < 0) {
            resetSnake(master);
            reset = 1;
            errcode = 3;
            // TODO: light bottom wall
            break;
        }
        tmp = tmp->next;
    }
    //master->length = count - 1;

    if(reset) { reset = 0; return; }

    /* Fruit detection */
    if((sHead_x == apple_x) && (sHead_y == apple_y) && (sHead_z == apple_z)) {
        generateFruit(master);
        errcode = 2;
        if(LLDEBUG) printf("debug: keeping tail [%d %d %d]\r\n", master->tail->x, master->tail->y, master->tail->z);
    }else{
        /* If we detect fruit, there is no need to remove the tail node.
         * Otherwise we can remove it to produce the snake movement
         */
        // BEAMMEUPSCOTTY
        
        myCube.clearPoint(master->tail->x, master->tail->y, master->tail->z);
        if(LLDEBUG) printf("debug: removing [%d %d %d] because no fruit detected\r\n", master->tail->x, master->tail->y, master->tail->z);
        __disable_irq();
        removeTailLL(master);
        __enable_irq();
    }
    
    if(LLDEBUG) printListLL(master);       /* Display LL header and node data */
}