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

GPN

parent a3fdf481
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@
#include <GPNBadge.hpp>
#include "BadgeUI.h"
#include "LaserMenuItem.hpp"
#include "Player.hpp"
#include "url-encode.h"
#include "PlayerListMenu.hpp"
......
......@@ -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");
......
......@@ -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");
}
}
......
......@@ -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++) {
......
......@@ -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
//
// 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
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