aboutsummaryrefslogtreecommitdiff
path: root/Marlin/src/lcd/menu/game/maze.cpp
blob: 85f752ee7de5cb2d39a027cb09e57494d2de3f1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * Marlin 3D Printer Firmware
 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 *
 * Based on Sprinter and grbl.
 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "../../../inc/MarlinConfigPre.h"

#if ENABLED(MARLIN_MAZE)

#include "game.h"

int8_t move_dir, last_move_dir, // NESW0
       prizex, prizey, prize_cnt, old_encoder;
fixed_t playerx, playery;

// Up to 50 lines, then you win!
typedef struct { int8_t x, y; } pos_t;
uint8_t head_ind;
pos_t maze_walls[50] = {
  { 0, 0 }
};

// Turn the player cw or ccw
inline void turn_player(const bool cw) {
  if (move_dir == 4) move_dir = last_move_dir;
  move_dir += cw ? 1 : -1;
  move_dir &= 0x03;
  last_move_dir = move_dir;
}

// Reset the player for a new game
void player_reset() {
  // Init position
  playerx = BTOF(1);
  playery = BTOF(GAME_H / 2);

  // Init motion with a ccw turn
  move_dir = 0;
  turn_player(false);

  // Clear prize flag
  prize_cnt = 255;

  // Clear the controls
  ui.encoderPosition = 0;
  old_encoder = 0;
}

void MazeGame::game_screen() {
  // Run the sprite logic
  if (game_frame()) do {     // Run logic twice for finer resolution

    // Move the man one unit in the current direction
    // Direction index 4 is for the stopped man
    const int8_t oldx = FTOB(playerx), oldy = FTOB(playery);
    pos_t dir_add[] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 } };
    playerx += dir_add[move_dir].x;
    playery += dir_add[move_dir].y;
    const int8_t x = FTOB(playerx), y = FTOB(playery);

  } while(0);

  u8g.setColorIndex(1);

  // Draw Score
  if (PAGE_UNDER(HEADER_H)) lcd_put_int(0, HEADER_H - 1, score);

  // Draw the maze
  // LOOP_L_N(n, head_ind) {
  //   const pos_t &p = maze_walls[n], &q = maze_walls[n + 1];
  //   if (p.x == q.x) {
  //     const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
  //     if (PAGE_CONTAINS(y1, y2))
  //       u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1);
  //   }
  //   else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) {
  //     const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x));
  //     u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1);
  //   }
  // }

  // Draw Man
  // const int8_t fy = GAMEY(foody);
  // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
  //   const int8_t fx = GAMEX(foodx);
  //   u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
  //   if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
  // }

  // Draw Ghosts
  // const int8_t fy = GAMEY(foody);
  // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
  //   const int8_t fx = GAMEX(foodx);
  //   u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
  //   if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
  // }

  // Draw Prize
  // if (PAGE_CONTAINS(prizey, prizey + PRIZE_WH - 1)) {
  //   u8g.drawFrame(prizex, prizey, PRIZE_WH, PRIZE_WH);
  //   if (PRIZE_WH == 5) u8g.drawPixel(prizex + 2, prizey + 2);
  // }

  // Draw GAME OVER
  if (!game_state) draw_game_over();

  // A click always exits this game
  if (ui.use_click()) exit_game();
}

void MazeGame::enter_game() {
  init_game(1, game_screen); // Game running
  reset_player();
  reset_enemies();
}

#endif // MARLIN_MAZE