Skip to content
Snippets Groups Projects
Commit c0f36fe9 authored by HoelShare's avatar HoelShare
Browse files

GPN

parent 1031a16d
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -42,7 +42,7 @@ GameServer *server; ...@@ -42,7 +42,7 @@ GameServer *server;
GameClient *client; GameClient *client;
char pixIndex = 0; char pixIndex = 0;
bool changePixels = true; bool changePixels = true;
unsigned long lastChange = 0; unsigned long lastChange = 0;
unsigned long lastNotificationPull = 0; unsigned long lastNotificationPull = 0;
void setup() { void setup() {
...@@ -76,6 +76,7 @@ void hostGame() { ...@@ -76,6 +76,7 @@ void hostGame() {
#endif #endif
client = new GameClient(ui, &badge, player); client = new GameClient(ui, &badge, player);
server->setGamestartCallback([=]() { client->startGame(); });
#ifdef DEBUG #ifdef DEBUG
Serial.printf("Host - Ende\n"); Serial.printf("Host - Ende\n");
#endif #endif
...@@ -165,6 +166,7 @@ void joinGame() { ...@@ -165,6 +166,7 @@ void joinGame() {
} }
void loop() { void loop() {
Serial.println("Loop");
ui->dispatchInput(badge.getJoystickState()); ui->dispatchInput(badge.getJoystickState());
ui->draw(); ui->draw();
...@@ -185,11 +187,14 @@ void loop() { ...@@ -185,11 +187,14 @@ void loop() {
pixels.show(); pixels.show();
if (server) { if (server) {
Serial.println("Loop - Server");
server->update(); server->update();
Serial.println("Loop - Server - Ende");
} }
if (client) { if (client) {
// client->update(); // client->update();
} }
Serial.println("Loop - Ende");
} }
char *loadNick() { char *loadNick() {
......
...@@ -6,18 +6,21 @@ ...@@ -6,18 +6,21 @@
void GameClient::createUIGameStart() { void GameClient::createUIGameStart() {
if (this->playerList) { if (this->playerList) {
delete this->playerList; this->playerList->Clear();
Serial.println("GameClient::createUIGameStart - Clear");
} else {
this->playerList = new PlayerListMenu(9);
} }
playerList = new PlayerListMenu(9); Serial.println("GameClient::createUIGameStart - new PlayerListMenu");
playerList->setLeftAction([=]() { this->player->prevWeapon(); }); playerList->setLeftAction([=]() { this->player->prevWeapon(); });
playerList->setRightAction([=]() { this->player->nextWeapon(); }); playerList->setRightAction([=]() { this->player->nextWeapon(); });
playerList->setDownAction([=]() { this->player->reload(); }); playerList->setDownAction([=]() { this->player->reload(); });
playerList->setEnterAction([=]() { this->player->shot(); }); playerList->setEnterAction([=]() { this->player->shot(); });
Serial.println("GameClient::createUIGameStart - set Lambdas");
LaserMenuItem *healthMI = new LaserMenuItem([]() {}, "/heart.bmp"); LaserMenuItem *healthMI = new LaserMenuItem([]() {}, "/heart.bmp");
LaserMenuItem *armorMI = new LaserMenuItem([]() {}, "/armor.bmp"); LaserMenuItem *armorMI = new LaserMenuItem([]() {}, "/armor.bmp");
LaserMenuItem *ammoMI = new LaserMenuItem([]() {}, "/ammo.bmp"); LaserMenuItem *ammoMI = new LaserMenuItem([]() {}, "/ammo1.bmp");
LaserMenuItem *nameMI = new LaserMenuItem([]() {}, nullptr); LaserMenuItem *nameMI = new LaserMenuItem([]() {}, nullptr);
nameMI->setText(player->getNickname());
player->setHealthMenuItem(healthMI); player->setHealthMenuItem(healthMI);
player->setArmorMenuItem(armorMI); player->setArmorMenuItem(armorMI);
player->setAmmoMenuItem(ammoMI); player->setAmmoMenuItem(ammoMI);
...@@ -25,15 +28,19 @@ void GameClient::createUIGameStart() { ...@@ -25,15 +28,19 @@ void GameClient::createUIGameStart() {
playerList->addMenuItem(armorMI); playerList->addMenuItem(armorMI);
playerList->addMenuItem(ammoMI); playerList->addMenuItem(ammoMI);
playerList->addMenuItem(nameMI); playerList->addMenuItem(nameMI);
ui->open(playerList); Serial.println("GameClient::createUIGameStart - added Lambdas");
nameMI->setText(player->getNickname());
Serial.println("GameClient::createUIGameStart - set Nickname");
playerList->setDirty();
reopen = true;
Serial.println("GameClient::createUIGameStart - Ende");
} }
void GameClient::startGame() { void GameClient::startGame() {
#ifdef DEBUG Serial.println("GameClient::startGame()");
Serial.printf("Start Game!");
#endif
this->createUIGameStart(); this->createUIGameStart();
Serial.println("GameClient::startGame() - Ende");
} }
...@@ -53,17 +60,22 @@ void GameClient::joinGame(String ip) { ...@@ -53,17 +60,22 @@ void GameClient::joinGame(String ip) {
void GameClient::update() { void GameClient::update() {
Serial.println("GameClient - Update");
String lastHitBy; if(reopen) {
int lastDamage; reopen = false;
ui->open(playerList);
if (wasHit(&lastHitBy, &lastDamage)){
player->hit(lastDamage);
tft.setCursor(20, 20);
tft.print("Hit by " + lastHitBy);
} }
// String lastHitBy;
// int lastDamage;
//
// if (wasHit(&lastHitBy, &lastDamage)){
// player->hit(lastDamage);
//
// tft.setCursor(20, 20);
// tft.print("Hit by " + lastHitBy);
// }
Serial.println("GameClient - Update - Ende");
} }
bool GameClient::wasHit(String* nick, int* damage, bool enable) { bool GameClient::wasHit(String* nick, int* damage, bool enable) {
......
...@@ -33,7 +33,7 @@ private: ...@@ -33,7 +33,7 @@ private:
char *host = "127.0.0.1"; // MaxArraySize char *host = "127.0.0.1"; // MaxArraySize
PlayerListMenu *playerList = new PlayerListMenu(9); PlayerListMenu *playerList = new PlayerListMenu(9);
WiFiClient *client; // the client connected on client side WiFiClient *client; // the client connected on client side
bool reopen = false;
}; };
#endif //GPN_LASERTAG_GAMECLIENT_HPP #endif //GPN_LASERTAG_GAMECLIENT_HPP
...@@ -22,9 +22,15 @@ void GameServer::secureQuestion() { ...@@ -22,9 +22,15 @@ void GameServer::secureQuestion() {
Serial.printf("Sicherheitsabfrage!"); Serial.printf("Sicherheitsabfrage!");
#endif #endif
secureMenu = new PlayerListMenu(4); secureMenu = new PlayerListMenu(4);
secureMenu->addMenuItem(new MenuItem("Start?", [=]() { this->startGame(); })); secureMenu->addMenuItem(new MenuItem("Start?", [=]() { this->startGame(); Serial.println("Lambda Start - Ende"); }));
secureMenu->addMenuItem(new MenuItem("Cancel?", [=]() { ui->closeCurrent(); })); secureMenu->addMenuItem(new MenuItem("Cancel?", [=]() {
secureMenu->setRightAction([=]() { ui->closeCurrent(); }); ui->open(playerList);
ui->draw();
}));
secureMenu->setRightAction([=]() {
ui->open(playerList);
ui->draw();
});
secureMenu->setLeftAction([=]() { this->startGame(); }); secureMenu->setLeftAction([=]() { this->startGame(); });
ui->open(secureMenu); ui->open(secureMenu);
} }
...@@ -46,13 +52,14 @@ void GameServer::update() { ...@@ -46,13 +52,14 @@ void GameServer::update() {
handleWelcome(&(serverClients[numConnections]), numConnections); handleWelcome(&(serverClients[numConnections]), numConnections);
} }
} else { } else {
for (short i = 0; i <= numConnections; i++) { //iterate throu the slots Serial.println("Waiting for packets");
for (short i = 0; i < numConnections; i++) { //iterate throu the slots
if (serverClients[i].connected() && serverClients[i].available()) { //skip unconnected slots if (serverClients[i].connected() && serverClients[i].available()) { //skip unconnected slots
String message = serverClients[i].readStringUntil('\n'); String message = serverClients[i].readStringUntil('\n');
UrlDecode decode(message.c_str()); UrlDecode decode(message.c_str());
String strTyp = String(decode.getKey("typ")); String strTyp = String(decode.getKey("typ"));
if (strTyp == "hit") { if (strTyp == "hit") {
HitPacket* packet = HitPacket::read(message); HitPacket *packet = HitPacket::read(message);
sendHit(packet); sendHit(packet);
delete packet; delete packet;
} }
...@@ -72,6 +79,7 @@ void GameServer::handleWelcome(WiFiClient *client, short iPlayer) { ...@@ -72,6 +79,7 @@ void GameServer::handleWelcome(WiFiClient *client, short iPlayer) {
rPlayers[iPlayer] = player; rPlayers[iPlayer] = player;
numConnections++; numConnections++;
sendAll(buildPlayerList()); sendAll(buildPlayerList());
playerList->addMenuItem(new MenuItem(message, []() {}));
} }
void GameServer::sendAll(String value) { void GameServer::sendAll(String value) {
...@@ -111,7 +119,7 @@ void GameServer::sendIP() { ...@@ -111,7 +119,7 @@ void GameServer::sendIP() {
void GameServer::startGame() { void GameServer::startGame() {
if (!gameStarted) { if (!gameStarted) {
Serial.println("Game started"); Serial.println("Start Game - GameServer");
ConfigPacket config; ConfigPacket config;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
config.magazineSize[i] = Player::magazineSize[i]; config.magazineSize[i] = Player::magazineSize[i];
...@@ -123,12 +131,17 @@ void GameServer::startGame() { ...@@ -123,12 +131,17 @@ void GameServer::startGame() {
config.health = HEALTH; config.health = HEALTH;
String value = config.serial() + "\n"; String value = config.serial() + "\n";
sendAll(value); sendAll(value);
sendAll(this->buildPlayerList());
this->gameStarted = true; this->gameStarted = true;
ui->closeCurrent(); if (gameStartCallback) {
Serial.println("Start Game - GameServer - call Callback");
gameStartCallback();
}
Serial.println("Start Game - GameServer - Ende");
} }
} }
void GameServer::sendHit(HitPacket* hitPacket) { void GameServer::sendHit(HitPacket *hitPacket) {
int pid = hitPacket->playerId; int pid = hitPacket->playerId;
serverClients[pid].print(hitPacket->serial()); serverClients[pid].print(hitPacket->serial());
serverClients[pid].flush(); serverClients[pid].flush();
......
...@@ -80,6 +80,10 @@ public: ...@@ -80,6 +80,10 @@ public:
virtual void update(); virtual void update();
bool isStarted() { return this->gameStarted; }
void setGamestartCallback(std::function<void(void)> callback) { this->gameStartCallback = callback; }
protected: protected:
void setTeamPlay(char isTeam); void setTeamPlay(char isTeam);
...@@ -95,6 +99,7 @@ protected: ...@@ -95,6 +99,7 @@ protected:
void sendHit(HitPacket *hitPacket); void sendHit(HitPacket *hitPacket);
private: private:
WindowSystem *ui; WindowSystem *ui;
Badge *badge; Badge *badge;
...@@ -105,6 +110,7 @@ private: ...@@ -105,6 +110,7 @@ private:
WiFiClient *serverClients; // the clients connected to the server WiFiClient *serverClients; // the clients connected to the server
short numConnections = 0; short numConnections = 0;
bool gameStarted = false; bool gameStarted = false;
std::function<void(void)> gameStartCallback;
void sendIP(); void sendIP();
......
...@@ -11,6 +11,7 @@ uint32_t read32(File &f); ...@@ -11,6 +11,7 @@ uint32_t read32(File &f);
uint16_t read16(File &f); uint16_t read16(File &f);
void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) { void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) {
Serial.println("LaserMenuItem::bmpDraw");
if (this->imagePath) { if (this->imagePath) {
File bmpFile; File bmpFile;
uint16_t bmpWidth, bmpHeight; // W+H in pixels uint16_t bmpWidth, bmpHeight; // W+H in pixels
...@@ -96,4 +97,5 @@ void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) { ...@@ -96,4 +97,5 @@ void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) {
tft->print("file unrecognized!"); tft->print("file unrecognized!");
} }
} }
Serial.println("LaserMenuItem::bmpDraw - Ende");
} }
...@@ -10,9 +10,12 @@ ...@@ -10,9 +10,12 @@
#include "BadgeUI.h" #include "BadgeUI.h"
class LaserMenuItem : public MenuItem { class LaserMenuItem : public MenuItem {
public: public:
LaserMenuItem(std::function<void(void)> trigger, const char *imagePath) : LaserMenuItem(std::function<void(void)> trigger, const char* imagePath) :
MenuItem("100%", trigger), imagePath(imagePath) { MenuItem("100%", trigger), imagePath(imagePath) {
}; };
void setImagePath(String imagePath) { this->imagePath = imagePath.c_str(); }
private: private:
const char *imagePath; const char *imagePath;
void bmpDraw(uint8_t, uint16_t, TFT_ILI9163C*); void bmpDraw(uint8_t, uint16_t, TFT_ILI9163C*);
......
...@@ -49,7 +49,8 @@ void Player::updateArmorMenuItem() { ...@@ -49,7 +49,8 @@ void Player::updateArmorMenuItem() {
snprintf(buff, sizeof(buff), "%d %", this->currentArmor / this->maxArmor * 100); snprintf(buff, sizeof(buff), "%d %", this->currentArmor / this->maxArmor * 100);
String percentage = buff; String percentage = buff;
delete[] buff; delete[] buff;
this->healthMenuItem->setText(percentage); this->armorMenuItem->setText(percentage);
this->armorMenuItem->setImagePath(String("/ammo" + String(this->weaponIndex) + ".bmp"));
} }
} }
......
...@@ -44,9 +44,6 @@ public: ...@@ -44,9 +44,6 @@ public:
unsigned short getIndex() { return index; } unsigned short getIndex() { return index; }
void dispatchInput(JoystickState state) { void dispatchInput(JoystickState state) {
#ifdef DEBUG
Serial.println("PlayerListMenu - dispatch");
#endif
switch (state) { switch (state) {
case JoystickState::BTN_UP: case JoystickState::BTN_UP:
if (upAction) { if (upAction) {
...@@ -87,9 +84,6 @@ public: ...@@ -87,9 +84,6 @@ public:
Menu::dispatchInput(state); Menu::dispatchInput(state);
break; break;
} }
#ifdef DEBUG
Serial.println("PlayerListMenu - dispatch - Ende");
#endif
}; };
void addMenuItem(MenuItem *item) { void addMenuItem(MenuItem *item) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment