multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
gamestate.cpp@27:fcc5fee18a24, 2020-11-23 (annotated)
- Committer:
- vsoltan
- Date:
- Mon Nov 23 01:19:17 2020 +0000
- Revision:
- 27:fcc5fee18a24
- Parent:
- 25:4a965eccb922
- Child:
- 28:a26a43cdaea8
fixed game over logic
Who changed what in which revision?
User | Revision | Line number | New 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 | 27:fcc5fee18a24 | 59 | int is_done_value = (serverGameState)["isOver"].get<int>(); |
vsoltan | 27:fcc5fee18a24 | 60 | printf("isDone value %i\n", is_done_value); |
vsoltan | 21:992294ffabf4 | 61 | this->is_done = (char)(serverGameState)["isOver"].get<int>(); |
vsoltan | 27:fcc5fee18a24 | 62 | printf("game is done? %c\n\r", this->is_done); |
vsoltan | 21:992294ffabf4 | 63 | } |
vsoltan | 27:fcc5fee18a24 | 64 | if (serverGameState.hasMember("score")) { |
vsoltan | 27:fcc5fee18a24 | 65 | this->score[0] = (serverGameState)["score"][0].get<int>(); |
vsoltan | 27:fcc5fee18a24 | 66 | this->score[1] = (serverGameState)["score"][1].get<int>(); |
vsoltan | 27:fcc5fee18a24 | 67 | } |
vsoltan | 23:c38680c32552 | 68 | // TODO: check if hash has already been set |
vsoltan | 21:992294ffabf4 | 69 | if (serverGameState.hasMember("hash")) { |
vsoltan | 23:c38680c32552 | 70 | strcpy(this->lobbyHash, |
vsoltan | 23:c38680c32552 | 71 | (serverGameState)["hash"].get<std::string>().c_str()); |
vsoltan | 27:fcc5fee18a24 | 72 | this->lobbyHash[20] = 0; |
vsoltan | 22:1c49e1fae846 | 73 | } |
vsoltan | 20:b2687089661c | 74 | gfx->renderGameState(this); |
vsoltan | 18:32fce82690a1 | 75 | } |
vsoltan | 15:9d90f68e53da | 76 | } |
vsoltan | 15:9d90f68e53da | 77 | |
vsoltan | 19:58cc5465f647 | 78 | Coord GameState::getPlayerLocation(char player) { |
vsoltan | 19:58cc5465f647 | 79 | if (player == 0) { |
vsoltan | 19:58cc5465f647 | 80 | return getPlayerOneLocation(); |
vsoltan | 19:58cc5465f647 | 81 | } |
vsoltan | 19:58cc5465f647 | 82 | return getPlayerTwoLocation(); |
vsoltan | 19:58cc5465f647 | 83 | } |
vsoltan | 19:58cc5465f647 | 84 | |
vsoltan | 23:c38680c32552 | 85 | char *GameState::getLobbyHash() { |
vsoltan | 23:c38680c32552 | 86 | return this->lobbyHash; |
vsoltan | 23:c38680c32552 | 87 | } |
vsoltan | 23:c38680c32552 | 88 | |
vsoltan | 19:58cc5465f647 | 89 | char GameState::getLocalPlayerNum() { |
vsoltan | 19:58cc5465f647 | 90 | return this->localPlayerNum; |
vsoltan | 19:58cc5465f647 | 91 | } |
vsoltan | 19:58cc5465f647 | 92 | |
vsoltan | 27:fcc5fee18a24 | 93 | int GameState::getPlayerOneScore() { |
vsoltan | 27:fcc5fee18a24 | 94 | return this->score[0]; |
vsoltan | 27:fcc5fee18a24 | 95 | } |
vsoltan | 27:fcc5fee18a24 | 96 | |
vsoltan | 27:fcc5fee18a24 | 97 | int GameState::getPlayerTwoScore() { |
vsoltan | 27:fcc5fee18a24 | 98 | return this->score[1]; |
vsoltan | 27:fcc5fee18a24 | 99 | } |
vsoltan | 27:fcc5fee18a24 | 100 | |
vsoltan | 27:fcc5fee18a24 | 101 | char GameState::done() { |
vsoltan | 15:9d90f68e53da | 102 | return this->is_done; |
vsoltan | 15:9d90f68e53da | 103 | } |