~ecs/tbrace

130cf43c614c055d50b9146ca9a456e7e4d8e35b — Eyal Sawady 10 months ago 3799c44
update.c: better lap counting
1 files changed, 13 insertions(+), 12 deletions(-)

M update.c
M update.c => update.c +13 -12
@@ 58,37 58,38 @@ static bool player_update(struct state state) {

static bool should_stop(struct state state, struct car *car) {
	int tile = state.track[car->y][car->x];
	int delta_sign = 0;

	if (car->x < 0 || car->x >= state.track_x
			|| car->y < 0 || car->y >= state.track_y) {
		return true;
	}

	switch (tile) {
	case EMPTY:
		return false;
	case LAPLINE_UP:
		if (car->vel_y < 0) {
			car->laps++;
		}
		delta_sign = -car->vel_y;
		break;
	case LAPLINE_DOWN:
		if (car->vel_y > 0) {
			car->laps++;
		}
		delta_sign = car->vel_y;
		break;
	case LAPLINE_LEFT:
		if (car->vel_y < 0) {
			car->laps++;
		}
		delta_sign = -car->vel_x;
		break;
	case LAPLINE_RIGHT:
		if (car->vel_y > 0) {
			car->laps++;
		}
		delta_sign = car->vel_x;
		break;
	default:
		return true;
	}

	if (delta_sign > 0) {
		car->laps++;
	} else if (delta_sign < 0) {
		car->laps--;
	}

	return false;
}