multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Mon Nov 16 03:01:50 2020 +0000
Revision:
23:c38680c32552
Parent:
22:1c49e1fae846
Child:
25:4a965eccb922
moving paddles work in progress

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