Remove "controllable" and other stuff

This commit is contained in:
tovjemam 2026-01-15 01:34:48 +01:00
parent 1831befb85
commit 899b0f990c
21 changed files with 280 additions and 190 deletions

View File

@ -97,8 +97,6 @@ set(CLIENT_ONLY_SOURCES
) )
set(SERVER_ONLY_SOURCES set(SERVER_ONLY_SOURCES
"src/game/controllable.cpp"
"src/game/controllable.hpp"
"src/game/entity.hpp" "src/game/entity.hpp"
"src/game/entity.cpp" "src/game/entity.cpp"
"src/game/game.hpp" "src/game/game.hpp"

View File

@ -38,7 +38,24 @@ void App::Frame()
} }
// detect inputs originating in this frame // detect inputs originating in this frame
game::PlayerInputFlags new_input = input_ & ~prev_input_; //game::PlayerInputFlags new_input = input_ & ~prev_input_;
// detect input changes
for (size_t i = 0; i < game::IN__COUNT; ++i)
{
auto in_old = prev_input_ & (1 << i);
auto in_new = input_ & (1 << i);
if (in_old > in_new) // released
{
SendInput(static_cast<game::PlayerInputType>(i), false);
}
else if (in_new > in_old) // pressed
{
SendInput(static_cast<game::PlayerInputType>(i), true);
}
}
prev_input_ = input_; prev_input_ = input_;
if (session_) if (session_)
@ -81,14 +98,14 @@ void App::Frame()
renderer_.DrawList(dlist_, params); renderer_.DrawList(dlist_, params);
if (time_ - last_send_time_ > 0.040f) // if (time_ - last_send_time_ > 0.040f)
{ // {
auto msg = BeginMsg(net::MSG_IN); // auto msg = BeginMsg(net::MSG_IN);
msg.Write(input_); // msg.Write(input_);
last_send_time_ = time_; // last_send_time_ = time_;
} // }
} }
void App::Connected() void App::Connected()
@ -153,9 +170,13 @@ void App::AddChatMessagePrefix(const std::string& prefix, const std::string& tex
App::~App() {} App::~App() {}
void App::Send(std::vector<char> data) void App::SendInput(game::PlayerInputType type, bool enable)
{ {
auto msg = BeginMsg(net::MSG_IN);
uint8_t val = type;
if (enable)
val |= 128;
msg.Write(val);
} }
void App::InitChat() void App::InitChat()

View File

@ -47,7 +47,7 @@ public:
~App(); ~App();
private: private:
void Send(std::vector<char> data); void SendInput(game::PlayerInputType type, bool enable);
void InitChat(); void InitChat();
void UpdateChat(); void UpdateChat();

View File

@ -314,41 +314,40 @@ static void Frame()
const uint8_t* kbd_state = SDL_GetKeyboardState(nullptr); const uint8_t* kbd_state = SDL_GetKeyboardState(nullptr);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_w)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_w)])
input |= game::IN_FORWARD; input |= (1 << game::IN_FORWARD);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_s)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_s)])
input |= game::IN_BACKWARD; input |= (1 << game::IN_BACKWARD);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_a)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_a)])
input |= game::IN_LEFT; input |= (1 << game::IN_LEFT);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_d)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_d)])
input |= game::IN_RIGHT; input |= (1 << game::IN_RIGHT);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_SPACE)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_SPACE)])
input |= game::IN_JUMP; input |= (1 << game::IN_JUMP);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_LCTRL)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_LCTRL)])
input |= game::IN_CROUCH; input |= (1 << game::IN_CROUCH);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_e)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_e)])
input |= game::IN_USE; input |= (1 << game::IN_USE);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_F3)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_F3)])
input |= game::IN_DEBUG1; input |= (1 << game::IN_DEBUG1);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_F4)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_F4)])
input |= game::IN_DEBUG2; input |= (1 << game::IN_DEBUG2);
if (kbd_state[SDL_GetScancodeFromKey(SDLK_F5)]) if (kbd_state[SDL_GetScancodeFromKey(SDLK_F5)])
input |= game::IN_DEBUG3; input |= (1 << game::IN_DEBUG3);
int mouse_state = SDL_GetMouseState(nullptr, nullptr); int mouse_state = SDL_GetMouseState(nullptr, nullptr);
if (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) if (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT))
input |= game::IN_ATTACK; input |= game::IN_ATTACK;
s_app->SetInput(input); s_app->SetInput(input);
s_app->Frame(); s_app->Frame();

View File

@ -1,9 +0,0 @@
#include "controllable.hpp"
#include "player.hpp"
game::Controllable::~Controllable()
{
if (controller_)
controller_->Control(nullptr);
}

View File

@ -1,30 +0,0 @@
#pragma once
#include "utils/defs.hpp"
namespace game
{
class Entity;
class Player;
class Controllable
{
public:
Controllable() = default;
DELETE_COPY_MOVE(Controllable)
virtual Entity& GetEntity() = 0;
Player* GetController() { return controller_; }
const Player* GetController() const { return controller_; }
~Controllable();
private:
Player* controller_ = nullptr;
friend class Player;
};
}

View File

@ -16,10 +16,15 @@ void game::Game::Update()
void game::Game::PlayerJoined(Player& player) void game::Game::PlayerJoined(Player& player)
{ {
player.SetWorld(default_world_.get()); player.SetWorld(default_world_);
} }
void game::Game::PlayerLeft(Player& player) void game::Game::PlayerLeft(Player& player)
{ {
} }
bool game::Game::PlayerInput(Player& player, PlayerInputType type, bool enabled)
{
return false; // not handled here
}

View File

@ -18,6 +18,8 @@ public:
void PlayerJoined(Player& player); void PlayerJoined(Player& player);
void PlayerLeft(Player& player); void PlayerLeft(Player& player);
bool PlayerInput(Player& player, PlayerInputType type, bool enabled);
private: private:
std::shared_ptr<World> default_world_; std::shared_ptr<World> default_world_;

View File

@ -1,15 +1,75 @@
#include "openworld.hpp" #include "openworld.hpp"
#include "vehicle.hpp"
#include "player.hpp" #include "player.hpp"
#include "vehicle.hpp"
game::OpenWorld::OpenWorld() : World("openworld") {} game::OpenWorld::OpenWorld() : World("openworld") {}
void game::OpenWorld::PlayerJoined(Player& player) void game::OpenWorld::PlayerJoined(Player& player)
{ {
SpawnVehicle(player);
}
void game::OpenWorld::PlayerInput(Player& player, PlayerInputType type, bool enabled)
{
auto vehicle = player_vehicles_.at(&player);
// player.SendChat("input zmenen: " + std::to_string(static_cast<int>(type)) + "=" + (enabled ? "1" : "0"));
switch (type)
{
case IN_FORWARD:
vehicle->SetInput(VIN_FORWARD, enabled);
break;
case IN_BACKWARD:
vehicle->SetInput(VIN_BACKWARD, enabled);
break;
case IN_LEFT:
vehicle->SetInput(VIN_LEFT, enabled);
break;
case IN_RIGHT:
vehicle->SetInput(VIN_RIGHT, enabled);
break;
case IN_DEBUG1:
if (enabled)
vehicle->SetPosition({ 100.0f, 100.0f, 5.0f });
break;
case IN_DEBUG2:
if (enabled)
SpawnVehicle(player);
break;
default:
break;
}
}
void game::OpenWorld::PlayerLeft(Player& player)
{
RemoveVehicle(player);
}
void game::OpenWorld::RemoveVehicle(Player& player)
{
auto it = player_vehicles_.find(&player);
if (it != player_vehicles_.end())
{
it->second->Remove();
player_vehicles_.erase(it);
}
}
void game::OpenWorld::SpawnVehicle(Player& player)
{
RemoveVehicle(player);
// spawn him car // spawn him car
// random model // random model
const char* vehicles[] = { "pickup", "passat" }; const char* vehicles[] = {"pickup", "passat"};
auto vehicle_name = vehicles[rand() % (sizeof(vehicles) / sizeof(vehicles[0]))]; auto vehicle_name = vehicles[rand() % (sizeof(vehicles) / sizeof(vehicles[0]))];
// ranodm color // ranodm color
@ -22,20 +82,11 @@ void game::OpenWorld::PlayerJoined(Player& player)
} }
auto& vehicle = Spawn<Vehicle>(vehicle_name, color); auto& vehicle = Spawn<Vehicle>(vehicle_name, color);
player.Control(&vehicle); vehicle.SetPosition({ 100.0f, 100.0f, 5.0f });
player_vehicles_[&player] = vehicle.GetEntNum();
}
void game::OpenWorld::PlayerLeft(Player& player)
{
auto it = player_vehicles_.find(&player);
Entity* ent = GetEntity(it->second);
if (ent)
ent->Remove();
player_vehicles_.erase(it);
player.SetCamera(vehicle.GetEntNum());
player_vehicles_[&player] = &vehicle;
player.SendChat("dostals " + std::string(vehicle_name));
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "world.hpp" #include "world.hpp"
#include "vehicle.hpp"
namespace game namespace game
{ {
@ -11,10 +12,15 @@ public:
OpenWorld(); OpenWorld();
virtual void PlayerJoined(Player& player) override; virtual void PlayerJoined(Player& player) override;
virtual void PlayerInput(Player& player, PlayerInputType type, bool enabled) override;
virtual void PlayerLeft(Player& player) override; virtual void PlayerLeft(Player& player) override;
private: private:
std::map<Player*, net::EntNum> player_vehicles_; void SpawnVehicle(Player& player);
void RemoveVehicle(Player& player);
private:
std::map<Player*, Vehicle*> player_vehicles_;
}; };

View File

@ -22,10 +22,10 @@ bool game::Player::ProcessMsg(net::MessageType type, net::InMessage& msg)
void game::Player::Update() void game::Player::Update()
{ {
if (world_ != known_world_) if (world_.get() != known_world_)
{ {
SendWorldMsg(); SendWorldMsg();
known_world_ = world_; known_world_ = world_.get();
known_ents_.clear(); known_ents_.clear();
} }
@ -33,36 +33,31 @@ void game::Player::Update()
SyncEntities(); SyncEntities();
} }
void game::Player::SetWorld(World* world) void game::Player::SetWorld(std::shared_ptr<World> world)
{ {
if (world == world_) if (world == world_)
return; return;
Control(nullptr);
if (world_) if (world_)
world_->PlayerLeft(*this); world_->PlayerLeft(*this);
world_ = world; world_ = std::move(world);
if (world_) if (world_)
world_->PlayerJoined(*this); world_->PlayerJoined(*this);
} }
void game::Player::Control(Controllable* ctl) void game::Player::SetCamera(net::EntNum entnum)
{ {
if (ctl == ctl_) auto msg = BeginMsg(net::MSG_CAM);
return; msg.Write(entnum);
}
if (ctl_) void game::Player::SendChat(const std::string text)
ctl_->controller_ = nullptr; // clear old {
auto msg = BeginMsg(net::MSG_CHAT);
ctl_ = ctl; net::ChatMessage chatm = text;
msg.Write(chatm);
if (ctl_)
ctl_->controller_ = this;
SendControl();
} }
game::Player::~Player() game::Player::~Player()
@ -78,13 +73,6 @@ void game::Player::SendWorldMsg()
msg.Write(net::MapName(world_->GetMapName())); msg.Write(net::MapName(world_->GetMapName()));
} }
void game::Player::SendControl()
{
auto msg = BeginMsg(net::MSG_CONTROL);
net::EntNum entnum = ctl_ ? ctl_->GetEntity().GetEntNum() : 0;
msg.Write(entnum);
}
void game::Player::SyncEntities() void game::Player::SyncEntities()
{ {
const auto& ents = world_->GetEntities(); const auto& ents = world_->GetEntities();
@ -161,8 +149,31 @@ void game::Player::SendDestroyEntity(net::EntNum entnum)
bool game::Player::ProcessInputMsg(net::InMessage& msg) bool game::Player::ProcessInputMsg(net::InMessage& msg)
{ {
if (!msg.Read(in_)) uint8_t val;
if (!msg.Read(val))
return false; return false;
bool enabled = false;
if (val & 128)
{
enabled = true;
val &= ~128;
}
Input(static_cast<PlayerInputType>(val), enabled);
return true; return true;
} }
void game::Player::Input(PlayerInputType type, bool enabled)
{
if (enabled)
in_ |= (1 << type);
else
in_ &= ~(1 << type);
if (!game_.PlayerInput(*this, type, enabled))
{
if (world_)
world_->PlayerInput(*this, type, enabled);
}
}

View File

@ -8,7 +8,6 @@
#include "player_input.hpp" #include "player_input.hpp"
#include "game.hpp" #include "game.hpp"
#include "controllable.hpp"
namespace game namespace game
{ {
@ -17,13 +16,6 @@ class World;
class Entity; class Entity;
class Vehicle; class Vehicle;
enum PlayerState
{
PS_NONE,
PS_CHARACTER,
PS_VEHICLE,
};
class Player : public net::MsgProducer class Player : public net::MsgProducer
{ {
public: public:
@ -33,8 +25,10 @@ public:
bool ProcessMsg(net::MessageType type, net::InMessage& msg); bool ProcessMsg(net::MessageType type, net::InMessage& msg);
void Update(); void Update();
void SetWorld(World* world); void SetWorld(std::shared_ptr<World> world);
void Control(Controllable* ctl);
void SetCamera(net::EntNum entnum);
void SendChat(const std::string text);
PlayerInputFlags GetInput() const { return in_; } PlayerInputFlags GetInput() const { return in_; }
@ -42,7 +36,6 @@ public:
private: private:
void SendWorldMsg(); void SendWorldMsg();
void SendControl();
// entities sync // entities sync
void SyncEntities(); void SyncEntities();
@ -54,19 +47,18 @@ private:
// msg handlers // msg handlers
bool ProcessInputMsg(net::InMessage& msg); bool ProcessInputMsg(net::InMessage& msg);
// events
void Input(PlayerInputType type, bool enabled);
private: private:
Game& game_; Game& game_;
std::string name_; std::string name_;
World* world_ = nullptr; std::shared_ptr<World> world_ = nullptr;
World* known_world_ = nullptr; World* known_world_ = nullptr;
std::set<net::EntNum> known_ents_; std::set<net::EntNum> known_ents_;
PlayerInputFlags in_ = 0; PlayerInputFlags in_ = 0;
PlayerState state_ = PS_NONE;
Controllable* ctl_ = nullptr;
}; };
} }

View File

@ -6,18 +6,22 @@ namespace game
{ {
using PlayerInputFlags = uint16_t; using PlayerInputFlags = uint16_t;
enum PlayerInputFlag : PlayerInputFlags enum PlayerInputType : uint8_t
{ {
IN_FORWARD = 1 << 0, IN_FORWARD,
IN_BACKWARD = 1 << 1, IN_BACKWARD,
IN_LEFT = 1 << 2, IN_LEFT,
IN_RIGHT = 1 << 3, IN_RIGHT,
IN_JUMP = 1 << 4, IN_JUMP,
IN_CROUCH = 1 << 5, IN_CROUCH,
IN_USE = 1 << 6, IN_USE,
IN_ATTACK = 1 << 7, IN_ATTACK,
IN_DEBUG1 = 1 << 8, IN_DEBUG1,
IN_DEBUG2 = 1 << 9, IN_DEBUG2,
IN_DEBUG3 = 1 << 10, IN_DEBUG3,
IN_DEBUG4,
IN_DEBUG5,
IN__COUNT,
}; };
} }

View File

@ -109,6 +109,22 @@ void game::Vehicle::SendInitData(Player& player, net::OutMessage& msg) const
net::ModelName name(model_name_); net::ModelName name(model_name_);
msg.Write(name); msg.Write(name);
net::WriteRGB(msg, color_); // primary color net::WriteRGB(msg, color_); // primary color
WriteState(msg);
}
void game::Vehicle::SetInput(VehicleInputType type, bool enable)
{
if (enable)
in_ |= (1 << type);
else
in_ &= ~(1 << type);
}
void game::Vehicle::SetPosition(const glm::vec3& pos)
{
auto t = body_->getWorldTransform();
t.setOrigin(btVector3(pos.x, pos.y, pos.z));
body_->setWorldTransform(t);
} }
game::Vehicle::~Vehicle() game::Vehicle::~Vehicle()
@ -130,16 +146,6 @@ void game::Vehicle::ProcessInput()
float engineForce = 0; float engineForce = 0;
float breakingForce = 0; float breakingForce = 0;
// process input
PlayerInputFlags in = GetController() ? GetController()->GetInput() : 0;
if (in & IN_DEBUG1)
{
auto t = body_->getWorldTransform();
t.setOrigin(btVector3(100, 100, 5));
body_->setWorldTransform(t);
}
float speed = vehicle_->getCurrentSpeedKmHour(); float speed = vehicle_->getCurrentSpeedKmHour();
float maxsc = .5f; float maxsc = .5f;
@ -151,14 +157,19 @@ void game::Vehicle::ProcessInput()
float t_delta = 1.0f / 25.0f; float t_delta = 1.0f / 25.0f;
if (in & IN_FORWARD) const bool in_forward = in_ & (1 << VIN_FORWARD);
const bool in_backward = in_ & (1 << VIN_BACKWARD);
const bool in_left = in_ & (1 << VIN_LEFT);
const bool in_right = in_ & (1 << VIN_RIGHT);
if (in_forward)
{ {
if (speed < -1) if (speed < -1)
breakingForce = maxBreakingForce; breakingForce = maxBreakingForce;
else else
engineForce = maxEngineForce; engineForce = maxEngineForce;
} }
if (in & IN_BACKWARD) if (in_backward)
{ {
if (speed > 1) if (speed > 1)
breakingForce = maxBreakingForce; breakingForce = maxBreakingForce;
@ -167,19 +178,19 @@ void game::Vehicle::ProcessInput()
} }
// idle breaking // idle breaking
if (!(in & IN_FORWARD) && !(in & IN_BACKWARD)) if (!in_forward && !in_backward)
{ {
breakingForce = maxBreakingForce * 0.05f; breakingForce = maxBreakingForce * 0.05f;
} }
if (in & IN_LEFT) if (in_left)
{ {
if (steering_ < steeringClamp) if (steering_ < steeringClamp)
steering_ += steeringIncrement * t_delta; steering_ += steeringIncrement * t_delta;
} }
else else
{ {
if (in & IN_RIGHT) if (in_right)
{ {
if (steering_ > -steeringClamp) if (steering_ > -steeringClamp)
steering_ -= steeringIncrement * t_delta; steering_ -= steeringIncrement * t_delta;
@ -229,9 +240,8 @@ void game::Vehicle::UpdateWheels()
} }
} }
void game::Vehicle::SendUpdateMsg() void game::Vehicle::WriteState(net::OutMessage& msg) const
{ {
auto msg = BeginEntMsg(net::EMSG_UPDATE);
msg.Write(flags_); msg.Write(flags_);
net::WriteTransform(msg, root_.local); net::WriteTransform(msg, root_.local);
@ -245,24 +255,10 @@ void game::Vehicle::SendUpdateMsg()
msg.Write<net::WheelZOffsetQ>(wheel.z_offset); msg.Write<net::WheelZOffsetQ>(wheel.z_offset);
msg.Write<net::RotationSpeedQ>(wheel.speed); msg.Write<net::RotationSpeedQ>(wheel.speed);
} }
}
// TEMP wheels
// TODO: REMOVE void game::Vehicle::SendUpdateMsg()
// for (size_t i =0; i < vehicle_->getNumWheels(); ++i) {
// { auto msg = BeginEntMsg(net::EMSG_UPDATE);
// vehicle_->updateWheelTransform(i, true); WriteState(msg);
// btTransform tr = vehicle_->getWheelTransformWS(i);
// Transform trans;
// trans.SetBtTransform(tr);
// net::WriteTransform(msg, trans);
// // static glm::vec3 min_angles(1000.0f);
// // static glm::vec3 max_angles(-1000.0f);
// // min_angles = glm::min(min_angles, angles);
// // max_angles= glm::max(max_angles, angles);
// // std::cout << angles.x << " " << angles.y << " " << angles.z << " | " <<std::endl;
// }
} }

View File

@ -5,7 +5,6 @@
#include "assets/vehiclemdl.hpp" #include "assets/vehiclemdl.hpp"
#include "collision/motionstate.hpp" #include "collision/motionstate.hpp"
#include "controllable.hpp"
#include "entity.hpp" #include "entity.hpp"
#include "world.hpp" #include "world.hpp"
#include "vehicleflags.hpp" #include "vehicleflags.hpp"
@ -22,7 +21,18 @@ struct VehicleWheelState
float z_offset = 0.0f; // [m] against model definition float z_offset = 0.0f; // [m] against model definition
}; };
class Vehicle : public Entity, public Controllable using VehicleInputFlags = uint8_t;
enum VehicleInputType
{
VIN_FORWARD,
VIN_BACKWARD,
VIN_LEFT,
VIN_RIGHT,
VIN_HANDBRAKE,
};
class Vehicle : public Entity
{ {
public: public:
using Super = Entity; using Super = Entity;
@ -32,14 +42,16 @@ public:
virtual void Update() override; virtual void Update() override;
virtual void SendInitData(Player& player, net::OutMessage& msg) const override; virtual void SendInitData(Player& player, net::OutMessage& msg) const override;
// Controllable void SetInput(VehicleInputType type, bool enable);
Entity& GetEntity() override { return *this; };
void SetPosition(const glm::vec3& pos);
virtual ~Vehicle(); virtual ~Vehicle();
private: private:
void ProcessInput(); void ProcessInput();
void UpdateWheels(); void UpdateWheels();
void WriteState(net::OutMessage& msg) const;
void SendUpdateMsg(); void SendUpdateMsg();
private: private:
@ -58,6 +70,8 @@ private:
std::array<VehicleWheelState, MAX_WHEELS> wheels_; std::array<VehicleWheelState, MAX_WHEELS> wheels_;
VehicleFlags flags_; VehicleFlags flags_;
VehicleInputFlags in_ = 0;
}; };
} // namespace game } // namespace game

View File

@ -6,6 +6,7 @@
#include "collision/dynamicsworld.hpp" #include "collision/dynamicsworld.hpp"
#include "entity.hpp" #include "entity.hpp"
#include "net/defs.hpp" #include "net/defs.hpp"
#include "player_input.hpp"
namespace game namespace game
{ {
@ -33,8 +34,9 @@ public:
// events // events
virtual void PlayerJoined(Player& player) {} virtual void PlayerJoined(Player& player) {}
virtual void PlayerInput(Player& player, PlayerInputType type, bool enabled) {}
virtual void PlayerLeft(Player& player) {} virtual void PlayerLeft(Player& player) {}
Entity* GetEntity(net::EntNum entnum); Entity* GetEntity(net::EntNum entnum);
const std::string& GetMapName() const { return mapname_; } const std::string& GetMapName() const { return mapname_; }

View File

@ -30,8 +30,11 @@ bool game::view::ClientSession::ProcessSingleMessage(net::MessageType type, net:
case net::MSG_CHWORLD: case net::MSG_CHWORLD:
return ProcessWorldMsg(msg); return ProcessWorldMsg(msg);
case net::MSG_CONTROL: case net::MSG_CAM:
return ProcessControlMsg(msg); return ProcessCameraMsg(msg);
case net::MSG_CHAT:
return ProcessChatMsg(msg);
default: default:
// try pass the msg to world // try pass the msg to world
@ -66,9 +69,9 @@ glm::mat4 game::view::ClientSession::GetViewMatrix() const
{ {
glm::vec3 center(0, 0, 3); glm::vec3 center(0, 0, 3);
if (world_ && ctl_) if (world_ && follow_ent_)
{ {
auto ent = world_->GetEntity(ctl_); auto ent = world_->GetEntity(follow_ent_);
if (ent) if (ent)
center += ent->GetRoot().local.position; center += ent->GetRoot().local.position;
} }
@ -103,10 +106,20 @@ bool game::view::ClientSession::ProcessWorldMsg(net::InMessage& msg)
return true; return true;
} }
bool game::view::ClientSession::ProcessControlMsg(net::InMessage& msg) bool game::view::ClientSession::ProcessCameraMsg(net::InMessage& msg)
{ {
if (!msg.Read(ctl_)) if (!msg.Read(follow_ent_))
return false; return false;
return true; return true;
} }
bool game::view::ClientSession::ProcessChatMsg(net::InMessage& msg)
{
net::ChatMessage chatm;
if (!msg.Read(chatm))
return false;
app_.AddChatMessagePrefix("Server", chatm);
return true;
}

View File

@ -34,7 +34,8 @@ public:
private: private:
// msg handlers // msg handlers
bool ProcessWorldMsg(net::InMessage& msg); bool ProcessWorldMsg(net::InMessage& msg);
bool ProcessControlMsg(net::InMessage& msg); bool ProcessCameraMsg(net::InMessage& msg);
bool ProcessChatMsg(net::InMessage& msg);
private: private:
App& app_; App& app_;
@ -42,7 +43,7 @@ private:
std::unique_ptr<WorldView> world_; std::unique_ptr<WorldView> world_;
float yaw_ = 0.0f, pitch_ = 0.0f; float yaw_ = 0.0f, pitch_ = 0.0f;
net::EntNum ctl_ = 0; net::EntNum follow_ent_ = 0;
}; };

View File

@ -31,7 +31,11 @@ std::unique_ptr<game::view::VehicleView> game::view::VehicleView::InitFromMsg(Wo
auto model = assets::CacheManager::GetVehicleModel("data/" + std::string(modelname) + ".veh"); auto model = assets::CacheManager::GetVehicleModel("data/" + std::string(modelname) + ".veh");
return std::make_unique<VehicleView>(world, std::move(model), color); auto vehicle = std::make_unique<VehicleView>(world, std::move(model), color);
vehicle->ReadState(msg);
vehicle->root_trans_[0] = vehicle->root_trans_[1];
return vehicle;
} }
bool game::view::VehicleView::ProcessMsg(net::EntMsgType type, net::InMessage& msg) bool game::view::VehicleView::ProcessMsg(net::EntMsgType type, net::InMessage& msg)
@ -125,7 +129,7 @@ void game::view::VehicleView::Draw(gfx::DrawList& dlist)
} }
} }
bool game::view::VehicleView::ProcessUpdateMsg(net::InMessage& msg) bool game::view::VehicleView::ReadState(net::InMessage& msg)
{ {
root_trans_[0] = root_.local; root_trans_[0] = root_.local;
auto& root_trans = root_trans_[1]; auto& root_trans = root_trans_[1];
@ -151,5 +155,9 @@ bool game::view::VehicleView::ProcessUpdateMsg(net::InMessage& msg)
wheel.steering = i < 2 ? steering : 0.0f; wheel.steering = i < 2 ? steering : 0.0f;
} }
return true; return true;}
bool game::view::VehicleView::ProcessUpdateMsg(net::InMessage& msg)
{
return ReadState(msg);
} }

View File

@ -31,6 +31,7 @@ public:
virtual void Draw(gfx::DrawList& dlist) override; virtual void Draw(gfx::DrawList& dlist) override;
private: private:
bool ReadState(net::InMessage& msg);
bool ProcessUpdateMsg(net::InMessage& msg); bool ProcessUpdateMsg(net::InMessage& msg);
private: private:

View File

@ -20,6 +20,10 @@ enum MessageType : uint8_t
// IN <PlayerInputFlags> <ViewYawQ> <ViewPitchQ> // IN <PlayerInputFlags> <ViewYawQ> <ViewPitchQ>
MSG_IN, MSG_IN,
/*~~~~~~~~ Session ~~~~~~~~*/
// CHAT <ChatMessage>
MSG_CHAT,
/*~~~~~~~~ World ~~~~~~~~*/ /*~~~~~~~~ World ~~~~~~~~*/
// CHWORLD <MapName> // CHWORLD <MapName>
MSG_CHWORLD, MSG_CHWORLD,
@ -32,8 +36,8 @@ enum MessageType : uint8_t
// ENTDESTROY <EntNum> // ENTDESTROY <EntNum>
MSG_ENTDESTROY, MSG_ENTDESTROY,
// CONTROL <EntNum> // CAM <EntNum>
MSG_CONTROL, MSG_CAM,
/*~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~*/
MSG_COUNT, MSG_COUNT,
@ -42,6 +46,7 @@ enum MessageType : uint8_t
using PlayerName = FixedStr<24>; using PlayerName = FixedStr<24>;
using MapName = FixedStr<32>; using MapName = FixedStr<32>;
using ModelName = FixedStr<64>; using ModelName = FixedStr<64>;
using ChatMessage = FixedStr<1024>;
// pi approx fraction // pi approx fraction
constexpr long long PI_N = 245850922; constexpr long long PI_N = 245850922;