void moveSnake() { voxel* nextDirection; nextDirection = getNextDirection(); if (nextDirection == NULL) { deathFrame = frameCount; return; } snakeDirection = *nextDirection; bool grow = (snake.size() < initialSnakeLength); voxel front = snake[0] + *nextDirection; for(auto it = treats.begin(); it != treats.end(); ++it) { if (*it == front) { treats.erase(it); grow = true; break; } } snake.insert(snake.begin(), front); if(!grow) { snake.pop_back(); } if (!treats.size()) { addTreat(); } }
void Pacman::ghostCollide(unsigned int ghostId) { if(m_ghostKillTimer.isStarted() && !m_ghost[ghostId].Invulnerable) { m_ghost[ghostId].IsDead = true; m_ghost[ghostId].Direction = getNextDirection(m_ghost[ghostId].CurrentCellIndex, m_ghost[ghostId].HomeIndex, m_mapNullCell); unsigned int scoreEarned = 0; if(m_powerupKillCounter == 3) { scoreEarned = 1600; } else if(m_powerupKillCounter == 2) { scoreEarned = 800; } else if(m_powerupKillCounter == 1) { scoreEarned = 400; } else { scoreEarned = 200; } addScore(scoreEarned); if(m_powerupKillCounter < m_ghostCount) { m_powerupKillCounter += 1; } m_sound.setBuffer(m_killSound); m_sound.play(); } else { m_player.Lives -= 1; m_player.IsDead = true; m_sound.setBuffer(m_deadSound); m_sound.play(); } m_pauseTimer.start(); m_gameTimer.pause(); m_frameTimer.pause(); if(m_ghostKillTimer.isStarted()) { m_ghostKillTimer.pause(); } }
void Stalfos::movement(std::vector<std::shared_ptr<GameObject>>* worldMap) { Point offsets(getXOffset(), getYOffset()); switch (dir){ case Direction::Down: { Point pt(position.x, position.y + minStep); if (!isColliding(worldMap, fullMask, offsets) && !isOutsideRoomBound(pt)) position.y += minStep; else getNextDirection(Direction::Down); break; } case Direction::Up: { Point pt(position.x, position.y - minStep); if (!isColliding(worldMap, fullMask, offsets) && !isOutsideRoomBound(pt)) position.y -= minStep; else getNextDirection(Direction::Up); break; } case Direction::Left: { Point pt(position.x - minStep, position.y); if (!isColliding(worldMap, fullMask, offsets) && !isOutsideRoomBound(pt)) position.x -= minStep; else getNextDirection(Direction::Left); break; } case Direction::Right: { Point pt(position.x + minStep, position.y); if (!isColliding(worldMap, fullMask, offsets) && !isOutsideRoomBound(pt)) position.x += minStep; else getNextDirection(Direction::Right); break; } } }
Stalfos::Stalfos(Point pos, bool canBeCollidedWith){ position = pos; width = Global::TileWidth; height = Global::TileHeight; isCollideable = canBeCollidedWith; loadAnimation(); isInvincible = false; healthPoint = 2; strength = 1; currentInvincibleFrame = 0; pushbackStep = 0; setupMask(&fullMask, width, height, sf::Color::Magenta); setupMask(&mask, width, height, sf::Color::Magenta); dir = Direction::None; getNextDirection(Direction::None); walkAnimIndex = 0; isParalyzed = false; currentParalyzeTime = 0; }
/* normal use funtions. These are the ones you should be using */ signed char getNextDirection() {//returns relative Path signed char temp=0; signed char counter=0 ; if(pathStorage[0]!=0) { temp = pathStorage[1]; pathStorage[0]--; /* if((temp==2)&&(directionOffset==1)) { temp=1; }else{ temp = shiftBits(temp,directionOffset); }*/ /* display_clear(1); display_goto_xy(0,0); display_int(directionOffset,1); display_goto_xy(3,0); display_int(pathStorage[1],1); display_goto_xy(1,6); display_int(temp,1);*/ temp = shiftBitsForDriving(temp,directionOffset); /* display_goto_xy(3,7); display_int(temp,1); */ switch(pathStorage[1]) { case 1: hasMoved(top);directionOffset = 0;break; case 2: hasMoved(right);directionOffset= 1;break; case 4: hasMoved(down);directionOffset = 2;break; case 8: hasMoved(left);directionOffset = 3;break; case 0: break; default: break; } /* display_goto_xy(1,1); display_int(directionOffset,1); display_update();*/ //systick_wait_ms(1200); directionOffset = directionOffset % 4; //ecrobot_sound_tone(100,100, 100); //*/ for(counter = 1; counter <=23;counter++){pathStorage[counter]=pathStorage[counter+1];}pathStorage[24]=0; return temp; }else{ getNextUnvisited(); //ecrobot_sound_tone(900,80, 100); /* display_goto_xy(13,0); display_int(pathStorage[0],1); display_update();*/ //systick_wait_ms(500); return getNextDirection(); } return 0; }
void speedRun(void) { resetSpeedProfile(); useIRSensors = 1; useSpeedProfile = 1; int nextDir[100] = {0}; int length = 0; xPos = 0; yPos = 0; orientation = 'N'; // Close off untraced routes closeUntracedCells(); updateDistance(); visualizeGrid(); // Simulate path for (int i = 0; !atCenter(); i++) { if (orientation == 'N') { while (!hasNorth(block[yPos][xPos]) && (distance[yPos + 1][xPos] == distance[yPos][xPos] - 1)) { length++; yPos++; } } else if (orientation == 'E') { while (!hasEast(block[yPos][xPos]) && (distance[yPos][xPos + 1] == distance[yPos][xPos] - 1)) { length++; xPos++; } } else if (orientation == 'S') { while (!hasSouth(block[yPos][xPos]) && (distance[yPos - 1][xPos] == distance[yPos][xPos] - 1)) { length++; yPos--; } } else if (orientation == 'W') { while (!hasWest(block[yPos][xPos]) && (distance[yPos][xPos - 1] == distance[yPos][xPos] - 1)) { length++; xPos--; } } distances[i] = length; nextDir[i] = getNextDirection(); length = 0; } /* Print values for (int i = 0; distances[i]; i++) printf("distances[%d] = %d | nextDir[%d] = %d\n\r", i, distances[i], i, nextDir[i]); */ orientation = 'N'; // Run path for (int i = 0; distances[i] != 0; i++) { moveForward(distances[i]); if (nextDir[i] == MOVEN) { moveN(); } else if (nextDir[i] == MOVEE) { moveE(); } else if (nextDir[i] == MOVES) { moveS(); } else if (nextDir[i] == MOVEW) { moveW(); } } useSpeedProfile = 0; turnMotorOff; }
void Pacman::onTick() { // Cambiar dirección deseada de Pacman if(m_input->getKeyPress()->Up.value) { m_player.DesiredDirection = ViewDirection::Up; } else if(m_input->getKeyPress()->Right.value) { m_player.DesiredDirection = ViewDirection::Right; } else if(m_input->getKeyPress()->Left.value) { m_player.DesiredDirection = ViewDirection::Left; } else if(m_input->getKeyPress()->Down.value) { m_player.DesiredDirection = ViewDirection::Down; } if(m_readyTimer.getTicks() > 0) { if(m_readyTimer.getTicks() >= 2000) { m_readyTimer.stop(); m_gameTimer.start(); m_frameTimer.start(); } return; } if(m_pauseTimer.getTicks() > 0) { if(m_pauseTimer.getTicks() >= 500) { m_pauseTimer.stop(); m_gameTimer.resume(); m_frameTimer.resume(); if(m_ghostKillTimer.isPaused()) { m_ghostKillTimer.resume(); } if(m_player.IsDead) { resetGame(); } } return; } // Animar Pacman y Fantasmas (Cambio de Frames) if(m_frameTimer.getTicks() > 50) { if(m_player.Moving) { m_player.CurrentFrame++; if(m_player.CurrentFrame > 3) { m_player.CurrentFrame = 0; } } else { m_player.CurrentFrame = 1; } for(unsigned int x = 0; x < m_ghostCount; ++x) { m_ghost[x].CurrentFrame++; if(m_ghost[x].CurrentFrame > 1) { m_ghost[x].CurrentFrame = 0; } } m_frameTimer.start(); } // Mover Pacman moveEntity(&m_player.CurrentCellIndex, &m_player.PositionOffset, &m_player.Direction, &m_player.DesiredDirection, &m_player.Moving, m_player.MoveSpeed, true); bool ghostForceRefresh = false; sf::Vector2i findCoin = sf::Vector2i(m_player.CurrentCellIndex % m_mapSize.x, floor(m_player.CurrentCellIndex / m_mapSize.x)); // Comer bolas (Pacman) y actualizar mapa for(unsigned int x = 0; x < m_ballPosition.size(); ++x) { if(m_ballPosition[x] == findCoin) { mapSetCell(m_player.CurrentCellIndex, CellType::Nothing); m_ballPosition.erase(m_ballPosition.begin() + x); addScore(10); m_sound.setBuffer(m_ballSound); m_sound.play(); if(m_ballPosition.empty() && m_ballBigPosition.empty()) { resetGame(); return; } } } for(unsigned int x = 0; x < m_ballBigPosition.size(); ++x) { if(m_ballBigPosition[x] == findCoin) { mapSetCell(m_player.CurrentCellIndex, CellType::Nothing); m_ballBigPosition.erase(m_ballBigPosition.begin() + x); m_powerupKillCounter = 0; m_ghostKillTimer.start(); ghostForceRefresh = true; m_sound.setBuffer(m_powerupSound); m_sound.play(); for(unsigned int x = 0; x < m_ghostCount; ++x) { m_ghost[x].Invulnerable = false; } addScore(50); if(m_ballPosition.empty() && m_ballBigPosition.empty()) { resetGame(); return; } } } if(m_ghostKillTimer.getTicks() > 7000) { m_ghostKillTimer.stop(); for(unsigned int x = 0; x < m_ghostCount; ++x) { m_ghost[x].Invulnerable = false; m_ghost[x].LastStateChange = m_gameTimer.getTicks(); m_ghost[x].StateDuration = 3000; m_ghost[x].IsInChase = true; } ghostForceRefresh = true; } // IA Fantasmas for(unsigned int x = 0; x < m_ghostCount; ++x) { if(m_ghost[x].TimeToRelease >= m_gameTimer.getTicks()) { break; } unsigned int ignoreCell; if(m_ghost[x].Direction == ViewDirection::Left) { ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right); } else if(m_ghost[x].Direction == ViewDirection::Right) { ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left); } else if(m_ghost[x].Direction == ViewDirection::Up) { ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down); } else { ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up); } float ghostMoveSpeed = m_ghostKillTimer.isStarted() && (m_ghost[x].IsDead ? false : !m_ghost[x].Invulnerable) ? (m_ghost[x].MoveSpeed / 2) : (m_ghost[x].IsDead ? m_ghost[x].MoveSpeed * 1.5: m_ghost[x].MoveSpeed); if(m_ghost[x].CurrentCellIndex == m_ghost[x].HomeIndex) { m_ghost[x].IsDead = false; if(m_ghostKillTimer.isStarted()) { m_ghost[x].Invulnerable = true; } m_ghost[x].LastStateChange = m_gameTimer.getTicks(); m_ghost[x].StateDuration = 1000; m_ghost[x].IsInChase = true; } if(m_gameTimer.getTicks() >= (m_ghost[x].LastStateChange + m_ghost[x].StateDuration)) { if(!m_ghostKillTimer.isStarted()) { ghostForceRefresh = true; } m_ghost[x].LastStateChange = m_gameTimer.getTicks(); if(m_ghost[x].IsInChase) { m_ghost[x].IsInChase = false; m_ghost[x].StateDuration = (3000 + (rand() % 3000)); } else { m_ghost[x].IsInChase = true; m_ghost[x].StateDuration = (5000 + (rand() % 3000)); } } if(m_ghost[x].LastCellChecked != m_ghost[x].CurrentCellIndex || ghostForceRefresh) { if(m_ghost[x].IsDead) { m_ghost[x].DesiredDirection = getNextDirection(m_ghost[x].CurrentCellIndex, m_ghost[x].HomeIndex, ignoreCell); } else if(m_ghost[x].IsInChase && (!m_ghostKillTimer.isStarted() || m_ghost[x].Invulnerable)) { if(m_ghost[x].CurrentCellIndex != m_player.CurrentCellIndex) { m_ghost[x].DesiredDirection = getNextDirection(m_ghost[x].CurrentCellIndex, m_player.CurrentCellIndex, ignoreCell); } } else { unsigned int tempIndex; std::vector<unsigned int> possibleCells; if(m_ghost[x].Direction != ViewDirection::Left) { tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left); if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall) { possibleCells.push_back(tempIndex); } } if(m_ghost[x].Direction != ViewDirection::Right) { tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right); if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall) { possibleCells.push_back(tempIndex); } } if(m_ghost[x].Direction != ViewDirection::Up) { tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up); if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall) { possibleCells.push_back(tempIndex); } } if(m_ghost[x].Direction != ViewDirection::Down) { tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down); if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall) { possibleCells.push_back(tempIndex); } } // Si hay caminos alternativos a seguir (Que no sea ni a donde se dirige ni la casilla anterior) o es un callejón sin salida... if(possibleCells.size() > 0 || getMapInfo(m_ghost[x].CurrentCellIndex, m_ghost[x].Direction) == CellType::Wall) { m_ghost[x].DesiredDirection = getRandomDirection(m_ghost[x].CurrentCellIndex, ignoreCell); } } m_ghost[x].LastCellChecked = m_ghost[x].CurrentCellIndex; } moveEntity(&m_ghost[x].CurrentCellIndex, &m_ghost[x].PositionOffset, &m_ghost[x].Direction, &m_ghost[x].DesiredDirection, &m_ghost[x].Moving, ghostMoveSpeed, false); if(!m_ghost[x].IsDead) { // Calcular colisión con Pacman if(m_ghost[x].CurrentCellIndex == m_player.CurrentCellIndex) { ghostCollide(x); } else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left) == m_player.CurrentCellIndex) { if((m_player.PositionOffset.x + (m_tileSize.x / 2)) - (m_ghost[x].PositionOffset.x + (m_tileSize.x / 2)) >= 0) { ghostCollide(x); } } else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right) == m_player.CurrentCellIndex) { if((m_ghost[x].PositionOffset.x + (m_tileSize.x / 2)) - (m_player.PositionOffset.x + (m_tileSize.x / 2)) >= 0) { ghostCollide(x); } } else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up) == m_player.CurrentCellIndex) { if((m_player.PositionOffset.y + (m_tileSize.y / 2)) - (m_ghost[x].PositionOffset.y + (m_tileSize.y / 2)) >= 0) { ghostCollide(x); } } else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down) == m_player.CurrentCellIndex) { if((m_ghost[x].PositionOffset.y + (m_tileSize.y / 2)) - (m_player.PositionOffset.y + (m_tileSize.y / 2)) >= 0) { ghostCollide(x); } } } } }