ID:160151
 
Ok Ive been wondering this for awhile but have put it off cause Ive been working on other stuff, but I think im missing an easier way to handle variables that are changed based on direction. Currently Im doing something like this
if(owner.dir==NORTH)
P.cx=-6
P.y+=1
if(owner.dir==EAST)
P.cx=-11
P.x+=1
if(owner.dir==WEST)
P.cx=11
P.x-=1
if(owner.dir==SOUTH)
P.cy=8
P.y-=1
if(owner.dir==NORTHEAST)
P.cy=-8
P.cx=-11
P.y+=1
P.x+=1
if(owner.dir==NORTHWEST)
P.cy=-8
P.cx=11
P.y+=1
P.x-=1
if(owner.dir==SOUTHEAST)
P.cy=8
P.cx=-11
P.y-=1
P.x+=1
if(owner.dir==SOUTHWEST)
P.cy=8
P.cx=11
P.y-=1
P.x-=1

But there has to be an easier way to do stuff like this than a whole bunch of if statements, I just cant seem to think of a good method
Directions are stored as bits, so one way you can can do this is by setting the variables based on a combination of cardinal directions, N,S,E,W. Because your snippet has a slight discrepancy when the direction is diagonal north, (NE or NW), you'll have to account for that under the NORTH condition. Other than that, you shouldn't need to define conditions for the diagonals, since they are accounted for by the combination of two cardinal conditions.

if(owner.dir & NORTH)
P.cx=-6
P.y+=1
if(owner.dir & EAST || owner.dir & WEST)
P.cy=-8
else if(owner.dir & SOUTH)
P.cy=8
P.y-=1
if(owner.dir & EAST)
P.cx=-11
P.x+=1
else if(owner.dir & WEST)
P.cx=11
P.x-=1
In response to Xooxer
ok but its basically the same process just using bits, its a little shorter cause of the formating but in general its the same idea which is checking through all the possible positions. I guess I can change to bit functions for the speed, but the logic is basically the same.
In response to NightJumper88
There really is no other alternative that would work better. You could do fancy things with lists and procs, but in the end, this set of if()s will do the job best.
In response to Xooxer
ok then that answers my question I wasnt sure and couldnt think of anything better. Good to know that im not completely retarded and missed something