Untitled

 avatar
unknown
c_cpp
2 months ago
4.9 kB
3
Indexable
void Player::RotateClockwise(Grid*pGrid)
{
	Output* pOut = pGrid->GetOutput();
	ClearDrawing(pOut);

	switch (currDirection)
	{
		if (currDirection == UP)
			currDirection = RIGHT;
		else if (currDirection == RIGHT)
			currDirection = DOWN;
		else if (currDirection == DOWN)
			currDirection = LEFT;
		else if (currDirection == LEFT)
			currDirection = UP;
		break;
	default:
		break;
	}
	Draw(pOut); 
}

void Player::RotateCounterClockwise(Grid*pGrid)
{
	Output* pOut = pGrid->GetOutput();
	ClearDrawing(pOut);
	switch (currDirection)
	{
		if (currDirection == UP)
			currDirection = LEFT;
		else if (currDirection == LEFT)
			currDirection = DOWN;
		else if (currDirection == RIGHT)
			currDirection = UP;
		else if (currDirection == DOWN)
			currDirection = RIGHT;
		break;
	default:
		break;
	}
	Draw(pOut);

} 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 MOVE_FORWARD_THREE_STEPS:
			switch (currDirection) {
			case UP: 
				newPos.SetVCell(currentPos.VCell() - 3);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() + 3);
				break;
			case LEFT: 
				newPos.SetHCell(currentPos.HCell() - 3); 
				break;
			case RIGHT: 
				newPos.SetHCell(currentPos.HCell() + 3); 
				break;
			}
			break;

		case MOVE_BACKWARD_ONE_STEP:
			switch (currDirection) {
			case UP: 
				newPos.SetVCell(currentPos.VCell() + 1);
				break;
			case DOWN: 
				newPos.SetVCell(currentPos.VCell() - 1); 
				break;
			case LEFT: 
				newPos.SetHCell(currentPos.HCell() + 1);
				break;
			case RIGHT:
				newPos.SetHCell(currentPos.HCell() - 1); 
				break;
			}
			break;

		case MOVE_BACKWARD_TWO_STEPS:
			switch (currDirection) {
			case UP: 
				newPos.SetVCell(currentPos.VCell() + 2);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() - 2); 
				break;
			case LEFT: 
				newPos.SetHCell(currentPos.HCell() + 2); 
				break;
			case RIGHT: 
				newPos.SetHCell(currentPos.HCell() - 2);
				break;
			}
			break;

		case MOVE_BACKWARD_THREE_STEPS:
			switch (currDirection) {
			case UP: 
				newPos.SetVCell(currentPos.VCell() + 3); 
				break;
			case DOWN: 
				newPos.SetVCell(currentPos.VCell() - 3); 
				break;
			case LEFT: 
				newPos.SetHCell(currentPos.HCell() + 3);
				break;
			case RIGHT: 
				newPos.SetHCell(currentPos.HCell() - 3);
				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