void Game::doAI() { for(int i = 1; i < 4; i++) { Crab* crab = crabs0[i]; if (crab == NULL) { continue; } //Find the position of the ball that is nearest the crab's side, and //store the result in targetPos float closestBallDist = 100; float targetPos = 0.5f; for(unsigned int j = 0; j < balls0.size(); j++) { Ball* ball = balls0[j]; float ballDist; float ballPos; switch(i) { case 1: ballDist = ball->x() - ball->radius(); ballPos = 1 - ball->z(); break; case 2: ballDist = 1 - ball->z() - ball->radius(); ballPos = 1 - ball->x(); break; case 3: ballDist = 1 - ball->x() - ball->radius(); ballPos = ball->z(); break; } if (ballDist < closestBallDist) { targetPos = ballPos; closestBallDist = ballDist; } } //Move toward targetPos. Stop so that the ball is in the middle 70% of //the crab. if (abs(stopPos(crab) - targetPos) < 0.7f * (CRAB_LENGTH / 2)) { crab->setDir(0); } else if (targetPos < crab->pos()) { crab->setDir(-1); } else { crab->setDir(1); } } }
void GameDrawer::drawCrabsAndPoles(bool isReflected) { if (crabModel != NULL) { glEnable(GL_NORMALIZE); for(int i = 0; i < 4; i++) { Crab* crab = game->crabs()[i]; //Translate and rotate to the appropriate side of the board glPushMatrix(); switch(i) { case 1: glTranslatef(0, 0, 1); glRotatef(90, 0, 1, 0); break; case 2: glTranslatef(1, 0, 1); glRotatef(180, 0, 1, 0); break; case 3: glTranslatef(1, 0, 0); glRotatef(270, 0, 1, 0); break; } if (crab != NULL || crabFadeAmounts[i] > 0) { //Draw the crab glPushMatrix(); float crabPos; if (crab != NULL) { crabPos = crab->pos(); } else { crabPos = oldCrabPos[i]; } glTranslatef(crabPos, 0.055f, CRAB_OFFSET); if (crab == NULL) { //Used for the shrinking effect, whereby crabs shrink //until they disappear when they are eliminated from play glTranslatef(0, -0.055f * (1 - crabFadeAmounts[i]), 0); glScalef(crabFadeAmounts[i], crabFadeAmounts[i], crabFadeAmounts[i]); } glRotatef(-90, 0, 1, 0); glRotatef(-90, 1, 0, 0); glScalef(0.05f, 0.05f, 0.05f); if (crab == NULL || crab->dir() == 0) { crabModel->setAnimation("stand"); } else { crabModel->setAnimation("run"); } glColor3f(1, 1, 1); crabModel->draw(i, animTimes[i]); glPopMatrix(); } if (crab == NULL) { //Draw the pole if (isReflected) { glDisable(GL_NORMALIZE); } glCallList(poleDisplayListId); if (isReflected) { glEnable(GL_NORMALIZE); } } glPopMatrix(); } } }
void GameDrawer::step() { //Advance the game game->advance(STEP_TIME); //Advance the water waterTextureOffset += STEP_TIME / WATER_TEXTURE_TIME; while (waterTextureOffset > WATER_TEXTURE_SIZE) { waterTextureOffset -= WATER_TEXTURE_SIZE; } //Update animTimes, crabFadeAmounts, and isGameOver0 bool opponentAlive = false; for(int i = 0; i < 4; i++) { Crab* crab = game->crabs()[i]; if (crab != NULL) { oldCrabPos[i] = crab->pos(); } //Update animation time if (crab != NULL || crabFadeAmounts[i] > 0) { if (crab != NULL && crab->dir() != 0) { if (crab->dir() > 0) { animTimes[i] += STEP_TIME / WALK_ANIM_TIME; } else { animTimes[i] -= STEP_TIME / WALK_ANIM_TIME; } } else { animTimes[i] += STEP_TIME / STAND_ANIM_TIME; } while (animTimes[i] > 1) { animTimes[i] -= 1; } while (animTimes[i] < 0) { animTimes[i] += 1; } } //Update fade amount if (crab == NULL) { crabFadeAmounts[i] -= STEP_TIME / CRAB_FADE_OUT_TIME; if (crabFadeAmounts[i] < 0) { if (i == 0) { isGameOver0 = true; } crabFadeAmounts[i] = 0; } else if (i != 0) { opponentAlive = true; } } else if (i != 0) { opponentAlive = true; } } if (!opponentAlive) { isGameOver0 = true; } }