onground &= !jump; //assignment and bit logic, low cost
the first version. I left it out of the other two, assuming you would find a place to put it. But this makes the first version that I gave maybe preferable now?
you can also do what is normally an error and assign inside an expression:
if( onground &= !jump) //the result of an assignment is jacked into 0/1 realm as bool for you
you will get a warning, because its a common error, but you can do it if you like.
ok so now you set the speed, which the original did not do.
here, it depends. if the speed is set in all branches, its still easy.
The trouble is I don't know what the glossed over ground and air code look like so placement is guesswork on my side. How about:
1 2 3 4
|
onground &= !jump; //assignment and bit logic, low cost
if(onground) //one compare and one jump
else //air
yspeed = 12; //or, yspeed = min(yspeed, 12) or max or some other expression??
|
it does not require a if statement for yspeed with anything you have shown.
worst case you can say yspeed = 12*onground + yspeed*(!onground);
//here yspeed is set to 12*1 if onground is true, and unchanged if onground is false.
This is exotic branch avoidance. If you want to use an if statement instead, most people would do that. At some point being able to read the code becomes an issue if you overdo it.
it may also be possible to init yspeed = 12 and change it only if needed to .. that would also remove the logic.
the simple example is not the important part. The idea is what is important... get rid of as much excess branching and conditional testing etc as you can. Again, a k-map is very useful for that in big systems. They don't stress those enough to programmers; it was used more in wiring circuits to avoid excess wires and costs.