samedi 27 juin 2015

Is there a better readable way to write this if statement chain?

I have the following code:

Creature::cancelWalk()
{
    Player* player = getPlayer();

    if (!player) {
        if (getMonster() && getMonster()->getMaster() && getMonster()->getMaster()->getPlayer()) {
            player = getMonster()->getMaster()->getPlayer();
        }
    }

    if (player) {
        player->sendCancelMessage(ret);
        player->sendCancelWalk();
    }
}

After a brief analysis, it's easy to understand I want to achieve something simple:

If the creature is the player itself, then sendCancelMessage and sendCancelWalk. Else, if the creature is a monster that also has a master that is a player, send the same stuff to the client.

Is there a better way to write this code without adding other methods on Monster, Creature and Player classes?

Monster and Player both are "siblings" deriving from Creature.

Aucun commentaire:

Enregistrer un commentaire