multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 18:32fce82690a1, committed 2020-11-15
- Comitter:
- vsoltan
- Date:
- Sun Nov 15 21:55:35 2020 +0000
- Parent:
- 17:32ae1f106002
- Child:
- 19:58cc5465f647
- Commit message:
- added different types of server response and handling for each case
Changed in this revision
--- a/gamestate.cpp Sat Nov 14 02:37:13 2020 +0000 +++ b/gamestate.cpp Sun Nov 15 21:55:35 2020 +0000 @@ -1,11 +1,12 @@ -#include "gamestate.h" +#include "gamestate.h" GameState::GameState() { Coord playerOneLocation = {0, 0}; Coord playerTwoLocation = {0, 0}; Coord ballLocation = {0, 0}; - this->localPlayer = 0; + this->localPlayerNum = 0; + this->lobbyHash = ""; this->p1_loc = playerOneLocation; this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; @@ -24,20 +25,22 @@ return this->ball_loc; } -void GameState::update(MbedJSONValue *serverResponse) { - this->p1_loc.y = (*serverResponse)["playerOnePos"].get<int>(); - this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>(); - int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>(); - int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>(); - this->ball_loc.x = updated_ball_x; - this->ball_loc.y = updated_ball_y; - this->is_done = (bool)(*serverResponse)["isOver"].get<int>(); -} - -void GameState::print() { - //char to_string[256]; -// sprintf(to_string, "Player1 Pos: [%i, %i], Player 2 Pos: [%i, %i], Ball Pos: [%i, %i], isOver: %i", this->p1_loc->x, this->p1_loc->y, this->p2_loc->x, this->p2_loc->y, this->ball_loc->x, this->ball_loc->y, this->is_done); -// printf("Game State: %s\n\r", to_string); +void GameState::update(MbedJSONValue *serverResponse, Graphics *gfx) { + string typeResponse = (*serverResponse)["type"].get<std::string>(); + printf("typeResponse: %s\n\r", typeResponse.c_str()); + if (typeResponse == "connected") { + gfx->renderWaitingRoom(); + } else if (typeResponse == "gameState") { + this->p1_loc.y = (*serverResponse)["playerOnePos"].get<int>(); + this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>(); + int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>(); + int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>(); + this->ball_loc.x = updated_ball_x; + this->ball_loc.y = updated_ball_y; + this->is_done = (char)(*serverResponse)["isOver"].get<int>(); + this->localPlayerNum = (char)(*serverResponse)["player"].get<int>(); + this->lobbyHash = (*serverResponse)["hash"].get<std::string>(); + } } bool GameState::done() {
--- a/gamestate.h Sat Nov 14 02:37:13 2020 +0000 +++ b/gamestate.h Sun Nov 15 21:55:35 2020 +0000 @@ -5,27 +5,31 @@ #include "mbed.h" #include "stdint.h" #include "MbedJSONValue.h" +#include "graphics.h" +#include <string> struct Coord { int8_t x; int8_t y; }; +class Graphics; + class GameState { private: Coord p1_loc; Coord p2_loc; Coord ball_loc; - bool is_done; - bool localPlayer; + char is_done; + char localPlayerNum; + string lobbyHash; public: GameState(); Coord getPlayerOneLocation(); Coord getPlayerTwoLocation(); Coord getBallLocation(); - void update(MbedJSONValue *serverResponse); + void update(MbedJSONValue *serverResponse, Graphics *gfx); bool done(); - void print(); }; #endif // GAME_STATE_H \ No newline at end of file
--- a/graphics.cpp Sat Nov 14 02:37:13 2020 +0000 +++ b/graphics.cpp Sun Nov 15 21:55:35 2020 +0000 @@ -17,6 +17,12 @@ tft->printf("%s", "Press any button to continue."); } +void Graphics::renderWaitingRoom() { + tft->fillScreen(ST7735_GREEN); + tft->setTextCursor(20, 20); + tft->printf("%s", "Waiting For Player"); +} + void Graphics::renderBall(GameState *gs) { }
--- a/graphics.h Sat Nov 14 02:37:13 2020 +0000 +++ b/graphics.h Sun Nov 15 21:55:35 2020 +0000 @@ -12,12 +12,15 @@ #define P_RS p20 #define P_DC p19 +class GameState; + class Graphics { private: Adafruit_ST7735 *tft; public: Graphics(); void renderLaunchScreen(); + void renderWaitingRoom(); void renderGameState(GameState *gs); void renderBall(GameState *gs); void renderPlayers(GameState *gs);
--- a/main.cpp Sat Nov 14 02:37:13 2020 +0000 +++ b/main.cpp Sun Nov 15 21:55:35 2020 +0000 @@ -1,10 +1,11 @@ + #include "mbed.h" #include "EthernetInterface.h" #include "network.h" #include "gamestate.h" #include "graphics.h" #include "DebouncedInterrupt.h" -#include "MbedJSONValue.h" +#include "MbedJSONValue.h" #define DEBOUNCE 50 @@ -48,7 +49,8 @@ MbedJSONValue serverResponse; - char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; + char connectionRequest[] = "{\"type\": \"connected\"}"; + char toSend[] = "{\"type\": \"move\", \"delta\": 5}"; char readTo[256]; gfx->renderLaunchScreen(); @@ -57,8 +59,9 @@ if (menuPress) { GameState *gs = new GameState(); - gfx->renderGameState(gs); - + // request an open lobby from the server + sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1); + // change ISRs to game controls leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); @@ -75,7 +78,11 @@ if (bytesRead > 0) { printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); parse(serverResponse, readTo); - gs->update(&serverResponse); + + printf("type is: %s\n\r", serverResponse["type"].get<std::string>().c_str()); + + // TODO: check for connection ping back + gs->update(&serverResponse, gfx); } wait(.1); }