I'm making an ASCII roguelike game and while writing the code for processing the player's move I can't decide between these two options. I have defined a class named point
to handle positions in the game, like this. I would also like to know if this is a good idea or if I should use a structure instead.
Should I pass an object as a parameter to a function or create it inside?
class point {
public:
point();
point(int x, int y);
int getX() const;
int getY() const;;
void setX(int x);
void setY(int y);
private:
int x;
int y;
};
OPTION 1
I define the target point object before calling the function.
point playerPos = player.get_position();
point moveTilePos;
switch (input) {
case MOVE_UP:
moveTilePos.setX(playerPos.getX());
moveTilePos.setY(playerPos.getY()-1);
processPlayerMove(player, moveTilePos);
which is defined as follows:
void Level::processPlayerMove(Player &player, point target) {
char moveTileSymbol;
moveTileSymbol = getTile(target);
switch(moveTileSymbol){
case WALL_SYMBOL:
break;
case GROUND_SYMBOL:
setTile(player.get_position(), GROUND_SYMBOL);
player.set_position(target);
setTile(target, PLAYER_SYMBOL);
break;
}
}
I think this is more readable overall but requires a setup that maybe should be part of the function.
Option 2
Everything is handled by the function:
point playerPos = player.get_position();
switch (input) {
case MOVE_UP:
processPlayerMove2(player, playerPos.getX(), playerPos.getY() -1)
Which is defined like this:
void Level::processPlayerMove2(Player &player, int targetX, int targetY) {
char moveTileSymbol;
point target;
target.setX(targetX);
target.setY(targetY);
moveTileSymbol = getTile(target);
switch(moveTileSymbol){
case WALL_SYMBOL:
break;
case GROUND_SYMBOL:
setTile(player.get_position(), GROUND_SYMBOL);
player.set_position(target);
setTile(target, PLAYER_SYMBOL);
break;
}
}
This case is more succinct, as the function handles everything.