# Better way to write a code that simulates monster movement

This topic is 1368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here is my approach written in Java:

I am not a fan of hardcoding indices which is why I was looking for a better way of writing the code.


private int[] monsterMovementFrames = {100,200,300,400,500};

public void update()
{

{
counter++;
}

if(counter == monsterMovementFrames[0] || counter == monsterMovementFrames[2])
{
state = ActionState.RUNNING;

}

if(counter == monsterMovementFrames[0] || counter == monsterMovementFrames[4] )
{
direction = Direction.RIGHT;

}

else if(counter == monsterMovementFrames[1] || counter == monsterMovementFrames[3])
{
state = ActionState.IDLE;
}

else if(counter == monsterMovementFrames[2])
{
direction = Direction.LEFT;

}

if(counter == monsterMovementFrames[4])
{
counter = 0;
}



##### Share on other sites

Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

To get rid of hard coded numbers, the usual way is to load a set of "instructions" from the resources. Mapping your use case, the data should then describe both when the behavior should be altered and to which behavior it should be altered. So let us assume a sequence of number tuples, where the one number denotes a moment in time and the other denotes an instruction. The sequence of behaviors is best ordered by the moment in time. E.g.:

enum Instruction {
Idle =0,
TurnRight,
TurnLeft,
Run,
Restart
};
struct Behavior {
uint16_t moment;
uint16_t instruction;
};
Behavior const* Monster::behaviors;


After loading the array of behavior, a general routine can access and process it.

The above just makes your hard-coded behavior data-driven. A dynamic solution, i.e. where the monster may react on a given situation, would be modeled totally different, of course.

##### Share on other sites
If you don't want to hard-code... don't. Load up a data file that describes the monster, it's frames (I'm assuming those are animation frames), and which frames map to which abstract states.

Your code is somewhat confusing; are your frames defining the behavior of how/when the monster moves? Separate that data. Make code that determines when the monster moves. It sets abstract animation state. The animation code takes that abstract state and the animation data defined for the monster to select the proper frames of animation. The animations can be played without moving, moving can happen without animation, and they work together when appropriate.

##### Share on other sites

Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

Nope isDead is only set to true when the monster life is less than or equal to zero but that code is not even posted. So you can't judge with your statement... =/ The counter reaching a certain value threshold only triggers behavior states that do not involving any death behavior on the monster's part.

##### Share on other sites

Your code is somewhat confusing; are your frames defining the behavior of how/when the monster moves?

It just specify "when" it is suppose to behave. You can think of the monster counting from 100 to 500 in-game. When it reaches a certain number, the monster behavior state changes.

The drawing code or "how" it gets displayed on screen is not posted because the drawing code is clean code.

##### Share on other sites

Judging on the code snippet shown in the OP alone, when the monster dies for example when counter is exactly 100, so isDead is set to true, in the next update iteration the counter is not altered, and hence the first conditional matches, the state is set to RUNNING, and so the monster became a zombie monster ;)

Nope isDead is only set to true when the monster life is less than or equal to zero but that code is not even posted. So you can't judge with your statement... =/ The counter reaching a certain value threshold only triggers behavior states that do not involving any death behavior on the monster's part.

Please read again: My comment targets the case when isDead is set to true (regardless where in your code this happens) at a moment when counter is equal to any of the moments in time listed in monsterMovementFrames, for example 100. In such a case one of the conditional branches is true, and it is true forever because the counter is not changed anymore. That means although the monster is tagged as dead, its update() method still applies behavior to the monster over and over again! For sure, this is an edge case, but it can and will happen if enough runs are made.