multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Mon Nov 23 01:26:24 2020 +0000
Revision:
28:a26a43cdaea8
Parent:
27:fcc5fee18a24
Child:
29:4708bfb863cb
removed print statements

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 15:9d90f68e53da 9 this->p1_loc = playerOneLocation;
vsoltan 15:9d90f68e53da 10 this->p2_loc = playerTwoLocation;
vsoltan 15:9d90f68e53da 11 this->ball_loc = ballLocation;
vsoltan 25:4a965eccb922 12 this->has_started = 0;
vsoltan 27:fcc5fee18a24 13 this->is_done = 0;
vsoltan 27:fcc5fee18a24 14 this->score[0] = 0;
vsoltan 27:fcc5fee18a24 15 this->score[1] = 0;
vsoltan 15:9d90f68e53da 16 }
vsoltan 15:9d90f68e53da 17
vsoltan 15:9d90f68e53da 18 Coord GameState::getPlayerOneLocation() {
vsoltan 15:9d90f68e53da 19 return this->p1_loc;
vsoltan 15:9d90f68e53da 20 }
vsoltan 15:9d90f68e53da 21
vsoltan 15:9d90f68e53da 22 Coord GameState::getPlayerTwoLocation() {
vsoltan 15:9d90f68e53da 23 return this->p2_loc;
vsoltan 15:9d90f68e53da 24 }
vsoltan 15:9d90f68e53da 25
vsoltan 15:9d90f68e53da 26 Coord GameState::getBallLocation() {
vsoltan 15:9d90f68e53da 27 return this->ball_loc;
vsoltan 15:9d90f68e53da 28 }
vsoltan 15:9d90f68e53da 29
vsoltan 27:fcc5fee18a24 30 void GameState::updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx) {
vsoltan 18:32fce82690a1 31 string typeResponse = (*serverResponse)["type"].get<std::string>();
vsoltan 18:32fce82690a1 32 if (typeResponse == "connected") {
vsoltan 25:4a965eccb922 33 if (serverResponse->hasMember("player")) {
vsoltan 25:4a965eccb922 34 this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();
vsoltan 25:4a965eccb922 35 }
vsoltan 18:32fce82690a1 36 gfx->renderWaitingRoom();
vsoltan 18:32fce82690a1 37 } else if (typeResponse == "gameState") {
vsoltan 23:c38680c32552 38 if (!has_started) {
vsoltan 23:c38680c32552 39 has_started = 1;
vsoltan 25:4a965eccb922 40 printf("reseting display\n\r");
vsoltan 23:c38680c32552 41 gfx->reset();
vsoltan 23:c38680c32552 42 }
vsoltan 23:c38680c32552 43 gfx->eraseGameState(this);
vsoltan 21:992294ffabf4 44 MbedJSONValue &serverGameState = (*serverResponse)["gameState"];
vsoltan 21:992294ffabf4 45 if (serverGameState.hasMember("playerOnePos")) {
vsoltan 23:c38680c32552 46 this->p1_loc.x = (serverGameState)["playerOnePos"].get<int>();
vsoltan 21:992294ffabf4 47 }
vsoltan 21:992294ffabf4 48 if (serverGameState.hasMember("playerTwoPos")) {
vsoltan 23:c38680c32552 49 this->p2_loc.x = (serverGameState)["playerTwoPos"].get<int>();
vsoltan 21:992294ffabf4 50 }
vsoltan 21:992294ffabf4 51 if (serverGameState.hasMember("ballPos")) {
vsoltan 22:1c49e1fae846 52
vsoltan 22:1c49e1fae846 53 int8_t updated_ball_x = (int8_t)(serverGameState)["ballPos"][0].get<int>();
vsoltan 22:1c49e1fae846 54 int8_t updated_ball_y = (int8_t)(serverGameState)["ballPos"][1].get<int>();
vsoltan 21:992294ffabf4 55 this->ball_loc.x = updated_ball_x;
vsoltan 21:992294ffabf4 56 this->ball_loc.y = updated_ball_y;
vsoltan 21:992294ffabf4 57 }
vsoltan 21:992294ffabf4 58 if (serverGameState.hasMember("isOver")) {
vsoltan 21:992294ffabf4 59 this->is_done = (char)(serverGameState)["isOver"].get<int>();
vsoltan 21:992294ffabf4 60 }
vsoltan 27:fcc5fee18a24 61 if (serverGameState.hasMember("score")) {
vsoltan 27:fcc5fee18a24 62 this->score[0] = (serverGameState)["score"][0].get<int>();
vsoltan 27:fcc5fee18a24 63 this->score[1] = (serverGameState)["score"][1].get<int>();
vsoltan 27:fcc5fee18a24 64 }
vsoltan 23:c38680c32552 65 // TODO: check if hash has already been set
vsoltan 21:992294ffabf4 66 if (serverGameState.hasMember("hash")) {
vsoltan 23:c38680c32552 67 strcpy(this->lobbyHash,
vsoltan 23:c38680c32552 68 (serverGameState)["hash"].get<std::string>().c_str());
vsoltan 27:fcc5fee18a24 69 this->lobbyHash[20] = 0;
vsoltan 22:1c49e1fae846 70 }
vsoltan 20:b2687089661c 71 gfx->renderGameState(this);
vsoltan 18:32fce82690a1 72 }
vsoltan 15:9d90f68e53da 73 }
vsoltan 15:9d90f68e53da 74
vsoltan 19:58cc5465f647 75 Coord GameState::getPlayerLocation(char player) {
vsoltan 19:58cc5465f647 76 if (player == 0) {
vsoltan 19:58cc5465f647 77 return getPlayerOneLocation();
vsoltan 19:58cc5465f647 78 }
vsoltan 19:58cc5465f647 79 return getPlayerTwoLocation();
vsoltan 19:58cc5465f647 80 }
vsoltan 19:58cc5465f647 81
vsoltan 23:c38680c32552 82 char *GameState::getLobbyHash() {
vsoltan 23:c38680c32552 83 return this->lobbyHash;
vsoltan 23:c38680c32552 84 }
vsoltan 23:c38680c32552 85
vsoltan 19:58cc5465f647 86 char GameState::getLocalPlayerNum() {
vsoltan 19:58cc5465f647 87 return this->localPlayerNum;
vsoltan 19:58cc5465f647 88 }
vsoltan 19:58cc5465f647 89
vsoltan 27:fcc5fee18a24 90 int GameState::getPlayerOneScore() {
vsoltan 27:fcc5fee18a24 91 return this->score[0];
vsoltan 27:fcc5fee18a24 92 }
vsoltan 27:fcc5fee18a24 93
vsoltan 27:fcc5fee18a24 94 int GameState::getPlayerTwoScore() {
vsoltan 27:fcc5fee18a24 95 return this->score[1];
vsoltan 27:fcc5fee18a24 96 }
vsoltan 27:fcc5fee18a24 97
vsoltan 27:fcc5fee18a24 98 char GameState::done() {
vsoltan 15:9d90f68e53da 99 return this->is_done;
vsoltan 15:9d90f68e53da 100 }