multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sun Nov 15 21:55:35 2020 +0000
Revision:
18:32fce82690a1
Parent:
17:32ae1f106002
Child:
19:58cc5465f647
added different types of server response and handling for each case

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 18:32fce82690a1 34 this->p1_loc.y = (*serverResponse)["playerOnePos"].get<int>();
vsoltan 18:32fce82690a1 35 this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>();
vsoltan 18:32fce82690a1 36 int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>();
vsoltan 18:32fce82690a1 37 int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>();
vsoltan 18:32fce82690a1 38 this->ball_loc.x = updated_ball_x;
vsoltan 18:32fce82690a1 39 this->ball_loc.y = updated_ball_y;
vsoltan 18:32fce82690a1 40 this->is_done = (char)(*serverResponse)["isOver"].get<int>();
vsoltan 18:32fce82690a1 41 this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();
vsoltan 18:32fce82690a1 42 this->lobbyHash = (*serverResponse)["hash"].get<std::string>();
vsoltan 18:32fce82690a1 43 }
vsoltan 15:9d90f68e53da 44 }
vsoltan 15:9d90f68e53da 45
vsoltan 15:9d90f68e53da 46 bool GameState::done() {
vsoltan 15:9d90f68e53da 47 return this->is_done;
vsoltan 15:9d90f68e53da 48 }