| 1 | #include <snake/scene/gamemain.hh> |
| 2 | #include <snake/snake.hh> |
| 3 | #include <raylib.h> |
| 4 | #include <iostream> |
| 5 | #include <vector> |
| 6 | |
| 7 | namespace snake { |
| 8 | int score; |
| 9 | } |
| 10 | |
| 11 | namespace snake::scene { |
| 12 | Vector2 gridPos = { 16, 54 }; |
| 13 | constexpr int CELL_SIZE = 24; |
| 14 | constexpr int MAX_CELLX = 32; |
| 15 | constexpr int MAX_CELLY = 22; |
| 16 | float speed; |
| 17 | bool isDead = false; |
| 18 | |
| 19 | enum SnakeDir { |
| 20 | Up, |
| 21 | Down, |
| 22 | Left, |
| 23 | Right, |
| 24 | }; |
| 25 | |
| 26 | struct SnakeSegment { |
| 27 | Vector2 cell; |
| 28 | Color color; |
| 29 | }; |
| 30 | |
| 31 | struct Snake { |
| 32 | std::vector<SnakeSegment> body; |
| 33 | SnakeDir dir; |
| 34 | SnakeDir nextDir; |
| 35 | }; |
| 36 | |
| 37 | struct Food { |
| 38 | Vector2 cell; |
| 39 | Color color; |
| 40 | }; |
| 41 | |
| 42 | Snake snek = {}; |
| 43 | Food food = { 0 }; |
| 44 | |
| 45 | int prevGridX = 0; |
| 46 | int prevGridY = 0; |
| 47 | |
| 48 | void placeFood() { |
| 49 | food.cell = { |
| 50 | static_cast<float>(GetRandomValue(0, MAX_CELLX - 1)), |
| 51 | static_cast<float>(GetRandomValue(0, MAX_CELLY - 1)), |
| 52 | }; |
| 53 | |
| 54 | Color foodCol; |
| 55 | foodCol.r = GetRandomValue(100, 255); |
| 56 | foodCol.g = GetRandomValue(100, 255); |
| 57 | foodCol.b = GetRandomValue(100, 255); |
| 58 | foodCol.a = 255; |
| 59 | food.color = foodCol; |
| 60 | } |
| 61 | |
| 62 | bool selfCollide() { |
| 63 | const Vector2 &head = snek.body[0].cell; |
| 64 | int headX = static_cast<int>(head.x); |
| 65 | int headY = static_cast<int>(head.y); |
| 66 | |
| 67 | for (size_t i = 1; i < snek.body.size(); ++i) { |
| 68 | int bodyX = static_cast<int>(snek.body[i].cell.x); |
| 69 | int bodyY = static_cast<int>(snek.body[i].cell.y); |
| 70 | |
| 71 | if (headX == bodyX && headY == bodyY) return true; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool GameMain::Init() { |
| 78 | score = 0; |
| 79 | speed = 2.5f; |
| 80 | |
| 81 | snek.body.clear(); |
| 82 | snek.body.push_back({ { 10.f, 10.f }, RED }); |
| 83 | snek.body.push_back({ { 9.f, 10.f }, GREEN }); |
| 84 | snek.body.push_back({ { 8.f, 10.f }, BLUE }); |
| 85 | |
| 86 | snek.dir = Right; |
| 87 | snek.nextDir = snek.dir; |
| 88 | |
| 89 | prevGridX = 10; |
| 90 | prevGridY = 10; |
| 91 | |
| 92 | placeFood(); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | Vector2 oldPos; |
| 98 | bool GameMain::Update() { |
| 99 | float deltaTime = GetFrameTime() * speed; |
| 100 | if (deltaTime > 1.f / 5) deltaTime = 1.f / 5; |
| 101 | |
| 102 | if (snek.dir == Left || snek.dir == Right) { |
| 103 | if (IsKeyPressed(KEY_UP)) snek.nextDir = Up; |
| 104 | else if (IsKeyPressed(KEY_DOWN)) snek.nextDir = Down; |
| 105 | } else { |
| 106 | if (IsKeyPressed(KEY_LEFT)) snek.nextDir = Left; |
| 107 | else if (IsKeyPressed(KEY_RIGHT)) snek.nextDir = Right; |
| 108 | } |
| 109 | |
| 110 | if ((snek.nextDir == Left && snek.dir == Right) |
| 111 | || (snek.nextDir == Right && snek.dir == Left) |
| 112 | || (snek.nextDir == Up && snek.dir == Down) |
| 113 | || (snek.nextDir == Down && snek.dir == Up)) { |
| 114 | snek.nextDir = snek.dir; |
| 115 | } |
| 116 | |
| 117 | oldPos = snek.body[0].cell; |
| 118 | |
| 119 | if (snek.dir == Left) snek.body[0].cell.x -= 1 * deltaTime; |
| 120 | else if (snek.dir == Right) snek.body[0].cell.x += 1 * deltaTime; |
| 121 | else if (snek.dir == Up) snek.body[0].cell.y -= 1 * deltaTime; |
| 122 | else if (snek.dir == Down) snek.body[0].cell.y += 1 * deltaTime; |
| 123 | |
| 124 | int curGridX = static_cast<int>(snek.body[0].cell.x); |
| 125 | int curGridY = static_cast<int>(snek.body[0].cell.y); |
| 126 | |
| 127 | if ((curGridX != prevGridX) || (curGridY != prevGridY)) { |
| 128 | snek.dir = snek.nextDir; |
| 129 | for (size_t i = snek.body.size() - 1; i > 0; --i) |
| 130 | snek.body[i].cell = snek.body[i - 1].cell; |
| 131 | |
| 132 | snek.body[1].cell = oldPos; |
| 133 | |
| 134 | prevGridX = curGridX; |
| 135 | prevGridY = curGridY; |
| 136 | |
| 137 | if (selfCollide()) { |
| 138 | snake::PlaySFX(snake::Die, 1.f); |
| 139 | snake::Close(); |
| 140 | snake::s = snake::GameOver; |
| 141 | snake::Init(); |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | if ((curGridX == static_cast<int>(food.cell.x)) |
| 148 | && (curGridY == static_cast<int>(food.cell.y))) { |
| 149 | score += 100; |
| 150 | speed += .5f; |
| 151 | |
| 152 | SnakeSegment seg = snek.body.back(); |
| 153 | seg.color = food.color; |
| 154 | snek.body.push_back(seg); |
| 155 | snake::PlaySFX(snake::Food, 1.f); |
| 156 | placeFood(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | if ((snek.body[0].cell.x < 0 || snek.body[0].cell.x > MAX_CELLX) |
| 161 | || (snek.body[0].cell.y < 0 || snek.body[0].cell.y > MAX_CELLY)) { |
| 162 | snake::PlaySFX(snake::Die, 1.f); |
| 163 | snake::Close(); |
| 164 | snake::s = snake::GameOver; |
| 165 | snake::Init(); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | ClearBackground(BLACK); |
| 170 | |
| 171 | |
| 172 | int cX = static_cast<int>(gridPos.x); |
| 173 | int cY = static_cast<int>(gridPos.y); |
| 174 | for (int h = 0; h <= MAX_CELLY; ++h) { |
| 175 | int y = cY + h * CELL_SIZE; |
| 176 | DrawLine(cX, y, cX + MAX_CELLX * CELL_SIZE, y, RAYWHITE); |
| 177 | } |
| 178 | for (int w = 0; w <= MAX_CELLX; ++w) { |
| 179 | int x = cX + w * CELL_SIZE; |
| 180 | DrawLine(x, cY, x, cY + MAX_CELLY * CELL_SIZE, RAYWHITE); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | for (const auto &seg : snek.body) { |
| 185 | int pX = static_cast<int>(seg.cell.x) * CELL_SIZE; |
| 186 | int pY = static_cast<int>(seg.cell.y) * CELL_SIZE; |
| 187 | DrawRectangle(cX + pX, cY + pY, CELL_SIZE, CELL_SIZE, seg.color); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | int fX = static_cast<int>(food.cell.x) * CELL_SIZE; |
| 192 | int fY = static_cast<int>(food.cell.y) * CELL_SIZE; |
| 193 | DrawRectangle(cX + fX, cY + fY, CELL_SIZE, CELL_SIZE, food.color); |
| 194 | |
| 195 | |
| 196 | DrawText(TextFormat("SCORE: %d", score), 10, 10, 20, GREEN); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | void GameMain::Close() { |
| 202 | } |
| 203 | } |