multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

gamestate.cpp

Committer:
vsoltan
Date:
2020-11-15
Revision:
20:b2687089661c
Parent:
19:58cc5465f647
Child:
21:992294ffabf4

File content as of revision 20:b2687089661c:


#include "gamestate.h"  

GameState::GameState() {
    Coord playerOneLocation = {0, 0};
    Coord playerTwoLocation = {0, 0};
    Coord ballLocation = {0, 0};
    this->localPlayerNum = 0; 
    this->lobbyHash = ""; 
    this->p1_loc = playerOneLocation;
    this->p2_loc = playerTwoLocation; 
    this->ball_loc = ballLocation; 
    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>(); 
    printf("typeResponse: %s\n\r", typeResponse.c_str());
    if (typeResponse == "connected") {
        gfx->renderWaitingRoom(); 
    } else if (typeResponse == "gameState") {
        this->p1_loc.y = (*serverResponse)["gameState"]["playerOnePos"].get<int>(); 
        printf("new player1 location: %i\n\r", this->p1_loc.y); 
        this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>(); 
        int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>(); 
        int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>(); 
        this->ball_loc.x = updated_ball_x; 
        this->ball_loc.y = updated_ball_y;
        this->is_done = (char)(*serverResponse)["isOver"].get<int>();
        this->localPlayerNum = (char)(*serverResponse)["player"].get<int>();  
        this->lobbyHash = (*serverResponse)["hash"].get<std::string>(); 
        gfx->renderGameState(this); 
    }
}

Coord GameState::getPlayerLocation(char player) {
    if (player == 0) {
        return getPlayerOneLocation(); 
    }
    return getPlayerTwoLocation(); 
}

char GameState::getLocalPlayerNum() {
    return this->localPlayerNum; 
} 

bool GameState::done() {
    return this->is_done; 
}