Untitled

 avatar
unknown
c_cpp
2 months ago
2.4 kB
3
Indexable
void Player::ExecuteCommand(Command* command, int commandCount, Grid* pGrid)
{
	Input* pIn = pGrid->GetInput();
	Output* pOut = pGrid->GetOutput(); 
	CellPosition currentPos = pCell->GetCellPosition();
	CellPosition newPos = currentPos;
	int x, y;
	for (int i = 0; i < commandCount; i++)
	{
		switch (command[i])
		{
		case MOVE_FORWARD_ONE_STEP:
			stepCount = 1;
			break;
		case MOVE_BACKWARD_ONE_STEP:
			stepCount = -1;
			break;
		case MOVE_FORWARD_TWO_STEPS:
			stepCount = 2;
			break;
		case MOVE_BACKWARD_TWO_STEPS:
			stepCount = -2;
			break;
		case MOVE_FORWARD_THREE_STEPS:
			stepCount = 3;
			break;
		case MOVE_BACKWARD_THREE_STEPS:
			stepCount = -3;
			break;
		case ROTATE_CLOCKWISE:
			RotateClockwise(pGrid);
			continue;
		case ROTATE_COUNTERCLOCKWISE:
			RotateCounterClockwise(pGrid);
			continue;
		case NO_COMMAND:
		default:
			return;
		}
	}
	for (int i = 0; i < abs(stepCount); i++)
	{

		switch (currDirection)
		{
		case RIGHT:
			newPos.SetHCell(newPos.HCell() + (stepCount>0 ? 1: -1 ));  
			break;
		case LEFT:
			newPos.SetHCell(newPos.HCell() - (stepCount > 0 ? 1 : -1));
			break;
		case DOWN:
			newPos.SetVCell(newPos.VCell() + (stepCount > 0 ? 1 : -1));
			break;
		case UP:
			newPos.SetVCell(newPos.VCell() - (stepCount > 0 ? 1 : -1)); //dec count of VCELL
			break;
		}
		if (!newPos.IsValidCell())
		{
			pOut->PrintMessage("you're out of bound, movement is stopped");
			return;
		}
		if (i < abs(stepCount) - 1) {
			pOut->PrintMessage("Click anywhere to execute the next step.");
			pIn->GetPointClicked(x, y);
		}
		pGrid->UpdatePlayerCell(this, newPos); 
	}
	pGrid->DisPlayerInfo();
	Cell* finalCell = pCell;

	if (finalCell->GetGameObject() != nullptr) {
		finalCell->GetGameObject()->Apply(pGrid, this);
	}

	// Handle game-ending or special object conditions
	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.");
			return;
		}
	}
}
Editor is loading...
Leave a Comment