void PhysicsComponent::bounceOnPaddle(Ball* _ball, double _dt) { for(GameObject::ptr paddle : map->paddles) { if(!_ball->inPlay && matchId(_ball, paddle)) { _ball->center.x = paddle->center.x; if(paddle->center.y > _ball->center.y) { _ball->center.y = paddle->center.y - _ball->size.y; } else { _ball->center.y = paddle->center.y + _ball->size.y; } } if(paddle->inPlay && !_ball->inPlay && matchId(_ball, paddle)) { _ball->setInPlay(true); if(paddle->center.y > _ball->center.y) { _ball->velocity.y = -1.f; } else { _ball->velocity.y = 1.f; } _ball->velocity.x = paddle->velocity.x; } if(_ball->inPlay) { glm::vec2 reflectDir; if (ballRectCollide(reflectDir, _ball, paddle.get())) { _ball->velocity = glm::normalize(glm::vec2(_ball->center - paddle->center)) * glm::length(_ball->velocity); ICoreSystem::getInstance().lock()->getSounds()->playSfx("wall_collision"); } } } }
void parser(char *parseToken) { /* function parser(char *) Parameters: char * Return: nothing description: Scanner grabs the token from the buffer and passes it to parser. Parser then knows what the token should be and matches it. If parser finds a unexpected token it prints a syntax error.*/ if (parse == 0) // the program is expecting begin matchBegin(parseToken); else if (parse == 1)// the program is expecting a read,write,id or end matchStatement(parseToken); else if (parse == 3)//the program is expecting a := matchAssignOp(parseToken); else if (parse == 4)// the program is expecting a ( matchLparen(parseToken); else if (parse == 5)//the program is expecting a Id matchId(parseToken); else if (parse == 6)//the program is expecting a ) or a comma matchRparen(parseToken); else if (parse == 7) //the program is expecting a ; matchSemiColon(parseToken); else if (parse == 8) matchExpression(parseToken);// the program is expecting a int or Id else if (parse == 9) matchSemiColonRead(parseToken);//the program is expecting a ; or + or - if(idFlag == 1) insertSymbolTable(parseToken); if(writeFlag == 1 && idFlag == 1) convertWrite(parseToken); if(readFlag == 1 && idFlag == 1) convertRead(parseToken); return; }
void getNextToken (int *token, int *value, char * str){ int pos = 0; int match = FALSE; int flag = FALSE; char c; char * buf = (char *)malloc(sizeof(char)*64); FILE * fp = globalFile(SCAN_GET, ""); while(!match){ c = readNext(fp); if(c == '\n'){ lineno++; } else if(c == '\t' || c == ' '|| c == '\0'); //ignore white spaces else if(c == '\004'){ *token = END; match = TRUE; } else{ switch(c){ case '(': *token = LEFTPARENTHESIS; match = TRUE; break; case ')': *token = RIGHTPARENTHESIS; match = TRUE; break; case ',': *token = COMMA; match = TRUE; break; case '{': *token = LEFTBRACE; match = TRUE; break; case '}': *token = RIGHTBRACE; match = TRUE; break; case ';': *token = SEMICOLON; match = TRUE; break; case '=': *token = RELATIONOP; c = readNext(fp); if(c == '='){ *value = EQUAL; } else{ ungetc(c, fp); *token = ASSIGNOP; } match = TRUE; break; case '+': *token = PLUSOP; match = TRUE; break; case '-': *token = MINUSOP; match = TRUE; break; case '/': c = readNext(fp); if(c == '*'){ while(c != '/' && !flag){ flag = FALSE; c = readNext(fp); if(c == '\n'){ lineno++; } else if(c == '*'){ flag = TRUE; c = readNext(fp); } } flag = FALSE; match = FALSE; break; } else{ *token = DIVOP; ungetc(c, fp); flag = FALSE; } match = TRUE; break; case '*': *token = MULTOP; match = TRUE; break; case '!': c = readNext(fp); if( c != '='){ *token = NOTOP; ungetc(c, fp); } else{ *token = RELATIONOP; *value = NOTEQUAL; } match = TRUE; break; case '>': c = readNext(fp); if( c != '=') ungetc(c, fp); else{ *token = RELATIONOP; *value = BIGGER; } match = TRUE; break; case '<': c = readNext(fp); if( c != '=') ungetc(c, fp); else{ *token = RELATIONOP; *value = SMALLER; } match = TRUE; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buf = matchNum(c); *token = NUM; *value = atoi(buf); match = TRUE; break; default: if(isAlpha(c)){ pos = 0; switch(c){ case 'r': buf[pos] = c; if(matchId("return", buf, &pos)){ *token = RETURN; } else if(matchId("read", buf, &pos)) *token = READ; else{ IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); } break; case 'i': buf[pos] = c; if(matchId("if", buf, &pos)) *token = IF; else if(matchId("int", buf, &pos)) *token = INT; else{ IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); } break; case 'w': buf[0] = c; if(matchId("while", buf, &pos)) *token = WHILE; else if(matchId("write", buf, &pos)) *token = WRITE; else{ IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); } break; case 'e': buf[0] = c; if(matchId("else", buf, &pos)){ *token = ELSE; } else{ IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); } break; case 'v': buf[0] = c; if(matchId("void", buf, &pos)) *token = VOID; else{ IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); } break; default: buf[0] = c; IdMatch(buf, &pos); *token = ID; strncpy(str, buf, 64); break; } //strncpy(str,buf,64); match = TRUE; c = readNext(fp); ungetc(c, fp); } else{ *token = ERROR; match = TRUE; } } } } // DEBUGGING PRINT printToken(*token); free(buf); printf("POS %d\n", pos); str[pos+1] = '\0'; return; }