|
@@ -0,0 +1,229 @@
|
|
|
|
|
+#include "GameState.h"
|
|
|
|
|
+#include "piaf/Archive.h"
|
|
|
|
|
+#include "input/Input.h"
|
|
|
|
|
+#include "render/Text.h"
|
|
|
|
|
+#include "Graphics.h"
|
|
|
|
|
+#include "Logger.h"
|
|
|
|
|
+
|
|
|
|
|
+#include "Player.h"
|
|
|
|
|
+#include "static_data.h"
|
|
|
|
|
+
|
|
|
|
|
+using Pacman::GameState;
|
|
|
|
|
+using Pacman::Player;
|
|
|
|
|
+using Pacman::ButtonInput;
|
|
|
|
|
+using Pacman::MoveIntent;
|
|
|
|
|
+using WalrusRPG::PIAF::Archive;
|
|
|
|
|
+using WalrusRPG::PIAF::File;
|
|
|
|
|
+using WalrusRPG::Utils::Rect;
|
|
|
|
|
+using namespace WalrusRPG;
|
|
|
|
|
+using namespace WalrusRPG::Graphics;
|
|
|
|
|
+using namespace WalrusRPG::Input;
|
|
|
|
|
+using WalrusRPG::Input::Key;
|
|
|
|
|
+
|
|
|
|
|
+uint16_t map_data[] = {
|
|
|
|
|
+ 5,3,3,3,3,3,3,3,3,3,3,3,3,13,14,3,3,3,3,3,3,3,3,3,3,3,3,6,
|
|
|
|
|
+1,0,0,0,0,0,0,0,0,0,0,0,0,18,17,0,0,0,0,0,0,0,0,0,0,0,0,2,
|
|
|
|
|
+1,0,9,15,15,10,0,9,15,15,15,10,0,18,17,0,9,15,15,15,10,0,9,15,15,10,0,2,
|
|
|
|
|
+1,0,18,0,0,17,0,18,0,0,0,17,0,18,17,0,18,0,0,0,17,0,18,0,0,17,0,2,
|
|
|
|
|
+1,0,11,16,16,12,0,11,16,16,16,12,0,11,12,0,11,16,16,16,12,0,11,16,16,12,0,2,
|
|
|
|
|
+1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
|
|
|
|
|
+1,0,9,15,15,10,0,9,10,0,9,15,15,15,15,15,15,10,0,9,10,0,9,15,15,10,0,2,
|
|
|
|
|
+1,0,11,16,16,12,0,18,17,0,11,16,16,24,23,16,16,12,0,18,17,0,11,16,16,12,0,2,
|
|
|
|
|
+1,0,0,0,0,0,0,18,17,0,0,0,0,18,17,0,0,0,0,18,17,0,0,0,0,0,0,2,
|
|
|
|
|
+7,4,4,4,4,10,0,18,25,15,15,10,0,18,17,0,9,15,15,26,17,0,9,4,4,4,4,8,
|
|
|
|
|
+0,0,0,0,0,1,0,18,23,16,16,12,0,11,12,0,11,16,16,24,17,0,2,0,0,0,0,0,
|
|
|
|
|
+0,0,0,0,0,1,0,18,17,0,0,0,0,0,0,0,0,0,0,18,17,0,2,0,0,0,0,0,
|
|
|
|
|
+0,0,0,0,0,1,0,18,17,0,19,4,4,31,31,4,4,20,0,18,17,0,2,0,0,0,0,0,
|
|
|
|
|
+3,3,3,3,3,12,0,11,12,0,2,0,0,0,0,0,0,1,0,11,12,0,11,3,3,3,3,3,
|
|
|
|
|
+0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
|
+4,4,4,4,4,10,0,9,10,0,2,0,0,0,0,0,0,1,0,9,10,0,9,4,4,4,4,4,
|
|
|
|
|
+0,0,0,0,0,1,0,18,17,0,21,3,3,3,3,3,3,22,0,18,17,0,2,0,0,0,0,0,
|
|
|
|
|
+0,0,0,0,0,1,0,18,17,0,0,0,0,0,0,0,0,0,0,18,17,0,2,0,0,0,0,0,
|
|
|
|
|
+0,0,0,0,0,1,0,18,17,0,9,15,15,15,15,15,15,10,0,18,17,0,2,0,0,0,0,0,
|
|
|
|
|
+5,3,3,3,3,12,0,11,12,0,11,16,16,24,23,16,16,12,0,11,12,0,11,3,3,3,3,6,
|
|
|
|
|
+1,0,0,0,0,0,0,0,0,0,0,0,0,18,17,0,0,0,0,0,0,0,0,0,0,0,0,2,
|
|
|
|
|
+1,0,9,15,15,10,0,9,15,15,15,10,0,18,17,0,9,15,15,15,10,0,9,15,15,10,0,2,
|
|
|
|
|
+1,0,11,16,24,17,0,11,16,16,16,12,0,11,12,0,11,16,16,16,12,0,18,23,16,12,0,2,
|
|
|
|
|
+1,0,0,0,18,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,17,0,0,0,2,
|
|
|
|
|
+27,15,10,0,18,17,0,9,10,0,9,15,15,15,15,15,15,10,0,9,10,0,18,17,0,9,15,29,
|
|
|
|
|
+28,16,12,0,11,12,0,18,17,0,11,16,16,24,23,16,16,12,0,18,17,0,11,12,0,11,16,30,
|
|
|
|
|
+1,0,0,0,0,0,0,18,17,0,0,0,0,18,17,0,0,0,0,18,17,0,0,0,0,0,0,2,
|
|
|
|
|
+1,0,9,15,15,15,15,26,25,15,15,10,0,18,17,0,9,15,15,26,25,15,15,15,15,10,0,2,
|
|
|
|
|
+1,0,11,16,16,16,16,16,16,16,16,12,0,11,12,0,11,16,16,16,16,16,16,16,16,12,0,2,
|
|
|
|
|
+1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
|
|
|
|
|
+7,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const uint8_t gums_model[] = {
|
|
|
|
|
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
|
+0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,
|
|
|
|
|
+0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,
|
|
|
|
|
+0,2,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,2,0,
|
|
|
|
|
+0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,
|
|
|
|
|
+0,1,1,1,1,1,1,1,1,1,1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,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,
|
|
|
|
|
+0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,
|
|
|
|
|
+0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,
|
|
|
|
|
+0,2,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,2,0,
|
|
|
|
|
+0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,
|
|
|
|
|
+0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,
|
|
|
|
|
+0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,
|
|
|
|
|
+0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,
|
|
|
|
|
+0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,
|
|
|
|
|
+0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
|
|
|
|
|
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+GameState::GameState() : level(1), map(28, 31, map_data, nullptr, map_texture), cam(-(320-28*8)/2,4),
|
|
|
|
|
+ p1(4,4), p2(204,228)
|
|
|
|
|
+
|
|
|
|
|
+{
|
|
|
|
|
+ p1.up = new ButtonInput(K_A);
|
|
|
|
|
+ p1.down = new ButtonInput(K_B);
|
|
|
|
|
+ p1.left = new ButtonInput(K_L);
|
|
|
|
|
+ p1.right = new ButtonInput(K_R);
|
|
|
|
|
+
|
|
|
|
|
+ p2.up = new ButtonInput(K_UP);
|
|
|
|
|
+ p2.down = new ButtonInput(K_DOWN);
|
|
|
|
|
+ p2.left = new ButtonInput(K_LEFT);
|
|
|
|
|
+ p2.right = new ButtonInput(K_RIGHT);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ set_gums();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+GameState::~GameState()
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void GameState::update(unsigned dt)
|
|
|
|
|
+{
|
|
|
|
|
+ map.update(dt);
|
|
|
|
|
+ p1.update(dt);
|
|
|
|
|
+ p2.update(dt);
|
|
|
|
|
+ move_player(p1);
|
|
|
|
|
+ move_player(p2);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void GameState::render(unsigned dt)
|
|
|
|
|
+{
|
|
|
|
|
+ // Map
|
|
|
|
|
+ map.render(cam, dt);
|
|
|
|
|
+ // Gums
|
|
|
|
|
+ for (int y = 0; y < 31; ++y)
|
|
|
|
|
+ {
|
|
|
|
|
+ for (int x = 0; x < 28; ++x)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(gums[y][x] == Pacgum::NORMAL)
|
|
|
|
|
+ put_rectangle({8*x - cam.get_x()+3, 8*y - cam.get_y()+3,2,2}, White);
|
|
|
|
|
+ if(gums[y][x] == Pacgum::SUPER)
|
|
|
|
|
+ put_rectangle({8*x - cam.get_x()+1, 8*y - cam.get_y()+1,6,6}, Yellow);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // Enemy Door
|
|
|
|
|
+ put_rectangle({104-cam.get_x(), 101-cam.get_y(), 16, 2}, Red);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // Player Character
|
|
|
|
|
+ // render_player(j1);
|
|
|
|
|
+ // render_player(j2);
|
|
|
|
|
+
|
|
|
|
|
+ p1.render(dt, cam);
|
|
|
|
|
+ p2.render(dt, cam);
|
|
|
|
|
+ // Borders
|
|
|
|
|
+
|
|
|
|
|
+ put_rectangle({0, 0, 0u-cam.get_x(), 320}, DarkGray);
|
|
|
|
|
+ put_rectangle({320+cam.get_x(), 0, 0u-cam.get_x(), 320}, DarkGray);
|
|
|
|
|
+ put_rectangle({-cam.get_x(),0, 1, 240}, White);
|
|
|
|
|
+ put_rectangle({320+cam.get_x(),0, 1, 240}, White);
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void GameState::set_gums()
|
|
|
|
|
+{
|
|
|
|
|
+ for (int y = 0; y < 31; ++y)
|
|
|
|
|
+ {
|
|
|
|
|
+ for (int x = 0; x < 28; ++x)
|
|
|
|
|
+ {
|
|
|
|
|
+ gums[y][x] = (Pacgum)gums_model[28*y + x];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Does a corner-based collision check for a small Rect.
|
|
|
|
|
+ * Assumes that said Rect as big or smaller than a tile
|
|
|
|
|
+ * (8 x 8 pixels).
|
|
|
|
|
+ * Also assumes that a corner outside the map doesn't collide anything.
|
|
|
|
|
+ * Assumes that you lost the game and any tile != 0 is solid.
|
|
|
|
|
+ */
|
|
|
|
|
+bool GameState::does_collide_a_wall(Rect r)
|
|
|
|
|
+{
|
|
|
|
|
+ struct Corner{int x; int y;};
|
|
|
|
|
+
|
|
|
|
|
+ Corner top_left{r.x, r.y};
|
|
|
|
|
+ Corner top_right{(r.x+(int)r.width-1), r.y};
|
|
|
|
|
+ Corner bottom_left{r.x, (r.y+(int)r.height-1)};
|
|
|
|
|
+ Corner bottom_right{(r.x+(int)r.width-1), (r.y+(int)r.height-1)};
|
|
|
|
|
+ return (is_inside_map(top_left.x, top_left.y) && (map.is_tile_solid(top_left.x/8, top_left.y/8) != 0))
|
|
|
|
|
+ || (is_inside_map(top_right.x, top_right.y) && (map.is_tile_solid(top_right.x/8, top_right.y/8) != 0))
|
|
|
|
|
+ || (is_inside_map(bottom_right.x, bottom_right.y) && (map.is_tile_solid(bottom_right.x/8, bottom_right.y/8) != 0))
|
|
|
|
|
+ || (is_inside_map(bottom_left.x, bottom_left.y) && (map.is_tile_solid(bottom_left.x/8, bottom_left.y/8) != 0));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void GameState::move_player(Player &p)
|
|
|
|
|
+{
|
|
|
|
|
+ if(p.intent == UP) {
|
|
|
|
|
+ if(!does_collide_a_wall({p.x+4, p.y+4-1, 8, 8})){
|
|
|
|
|
+ p.vy = -1;
|
|
|
|
|
+ p.vx = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(p.intent == DOWN) {
|
|
|
|
|
+ if(!does_collide_a_wall({p.x+4, p.y+4+1, 8, 8})){
|
|
|
|
|
+ p.vy = 1;
|
|
|
|
|
+ p.vx = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(p.intent == LEFT) {
|
|
|
|
|
+ if(!does_collide_a_wall({p.x+4-1, p.y+4, 8, 8})){
|
|
|
|
|
+ p.vx = -1;
|
|
|
|
|
+ p.vy = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(p.intent == RIGHT) {
|
|
|
|
|
+ if(!does_collide_a_wall({p.x+4+1, p.y+4, 8, 8})){
|
|
|
|
|
+ p.vx = 1;
|
|
|
|
|
+ p.vy = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(does_collide_a_wall({p.x+4+p.vx, p.y+4+p.vy, 8, 8})){
|
|
|
|
|
+ p.vy = 0;
|
|
|
|
|
+ p.vx = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ p.x += p.vx;
|
|
|
|
|
+ p.y += p.vy;
|
|
|
|
|
+
|
|
|
|
|
+ if(p.x < -8)
|
|
|
|
|
+ p.x = 224-8;
|
|
|
|
|
+ if(p.x+16 > 232)
|
|
|
|
|
+ p.x = 0;
|
|
|
|
|
+}
|