multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sun Nov 15 23:45:34 2020 +0000
Revision:
20:b2687089661c
Parent:
19:58cc5465f647
Child:
21:992294ffabf4
still need to change update gamestate to check for nested structure in JSON

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsoltan 17:32ae1f106002 1
vsoltan 18:32fce82690a1 2 #include "gamestate.h"
vsoltan 15:9d90f68e53da 3
vsoltan 15:9d90f68e53da 4 GameState::GameState() {
vsoltan 15:9d90f68e53da 5 Coord playerOneLocation = {0, 0};
vsoltan 15:9d90f68e53da 6 Coord playerTwoLocation = {0, 0};
vsoltan 15:9d90f68e53da 7 Coord ballLocation = {0, 0};
vsoltan 18:32fce82690a1 8 this->localPlayerNum = 0;
vsoltan 18:32fce82690a1 9 this->lobbyHash = "";
vsoltan 15:9d90f68e53da 10 this->p1_loc = playerOneLocation;
vsoltan 15:9d90f68e53da 11 this->p2_loc = playerTwoLocation;
vsoltan 15:9d90f68e53da 12 this->ball_loc = ballLocation;
vsoltan 15:9d90f68e53da 13 this->is_done = false;
vsoltan 15:9d90f68e53da 14 }
vsoltan 15:9d90f68e53da 15
vsoltan 15:9d90f68e53da 16 Coord GameState::getPlayerOneLocation() {
vsoltan 15:9d90f68e53da 17 return this->p1_loc;
vsoltan 15:9d90f68e53da 18 }
vsoltan 15:9d90f68e53da 19
vsoltan 15:9d90f68e53da 20 Coord GameState::getPlayerTwoLocation() {
vsoltan 15:9d90f68e53da 21 return this->p2_loc;
vsoltan 15:9d90f68e53da 22 }
vsoltan 15:9d90f68e53da 23
vsoltan 15:9d90f68e53da 24 Coord GameState::getBallLocation() {
vsoltan 15:9d90f68e53da 25 return this->ball_loc;
vsoltan 15:9d90f68e53da 26 }
vsoltan 15:9d90f68e53da 27
vsoltan 18:32fce82690a1 28 void GameState::update(MbedJSONValue *serverResponse, Graphics *gfx) {
vsoltan 18:32fce82690a1 29 string typeResponse = (*serverResponse)["type"].get<std::string>();
vsoltan 18:32fce82690a1 30 printf("typeResponse: %s\n\r", typeResponse.c_str());
vsoltan 18:32fce82690a1 31 if (typeResponse == "connected") {
vsoltan 18:32fce82690a1 32 gfx->renderWaitingRoom();
vsoltan 18:32fce82690a1 33 } else if (typeResponse == "gameState") {
vsoltan 20:b2687089661c 34 this->p1_loc.y = (*serverResponse)["gameState"]["playerOnePos"].get<int>();
vsoltan 20:b2687089661c 35 printf("new player1 location: %i\n\r", this->p1_loc.y);
vsoltan 18:32fce82690a1 36 this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>();
vsoltan 18:32fce82690a1 37 int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>();
vsoltan 18:32fce82690a1 38 int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>();
vsoltan 18:32fce82690a1 39 this->ball_loc.x = updated_ball_x;
vsoltan 18:32fce82690a1 40 this->ball_loc.y = updated_ball_y;
vsoltan 18:32fce82690a1 41 this->is_done = (char)(*serverResponse)["isOver"].get<int>();
vsoltan 20:b2687089661c 42 this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();
vsoltan 18:32fce82690a1 43 this->lobbyHash = (*serverResponse)["hash"].get<std::string>();
vsoltan 20:b2687089661c 44 gfx->renderGameState(this);
vsoltan 18:32fce82690a1 45 }
vsoltan 15:9d90f68e53da 46 }
vsoltan 15:9d90f68e53da 47
vsoltan 19:58cc5465f647 48 Coord GameState::getPlayerLocation(char player) {
vsoltan 19:58cc5465f647 49 if (player == 0) {
vsoltan 19:58cc5465f647 50 return getPlayerOneLocation();
vsoltan 19:58cc5465f647 51 }
vsoltan 19:58cc5465f647 52 return getPlayerTwoLocation();
vsoltan 19:58cc5465f647 53 }
vsoltan 19:58cc5465f647 54
vsoltan 19:58cc5465f647 55 char GameState::getLocalPlayerNum() {
vsoltan 19:58cc5465f647 56 return this->localPlayerNum;
vsoltan 19:58cc5465f647 57 }
vsoltan 19:58cc5465f647 58
vsoltan 15:9d90f68e53da 59 bool GameState::done() {
vsoltan 15:9d90f68e53da 60 return this->is_done;
vsoltan 15:9d90f68e53da 61 }