Client Positions are shown using crosshairs on all other clients!

* Server Regurgitates player location info to all clients
* PlayerEntity class for representing other Players on the server
* Temporary ServerEntity rendering code in GameScene
This commit is contained in:
aurailus 2019-02-08 15:35:59 -08:00
parent a0cd2c554b
commit df0e74b3e7
23 changed files with 168 additions and 96 deletions

View File

@ -30,15 +30,15 @@ set(ZEUS_SRC_FILES
generic/blocks/BlockModel.h
generic/blocks/TextureAtlas.cpp
generic/blocks/TextureAtlas.h
client/gameworld/World.cpp
client/gameworld/World.h
client/game/gameworld/World.cpp
client/game/gameworld/World.h
generic/blocks/BlockChunk.cpp
generic/blocks/BlockChunk.h
generic/helpers/ArrayTrans3D.h
client/graphics/mesh/MeshChunk.cpp
client/graphics/mesh/MeshChunk.h
client/scene/GameScene.cpp
client/scene/GameScene.h
client/game/GameScene.cpp
client/game/GameScene.h
client/engine/graphics/Renderer.cpp
client/engine/graphics/Renderer.h
client/lua/LuaParser.cpp
@ -52,8 +52,8 @@ set(ZEUS_SRC_FILES
client/engine/graphics/HudText.h
client/graphics/gui/DebugGui.cpp
client/graphics/gui/DebugGui.h
client/gameworld/Player.cpp
client/gameworld/Player.h
client/game/gameworld/Player.cpp
client/game/gameworld/Player.h
client/engine/Ray.cpp
client/engine/Ray.h
client/lua/l_register_blockmodel.cpp
@ -66,8 +66,8 @@ set(ZEUS_SRC_FILES
client/engine/scene/SceneManager.h
client/engine/scene/Scene.h
client/ClientState.h
client/scene/MenuScene.cpp
client/scene/MenuScene.h
client/menu/MenuScene.cpp
client/menu/MenuScene.h
server/Server.cpp
server/Server.h
generic/network/Packet.cpp
@ -76,8 +76,8 @@ set(ZEUS_SRC_FILES
server/ServerPlayer.h
client/network/ServerConnection.cpp
client/network/ServerConnection.h
client/gameworld/WorldThreadDefs.cpp
client/gameworld/WorldThreadDefs.h
client/game/gameworld/WorldThreadDefs.cpp
client/game/gameworld/WorldThreadDefs.h
generic/gen/MapGen.cpp
generic/gen/MapGen.h
generic/network/NetHandler.cpp
@ -90,6 +90,6 @@ set(ZEUS_SRC_FILES
server/ServerState.h
server/ConnectionList.cpp
server/ConnectionList.h
server/ServerPeer.h generic/network/PacketType.h generic/network/PacketType.h generic/network/PacketChannel.h)
server/ServerPeer.h generic/network/PacketType.h generic/network/PacketType.h generic/network/PacketChannel.h client/game/gameworld/PlayerEntity.cpp client/game/gameworld/PlayerEntity.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -5,12 +5,12 @@
#ifndef ZEUS_CLIENT_H
#define ZEUS_CLIENT_H
#include "engine/graphics/Renderer.h"
#include "ClientState.h"
#include "engine/graphics/Renderer.h"
#include "engine/scene/SceneManager.h"
#include "scene/GameScene.h"
#include "scene/MenuScene.h"
#include "game/GameScene.h"
#include "menu/MenuScene.h"
#include "engine/Timer.h"

View File

@ -14,5 +14,4 @@ struct ClientState {
double deltaTime;
};
#endif //ZEUS_CLIENTSTATE_H

View File

@ -9,7 +9,7 @@
#include <cmath>
#include <glm.hpp>
#include "../gameworld/Player.h"
#include "../game/gameworld/Player.h"
class Ray {
public:

View File

@ -45,53 +45,11 @@ GameScene::GameScene(ClientState* state) :
debugGui.pushGuiObjects(guiEntities);
gui.pushGuiObjects(guiEntities);
auto crosshairTexture = new Texture((char*)"../res/tex/gui/crosshair.png");
crosshairTexture->load();
auto squareVerts = new std::vector<float> {
-0.5, -0.5, 0, 0, 0, 0, 0, 0,
-0.5, 0.5, 0, 0, 1, 0, 0, 0,
0.5, 0.5, 0, 1, 1, 0, 0, 0,
0.5, -0.5, 0, 1, 0, 0, 0, 0,
0, -0.5, -0.5, 0, 0, 0, 0, 0,
0, 0.5, -0.5, 0, 1, 0, 0, 0,
0, 0.5, 0.5, 1, 1, 0, 0, 0,
0, -0.5, 0.5, 1, 0, 0, 0, 0,
-0.5, 0, -0.5, 0, 0, 0, 0, 0,
0.5, 0, -0.5, 0, 1, 0, 0, 0,
0.5, 0, 0.5, 1, 1, 0, 0, 0,
-0.5, 0, 0.5, 1, 0, 0, 0, 0,
};
auto squareInds = new std::vector<unsigned int> {
0, 1, 2, 2, 3, 0,
0, 2, 1, 2, 0, 3,
4, 5, 6, 6, 7, 4,
4, 6, 5, 6, 4, 7,
8, 9, 10, 10, 11, 8,
8, 10, 9, 10, 8, 11
};
auto m = new Mesh();
m->create(squareVerts, squareInds);
pointer = new Entity();
pointer->create(m, crosshairTexture);
delete squareVerts;
delete squareInds;
pointer->setPosition(glm::vec3(1, 18, 1));
pointer->setScale(0.5);
entities.push_back(pointer);
}
void GameScene::update() {
server->update(*player, *pointer);
server->update(*player, playerEntities);
auto window = state->renderer->getWindow();
@ -99,8 +57,6 @@ void GameScene::update() {
debugGui.update(player, world, window, blockAtlas, state->fps);
world->update();
// pointer->setPosition(*player->getPos() + glm::vec3(0, -1, 0));
}
void GameScene::draw() {
@ -125,6 +81,18 @@ void GameScene::draw() {
state->renderer->draw(entity);
}
//TEMPORARY
for (auto &entity : playerEntities) {
auto newTexture = entity->getTexture();
if (newTexture != nullptr && newTexture != prevTexture) {
prevTexture = newTexture;
newTexture->use();
}
state->renderer->draw(entity);
}
state->renderer->enableGuiShader();
for (auto &entity : guiEntities) {

View File

@ -13,13 +13,14 @@
#include "../lua/LuaParser.h"
#include "../gameworld/World.h"
#include "../gameworld/Player.h"
#include "gameworld/World.h"
#include "gameworld/Player.h"
#include "../network/ServerConnection.h"
#include "../../generic/blocks/TextureAtlas.h"
#include "../../generic/blocks/BlockAtlas.h"
#include "gameworld/PlayerEntity.h"
class GameScene : public Scene {
public:
@ -40,12 +41,12 @@ public:
//Entities to be drawn with gameworld shaders
std::vector<Entity*> entities;
std::vector<PlayerEntity*> playerEntities;
//GUI Related things
std::vector<Entity*> guiEntities;
DebugGui debugGui;
GameGui gui;
Entity* pointer;
};

View File

@ -9,8 +9,8 @@
#include <iostream>
#include "World.h"
#include "../engine/Camera.h"
#include "../engine/Timer.h"
#include "../../engine/Camera.h"
#include "../../engine/Timer.h"
class Player {
public:

View File

@ -0,0 +1,53 @@
//
// Created by aurailus on 08/02/19.
//
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(glm::vec3 pos, int peer_id) {
this->peer_id = peer_id;
this->crosshairTexture = new Texture((char*)"../res/tex/gui/crosshair.png");
crosshairTexture->load();
auto vertices = new std::vector<float> {
-0.5, -0.5, 0, 0, 0, 0, 0, 0,
-0.5, 0.5, 0, 0, 1, 0, 0, 0,
0.5, 0.5, 0, 1, 1, 0, 0, 0,
0.5, -0.5, 0, 1, 0, 0, 0, 0,
0, -0.5, -0.5, 0, 0, 0, 0, 0,
0, 0.5, -0.5, 0, 1, 0, 0, 0,
0, 0.5, 0.5, 1, 1, 0, 0, 0,
0, -0.5, 0.5, 1, 0, 0, 0, 0,
-0.5, 0, -0.5, 0, 0, 0, 0, 0,
0.5, 0, -0.5, 0, 1, 0, 0, 0,
0.5, 0, 0.5, 1, 1, 0, 0, 0,
-0.5, 0, 0.5, 1, 0, 0, 0, 0,
};
auto indices = new std::vector<unsigned int> {
0, 1, 2, 2, 3, 0,
0, 2, 1, 2, 0, 3,
4, 5, 6, 6, 7, 4,
4, 6, 5, 6, 4, 7,
8, 9, 10, 10, 11, 8,
8, 10, 9, 10, 8, 11
};
auto m = new Mesh();
m->create(vertices, indices);
create(m, crosshairTexture);
delete vertices;
delete indices;
this->setPosition(pos);
this->setScale(0.5);
}
PlayerEntity::~PlayerEntity() {
crosshairTexture->clear();
delete crosshairTexture;
}

View File

@ -0,0 +1,22 @@
//
// Created by aurailus on 08/02/19.
//
#ifndef ZEUS_PLAYERENTITY_H
#define ZEUS_PLAYERENTITY_H
#include "../../engine/Entity.h"
class PlayerEntity : public Entity {
public:
PlayerEntity(glm::vec3 pos, int peer_id);
~PlayerEntity();
int peer_id;
private:
Texture* crosshairTexture;
};
#endif //ZEUS_PLAYERENTITY_H

View File

@ -14,12 +14,12 @@
#include <gtc/type_ptr.hpp>
#include "WorldThreadDefs.h"
#include "../../generic/helpers/PerlinNoise.h"
#include "../../generic/helpers/ArrayTrans3D.h"
#include "../../generic/blocks/BlockAtlas.h"
#include "../../generic/blocks/BlockChunk.h"
#include "../../generic/gen/MapGen.h"
#include "../graphics/mesh/MeshChunk.h"
#include "../../../generic/helpers/PerlinNoise.h"
#include "../../../generic/helpers/ArrayTrans3D.h"
#include "../../../generic/blocks/BlockAtlas.h"
#include "../../../generic/blocks/BlockChunk.h"
#include "../../../generic/gen/MapGen.h"
#include "../../graphics/mesh/MeshChunk.h"
class World {
public:

View File

@ -9,9 +9,9 @@
#include <thread>
#include <mutex>
#include "../../generic/blocks/BlockChunk.h"
#include "../../generic/blocks/BlockAtlas.h"
#include "../../generic/gen/MapGen.h"
#include "../../../generic/blocks/BlockChunk.h"
#include "../../../generic/blocks/BlockAtlas.h"
#include "../../../generic/gen/MapGen.h"
//Structs for storing the threads used in World, and passing data to and from them.

View File

@ -6,7 +6,7 @@
#define ZEUS_LUAAPI_H
#include "LuaParser.h"
#include "../scene/GameScene.h"
#include "../game/GameScene.h"
class LuaApi {
public:

View File

@ -18,7 +18,7 @@ void ServerConnection::init() {
}
void ServerConnection::update(Player &player, Entity& pointer) {
void ServerConnection::update(Player &player, std::vector<PlayerEntity*>& playerEntities) {
ENetEvent event;
while (handler.update(&event)) {
@ -39,12 +39,25 @@ void ServerConnection::update(Player &player, Entity& pointer) {
player.setPos(playerPos);
}
else if (p.type == PacketType::ENTITYINFO) {
int peer_id = Serializer::decodeInt(&p.data[0]);
glm::vec3 playerPos = glm::vec3(
Serializer::decodeFloat(&p.data[0]),
Serializer::decodeFloat(&p.data[4]),
Serializer::decodeFloat(&p.data[8])
Serializer::decodeFloat(&p.data[8]),
Serializer::decodeFloat(&p.data[12])
);
pointer.setPosition(playerPos);
bool found = false;
for (auto plrEnt : playerEntities) {
if (plrEnt->peer_id == peer_id) {
plrEnt->setPosition(playerPos);
found = true;
break;
}
}
if (!found) {
playerEntities.push_back(new PlayerEntity(playerPos, peer_id));
}
}
enet_packet_destroy(event.packet);

View File

@ -13,14 +13,15 @@
#include "../../generic/network/Packet.h"
#include "../../generic/network/PacketType.h"
#include "../../generic/network/NetHandler.h"
#include "../../client/gameworld/Player.h"
#include "../game/gameworld/Player.h"
#include "../game/gameworld/PlayerEntity.h"
class ServerConnection {
public:
ServerConnection(std::string address, unsigned short port);
void init();
void update(Player& player, Entity& pointer);
void update(Player &player, std::vector<PlayerEntity*>& playerEntities);
void cleanup();
~ServerConnection();

View File

@ -8,7 +8,7 @@
ServerPeer* ConnectionList::addPeer(ENetPeer *eNetPeer) {
printf("[INFO] %x:%u connected.\n", eNetPeer->address.host, eNetPeer->address.port);
auto peer = new ServerPeer {.peer = eNetPeer, .player = nullptr};
auto peer = new ServerPeer {.peer = eNetPeer, .player = nullptr, .index = peers.size()};
eNetPeer->data = (void*)peer;
peers.push_back(peer);

View File

@ -25,26 +25,38 @@ void Server::update() {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: {
auto peer = connections.addPeer(event.peer);
auto player = connections.addPlayer(peer, "Aurailus");
connections.addPlayer(peer, "Aurailus");
break;
}
case ENET_EVENT_TYPE_RECEIVE: {
Packet p(event.packet);
if (p.type == PacketType::PLAYERINFO) {
glm::vec3 playerPos = glm::vec3(
Serializer::decodeFloat(&p.data[0]),
Serializer::decodeFloat(&p.data[4]),
Serializer::decodeFloat(&p.data[8])
);
auto from = (ServerPeer*)event.peer->data;
auto player = from->player;
Packet r(PacketType::ENTITYINFO);
if (player != nullptr) {
if (p.type == PacketType::PLAYERINFO) {
Serializer::encodeFloat(r.data, playerPos.x);
Serializer::encodeFloat(r.data, playerPos.y - 1.0f);
Serializer::encodeFloat(r.data, playerPos.z);
//Update Player Object
glm::vec3 newPos = glm::vec3(
Serializer::decodeFloat(&p.data[0]),
Serializer::decodeFloat(&p.data[4]),
Serializer::decodeFloat(&p.data[8])
);
player->pos = newPos;
r.sendTo(connections.players[0]->peer->peer, PacketChannel::ENTITYINFO);
//Send All Clients the new positon
Packet r(PacketType::ENTITYINFO);
Serializer::encodeInt (r.data, player->peer->index);
Serializer::encodeFloat(r.data, newPos.x);
Serializer::encodeFloat(r.data, newPos.y - 1.0f);
Serializer::encodeFloat(r.data, newPos.z);
for (auto peer : connections.peers) {
r.sendTo(peer->peer, PacketChannel::ENTITYINFO);
}
}
}
enet_packet_destroy(event.packet);
@ -54,6 +66,7 @@ void Server::update() {
connections.removePeer(event.peer);
break;
}
case ENET_EVENT_TYPE_NONE:
default:
break;
}

View File

@ -13,6 +13,7 @@ class ServerPlayer;
struct ServerPeer {
ENetPeer* peer;
ServerPlayer* player;
int index;
void cleanup() {};
};

View File

@ -6,6 +6,7 @@
ServerPlayer::ServerPlayer(ServerPeer *peer) {
this->peer = peer;
peer->player = this;
}
ServerPlayer::~ServerPlayer() {