HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding and moving the next obstacle in a player's path

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
paththeplayernextmovingfindingandobstacle

Problem

This code finds the next obstacle in the player's path, then moves it. I am trying to make it more efficient, because this code runs for each frame, and it lags a bit. It does work though.

There are 8 blockers, which are basically obstacles. We subtract from the players rotation until the rotation is equal to one of these eight blockers. Then we move the blocker. Each blocker cannot be moved two times in a row, though.

You might be wondering why rotation determines if the player collides. That is because my game is based around a planet, to where the player rotates.

whileLoop:
    while (true) {

        for (Blocker blocker : blockers) {
            if (playerRotation == (int) blocker.getRotation()) {
                if(lastBlockerMoved != blocker) {
                    lastBlockerMoved = blocker;
                    float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);
                    float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY - gap);
                    float duration = Tools.generateFloat(Config.minBlockerYDuration, Config.maxBlockerYDuration);
                    blocker.addAction(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation));
                    blocker.addAction(GapSizeAction.getGapSizeAction(gap, duration));
                }

                break whileLoop;
            }
        }

        playerRotation--; //-- is going right, ++ would be going left
        while (playerRotation > 360) {
            playerRotation -= 360;
        }
        while (playerRotation <= 0) {
            playerRotation += 360;
        }
    }

Solution

Take out the variables that are not changing within the loops and initialise them once.

float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);
float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY - gap);
float duration = Tools.generateFloat(Config.minBlockerYDuration, Config.maxBlockerYDuration);


It is unnecessary to continue to break out of the for loop to change the playerRotation. so then add within your for loop the playerRotation decrement with two if conditions (the while is unnecessary, it can only change by 1, so cannot exceed greater than 360 or less than 0.

while (true) {

    for (Blocker blocker : blockers) {
        if (playerRotation == (int) blocker.getRotation()) {            
            if(lastBlockerMoved != blocker) {
                lastBlockerMoved = blocker;
                blocker.addAction(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation));
                blocker.addAction(GapSizeAction.getGapSizeAction(gap, duration));

                playerRotation--; //-- is going right, ++ would be going left
                if (playerRotation > 360) 
                    playerRotation -= 360;

                if (playerRotation  break;
}


I am not sure of what condition you need to end this game, but you will need to add this within your while loop to break out.

Code Snippets

float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);
float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY - gap);
float duration = Tools.generateFloat(Config.minBlockerYDuration, Config.maxBlockerYDuration);
while (true) {

    for (Blocker blocker : blockers) {
        if (playerRotation == (int) blocker.getRotation()) {            
            if(lastBlockerMoved != blocker) {
                lastBlockerMoved = blocker;
                blocker.addAction(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation));
                blocker.addAction(GapSizeAction.getGapSizeAction(gap, duration));

                playerRotation--; //-- is going right, ++ would be going left
                if (playerRotation > 360) 
                    playerRotation -= 360;

                if (playerRotation <= 0) 
                    playerRotation += 360;

            }
        }
    }
    // If game over -> break;
}

Context

StackExchange Code Review Q#140762, answer score: 7

Revisions (0)

No revisions yet.