first motion table

This commit is contained in:
Wolfgang Hottgenroth 2024-03-14 17:25:45 +01:00
parent a7ea698f9b
commit f4b614bf0f
4 changed files with 117 additions and 266 deletions

View File

@ -15,10 +15,10 @@ uint16_t counter;
static uint8_t buttonsMoveLeftPressed() {
// -----------------------
// TEST CODE
if (counter == 25) {
ledGreenToggle();
return 1;
}
//if (counter == 25) {
// ledGreenToggle();
// return 1;
//}
// -----------------------
return 0;
}
@ -28,24 +28,17 @@ static uint8_t buttonsMoveRightPressed() {
}
static uint8_t buttonsRotateLeftPressed() {
// -----------------------
// TEST CODE
if (counter == 35) {
ledGreenToggle();
return 1;
}
// -----------------------
return 0;
}
static uint8_t buttonsRotateRightPressed() {
// -----------------------
// TEST CODE
if (counter == 40) {
ledGreenToggle();
return 1;
}
// -----------------------
// -----------------------
// TEST CODE
if (counter == 80) {
ledGreenToggle();
return 1;
}
// -----------------------
return 0;
}

View File

@ -62,6 +62,6 @@ void gameExec(void *handle) {
}
void gameInit() {
schAdd(gameExec, NULL, 0, 5000);
schAdd(gameExec, NULL, 0, 1000);
}

View File

@ -6,275 +6,132 @@
#define COLOR _cyan
typedef struct {
uint8_t x;
uint8_t y;
} pixel_t;
typedef struct {
int8_t x;
int8_t y;
} offset_t;
typedef struct {
offset_t set[4];
offset_t reset[4];
offset_t offset;
} motion_t;
typedef struct {
uint8_t color;
pixel_t draw[4];
motion_t motion[5][4];
} motionTable_t;
const motionTable_t motions[1] = {
{ // i
.color = _cyan,
.draw = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} },
.motion = {
{
// move down
{ .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 0
{ .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90
{ .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 180
{ .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} } // 270
},
{
// move left
{ .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 0
{ .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90
{ .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 180
{ .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 270
},
{
// move right
{ .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 0
{ .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 90
{ .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 180
{ .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 270
},
{
// rotate left
{ .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 0
{ .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 90
{ .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 180
{ .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 270
},
{
// rotate right
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 0
{ .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 90
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 180
{ .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 270
},
}
}
};
uint8_t draw_i() {
uint8_t res = 0;
if (canvasIsPixelFree(stone.x, stone.y) &&
canvasIsPixelFree(stone.x, stone.y+1) &&
canvasIsPixelFree(stone.x, stone.y+2) &&
canvasIsPixelFree(stone.x, stone.y+3)) {
canvasSetPixel(stone.x, stone.y, COLOR);
canvasSetPixel(stone.x, stone.y+1, COLOR);
canvasSetPixel(stone.x, stone.y+2, COLOR);
canvasSetPixel(stone.x, stone.y+3, COLOR);
if (canvasIsPixelFree(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y)) {
canvasSetPixel(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y, motions[stone.shape].color);
res = 1;
}
return res;
}
uint8_t move(direction_t direction) {
uint8_t res = 0;
if (canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y) &&
canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y)) {
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[0].y, _off);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[1].y, _off);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[2].y, _off);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[3].y, _off);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y, motions[stone.shape].color);
canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y, motions[stone.shape].color);
stone.x += motions[stone.shape].motion[direction][stone.orientation].offset.x;
stone.y += motions[stone.shape].motion[direction][stone.orientation].offset.y;
res = 1;
}
return res;
}
uint8_t moveDown_i() {
uint8_t res = 0;
switch (stone.orientation) {
case e_0:
case e_180:
// check whether the destination pixel to the bottom are free
if (canvasIsPixelFree(stone.x, stone.y+4)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x, stone.y+4, COLOR);
// update the coordinates
stone.y += 1;
// positive result
res = 1;
}
break;
case e_90:
case e_270:
// check whether the destination pixel to the bottom are free
if (canvasIsPixelFree(stone.x, stone.y+1) &&
canvasIsPixelFree(stone.x+1, stone.y+1) &&
canvasIsPixelFree(stone.x+2, stone.y+1) &&
canvasIsPixelFree(stone.x+3, stone.y+1)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x+1, stone.y, _off);
canvasSetPixel(stone.x+2, stone.y, _off);
canvasSetPixel(stone.x+3, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x, stone.y+1, COLOR);
canvasSetPixel(stone.x+1, stone.y+1, COLOR);
canvasSetPixel(stone.x+2, stone.y+1, COLOR);
canvasSetPixel(stone.x+3, stone.y+1, COLOR);
// update the coordinates
stone.y += 1;
// positive result
res = 1;
}
break;
}
return res;
return move(e_MoveDown);
}
uint8_t moveLeft_i() {
uint8_t res = 0;
switch (stone.orientation) {
case e_0:
case e_180:
// check whether the destination pixel to the bottom are free
if ((stone.x > 0) &&
canvasIsPixelFree(stone.x-1, stone.y) &&
canvasIsPixelFree(stone.x-1, stone.y+1) &&
canvasIsPixelFree(stone.x-1, stone.y+2) &&
canvasIsPixelFree(stone.x-1, stone.y+3)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x, stone.y+1, _off);
canvasSetPixel(stone.x, stone.y+2, _off);
canvasSetPixel(stone.x, stone.y+3, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x-1, stone.y, COLOR);
canvasSetPixel(stone.x-1, stone.y+1, COLOR);
canvasSetPixel(stone.x-1, stone.y+2, COLOR);
canvasSetPixel(stone.x-1, stone.y+3, COLOR);
// update the coordinates
stone.x -= 1;
// positive result
res = 1;
}
break;
case e_90:
case e_270:
// check whether the destination pixel to the bottom are free
if ((stone.x > 0) &&
canvasIsPixelFree(stone.x-1, stone.y)) {
// clear the pixel we move away from
canvasSetPixel(stone.x+3, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x-1, stone.y, COLOR);
// update the coordinates
stone.x -= 1;
// positive result
res = 1;
}
break;
}
return res;
return move(e_MoveLeft);
}
uint8_t moveRight_i() {
uint8_t res = 0;
switch (stone.orientation) {
case e_0:
case e_180:
// check whether the destination pixel to the bottom are free
if (canvasIsPixelFree(stone.x+1, stone.y) &&
canvasIsPixelFree(stone.x+1, stone.y+1) &&
canvasIsPixelFree(stone.x+1, stone.y+2) &&
canvasIsPixelFree(stone.x+1, stone.y+3)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x, stone.y+1, _off);
canvasSetPixel(stone.x, stone.y+2, _off);
canvasSetPixel(stone.x, stone.y+3, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x+1, stone.y, COLOR);
canvasSetPixel(stone.x+1, stone.y+1, COLOR);
canvasSetPixel(stone.x+1, stone.y+2, COLOR);
canvasSetPixel(stone.x+1, stone.y+3, COLOR);
// update the coordinates
stone.x += 1;
// positive result
res = 1;
}
break;
case e_90:
case e_270:
// check whether the destination pixel to the bottom are free
if (canvasIsPixelFree(stone.x+4, stone.y)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x+4, stone.y, COLOR);
// update the coordinates
stone.x += 1;
// positive result
res = 1;
}
break;
}
return res;
return move(e_MoveRight);
}
uint8_t rotateLeft_i() {
uint8_t res = 0;
switch (stone.orientation) {
case e_0:
case e_180:
// check whether the destination pixel to the bottom are free
if (stone.x > 0 &&
canvasIsPixelFree(stone.x-1, stone.y+1) &&
canvasIsPixelFree(stone.x+1, stone.y+1) &&
canvasIsPixelFree(stone.x+2, stone.y+1)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x, stone.y+1, _off);
canvasSetPixel(stone.x, stone.y+2, _off);
canvasSetPixel(stone.x, stone.y+3, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x-1, stone.y+1, COLOR);
canvasSetPixel(stone.x, stone.y+1, COLOR);
canvasSetPixel(stone.x+1, stone.y+1, COLOR);
canvasSetPixel(stone.x+2, stone.y+1, COLOR);
// update the coordinates
stone.x -= 1;
stone.y += 1;
if (stone.orientation == e_0) {
stone.orientation = e_270;
} else {
stone.orientation = e_90;
}
// positive result
res = 1;
}
break;
case e_90:
case e_270:
// check whether the destination pixel to the bottom are free
if (stone.y > 1 &&
canvasIsPixelFree(stone.x+1, stone.y-2) &&
canvasIsPixelFree(stone.x+1, stone.y-1) &&
canvasIsPixelFree(stone.x+1, stone.y+1)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x+2, stone.y, _off);
canvasSetPixel(stone.x+3, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x+1, stone.y-2, COLOR);
canvasSetPixel(stone.x+1, stone.y-1, COLOR);
canvasSetPixel(stone.x+1, stone.y+1, COLOR);
// update the coordinates
stone.x += 1;
stone.y -= 2;
if (stone.orientation == e_90) {
stone.orientation = e_0;
} else {
stone.orientation = e_180;
}
// positive result
res = 1;
}
break;
uint8_t res = move(e_RotateLeft);
if (res) {
stone.orientation = ((orientation_t[]){ e_270, e_0, e_90, e_180 })[stone.orientation];
}
return res;
}
uint8_t rotateRight_i() {
uint8_t res = 0;
switch (stone.orientation) {
case e_0:
case e_180:
// check whether the destination pixel to the bottom are free
if (stone.x > 1 &&
canvasIsPixelFree(stone.x-2, stone.y+1) &&
canvasIsPixelFree(stone.x-1, stone.y+1) &&
canvasIsPixelFree(stone.x+1, stone.y+1)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x, stone.y+2, _off);
canvasSetPixel(stone.x, stone.y+3, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x-2, stone.y+1, COLOR);
canvasSetPixel(stone.x-1, stone.y+1, COLOR);
canvasSetPixel(stone.x+1, stone.y+1, COLOR);
// update the coordinates
stone.x -= 2;
stone.y += 1;
if (stone.orientation == e_0) {
stone.orientation = e_270;
} else {
stone.orientation = e_90;
}
// positive result
res = 1;
}
break;
case e_90:
case e_270:
// check whether the destination pixel to the bottom are free
if (stone.y > 1 &&
canvasIsPixelFree(stone.x+2, stone.y-2) &&
canvasIsPixelFree(stone.x+2, stone.y-1) &&
canvasIsPixelFree(stone.x+2, stone.y+1)) {
// clear the pixel we move away from
canvasSetPixel(stone.x, stone.y, _off);
canvasSetPixel(stone.x+1, stone.y, _off);
canvasSetPixel(stone.x+3, stone.y, _off);
// set the pixel we are moving into
canvasSetPixel(stone.x+2, stone.y-2, COLOR);
canvasSetPixel(stone.x+2, stone.y-1, COLOR);
canvasSetPixel(stone.x+2, stone.y+1, COLOR);
// update the coordinates
stone.x += 2;
stone.y -= 2;
if (stone.orientation == e_90) {
stone.orientation = e_0;
} else {
stone.orientation = e_180;
}
// positive result
res = 1;
}
break;
uint8_t res = move(e_RotateRight);
if (res) {
stone.orientation = ((orientation_t[]){ e_90, e_180, e_270, e_0 })[stone.orientation];
}
return res;
}

View File

@ -14,6 +14,7 @@ uint8_t stoneRotateRight();
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeInvalid } shape_t;
typedef enum { e_MoveDown, e_MoveLeft, e_MoveRight, e_RotateLeft, e_RotateRight } direction_t;
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
typedef struct {