void CEnemy::RetreatHome() { if (_state != EnemyStateEyes) { changeState(EnemyStateEyes); // we got eaten Move(chooseDirection(_startX, _startY)); } }
//---------------------------------------------------------------- void GRTrill::placeMord (GREvent * inParent, NVPoint & newPoint) { // Checks the direction of the stems const GDirection dir = chooseDirection( inParent ); // dir = stemDir const GRStaff * staff = inParent->getGRStaff(); const float currLSpace = staff->getStaffLSPACE(); const float halfSpace = float(0.5) * currLSpace; if( dir == dirUP ) { if (newPoint.y >= inParent->getStemLength()) { newPoint.y = -halfSpace; } else { newPoint.y -= (halfSpace + inParent->getStemLength()); } } else // stemDown { if ( newPoint.y > 0) { newPoint.y = -halfSpace; } else { newPoint.y -= halfSpace; } } }
//------------------------------------------------------------------- void GRTrill::tellPosition( GObject * caller, const NVPoint & np ) { fType = static_cast<ARTrill *>(mAbstractRepresentation)->getType(); GREvent * ev = dynamic_cast<GREvent *>(caller); if (ev) { const int dir = chooseDirection(ev); NVPoint newPoint (np); const bool upward = (dir == dirUP); if( upward ) // if stemsUp { if (newPoint.y >= ev->getStemLength()) newPoint.y = -LSPACE; else newPoint.y -= (ev->getStemLength() + LSPACE); } else { newPoint.y = (newPoint.y > 0)? -LSPACE : newPoint.y - 1.5f*LSPACE; } setPosition(newPoint); // Accidental NVPoint accidentalPos = NVPoint(newPoint); accidentalPos.x += GetSymbolExtent(mSymbol)*0.5f + 0.4f*LSPACE; accidentalPos.y -= 1.5f*LSPACE; fAccidental->setPosition(accidentalPos); } }
void CEnemy::Update() { CEntity::Update(); // animation and movement stuff // handling new movement in different states if (!_moving && _state == EnemyStateScatter) { Move(chooseDirection(_scatterX, _scatterY)); // try to move to our scatter corner } else if (!_moving && _state == EnemyStateChase) { Move(chooseDirection(_chaseX, _chaseY)); // try to move to our chase position (different for every ghost) } else if (!_moving && _state == EnemyStateFrighten) { while (true) { // just move in random direction int direction = rand() % 4; if (!isReverse((Direction)direction)) { Move((Direction)direction); if (_moving) { break; } } } } else if (!_moving && _state == EnemyStateEyes) { if (getXposition() == _startX && getYposition() == _startY) { Reset(); // reset our states when home reached _state = EnemyStateChase; Update(); // start chase over } else { Move(chooseDirection(_startX, _startY)); // go home } } }
/* * Make a new step */ direction pathfinder_NextStep(direction currentDirection, uint8_t x, uint8_t y) { // first check if we have reached the target already if ((x == targetX) && (y == targetY)) { // we ain't going anywhere soon! return NONE; } setCursorPosLCD(0, 2); writeIntegerLCD(root->x, DEC); setCursorPosLCD(0, 5); writeIntegerLCD(root->y, DEC); setCursorPosLCD(1, 0); writeIntegerLCD(current->count, DEC); setCursorPosLCD(1, 4); writeIntegerLCD(nodeCount, DEC); // now check if we can only go one direction if (hal_hasWallLeft() && hal_hasWallRight() && !hal_hasWallFront()) { return currentDirection; } else if (hal_hasWallLeft() && !hal_hasWallRight() && hal_hasWallFront()) { return turnRight(currentDirection); } else if (!hal_hasWallLeft() && hal_hasWallRight() && hal_hasWallFront()) { return turnLeft(currentDirection); } // we cannot keep cruising normally // get the node or create a new one struct node *node = getNode(current); if (!node) { node = createNode(current, currentDirection, x, y); } if (hal_hasWallLeft() && hal_hasWallRight() && hal_hasWallFront()) { // dead end current = node; currentDirection = turnAround(currentDirection); } else { // ok, decide where we're gonna go chooseDirection(currentDirection, node, x, y); current = node; } lastDirection = currentDirection; return currentDirection; }
// If the player doesn't move the character it will start to move around the room in randomly selected directions void Character::moveCharacter(const Uint8* keyboardState) { // checks for keyboard input if there is none it assigns a random directions chooseDirection(keyboardState); // if the direction will keep the charcter on screen and is part of a room move Character down if (direction == 0 && getY() + getSpeed() < windowHeight && isCellARoom(getX(), getY() + getSpeed())) { // If the Characer is wandering also check is the cell is a door if (isWandering == true && isCellADoor(getX(), getY() + getSpeed())) direction = rand() % 4; else setY(getY() + getSpeed()); } // if the direction will keep the charcter on screen and is part of a room move Character up else if (direction == 1 && (getY() + getSpeed()) > 0 && isCellARoom(getX(), getY() - getSpeed())) { // If the Characer is wandering also check is the cell is a door if (isWandering == true && isCellADoor(getX(), getY() - getSpeed())) direction = rand() % 4; else setY(getY() - getSpeed()); } // if the direction will keep the charcter on screen and is part of a room move Character right else if (direction == 2 && (getX() + getSpeed()) < windowWidth && isCellARoom(getX() + getSpeed(), getY())) { // If the Characer is wandering also check is the cell is a door if (isWandering == true && isCellADoor(getX() + getSpeed(), getY())) direction = rand() % 4; else setX(getX() + getSpeed()); } // if the direction will keep the charcter on screen and is part of a room move Character left else if (direction == 3 && (getX() - getSpeed()) > 0 && isCellARoom(getX() - getSpeed(), getY())) { // If the Characer is wandering also check is the cell is a door if (isWandering == true && isCellADoor(getX() - getSpeed(), getY())) direction = rand() % 4; else setX(getX() - getSpeed()); } // If Character cannot move in current direction randomly choose a new one else { direction = rand() % 4; } }
int main(void){ char board[SIZE][SIZE] = { '.' }, currentLetter = 'B'; int currentRow = 0, currentColumn = 0, direction, movesTried = 0;; for(int i = 0; i < SIZE; i++){ for(int j = 0; j < SIZE; j++){ board[i][j] = '.'; } } board[0][0] = 'A'; initRNG(); while(movesTried < 4 && currentLetter != 'Z'+1 ){ direction = chooseDirection(); switch((direction + movesTried) % DIRECTIONS){ case 0: if(currentColumn - 1 >= 0){ if(board[currentRow][currentColumn - 1] == '.'){ board[currentRow][--currentColumn] = currentLetter++; movesTried = 0; }else{ movesTried++; } } break; case 1: if(currentRow - 1 >= 0){ if(board[currentRow - 1][currentColumn] == '.'){ board[--currentRow][currentColumn] = currentLetter++; movesTried = 0; }else{ } movesTried++; } break; case 2: if(currentColumn + 1 != SIZE){ if(board[currentRow][currentColumn + 1] == '.'){ board[currentRow][++currentColumn] = currentLetter++; movesTried = 0; }else{ } movesTried++; } break; case 3: if(currentRow + 1 != SIZE){ if(board[currentRow + 1][currentColumn] == '.'){ board[++currentRow][currentColumn] = currentLetter++; movesTried = 0; }else{ movesTried++; } } break; } } for(int i = 0; i < SIZE; i++){ for(int j = 0; j < SIZE; j++){ printf("%c",board[i][j]); } printf("\n"); } return 0; }