Created
February 12, 2011 09:52
-
-
Save acdimalev/823655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define FPS 30 | |
enum player_state { | |
PLAYER_HALT, | |
PLAYER_LEFT, | |
PLAYER_RIGHT, | |
PLAYER_DOWN, | |
PLAYER_UP | |
}; | |
struct player { | |
double x, y, v; | |
enum player_state state; | |
}; | |
void animate_player(struct player *player) { | |
double p0, p1, p2; | |
double d = player->v / FPS; | |
while (d) { | |
switch(player->state) { | |
case PLAYER_HALT: | |
d = 0; | |
break; | |
case PLAYER_LEFT: | |
p0 = player->x; | |
p1 = p0 - d; | |
p2 = ceil(p0 - 1); | |
if (p1 < p2) { | |
player->x = p2; | |
d = p2 - p1; | |
} else { | |
player->x = p1; | |
d = 0; | |
} | |
break; | |
case PLAYER_RIGHT: | |
p0 = player->x; | |
p1 = p0 + d; | |
p2 = floor(p0 + 1); | |
if (p2 < p1) { | |
player->x = p2; | |
d = p1 - p2; | |
} else { | |
player->x = p1; | |
d = 0; | |
} | |
break; | |
case PLAYER_DOWN: | |
p0 = player->y; | |
p1 = p0 - d; | |
p2 = ceil(p0 - 1); | |
if (p1 < p2) { | |
player->y = p2; | |
d = p2 - p1; | |
} else { | |
player->y = p1; | |
d = 0; | |
} | |
break; | |
case PLAYER_UP: | |
p0 = player->y; | |
p1 = p0 + d; | |
p2 = floor(p0 + 1); | |
if (p2 < p1) { | |
player->y = p2; | |
d = p1 - p2; | |
} else { | |
player->y = p1; | |
d = 0; | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment