From a3eaa5d7cd4bdb5cf1977c4cd4e8b4dd28e737ea Mon Sep 17 00:00:00 2001 From: Oxore Date: Mon, 9 Jul 2018 03:53:22 +0300 Subject: Introduce wall kick, refactor Refactor controls a little: move key repeat clocks reset to the keys handler. Refactor field: rename rotation function (add "clockwise" postfix). --- src/field.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'src/field.c') diff --git a/src/field.c b/src/field.c index 62b6455..ebe61d5 100644 --- a/src/field.c +++ b/src/field.c @@ -179,12 +179,55 @@ static void rotate_shape_right(struct shape *shape) shape->c[j][i] = arr[i+1][3-j]; } -void field_rotate_shape(struct field *fld, unsigned int index) +static int wall_kick(struct field *fld, struct shape *shape) +{ + // try kick the left wall + shape->x++; + if (field_shape_collision(fld, shape)) + shape->x--; + else + return 1; + + // try kick the right wall + shape->x--; + if (field_shape_collision(fld, shape)) { + if (shape->t == 6) { + shape->x--; + if (field_shape_collision(fld, shape)) + shape->x++; + else + return 1; + } + shape->x++; + } else { + return 1; + } + + // try kick the floor + shape->y++; + if (field_shape_collision(fld, shape)) { + if (shape->t == 6) { + shape->y++; + if (field_shape_collision(fld, shape)) + shape->y--; + else + return 1; + } + shape->y--; + } else { + return 1; + } + + return 0; +} + +void field_rotate_shape_clockwise(struct field *fld, unsigned int index) { struct shape *shape = &fld->shape[index]; rotate_shape_right(shape); if (field_shape_collision(fld, shape)) - rotate_shape_left(shape); + if (!wall_kick(fld, shape)) + rotate_shape_left(shape); } int field_move_shape_down(struct field *fld, unsigned int index) -- cgit v1.2.3