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