diff --git a/GPN_BadgeLasertag.ino b/GPN_BadgeLasertag.ino index a77138a7d55b807451d762c4d879ee7c68b1d538..19a77a302f711fe8cbe337b5c4d76fb1cbf7bf6b 100644 --- a/GPN_BadgeLasertag.ino +++ b/GPN_BadgeLasertag.ino @@ -2,7 +2,6 @@ #include <GPNBadge.hpp> #include "BadgeUI.h" -#include "LaserMenuItem.hpp" #include "Player.hpp" #include "url-encode.h" #include "PlayerListMenu.hpp" diff --git a/GameClient.cpp b/GameClient.cpp index e544e5d472b101cb412da69359f6a52a4a41473c..8fcf6a171ab024592e17641b5533129f30eba23c 100644 --- a/GameClient.cpp +++ b/GameClient.cpp @@ -14,7 +14,7 @@ void GameClient::createUIGameStart() { Serial.println("GameClient::createUIGameStart - new PlayerListMenu"); playerList->setLeftAction([=]() { this->player->prevWeapon(); }); playerList->setRightAction([=]() { this->player->nextWeapon(); }); -// playerList->setDownAction([=]() { this->player->reload(); }); + playerList->setDownAction([=]() { this->player->reload(); }); playerList->setEnterAction([=]() { this->player->shot(); }); Serial.println("GameClient::createUIGameStart - set Lambdas"); MenuItem *healthMI = new MenuItem("100%", []() {}); @@ -26,7 +26,7 @@ void GameClient::createUIGameStart() { player->setAmmoMenuItem(ammoMI); playerList->addMenuItem(new MenuItem("% Otter", []() {})); playerList->addMenuItem(healthMI); -// playerList->addMenuItem(armorMI); + playerList->addMenuItem(armorMI); // playerList->addMenuItem(ammoMI); playerList->addMenuItem(nameMI); Serial.println("GameClient::createUIGameStart - added Lambdas"); diff --git a/GameServer.cpp b/GameServer.cpp index d0d1d7913ef3076b3a6b0ae95e0d0ca55e52b4c8..ae852ed758cfae4ab0c841f42904c30b6d49dd45 100644 --- a/GameServer.cpp +++ b/GameServer.cpp @@ -122,25 +122,28 @@ void GameServer::sendIP() { void GameServer::startGame() { if (!gameStarted) { - Serial.println("Start Game - GameServer"); - ConfigPacket config; - for (int i = 0; i < 3; i++) { - config.magazineSize[i] = Player::magazineSize[i]; - config.magazineReloadTime[i] = Player::magazineReloadTime[i]; - config.shotTime[i] = Player::shotTime[i]; - config.damage[i] = Player::damage[i]; - } - config.resistance = RESISTANCE; - config.health = HEALTH; - String value = config.serial() + "\n"; - sendAll(value); - sendAll(buildPlayerList()); - this->gameStarted = true; - if (gameStartCallback) { - Serial.println("Start Game - GameServer - call Callback"); - gameStartCallback(); + if (numConnections > 1) { + Serial.println("Start Game - GameServer"); + ConfigPacket config; + Player p(badge); + for (int i = 0; i < 3; i++) { + config.magazineSize[i] = p.magazineSize[i]; + config.magazineReloadTime[i] = p.magazineReloadTime[i]; + config.shotTime[i] = p.shotTime[i]; + config.damage[i] = p.damage[i]; + } + config.resistance = RESISTANCE; + config.health = HEALTH; + String value = config.serial() + "\n"; + sendAll(value); + sendAll(buildPlayerList()); + this->gameStarted = true; + if (gameStartCallback) { + Serial.println("Start Game - GameServer - call Callback"); + gameStartCallback(); + } + Serial.println("Start Game - GameServer - Ende"); } - Serial.println("Start Game - GameServer - Ende"); } } diff --git a/Player.cpp b/Player.cpp index 851ccc1c30f9650d6e914010eb30c65aa9226fdf..747d9c96d75af04793c0a3464e0386db2b2a10a5 100644 --- a/Player.cpp +++ b/Player.cpp @@ -2,15 +2,13 @@ // Created by hoelshare on 21.05.17. // #include "Player.hpp" +#include "ShotPacket.hpp" #define min(a, b) ((a)<(b)?(a):(b)) #define max(a, b) ((a)>(b)?(a):(b)) // TODO set constants -const int Player::magazineSize[] = {10, 25, 1}; -const int Player::shotTime[] = {100, 20, 300}; -const int Player::magazineReloadTime[] = {800, 400, 2000}; -const int Player::damage[] = {25, 10, 100}; + void Player::heal(unsigned short healthPoints) { @@ -66,6 +64,7 @@ void Player::updateAmmoMenuItem() { } void Player::reload() { + Serial.println("Reload!"); // Magazine not empty // and current not full if (this->shotMagazine[this->weaponIndex] && @@ -78,18 +77,56 @@ void Player::reload() { min(Player::magazineSize[this->weaponIndex], this->shotMagazine[this->weaponIndex]) - this->currentShot[this->weaponIndex]; } + Serial.println("Reload ende"); } void Player::shot() { + Serial.println("Shot!"); if(this->currentShot[this->weaponIndex]) { this->currentShot[this->weaponIndex]--; + sendShotIR(); delay(Player::shotTime[this->weaponIndex]); + Serial.println("Nach delay"); } else { reload(); } } +void Player::sendShotIR() { + ShotPacket packet; + packet.pid = this->pID; + packet.damage = damage[this->weaponIndex]; + packet.nickname = String(this->nickname); + String shareString = packet.serial(); + Serial.println(shareString); + badge->setGPIO(IR_EN, HIGH); + Serial.println("NEC"); + uint32_t code = 0; + uint8_t checksum = 0; + for (int i = 0; i < shareString.length(); i++) { + checksum += shareString.charAt(i); + code = code | shareString.charAt(i) << (i % 4) * 8; + if (i % 4 == 3) { + irsend.sendNEC(code, 32); + Serial.println(code, HEX); + code = 0; + } + } + if (code != 0) { + irsend.sendNEC(code, 32); + Serial.println(code, HEX); + } + Serial.print("Checksum: "); + Serial.println(checksum); //224 + code = 0; + code = checksum << 8 | 222; + irsend.sendNEC(code, 32); + badge->setGPIO(IR_EN, LOW); + Serial.println("Senden fertig"); +} + void Player::die() { + Serial.println("Die!"); for (unsigned char i = 0; i < 10; i++) { pixels.setPixelColor(1, pixels.Color(255, 0, 0)); pixels.setPixelColor(2, pixels.Color(255, 0, 0)); @@ -109,6 +146,7 @@ void Player::die() { } void Player::rebirth() { + Serial.println("Rebirth"); this->currentHP = this->maxHP; this->currentArmor = 0; for(short i = 0; i < 3; i++) { diff --git a/Player.hpp b/Player.hpp index 2d18fd98e92df1cb3cc46240d533545c9d2df60b..b0ac9c355e1ff1b9b4b5707ff4aaa903c2060b1d 100644 --- a/Player.hpp +++ b/Player.hpp @@ -25,9 +25,13 @@ public: void hit(unsigned short hitPoints); - void nextWeapon() { weaponIndex = (weaponIndex + 1) % 3; } + void nextWeapon() { + Serial.println("Next-Weapon"); + weaponIndex = (weaponIndex + 1) % 3; + } void prevWeapon() { + Serial.println("Prev-Weapon"); nextWeapon(); nextWeapon(); } @@ -62,11 +66,14 @@ public: delete healthMenuItem, armorMenuItem, ammoMenuItem; } - static const int magazineSize[3]; - static const int shotTime[3]; - static const int magazineReloadTime[3]; - static const int damage[3]; - +// const int magazineSize[3]; +// const int shotTime[3]; +// const int magazineReloadTime[3]; +// const int damage[3]; + const int magazineSize[3] = {10, 25, 1}; + const int shotTime[3] = {100, 20, 300}; + const int magazineReloadTime[3] = {800, 400, 2000}; + const int damage[3] = {25, 10, 100}; private: unsigned int pID; unsigned int tID; @@ -80,8 +87,9 @@ private: unsigned int currentArmor; unsigned int maxArmor; float maxArmorPercent; // max protection between 0 and 1 - + void sendShotIR(); void die(); + Badge *badge; protected: @@ -95,4 +103,5 @@ protected: MenuItem *armorMenuItem; MenuItem *ammoMenuItem; }; + #endif //GPN_LASERTAG_PLAYER_HPP diff --git a/ShotPacket.hpp b/ShotPacket.hpp new file mode 100644 index 0000000000000000000000000000000000000000..58cb22b0948426ad1b9d0b7edccfc01c56cefa01 --- /dev/null +++ b/ShotPacket.hpp @@ -0,0 +1,34 @@ +// +// Created by hoelshare on 28.05.17. +// + +#ifndef GPN_BADGELASERTAG_SHOTPACKET_HPP +#define GPN_BADGELASERTAG_SHOTPACKET_HPP + +#include "url-encode.h" + +struct ShotPacket { + String nickname; + unsigned short pid; + unsigned short damage; +#define TYP "ShotPacket" + String serial() { + String ret = ""; + ret += "typ=" + String(TYP); + ret += "&nick=" + String(nickname); + ret += "&pid=" + String(pid); + ret += "&damage=" + String(damage); + return ret; + } + + static ShotPacket* read(String val) { + UrlDecode decode(val.c_str()); + ShotPacket * ret = new ShotPacket(); + ret->nickname = String(decode.getKey("nick")); + ret->damage = int(decode.getKey("damage")); + ret->pid = int(decode.getKey("pid")); + return ret; + } +}; + +#endif //GPN_BADGELASERTAG_SHOTPACKET_HPP