Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _common/include/Packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum class PacketType {
CLIENT_READY,
END_GAME,
NEXT_PHASE,
COMBO_INDEX,
};
class Packet {
private:
Expand Down
10 changes: 3 additions & 7 deletions _common/include/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
#include "NetTransform.hpp"
#include "Mover.hpp"

enum Keys {
I = 73,
J = 74,
K = 75,
L = 76
};
enum Keys { I = 73, J = 74, K = 75, L = 76 };

class Player : public Entity {
public:
// glm::vec2 inputs = glm::vec2();
// glm::vec3 velocity = glm::vec3();
// float speed = 0.2f;
bool alive;
int clientId = -1;

// I = 73, J = 74, K = 75, L = 76
std::vector<int> attack1 = {J, J, J, J};
Expand All @@ -37,7 +33,7 @@ class Player : public Entity {
int32_t TypeID() const override { return PLAYER; }

// Used by server
Player(glm::vec3 position);
Player(glm::vec3 position, int clientId);
void onDestroy();

virtual void update(float deltaTime) override;
Expand Down
38 changes: 26 additions & 12 deletions client/src/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void GameManager::handle_packet(Packet* packet) {
std::cout << "GAME OVER" << std::endl;
}
break;
case PacketType::COMBO_INDEX:
int index;
packet->read_int(&index);
printf("combo index: %d\n", index);
break;
default:
std::cout << " PacketType: ERROR" << std::endl;
break;
Expand All @@ -82,10 +87,11 @@ void GameManager::update(Packet* pkt) {
case NetworkObjectTypeID::ENEMY: {
// std::cout << " ObjTypeID: Enemy" << std::endl;
int network_id;
pkt->read_int(&network_id); // J: I did not thoroughly check if the packet is read correctly
pkt->read_int(&network_id); // J: I did not thoroughly check if the
// packet is read correctly
if (!boss) {
boss = new Enemy(enemyPath, network_id);
std::vector<AnimationClip*> prefabClips =
std::vector<AnimationClip*> prefabClips =
AssetManager::Instance().GetClips(enemyPath);
for (int i = 0; i < prefabClips.size(); i++) {
AnimationClip* clip = new AnimationClip(prefabClips[i]);
Expand All @@ -97,8 +103,10 @@ void GameManager::update(Packet* pkt) {

boss->deserialize(pkt);

// also look up at the boss, probably needs to be the center of it which is like 1000 or something rn
glm::vec3 bossPos = boss->GetComponent<NetTransform>()->GetPosition();
// also look up at the boss, probably needs to be the center of it
// which is like 1000 or something rn
glm::vec3 bossPos =
boss->GetComponent<NetTransform>()->GetPosition();
cam->SetTarget(glm::vec3(bossPos.x, 0.0f, bossPos.z));

break;
Expand All @@ -116,7 +124,8 @@ void GameManager::update(Packet* pkt) {
AnimationClip* clip = new AnimationClip(prefabClips[i]);
// std::cout << "Adding clip: " << clip->getName()
// << std::endl;
playerPrefab->GetComponent<AnimationPlayer>()->AddClip(clip);
playerPrefab->GetComponent<AnimationPlayer>()->AddClip(
clip);
}

players[network_id] = playerPrefab;
Expand Down Expand Up @@ -159,7 +168,7 @@ void GameManager::update(Packet* pkt) {
players[network_id]->deserialize(pkt);

if (localPlayerObject == network_id) {

auto playerPos = players[localPlayerObject]
->GetComponent<NetTransform>()
->position;
Expand All @@ -172,7 +181,9 @@ void GameManager::update(Packet* pkt) {
glm::vec3(0, 2.0f, 0) + playerRightVector * 0.7f);
}

// std::cout << players[network_id]->GetComponent<Status>()->ToString() << std::endl;
// std::cout <<
// players[network_id]->GetComponent<Status>()->ToString() <<
// std::endl;

break;
}
Expand All @@ -184,14 +195,15 @@ void GameManager::update(Packet* pkt) {
if (playerSkills.find(network_id) == playerSkills.end()) {
PlayerSkill* playerSkillPrefab = new PlayerSkill(network_id);
playerSkillPrefab->deserialize(pkt);
playerSkillPrefab->initComponent(playerSkillPrefab->GetComponent<PlayerSkillType>()->GetState());
playerSkillPrefab->initComponent(
playerSkillPrefab->GetComponent<PlayerSkillType>()
->GetState());
playerSkills[network_id] = playerSkillPrefab;
scene.Instantiate(playerSkillPrefab);
}
else {
} else {
playerSkills[network_id]->deserialize(pkt);
}

break;
}
case NetworkObjectTypeID::ENEMY_ATTACK: {
Expand All @@ -200,7 +212,9 @@ void GameManager::update(Packet* pkt) {
pkt->read_int(&network_id);
// Could not find object, create it
if (enemyAttacks.find(network_id) == enemyAttacks.end()) {
EnemyAttack* enemyAttackPrefab = new EnemyAttack(boss->GetComponent<EnemyComponent>()->GetState(), network_id);
EnemyAttack* enemyAttackPrefab = new EnemyAttack(
boss->GetComponent<EnemyComponent>()->GetState(),
network_id);

enemyAttacks[network_id] = enemyAttackPrefab;
scene.Instantiate(enemyAttackPrefab);
Expand Down
27 changes: 27 additions & 0 deletions server/components/PlayerCombat.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#include "PlayerCombat.hpp"
#include "Health.hpp"
#include <NetworkManager.hpp>
#include <Player.hpp>

void PlayerCombat::Update(float deltaTime) {
timeSinceLastInput += deltaTime;

if (timeSinceLastInput >= COMBO_RESET_TIME && shouldResetCombo) {
ResetAllCombos();
shouldResetCombo = false;

NetworkManager::instance().send_combo(((Player*)owner)->clientId, 0);
}
}

void PlayerCombat::AddCombo(const std::vector<int>& sequence) {
Combo combo;
Expand All @@ -12,6 +25,11 @@ std::vector<int> PlayerCombat::CheckCombo(int input) {
if (owner->GetComponent<Health>()->GetDead()) {
return {};
}

timeSinceLastInput = 0;
shouldResetCombo = true;

int maxComboIndex = 0;
for (auto& combo : combos) {
// Check if current input matches combo's input
if (combo.sequence[combo.comboIndex] != input) {
Expand All @@ -22,13 +40,22 @@ std::vector<int> PlayerCombat::CheckCombo(int input) {

combo.comboIndex++;

if (combo.comboIndex > maxComboIndex) {
maxComboIndex = combo.comboIndex;
}

// Reached end of combo, success
if (combo.comboIndex == combo.sequence.size()) {
ResetAllCombos();
NetworkManager::instance().send_combo(((Player*)owner)->clientId,
maxComboIndex);
return combo.sequence;
}
}

NetworkManager::instance().send_combo(((Player*)owner)->clientId,
maxComboIndex);

return {};
}

Expand Down
5 changes: 5 additions & 0 deletions server/components/PlayerCombat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
#include <vector>
#include "IComponent.hpp"

#define COMBO_RESET_TIME 0.5172 * 4

class PlayerCombat : public IComponent {
private:
float timeSinceLastInput = 0;
bool shouldResetCombo = true;
struct Combo {
int comboIndex;
std::vector<int> sequence;
Expand All @@ -16,6 +20,7 @@ class PlayerCombat : public IComponent {
std::vector<int> CheckCombo(int input);
void ResetCombo(Combo& combo);
void ResetAllCombos();
void Update(float deltaTime) override;

std::string ToString() { return "PlayerCombat"; }
};
1 change: 1 addition & 0 deletions server/include/NetworkManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NetworkManager {
void send_state();
void process_input();
void send_next_phase();
void send_combo(int clientId, int comboIndex);
void on_message_received(const EventArgs* e);
void on_client_joined(const EventArgs* e);
static NetworkManager& instance() {
Expand Down
27 changes: 19 additions & 8 deletions server/src/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Server server;
bool isServerReady = false;
int playersReady = 0;
Enemy* enemyPrefab;
std::vector<glm::vec3> spawnPoints = {
glm::vec3(60, 0, 0), glm::vec3(0, 0, 60), glm::vec3(-60, 0, 0),
glm::vec3(0, 0, -60)};
std::vector<glm::vec3> spawnPoints = {glm::vec3(60, 0, 0), glm::vec3(0, 0, 60),
glm::vec3(-60, 0, 0),
glm::vec3(0, 0, -60)};
int spawnIndex = 0;
union FloatUnion {
float f;
Expand Down Expand Up @@ -162,14 +162,17 @@ void NetworkManager::process_input() {
->p->GetComponent<PlayerCombat>()
->ResetAllCombos();
printf(RED "MISSED\n" RST);
send_combo(client_id, 0);
break;
}

std::vector<int> comboSeq = clients[client_id]->p->GetComponent<PlayerCombat>()->CheckCombo(key);
std::vector<int> comboSeq =
clients[client_id]->p->GetComponent<PlayerCombat>()->CheckCombo(
key);
if (!comboSeq.empty()) {
printf(YLW "COMBO HIT\n" RST);
if (comboSeq == clients[client_id]->p->attack1 ||

if (comboSeq == clients[client_id]->p->attack1 ||
comboSeq == clients[client_id]->p->attack2) {
AttackManager::instance().newPlayerAttack(
clients[client_id]->p);
Expand Down Expand Up @@ -226,7 +229,8 @@ void NetworkManager::process_input() {

void NetworkManager::update(float deltaTime) {
// AttackManager update goes first so that whatever needs to be destroyed
// gets destroyed in the next tick, to account for effects that happen instantaneously
// gets destroyed in the next tick, to account for effects that happen
// instantaneously
AttackManager::instance().update(deltaTime);
scene.Update(deltaTime);
}
Expand Down Expand Up @@ -298,6 +302,13 @@ void NetworkManager::send_next_phase() {
}
}

void NetworkManager::send_combo(int clientId, int comboIndex) {
Packet* p = new Packet();
p->write_int((int)PacketType::COMBO_INDEX);
p->write_int(comboIndex);
server.send(clientId, p);
}

// FIXME handle incomplete packets or multiple packets per send() call
// thread-safe
void NetworkManager::on_message_received(const EventArgs* e) {
Expand All @@ -316,7 +327,7 @@ void NetworkManager::on_client_joined(const EventArgs* e) {

// Give client control over player
glm::vec3 position = spawnPoints[spawnIndex++ % spawnPoints.size()];
Player* p = new Player(position);
Player* p = new Player(position, args->clientId);
server.clients[args->clientId]->p = p;
AttackManager::instance().addPlayer(p);
numAlive++;
Expand Down
3 changes: 2 additions & 1 deletion server/src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include "CooldownComponent.hpp"
#include "MovementStateMachine.hpp"

Player::Player(glm::vec3 position) : Entity() {
Player::Player(glm::vec3 position, int clientId) : Entity() {
this->clientId = clientId;
this->GetComponent<NetTransform>()->SetPosition(position);
alive = true;
Mover* mover = new Mover(this);
Expand Down