Untitled
void Player::Move(Grid* pGrid, Command moveCommands[]) { int x, y; const int maxCommands = 5; Input* pIn = pGrid->GetInput(); Output* pOut = pGrid->GetOutput(); for (int i = 0; i < maxCommands; i++) { if (moveCommands[i] == NO_COMMAND) { break; } CellPosition currentPos = pCell->GetCellPosition(); CellPosition newPos = currentPos; switch (moveCommands[i]) { case MOVE_FORWARD_ONE_STEP: // Move forward based on the player's direction switch (currDirection) { case UP: newPos.SetVCell(newPos.VCell() - 1); // Move up break; case DOWN: newPos.SetVCell(newPos.VCell() + 1); // Move down break; case LEFT: newPos.SetHCell(newPos.HCell() - 1); // Move left break; case RIGHT: newPos.SetHCell(newPos.HCell() + 1); // Move right break; } break; case MOVE_FORWARD_TWO_STEPS: switch (currDirection) { case UP: newPos.SetVCell(newPos.VCell() - 2); // Move up 2 steps break; case DOWN: newPos.SetVCell(newPos.VCell() + 2); // Move down 2 steps break; case LEFT: newPos.SetHCell(newPos.HCell() - 2); // Move left 2 steps break; case RIGHT: newPos.SetHCell(newPos.HCell() + 2); // Move right 2 steps break; } break; case ROTATE_CLOCKWISE: RotateClockwise(); continue; case ROTATE_COUNTERCLOCKWISE: RotateCounterClockwise(); continue; default: continue; } if (!newPos.IsValidCell()) { pOut->PrintMessage("invalid move"); pIn->GetPointClicked(x, y); return; } pGrid->UpdatePlayerCell(this, newPos); if (i < maxCommands - 1 && moveCommands[i + 1] != NO_COMMAND) { pOut->PrintMessage("click anywhere to execute next command"); pIn->GetPointClicked(x, y); } } Cell* finalCell = pGrid->GetCell(pCell->GetCellPosition()); GameObject* obj = finalCell->GetGameObject(); if (obj != nullptr) { obj->Apply(pGrid, this); } if (finalCell->HasFlag() != nullptr) { pOut->PrintMessage("Player " + to_string(playerNum) + " wins!"); } else if (finalCell->HasWaterPit() != nullptr) { pOut->PrintMessage("You fell into a water pit! Game over for Player " + to_string(playerNum)); return; } else if (finalCell->HasDangerZone() != nullptr) { health -= 1; // Reduce health for entering the danger zone if (health <= 0) { pOut->PrintMessage("Player " + to_string(playerNum) + " has died in the danger zone."); } } }
Leave a Comment