|
| 1 | +#include <Arduboy2.h> |
| 2 | + |
| 3 | +Arduboy2 arduboy; |
| 4 | + |
| 5 | +#include "poulet.h" |
| 6 | +#include "graphics.h" |
| 7 | +#include "tilemap.h" |
| 8 | +#include "level.h" |
| 9 | +#include "entity.h" |
| 10 | +#include "physics.h" |
| 11 | +#include "numbers.h" |
| 12 | + |
| 13 | + |
| 14 | +// Forward declaration of essential types: |
| 15 | +struct Entity; |
| 16 | +struct Game; |
| 17 | + |
| 18 | +Entity player; |
| 19 | +Game game; |
| 20 | + |
| 21 | +// later I'll init this as zero size |
| 22 | +Entity ents[4]; |
| 23 | + |
| 24 | + |
| 25 | +// Init player |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +void setup() { |
| 30 | + |
| 31 | + // Player setup |
| 32 | + player.walkSpeed = 1; |
| 33 | + player.walkAccel = 0.1; |
| 34 | + player.animSpeed = 4; |
| 35 | + |
| 36 | + game.mode = 1; |
| 37 | + |
| 38 | + // Arduboy housekeeping; |
| 39 | + arduboy.begin(); |
| 40 | + arduboy.setFrameRate(60); |
| 41 | + arduboy.clear(); |
| 42 | + |
| 43 | + // Begin screen mirroring serial output. |
| 44 | + Serial.begin(9600); |
| 45 | + |
| 46 | + gameInit(&player, &game); |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +void loop() { |
| 52 | + |
| 53 | + // Check is a frame has completed: |
| 54 | + if(!(arduboy.nextFrame())) return; |
| 55 | + arduboy.pollButtons(); |
| 56 | + arduboy.clear(); |
| 57 | + |
| 58 | + if(game.mode == 0){ |
| 59 | + play(&player, &game); |
| 60 | + } |
| 61 | + |
| 62 | + if(game.mode == 1){ |
| 63 | + title(&game); |
| 64 | + } |
| 65 | + |
| 66 | + // Write screen contents to serial port for screen mirroring: |
| 67 | + Serial.write(arduboy.getBuffer(), 128 * 64 / 8); |
| 68 | + |
| 69 | + arduboy.display(); |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void title(Game *g){ |
| 75 | + Sprites::drawOverwrite(0, -10, titlescreen, 0); |
| 76 | + arduboy.setCursor(40, 40); |
| 77 | + arduboy.print("Start"); |
| 78 | + arduboy.setCursor(40, 48); |
| 79 | + if(g->debug == false){ |
| 80 | + arduboy.print("Debug: Off"); |
| 81 | + } else { |
| 82 | + arduboy.print("Debug: On"); |
| 83 | + } |
| 84 | + arduboy.setCursor(40, 56); |
| 85 | + arduboy.print("Sound: Off"); |
| 86 | + |
| 87 | + |
| 88 | + if(arduboy.justPressed(B_BUTTON) && g->menuItem == 0){ |
| 89 | + g->mode = 0; |
| 90 | + } |
| 91 | + |
| 92 | + if(arduboy.justPressed(UP_BUTTON)) g->menuItem -= 1; |
| 93 | + if(arduboy.justPressed(DOWN_BUTTON)) g->menuItem += 1; |
| 94 | + if(g->menuItem > 2) g->menuItem = 0; |
| 95 | + if(g->menuItem < 0) g->menuItem = 2; |
| 96 | + |
| 97 | + arduboy.setCursor(30, (40 + (g->menuItem * 8))); |
| 98 | + arduboy.print(">"); |
| 99 | + |
| 100 | + |
| 101 | + if(arduboy.justPressed(LEFT_BUTTON) || arduboy.justPressed(RIGHT_BUTTON)){ |
| 102 | + if(g->menuItem == 1){ |
| 103 | + if(g->debug){ |
| 104 | + g->debug = false; |
| 105 | + } else { |
| 106 | + g->debug = true; |
| 107 | + } |
| 108 | + } |
| 109 | + // When sound is ready the switch shall get made here. |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +void play(Entity *p, Game *g){ |
| 115 | + |
| 116 | + if (arduboy.pressed(LEFT_BUTTON)){ |
| 117 | + p->ax -= p->walkAccel; |
| 118 | + } |
| 119 | + if (arduboy.pressed(RIGHT_BUTTON)){ |
| 120 | + p->ax += p->walkAccel; |
| 121 | + } |
| 122 | + if (arduboy.justPressed(B_BUTTON) && p->grounded){ |
| 123 | + p->ay = -pJumpPower; |
| 124 | + p->grounded = false; |
| 125 | + } |
| 126 | + if (arduboy.justPressed(A_BUTTON)){ |
| 127 | + // YOU WIN! |
| 128 | + g->score += 1000; |
| 129 | + } |
| 130 | + |
| 131 | + physicsUpdate(&player); |
| 132 | + camera(&player, &game); |
| 133 | + if(game.debug) debug(&player, &game); |
| 134 | + drawLevel(0, 0, &game); |
| 135 | + showHUD(&game); |
| 136 | + animation(p, g); |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +void gameInit(Entity *p, Game *g){ |
| 142 | + |
| 143 | + p->x = 20; |
| 144 | + p->y = 0; |
| 145 | + |
| 146 | + if(arduboy.pressed(A_BUTTON) && arduboy.pressed(UP_BUTTON)){ |
| 147 | + g->debug = true; |
| 148 | + } |
| 149 | + |
| 150 | + g->mode = 1; |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +void animation(Entity *p, Game *game){ |
| 156 | + |
| 157 | + p->ticker += abs(p->ax); |
| 158 | + if(p->ticker >= p->animSpeed){ |
| 159 | + p->frame++; |
| 160 | + p->ticker = 0; |
| 161 | + if(p->frame > 3){ |
| 162 | + p->frame = 0; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if(p->ax > 0) p->flip = 0; |
| 167 | + if(p->ax < 0) p->flip = 8; |
| 168 | + |
| 169 | + if(p->ax == 0) p->frame = 0; |
| 170 | + |
| 171 | + |
| 172 | + if(p->grounded == false){ |
| 173 | + if(p->ay <= 0) p->frame = 6; |
| 174 | + if(p->ay > 0) p->frame = 7; |
| 175 | + } |
| 176 | + |
| 177 | + Sprites::drawPlusMask(p->x - game->camerax, p->y, poulet, p->frame + p->flip); |
| 178 | + |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +void camera(Entity *p, Game *game){ |
| 183 | + |
| 184 | + if(p->x > (game->camerax + 70)){ |
| 185 | + game->camerax += 1; |
| 186 | + } |
| 187 | + if(p->x < (game->camerax + 58)){ |
| 188 | + game->camerax -= 1; |
| 189 | + } |
| 190 | + |
| 191 | + if(game->camerax < 0) game->camerax = 0; |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +void debug(Entity *p, Game *game){ |
| 197 | + arduboy.print("X: "); |
| 198 | + arduboy.print(p->x); |
| 199 | + arduboy.print(" Y: "); |
| 200 | + arduboy.print(p->y); |
| 201 | + arduboy.print("\nTX: "); |
| 202 | + arduboy.print(floor(p->x / 8)); |
| 203 | + arduboy.print(" TY: "); |
| 204 | + arduboy.print(floor(p->y / 8)); |
| 205 | + arduboy.print("\nAX: "); |
| 206 | + arduboy.print(p->ax); |
| 207 | + arduboy.print(" AY: "); |
| 208 | + arduboy.print(p->ay); |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +void showHUD(Game *g){ |
| 213 | + |
| 214 | + drawScore(g->score); |
| 215 | +} |
| 216 | + |
| 217 | + |
0 commit comments