multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sat Nov 28 04:21:08 2020 +0000
Revision:
36:46bb54b669bc
Parent:
34:b19f39ad74ac
Child:
37:8a0fc62a0512
starting to refactor

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 29:4708bfb863cb 13 this->countdown = 0;
vsoltan 27:fcc5fee18a24 14 this->is_done = 0;
vsoltan 27:fcc5fee18a24 15 this->score[0] = 0;
vsoltan 27:fcc5fee18a24 16 this->score[1] = 0;
vsoltan 15:9d90f68e53da 17 }
vsoltan 15:9d90f68e53da 18
vsoltan 15:9d90f68e53da 19 Coord GameState::getPlayerOneLocation() {
vsoltan 15:9d90f68e53da 20 return this->p1_loc;
vsoltan 15:9d90f68e53da 21 }
vsoltan 15:9d90f68e53da 22
vsoltan 15:9d90f68e53da 23 Coord GameState::getPlayerTwoLocation() {
vsoltan 15:9d90f68e53da 24 return this->p2_loc;
vsoltan 15:9d90f68e53da 25 }
vsoltan 15:9d90f68e53da 26
vsoltan 15:9d90f68e53da 27 Coord GameState::getBallLocation() {
vsoltan 15:9d90f68e53da 28 return this->ball_loc;
vsoltan 15:9d90f68e53da 29 }
vsoltan 15:9d90f68e53da 30
vsoltan 27:fcc5fee18a24 31 void GameState::updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx) {
vsoltan 18:32fce82690a1 32 string typeResponse = (*serverResponse)["type"].get<std::string>();
vsoltan 32:7cbf4d9a82af 33 if (typeResponse == "connected") {
vsoltan 34:b19f39ad74ac 34 printf("connected!!!!!\n\r");
vsoltan 25:4a965eccb922 35 if (serverResponse->hasMember("player")) {
vsoltan 25:4a965eccb922 36 this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();
vsoltan 25:4a965eccb922 37 }
vsoltan 18:32fce82690a1 38 gfx->renderWaitingRoom();
vsoltan 18:32fce82690a1 39 } else if (typeResponse == "gameState") {
vsoltan 29:4708bfb863cb 40 MbedJSONValue &serverGameState = (*serverResponse)["gameState"];
vsoltan 29:4708bfb863cb 41 if (!has_started && serverGameState.hasMember("countdown")) {
vsoltan 30:59e9a5409e65 42 int countdownValue = (serverGameState)["countdown"].get<int>() / 1000;
vsoltan 29:4708bfb863cb 43 if (countdownValue > 0) {
vsoltan 30:59e9a5409e65 44 gfx->eraseCountdown(this);
vsoltan 30:59e9a5409e65 45 setCountdown(countdownValue);
vsoltan 30:59e9a5409e65 46 gfx->renderCountdown(this);
vsoltan 29:4708bfb863cb 47 return;
vsoltan 29:4708bfb863cb 48 } else {
vsoltan 29:4708bfb863cb 49 has_started = 1;
vsoltan 29:4708bfb863cb 50 printf("reseting display\n\r");
vsoltan 29:4708bfb863cb 51 gfx->reset();
vsoltan 29:4708bfb863cb 52 }
vsoltan 23:c38680c32552 53 }
vsoltan 23:c38680c32552 54 gfx->eraseGameState(this);
vsoltan 29:4708bfb863cb 55
vsoltan 21:992294ffabf4 56 if (serverGameState.hasMember("playerOnePos")) {
vsoltan 23:c38680c32552 57 this->p1_loc.x = (serverGameState)["playerOnePos"].get<int>();
vsoltan 21:992294ffabf4 58 }
vsoltan 21:992294ffabf4 59 if (serverGameState.hasMember("playerTwoPos")) {
vsoltan 23:c38680c32552 60 this->p2_loc.x = (serverGameState)["playerTwoPos"].get<int>();
vsoltan 21:992294ffabf4 61 }
vsoltan 21:992294ffabf4 62 if (serverGameState.hasMember("ballPos")) {
vsoltan 22:1c49e1fae846 63
vsoltan 22:1c49e1fae846 64 int8_t updated_ball_x = (int8_t)(serverGameState)["ballPos"][0].get<int>();
vsoltan 22:1c49e1fae846 65 int8_t updated_ball_y = (int8_t)(serverGameState)["ballPos"][1].get<int>();
vsoltan 21:992294ffabf4 66 this->ball_loc.x = updated_ball_x;
vsoltan 21:992294ffabf4 67 this->ball_loc.y = updated_ball_y;
vsoltan 21:992294ffabf4 68 }
vsoltan 21:992294ffabf4 69 if (serverGameState.hasMember("isOver")) {
vsoltan 21:992294ffabf4 70 this->is_done = (char)(serverGameState)["isOver"].get<int>();
vsoltan 21:992294ffabf4 71 }
vsoltan 27:fcc5fee18a24 72 if (serverGameState.hasMember("score")) {
vsoltan 27:fcc5fee18a24 73 this->score[0] = (serverGameState)["score"][0].get<int>();
vsoltan 27:fcc5fee18a24 74 this->score[1] = (serverGameState)["score"][1].get<int>();
vsoltan 27:fcc5fee18a24 75 }
vsoltan 23:c38680c32552 76 // TODO: check if hash has already been set
vsoltan 21:992294ffabf4 77 if (serverGameState.hasMember("hash")) {
vsoltan 23:c38680c32552 78 strcpy(this->lobbyHash,
vsoltan 23:c38680c32552 79 (serverGameState)["hash"].get<std::string>().c_str());
vsoltan 27:fcc5fee18a24 80 this->lobbyHash[20] = 0;
vsoltan 22:1c49e1fae846 81 }
vsoltan 20:b2687089661c 82 gfx->renderGameState(this);
vsoltan 18:32fce82690a1 83 }
vsoltan 15:9d90f68e53da 84 }
vsoltan 15:9d90f68e53da 85
vsoltan 19:58cc5465f647 86 Coord GameState::getPlayerLocation(char player) {
vsoltan 19:58cc5465f647 87 if (player == 0) {
vsoltan 19:58cc5465f647 88 return getPlayerOneLocation();
vsoltan 19:58cc5465f647 89 }
vsoltan 19:58cc5465f647 90 return getPlayerTwoLocation();
vsoltan 19:58cc5465f647 91 }
vsoltan 19:58cc5465f647 92
vsoltan 23:c38680c32552 93 char *GameState::getLobbyHash() {
vsoltan 23:c38680c32552 94 return this->lobbyHash;
vsoltan 23:c38680c32552 95 }
vsoltan 23:c38680c32552 96
vsoltan 19:58cc5465f647 97 char GameState::getLocalPlayerNum() {
vsoltan 19:58cc5465f647 98 return this->localPlayerNum;
vsoltan 30:59e9a5409e65 99 }
vsoltan 19:58cc5465f647 100
vsoltan 30:59e9a5409e65 101 int GameState::getCountdown() {
vsoltan 29:4708bfb863cb 102 return this->countdown;
vsoltan 29:4708bfb863cb 103 }
vsoltan 29:4708bfb863cb 104
vsoltan 29:4708bfb863cb 105 void GameState::setCountdown(int val) {
vsoltan 29:4708bfb863cb 106 this->countdown = val;
vsoltan 29:4708bfb863cb 107 }
vsoltan 29:4708bfb863cb 108
vsoltan 27:fcc5fee18a24 109 int GameState::getPlayerOneScore() {
vsoltan 27:fcc5fee18a24 110 return this->score[0];
vsoltan 27:fcc5fee18a24 111 }
vsoltan 27:fcc5fee18a24 112
vsoltan 27:fcc5fee18a24 113 int GameState::getPlayerTwoScore() {
vsoltan 27:fcc5fee18a24 114 return this->score[1];
vsoltan 27:fcc5fee18a24 115 }
vsoltan 27:fcc5fee18a24 116
vsoltan 36:46bb54b669bc 117 int GameState::getPlayerScore(int player) {
vsoltan 36:46bb54b669bc 118 if (player == 0) {
vsoltan 36:46bb54b669bc 119 return getPlayerOneScore();
vsoltan 36:46bb54b669bc 120 }
vsoltan 36:46bb54b669bc 121 return getPlayerTwoScore();
vsoltan 36:46bb54b669bc 122 }
vsoltan 36:46bb54b669bc 123
vsoltan 30:59e9a5409e65 124 char GameState::hasStarted() {
vsoltan 30:59e9a5409e65 125 return this->has_started;
vsoltan 30:59e9a5409e65 126 }
vsoltan 30:59e9a5409e65 127
vsoltan 27:fcc5fee18a24 128 char GameState::done() {
vsoltan 15:9d90f68e53da 129 return this->is_done;
vsoltan 15:9d90f68e53da 130 }