collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Joseph RyanFinishing touchesca230b6

master
2.9 KiB100 linesraw
1#include "../inc/game.h"
2#include "../inc/random.h"
3#include "../inc/tm4c123gh6pm.h"
4#include "../inc/ST7735.h"
5#include "../inc/graphics.h"
6#include "../inc/song.h"
7
8#define CUBE_SIZE 20
9#define SPREAD 500
10#define DIST 300
11#define GRACE_PERIOD 500
12
13int vel = 0;
14int difficulty = 1;
15int speed = 1;
16int score = 0;
17
18Cube cubes[NUM_CUBES];
19
20int bgColor;
21int cubeColor;
22int playerColor;
23bool redraw = false;
24int widthOffset = 0;
25int depthOffset = 0;
26
27void initField(){
28    for (int i = 0; i < NUM_CUBES; ++i) {
29        cubes[i] = Cube(Vec3(Random32() % SPREAD - SPREAD / 2,
30                             -50, GRACE_PERIOD + (DIST / NUM_CUBES) * i), CUBE_SIZE);
31    }
32    shuffleColor(Random() % 8);
33}
34
35void step(){
36    if (redraw) {
37        ST7735_FillRect(0, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, bgColor);
38        redraw = false;
39    }
40    for (int i = 0; i < NUM_CUBES; ++i) {
41        int tempColor = cubes[i].color;
42        cubes[i].color = bgColor;
43        cubes[i].draw();
44        cubes[i].color = tempColor;
45        cubes[i].pos.z -= difficulty + speed;
46        cubes[i].pos.x += vel * (1 + speed / 2 + difficulty / 2); // todo: shift and fixed point
47        if (cubes[i].pos.z < -10) {
48            if (cubes[i].pos.z < -20 - CUBE_SIZE) { // moved behind player
49                cubes[i].pos.z = DIST;
50                cubes[i].pos.x = Random32() % SPREAD - SPREAD / 2;
51                if ((score + difficulty) >> 9 > score >> 9) { // next level
52                    ++speed;
53                    shuffleColor();
54                }
55                printScore(score += difficulty);
56            }
57            // collision detection
58            else if (cubes[i].pos.x < 0 && cubes[i].pos.x + cubes[i].size > 0) {
59                cubes[i].draw();
60                //todo: display endgame graphic
61                Song_Play(meme_pitch, meme_dur, meme_speed, sizeof(meme_dur));
62                printScore(score);
63                while (true) ;
64            }
65        }
66        cubes[i].draw();
67    }
68    // widthOffset += vel * (1 + difficulty / 2);
69    // depthOffset -= difficulty;
70    // drawGround(widthOffset, depthOffset);
71    drawPlayer();
72}
73
74// size being power of 2 allows optimizing mod to bitwise and
75static const int palettes[][2] = {
76    { ST7735_BLACK, 1337 },
77    { ST7735_BLACK, ST7735_GREEN },
78    { ST7735_GREEN, ST7735_BLACK },
79    { ST7735_WHITE, ST7735_BLACK },
80    { ST7735_RED, ST7735_BLACK },
81    { ST7735_BLACK, ST7735_RED },
82    { ST7735_BLACK, 1337 },
83    { ST7735_WHITE, ST7735_BLUE },
84}; // todo: fix backwards colors
85
86void shuffleColor(int x){
87    static int palette = 0;
88    palette = x == -1 ? (palette + 1) % 8 : x;
89    bgColor = palettes[palette][0];
90    cubeColor = palettes[palette][1];
91    for (int i = 0; i < NUM_CUBES; ++i) {
92        if (cubeColor == 1337) { // rainbow
93            cubes[i].color = randomColor();
94        } else {
95            cubes[i].color = cubeColor;
96        }
97    }
98    playerColor = ~bgColor;
99    redraw = true;
100}