summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2018-07-09 03:53:22 +0300
committerOxore <oxore@protonmail.com>2018-07-09 03:53:22 +0300
commita3eaa5d7cd4bdb5cf1977c4cd4e8b4dd28e737ea (patch)
tree16b61f03e1dab98aa9aebcf8a0a37f43c673654d
parenta673d86621054f18b25add5878efab6fa035e352 (diff)
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).
-rw-r--r--README.md8
-rw-r--r--include/field.h2
-rw-r--r--src/engine.c9
-rw-r--r--src/field.c47
4 files changed, 58 insertions, 8 deletions
diff --git a/README.md b/README.md
index dc4633a..5be3baf 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,12 @@ make all
```
File named "tetris" is the final binary. Run it. Hit *S* to start game.
+- `s` - start a game.
+- `Left arrow` - move shape left.
+- `Right arrow` - move shape right.
+- `Up arrow` - rotate shape clockwise.
+- `Space` - hard drop.
+
Cleaning:
```
make clean
@@ -36,7 +42,7 @@ Look at the [repo releases](https://github.com/Oxore/tetris-csfml/releases)! Now
- Add screenshots and gameplay gif
- [x] Levels speed and scores calibration
- [x] Scoring system as in Tetris for NES which is described on [this page](https://tetris.wiki/Scoring)
-- [Wall kick](https://tetris.wiki/Wall_kick)
+- [x] [Wall kick](https://tetris.wiki/Wall_kick). Implemented order: left wall, right wall, floor
- [x] Hard drop
- [x] Phantom shape
- Table of records
diff --git a/include/field.h b/include/field.h
index 52b4de8..c63a836 100644
--- a/include/field.h
+++ b/include/field.h
@@ -47,7 +47,7 @@ void field_init(struct field *fld);
void field_deinit(struct field *fld);
void field_fill_random(struct field *fld);
void field_clear(struct field *fld);
-void field_rotate_shape(struct field *fld, unsigned int index);
+void field_rotate_shape_clockwise(struct field *fld, unsigned int index);
int field_move_shape_down(struct field *fld, unsigned int index);
int field_move_shape_left(struct field *fld, unsigned int index);
int field_move_shape_right(struct field *fld, unsigned int index);
diff --git a/src/engine.c b/src/engine.c
index 6abda95..fd00e34 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -220,8 +220,9 @@ static void game_tick()
static void signal_up()
{
- field_rotate_shape(&fld, 1);
+ field_rotate_shape_clockwise(&fld, 1);
project_ghost_shape(&fld, 1, 0);
+ sfClock_restart(game.putTick);
}
static void signal_harddrop()
@@ -239,7 +240,6 @@ static void signal_down()
sfClock_restart(game.gameTick);
game.scoreCurrent++;
}
- sfClock_restart(game.repPushDown);
}
static void signal_left()
@@ -248,7 +248,6 @@ static void signal_left()
project_ghost_shape(&fld, 1, 0);
sfClock_restart(game.putTick);
}
- sfClock_restart(game.repKeyLeft);
}
static void signal_right()
@@ -257,7 +256,6 @@ static void signal_right()
project_ghost_shape(&fld, 1, 0);
sfClock_restart(game.putTick);
}
- sfClock_restart(game.repKeyRight);
}
static void game_keys()
@@ -287,6 +285,7 @@ static void game_keys()
if (!(arrKeys & DOWN)) {
arrKeys = arrKeys | DOWN;
signal_down();
+ sfClock_restart(game.repPushDown);
} else {
if (sfClock_getElapsedTime(game.repPushDown).microseconds >= moveRepeatLatency2)
arrKeys = arrKeys & ~DOWN;
@@ -300,6 +299,7 @@ static void game_keys()
if (!(arrKeys & LEFT)) {
arrKeys = arrKeys | LEFT;
signal_left();
+ sfClock_restart(game.repKeyLeft);
} else if (!(arrKeys & LEFTHOLD)) {
if (sfClock_getElapsedTime(game.repKeyLeft).microseconds >= moveRepeatLatency1) {
arrKeys = arrKeys | LEFTHOLD;
@@ -319,6 +319,7 @@ static void game_keys()
if (!(arrKeys & RIGHT)) {
arrKeys = arrKeys | RIGHT;
signal_right();
+ sfClock_restart(game.repKeyRight);
} else if (!(arrKeys & RIGHTHOLD)) {
if (sfClock_getElapsedTime(game.repKeyRight).microseconds >= moveRepeatLatency1) {
arrKeys = arrKeys | RIGHTHOLD;
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)