vendredi 26 février 2016

What is an efficient way to move in a specific direction relative to its data?

So I'm working on a mini-game where you are a player in an arena with monsters. The goal is to kill all the monsters by shooting them. You can move up/right/down/left, and so can the monsters. If the monsters touch you, you die.

I have to create a player AI for the player to try to stay alive.

So I have defined an integer for each "Danger Zone" for each direction (DangerUp/DangerDown/DangerRight/DangerLeft) and initialized them all to zero. So it looks like this:

int DangerUp = 0, DangerDown = 0, DangerRight = 0, DangerLeft = 0;

If there is a monster above of me, dangerUp will increment by one. If there is one below me, dangerDown will increment by one. Same with the other two.

So what I'm trying to write (in psuedeocode) is essentially:

if I'm surrounded by three (which means 3 of the 4 Danger variables have a value of 1), move to the direction where the danger is 0.

I was planning on doing this:

if (DangerUP == 1 && DangerDOWN == 1 && DangerLEFT == 1 && DangerRIGHT == 0) // if surrounded by 3 in the up, down, left direction

{
// move right;
player_x_direction++; // (this moves my character one direction to the right)

}

and then I plan on doing three else if's where I test for all combinations of having 3 of the 4 variables equal to 1.

Is there a simpler way to do this where I can just move in the direction of whatever danger direction is = 0?

Aucun commentaire:

Enregistrer un commentaire