multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
gamestate.cpp
- Committer:
- vsoltan
- Date:
- 2020-11-16
- Revision:
- 25:4a965eccb922
- Parent:
- 23:c38680c32552
- Child:
- 27:fcc5fee18a24
File content as of revision 25:4a965eccb922:
#include "gamestate.h" GameState::GameState() { Coord playerOneLocation = {0, 0}; Coord playerTwoLocation = {0, 0}; Coord ballLocation = {0, 0}; this->localPlayerNum = 0; this->p1_loc = playerOneLocation; this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; this->has_started = 0; this->is_done = false; } Coord GameState::getPlayerOneLocation() { return this->p1_loc; } Coord GameState::getPlayerTwoLocation() { return this->p2_loc; } Coord GameState::getBallLocation() { return this->ball_loc; } void GameState::update(MbedJSONValue *serverResponse, Graphics *gfx) { string typeResponse = (*serverResponse)["type"].get<std::string>(); if (typeResponse == "connected") { if (serverResponse->hasMember("player")) { this->localPlayerNum = (char)(*serverResponse)["player"].get<int>(); } gfx->renderWaitingRoom(); } else if (typeResponse == "gameState") { if (!has_started) { has_started = 1; printf("reseting display\n\r"); gfx->reset(); } gfx->eraseGameState(this); MbedJSONValue &serverGameState = (*serverResponse)["gameState"]; if (serverGameState.hasMember("playerOnePos")) { this->p1_loc.x = (serverGameState)["playerOnePos"].get<int>(); } if (serverGameState.hasMember("playerTwoPos")) { this->p2_loc.x = (serverGameState)["playerTwoPos"].get<int>(); } if (serverGameState.hasMember("ballPos")) { int8_t updated_ball_x = (int8_t)(serverGameState)["ballPos"][0].get<int>(); int8_t updated_ball_y = (int8_t)(serverGameState)["ballPos"][1].get<int>(); this->ball_loc.x = updated_ball_x; this->ball_loc.y = updated_ball_y; } if (serverGameState.hasMember("isOver")) { this->is_done = (char)(serverGameState)["isOver"].get<int>(); this->is_done = (char)(serverGameState)["isOver"].get<int>(); } // TODO: check if hash has already been set if (serverGameState.hasMember("hash")) { strcpy(this->lobbyHash, (serverGameState)["hash"].get<std::string>().c_str()); this->lobbyHash[21] = 0; } gfx->renderGameState(this); } } Coord GameState::getPlayerLocation(char player) { if (player == 0) { return getPlayerOneLocation(); } return getPlayerTwoLocation(); } char *GameState::getLobbyHash() { return this->lobbyHash; } char GameState::getLocalPlayerNum() { return this->localPlayerNum; } bool GameState::done() { return this->is_done; }