[Refactor] Unify client and server player classes.
A bit of an architectural change before we progress further. Moved player code into a shared PlayerBass class to elimnate some severe code duplication while i was getting things working.
This commit is contained in:
parent
c083df9958
commit
ad2a540554
@ -5,4 +5,4 @@ add_library(bettola_lib STATIC ${SOURCES})
|
|||||||
|
|
||||||
# Tell any target that links against this lib to
|
# Tell any target that links against this lib to
|
||||||
# automagically include the #include' dir.
|
# automagically include the #include' dir.
|
||||||
target_include_directories(bettola_lib PUBLIC "include/bettola")
|
target_include_directories(bettola_lib PUBLIC "include")
|
||||||
|
|||||||
34
libbettola/include/bettola/game/player_base.h
Normal file
34
libbettola/include/bettola/game/player_base.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "bettola/math/vec3.h"
|
||||||
|
|
||||||
|
namespace BettolaLib { namespace Network { struct PlayerInputMessage; } }
|
||||||
|
|
||||||
|
class PlayerBase {
|
||||||
|
public:
|
||||||
|
struct InputState {
|
||||||
|
bool up = false;
|
||||||
|
bool down = false;
|
||||||
|
bool left = false;
|
||||||
|
bool right = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
PlayerBase(void);
|
||||||
|
|
||||||
|
void update(double dt);
|
||||||
|
void set_velocity_direction(const InputState& input, const BettolaMath::Vec3& cam_front);
|
||||||
|
void set_position(const BettolaMath::Vec3& pos);
|
||||||
|
void set_yaw(float yaw) { _yaw = yaw; }
|
||||||
|
|
||||||
|
unsigned int get_id(void) const { return _id; }
|
||||||
|
const BettolaMath::Vec3& get_position(void) const { return _position; }
|
||||||
|
float get_yaw(void) const { return _yaw; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
unsigned int _id;
|
||||||
|
BettolaMath::Vec3 _position;
|
||||||
|
BettolaMath::Vec3 _velocity;
|
||||||
|
float _yaw;
|
||||||
|
float _speed;
|
||||||
|
};
|
||||||
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
|
|
||||||
namespace BettolaMath {
|
namespace BettolaMath {
|
||||||
struct Mat4 {
|
struct Mat4 {
|
||||||
|
|||||||
50
libbettola/src/bettola/game/player_base.cpp
Normal file
50
libbettola/src/bettola/game/player_base.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include "bettola/game/player_base.h"
|
||||||
|
#include "bettola/network/player_input_message.h"
|
||||||
|
|
||||||
|
/* Use a static variable for ID generation across all player types. */
|
||||||
|
static unsigned int next_player_id = 1;
|
||||||
|
|
||||||
|
PlayerBase::PlayerBase(void) :
|
||||||
|
_id(next_player_id++),
|
||||||
|
_position({0.0f, 0.0f, 0.0f}),
|
||||||
|
_velocity({0.0f, 0.0f, 0.0f}),
|
||||||
|
_yaw(0.0f),
|
||||||
|
_speed(20.0f) {}
|
||||||
|
|
||||||
|
void PlayerBase::update(double dt) {
|
||||||
|
_position.x += _velocity.x * dt;
|
||||||
|
_position.y += _velocity.y * dt;
|
||||||
|
_position.z += _velocity.z * dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerBase::set_position(const BettolaMath::Vec3& pos) {
|
||||||
|
_position = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerBase::set_velocity_direction(const InputState& input, const BettolaMath::Vec3& cam_front) {
|
||||||
|
BettolaMath::Vec3 forward = { cam_front.x, 0.0, cam_front.z };
|
||||||
|
float f_mag = sqrt(forward.x*forward.x + forward.z*forward.z);
|
||||||
|
if(f_mag > 0.0f) {
|
||||||
|
forward.x /= f_mag; forward.z /= f_mag;
|
||||||
|
}
|
||||||
|
|
||||||
|
BettolaMath::Vec3 right = { -forward.z, 0.0f, forward.x };
|
||||||
|
|
||||||
|
BettolaMath::Vec3 move_dir = { 0.0f, 0.0f, 0.0f };
|
||||||
|
if(input.up) move_dir = move_dir + forward;
|
||||||
|
if(input.down) move_dir = move_dir + BettolaMath::Vec3{ -forward.x, 0.0f, -forward.z };
|
||||||
|
if(input.left) move_dir = move_dir + BettolaMath::Vec3{ -right.x, 0.0f, -right.z };
|
||||||
|
if(input.right) move_dir = move_dir + right;
|
||||||
|
|
||||||
|
float move_mag = sqrt(move_dir.x*move_dir.x + move_dir.z*move_dir.z);
|
||||||
|
if(move_mag > 0.0f) {
|
||||||
|
_velocity.x = (move_dir.x / move_mag) * _speed;
|
||||||
|
_velocity.y = 0.0;
|
||||||
|
_velocity.z = (move_dir.z / move_mag) * _speed; /* y velocity is for z-axis. */
|
||||||
|
} else {
|
||||||
|
_velocity.x = 0.0f;
|
||||||
|
_velocity.y = 0.0f;
|
||||||
|
_velocity.z = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "math/mat4.h"
|
#include "bettola/math/mat4.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
|
|
||||||
namespace BettolaMath {
|
namespace BettolaMath {
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include "math/vec2.h"
|
#include "bettola/math/vec2.h"
|
||||||
|
|
||||||
namespace BettolaMath {
|
namespace BettolaMath {
|
||||||
Vec2::Vec2(float x, float y) : x(x), y(y) {}
|
Vec2::Vec2(float x, float y) : x(x), y(y) {}
|
||||||
|
|||||||
@ -114,7 +114,7 @@ void Bettola::update(double dt) {
|
|||||||
_game_client.update_players(dt);
|
_game_client.update_players(dt);
|
||||||
|
|
||||||
/* Update camera to follow the local player. */
|
/* Update camera to follow the local player. */
|
||||||
BettolaMath::Vec3 player_pos = { player.get_x(), 0.0f, player.get_y() };
|
const BettolaMath::Vec3 player_pos = player.get_position();
|
||||||
_camera.update(player_pos);
|
_camera.update(player_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include "bettola/game/player_base.h"
|
||||||
#include "graphics/renderer.h"
|
#include "graphics/renderer.h"
|
||||||
#include "graphics/camera.h"
|
#include "graphics/camera.h"
|
||||||
#include "game_client.h"
|
#include "game_client.h"
|
||||||
@ -31,5 +32,5 @@ private:
|
|||||||
Renderer _renderer;
|
Renderer _renderer;
|
||||||
GameClient _game_client;
|
GameClient _game_client;
|
||||||
Camera _camera;
|
Camera _camera;
|
||||||
Player::InputState _input;
|
PlayerBase::InputState _input;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,54 +1,18 @@
|
|||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "math/vec3.h"
|
#include "bettola/game/player_base.h"
|
||||||
#include "network/player_input_message.h"
|
#include "bettola/math/vec3.h"
|
||||||
|
#include "bettola/network/player_input_message.h"
|
||||||
|
|
||||||
Player::Player(void) :
|
Player::Player(void) :
|
||||||
_x(400.0f),
|
PlayerBase(),
|
||||||
_y(300.0f),
|
|
||||||
_width(50.0f),
|
_width(50.0f),
|
||||||
_height(50.0f),
|
_height(50.0f) {}
|
||||||
_vx(0.0f),
|
|
||||||
_vy(0.0f),
|
|
||||||
_speed(10.0f) {}
|
|
||||||
|
|
||||||
void Player::set_position(float x, float y) {
|
|
||||||
_x = x; _y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Player::update(double dt) {
|
|
||||||
_x += _vx * dt;
|
|
||||||
_y += _vy * dt;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Player::apply_input(const BettolaLib::Network::PlayerInputMessage& input) {
|
void Player::apply_input(const BettolaLib::Network::PlayerInputMessage& input) {
|
||||||
/* Server side replay doesn't have a cam, so we use a default forward vector. */
|
/* Server side replay doesn't have a camera. use a default forward vector. */
|
||||||
BettolaMath::Vec3 fake_cam_front = { 0.0f, 0.0f, -1.0f };
|
BettolaMath::Vec3 face_cam_front = { 0.0f, 0.0f, -1.0f };
|
||||||
InputState state = { input.up, input.down, input.left, input.right };
|
InputState state = { input.up, input.down, input.left, input.right };
|
||||||
set_velocity_direction(state, fake_cam_front);
|
set_velocity_direction(state, face_cam_front);
|
||||||
update(input.dt);
|
update(input.dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::set_velocity_direction(const InputState& input , const BettolaMath::Vec3& cam_front) {
|
|
||||||
BettolaMath::Vec3 forward = { cam_front.x, 0.0f, cam_front.z };
|
|
||||||
float f_mag = sqrt(forward.x*forward.x + forward.z*forward.z);
|
|
||||||
if(f_mag > 0.0f) {
|
|
||||||
forward.x /= f_mag; forward.z /= f_mag;
|
|
||||||
}
|
|
||||||
|
|
||||||
BettolaMath::Vec3 right = { -forward.z, 0.0f, forward.x };
|
|
||||||
|
|
||||||
BettolaMath::Vec3 move_dir = { 0.0f, 0.0f, 0.0f };
|
|
||||||
if(input.up) move_dir = move_dir + forward;
|
|
||||||
if(input.down) move_dir = move_dir + BettolaMath::Vec3{-forward.x, 0.0f, -forward.z};
|
|
||||||
if(input.left) move_dir = move_dir + BettolaMath::Vec3{-right.x, 0.0f, -right.z};
|
|
||||||
if(input.right) move_dir = move_dir + right;
|
|
||||||
|
|
||||||
float move_mag = sqrt(move_dir.x*move_dir.x + move_dir.z*move_dir.z);
|
|
||||||
if(move_mag > 0.0f) {
|
|
||||||
_vx = (move_dir.x / move_mag) * _speed;
|
|
||||||
_vy = (move_dir.z / move_mag) * _speed; /* vy controls Z-axis movement. */
|
|
||||||
} else {
|
|
||||||
_vx = 0.0f; _vy = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,37 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "math/vec3.h"
|
#include "bettola/game/player_base.h"
|
||||||
|
|
||||||
namespace BettolaLib { namespace Network { struct PlayerInputMessage; } }
|
namespace BettolaLib { namespace Network { struct PlayerInputMessage; } }
|
||||||
|
|
||||||
class Player {
|
class Player : public PlayerBase {
|
||||||
public:
|
public:
|
||||||
struct InputState {
|
|
||||||
bool up =false;
|
|
||||||
bool down =false;
|
|
||||||
bool left =false;
|
|
||||||
bool right =false;
|
|
||||||
};
|
|
||||||
|
|
||||||
Player(void);
|
Player(void);
|
||||||
|
|
||||||
void update(double dt);
|
|
||||||
void set_position(float x, float y);
|
|
||||||
void apply_input(const BettolaLib::Network::PlayerInputMessage& input);
|
void apply_input(const BettolaLib::Network::PlayerInputMessage& input);
|
||||||
|
|
||||||
void set_velocity_direction(const InputState& input, const BettolaMath::Vec3& cam_front);
|
|
||||||
|
|
||||||
float get_x() const { return _x; }
|
|
||||||
float get_y() const { return _y; }
|
|
||||||
float get_width() const { return _width; }
|
float get_width() const { return _width; }
|
||||||
float get_height() const { return _height; }
|
float get_height() const { return _height; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _x;
|
|
||||||
float _y;
|
|
||||||
float _width;
|
float _width;
|
||||||
float _height;
|
float _height;
|
||||||
|
|
||||||
float _vx;
|
|
||||||
float _vy;
|
|
||||||
float _speed;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,16 +1,20 @@
|
|||||||
#include "remote_player.h"
|
#include "remote_player.h"
|
||||||
|
|
||||||
RemotePlayer::RemotePlayer(unsigned int id, float x, float y) :
|
RemotePlayer::RemotePlayer(unsigned int id, float x, float y) :
|
||||||
_id(id), _x(x), _y(y), _yaw(0.0f), _target_x(x), _target_y(y) {}
|
PlayerBase(), _target_x(x), _target_y(y), _target_yaw(0.0f) {
|
||||||
|
_id = id; /* Manually set id from the server. */
|
||||||
|
_position = {x, 0.0f, y };
|
||||||
|
}
|
||||||
|
|
||||||
void RemotePlayer::update(double dt) {
|
void RemotePlayer::update(double dt) {
|
||||||
const float interp_speed = 15.0f;
|
const float interp_speed = 15.0f;
|
||||||
|
|
||||||
_x += (_target_x - _x) * interp_speed * dt;
|
_position.x += (_target_x - _position.x) * interp_speed * dt;
|
||||||
_y += (_target_y - _y) * interp_speed * dt;
|
_position.z += (_target_y - _position.z) * interp_speed * dt; /* y is z. */
|
||||||
/* TODO: Snap the yaw, we'll interpolate later if we need. */
|
/* TODO: Snap the yaw, we'll interpolate later if we need. */
|
||||||
|
_yaw = _target_yaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemotePlayer::set_target_position(float x, float y, float yaw) {
|
void RemotePlayer::set_target_position(float x, float y, float yaw) {
|
||||||
_target_x = x; _target_y = y; _yaw = yaw;
|
_target_x = x; _target_y = y; _target_yaw = yaw;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,22 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class RemotePlayer {
|
#include "bettola/game/player_base.h"
|
||||||
|
|
||||||
|
class RemotePlayer : public PlayerBase {
|
||||||
public:
|
public:
|
||||||
RemotePlayer(unsigned int id, float x, float y);
|
RemotePlayer(unsigned int id, float x, float y);
|
||||||
|
|
||||||
void update(double dt);
|
void update(double dt);
|
||||||
void set_target_position(float x, float y, float yaw);
|
void set_target_position(float x, float y, float yaw);
|
||||||
|
|
||||||
unsigned int get_id(void) const { return _id; }
|
|
||||||
float get_x(void) const { return _x; }
|
|
||||||
float get_y(void) const { return _y; }
|
|
||||||
float get_yaw(void) const { return _yaw; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int _id;
|
|
||||||
float _x;
|
|
||||||
float _y;
|
|
||||||
float _yaw;
|
|
||||||
float _target_x;
|
float _target_x;
|
||||||
float _target_y;
|
float _target_y;
|
||||||
|
float _target_yaw;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,10 +6,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "game_client.h"
|
#include "game_client.h"
|
||||||
|
#include "bettola/game/player_base.h"
|
||||||
#include "game/remote_player.h"
|
#include "game/remote_player.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
#include "network/game_state_message.h"
|
#include "bettola/network/game_state_message.h"
|
||||||
#include "network/message.h"
|
#include "bettola/network/message.h"
|
||||||
|
|
||||||
GameClient::GameClient(void) : _our_player_id(0), _input_sequence_number(0) {
|
GameClient::GameClient(void) : _our_player_id(0), _input_sequence_number(0) {
|
||||||
memset(&_server_addr, 0, sizeof(_server_addr));
|
memset(&_server_addr, 0, sizeof(_server_addr));
|
||||||
@ -104,7 +105,7 @@ void GameClient::_process_game_state(const BettolaLib::Network::GameStateMessage
|
|||||||
|
|
||||||
if(ps.player_id == _our_player_id) {
|
if(ps.player_id == _our_player_id) {
|
||||||
/* This is our player. Reconcile. */
|
/* This is our player. Reconcile. */
|
||||||
_player.set_position(ps.x, ps.y);
|
_player.set_position({ps.x, 0.0f, ps.y});
|
||||||
|
|
||||||
/* Remove all inputs from our history that the server has processed. */
|
/* Remove all inputs from our history that the server has processed. */
|
||||||
while(!_pending_inputs.empty() &&
|
while(!_pending_inputs.empty() &&
|
||||||
@ -139,7 +140,8 @@ void GameClient::_process_game_state(const BettolaLib::Network::GameStateMessage
|
|||||||
players_in_message.end(); }), _remote_players.end());
|
players_in_message.end(); }), _remote_players.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameClient::send_input(const Player::InputState& input, const Camera& camera, float dt) {
|
void GameClient::send_input(PlayerBase::InputState& input,
|
||||||
|
const Camera& camera, float dt) {
|
||||||
if(_our_player_id > 0) {
|
if(_our_player_id > 0) {
|
||||||
/* Sent our current input state to the server. */
|
/* Sent our current input state to the server. */
|
||||||
BettolaLib::Network::PlayerInputMessage input_msg;
|
BettolaLib::Network::PlayerInputMessage input_msg;
|
||||||
|
|||||||
@ -7,10 +7,9 @@
|
|||||||
#include "game/player.h"
|
#include "game/player.h"
|
||||||
#include "game/remote_player.h"
|
#include "game/remote_player.h"
|
||||||
#include "graphics/camera.h"
|
#include "graphics/camera.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/network/tcpsocket.h"
|
||||||
#include "network/tcpsocket.h"
|
#include "bettola/network/udpsocket.h"
|
||||||
#include "network/udpsocket.h"
|
#include "bettola/network/player_input_message.h"
|
||||||
#include "network/player_input_message.h"
|
|
||||||
namespace BettolaLib { namespace Network { struct GameStateMessage; } }
|
namespace BettolaLib { namespace Network { struct GameStateMessage; } }
|
||||||
|
|
||||||
class GameClient {
|
class GameClient {
|
||||||
@ -19,7 +18,7 @@ public:
|
|||||||
|
|
||||||
bool connect(const char* host, unsigned short port);
|
bool connect(const char* host, unsigned short port);
|
||||||
void process_network_messages(void);
|
void process_network_messages(void);
|
||||||
void send_input(const Player::InputState& input, const Camera& camera, float dt);
|
void send_input(PlayerBase::InputState& input, const Camera& camera, float dt);
|
||||||
|
|
||||||
const Player& get_player(void) const { return _player; }
|
const Player& get_player(void) const { return _player; }
|
||||||
Player& get_player_for_write(void) { return _player; }
|
Player& get_player_for_write(void) { return _player; }
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "math/mat4.h"
|
#include "bettola/math/mat4.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "math/mat4.h"
|
#include "bettola/math/mat4.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -10,8 +10,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "math/mat4.h"
|
#include "bettola/math/mat4.h"
|
||||||
#include "math/vec3.h" /* Going to need this for the camera. */
|
|
||||||
|
|
||||||
#define GL_CHECK_ERROR() \
|
#define GL_CHECK_ERROR() \
|
||||||
do { \
|
do { \
|
||||||
@ -170,8 +169,9 @@ void Renderer::render(const Camera& camera, const Player& player,
|
|||||||
glUniform3f(color_loc, 0.2f, 0.5f, 0.8f);
|
glUniform3f(color_loc, 0.2f, 0.5f, 0.8f);
|
||||||
|
|
||||||
/* Draw the local player's cube. */
|
/* Draw the local player's cube. */
|
||||||
BettolaMath::Mat4 trans_matrix = BettolaMath::Mat4::translation(player.get_x(),
|
const auto& player_pos = player.get_position();
|
||||||
0.0f, player.get_y());
|
BettolaMath::Mat4 trans_matrix = BettolaMath::Mat4::translation(player_pos.x,
|
||||||
|
player_pos.y, player_pos.z);
|
||||||
BettolaMath::Mat4 rot_matrix = BettolaMath::Mat4::rotation(-camera.get_yaw()-90.0f,
|
BettolaMath::Mat4 rot_matrix = BettolaMath::Mat4::rotation(-camera.get_yaw()-90.0f,
|
||||||
{0.0f,1.0f,0.0f});
|
{0.0f,1.0f,0.0f});
|
||||||
BettolaMath::Mat4 model = trans_matrix.multiply(rot_matrix);
|
BettolaMath::Mat4 model = trans_matrix.multiply(rot_matrix);
|
||||||
@ -185,8 +185,9 @@ void Renderer::render(const Camera& camera, const Player& player,
|
|||||||
/* Draw remote players' cube. */
|
/* Draw remote players' cube. */
|
||||||
for(const auto& remote_player : remote_players) {
|
for(const auto& remote_player : remote_players) {
|
||||||
glBindVertexArray(_vao); /* bind cube VAO for each remote player. */
|
glBindVertexArray(_vao); /* bind cube VAO for each remote player. */
|
||||||
BettolaMath::Mat4 remote_trans = BettolaMath::Mat4::translation(remote_player.get_x(),
|
const auto& remote_pos = remote_player.get_position();
|
||||||
0.0f, remote_player.get_y());
|
BettolaMath::Mat4 remote_trans = BettolaMath::Mat4::translation(remote_pos.x,
|
||||||
|
remote_pos.y, remote_pos.z);
|
||||||
BettolaMath::Mat4 remote_rot = BettolaMath::Mat4::rotation(-remote_player.get_yaw()-90.0f,
|
BettolaMath::Mat4 remote_rot = BettolaMath::Mat4::rotation(-remote_player.get_yaw()-90.0f,
|
||||||
{0.0f,1.0f,0.0f});
|
{0.0f,1.0f,0.0f});
|
||||||
BettolaMath::Mat4 remote_model = remote_trans.multiply(remote_rot);
|
BettolaMath::Mat4 remote_model = remote_trans.multiply(remote_rot);
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "game/player.h"
|
#include "game/player.h"
|
||||||
#include "game/remote_player.h"
|
#include "game/remote_player.h"
|
||||||
#include "math/mat4.h"
|
#include "bettola/math/mat4.h"
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -3,12 +3,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/math/vec3.h"
|
||||||
#include "network/game_state_message.h"
|
#include "bettola/network/game_state_message.h"
|
||||||
#include "network/player_input_message.h"
|
#include "bettola/network/player_input_message.h"
|
||||||
#include "network/tcpsocket.h"
|
#include "bettola/network/tcpsocket.h"
|
||||||
#include "network/message.h"
|
#include "bettola/network/message.h"
|
||||||
#include "network/udpsocket.h"
|
#include "bettola/network/udpsocket.h"
|
||||||
|
|
||||||
Player* Game::add_player(BettolaLib::Network::TCPSocket* socket) {
|
Player* Game::add_player(BettolaLib::Network::TCPSocket* socket) {
|
||||||
_players.push_back(new Player(socket));
|
_players.push_back(new Player(socket));
|
||||||
@ -64,8 +64,9 @@ void Game::broadcast_game_state(BettolaLib::Network::UDPSocket& udp_socket) {
|
|||||||
|
|
||||||
for(size_t i = 0; i < _players.size(); ++i) {
|
for(size_t i = 0; i < _players.size(); ++i) {
|
||||||
msg.players[i].player_id = _players[i]->get_id();
|
msg.players[i].player_id = _players[i]->get_id();
|
||||||
msg.players[i].x = _players[i]->get_x();
|
const auto& pos = _players[i]->get_position();
|
||||||
msg.players[i].y = _players[i]->get_y();
|
msg.players[i].x = pos.x;
|
||||||
|
msg.players[i].y = pos.z; /* Send z as y for the 2D style network message. */
|
||||||
msg.players[i].yaw = _players[i]->get_yaw();
|
msg.players[i].yaw = _players[i]->get_yaw();
|
||||||
msg.players[i].last_processed_sequence = _players[i]->get_last_processed_sequence();
|
msg.players[i].last_processed_sequence = _players[i]->get_last_processed_sequence();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,53 +2,14 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "network/tcpsocket.h"
|
#include "bettola/game/player_base.h"
|
||||||
|
#include "bettola/network/tcpsocket.h"
|
||||||
unsigned int Player::_next_player_id = 0;
|
|
||||||
|
|
||||||
Player::Player(BettolaLib::Network::TCPSocket* socket) :
|
Player::Player(BettolaLib::Network::TCPSocket* socket) :
|
||||||
|
PlayerBase(),
|
||||||
_socket(socket),
|
_socket(socket),
|
||||||
_has_udp_addr(false) {
|
_has_udp_addr(false) {
|
||||||
|
|
||||||
/* Try and set a player id to 0 now you audacious pr.ck! */
|
|
||||||
if(_next_player_id == 0) {
|
|
||||||
_next_player_id = 1;
|
|
||||||
}
|
|
||||||
_id = _next_player_id++;
|
|
||||||
_x = 0.0f; _y = 0.0f;
|
|
||||||
_vx = 0.0f; _vy = 0.0f;
|
|
||||||
_yaw = 0.0f;
|
|
||||||
_speed = 10.0f; /* Must match client! */
|
|
||||||
_last_processed_sequence = 0;
|
_last_processed_sequence = 0;
|
||||||
memset(&_udp_addr, 0, sizeof(_udp_addr));
|
memset(&_udp_addr, 0, sizeof(_udp_addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::update(double dt) {
|
|
||||||
_x += _vx * dt;
|
|
||||||
_y += _vy * dt;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Player::set_velocity_direction(const InputState& input , const BettolaMath::Vec3& cam_front) {
|
|
||||||
BettolaMath::Vec3 forward = { cam_front.x, 0.0f, cam_front.z };
|
|
||||||
float f_mag = sqrt(forward.x*forward.x + forward.z*forward.z);
|
|
||||||
if(f_mag > 0.0f) {
|
|
||||||
forward.x /= f_mag; forward.z /= f_mag;
|
|
||||||
}
|
|
||||||
|
|
||||||
BettolaMath::Vec3 right = { -forward.z, 0.0f, forward.x };
|
|
||||||
|
|
||||||
BettolaMath::Vec3 move_dir = { 0.0f, 0.0f, 0.0f };
|
|
||||||
if(input.up) move_dir = move_dir + forward;
|
|
||||||
if(input.down) move_dir = move_dir + BettolaMath::Vec3{-forward.x, 0.0f, -forward.z};
|
|
||||||
if(input.left) move_dir = move_dir + BettolaMath::Vec3{-right.x, 0.0f, -right.z};
|
|
||||||
if(input.right) move_dir = move_dir + right;
|
|
||||||
|
|
||||||
float move_mag = sqrt(move_dir.x*move_dir.x + move_dir.z*move_dir.z);
|
|
||||||
if(move_mag > 0.0f) {
|
|
||||||
_vx = (move_dir.x / move_mag) * _speed;
|
|
||||||
_vy = (move_dir.z / move_mag) * _speed; /* vy controls Z-axis movement. */
|
|
||||||
} else {
|
|
||||||
_vx = 0.0f; _vy = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@ -3,47 +3,25 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include "bettola/network/tcpsocket.h"
|
#include "bettola/network/tcpsocket.h"
|
||||||
#include "math/vec3.h"
|
#include "bettola/game/player_base.h"
|
||||||
|
|
||||||
class Player {
|
class Player : public PlayerBase {
|
||||||
public:
|
public:
|
||||||
struct InputState {
|
|
||||||
bool up;
|
|
||||||
bool down;
|
|
||||||
bool left;
|
|
||||||
bool right;
|
|
||||||
};
|
|
||||||
|
|
||||||
Player(BettolaLib::Network::TCPSocket* socket);
|
Player(BettolaLib::Network::TCPSocket* socket);
|
||||||
|
|
||||||
void update(double dt);
|
|
||||||
void set_velocity_direction(const InputState& input, const BettolaMath::Vec3& cam_front);
|
|
||||||
void set_position(float x, float y) { _x = x; _y = y; }
|
|
||||||
void set_yaw(float yaw) { _yaw = yaw; }
|
|
||||||
void set_udp_addr(const sockaddr_in& addr) { _udp_addr = addr; _has_udp_addr = true; }
|
void set_udp_addr(const sockaddr_in& addr) { _udp_addr = addr; _has_udp_addr = true; }
|
||||||
|
|
||||||
unsigned int get_id(void) const { return _id; }
|
|
||||||
float get_x(void) const { return _x; }
|
|
||||||
float get_y(void) const { return _y; }
|
|
||||||
float get_yaw(void) const { return _yaw; }
|
|
||||||
|
|
||||||
BettolaLib::Network::TCPSocket& get_socket(void) const { return *_socket; }
|
BettolaLib::Network::TCPSocket& get_socket(void) const { return *_socket; }
|
||||||
const sockaddr_in& get_udp_addr(void) const { return _udp_addr; }
|
const sockaddr_in& get_udp_addr(void) const { return _udp_addr; }
|
||||||
bool has_udp_addr(void) const { return _has_udp_addr; }
|
bool has_udp_addr(void) const { return _has_udp_addr; }
|
||||||
|
|
||||||
void set_last_processed_sequence(unsigned int sequence) { _last_processed_sequence = sequence; }
|
void set_last_processed_sequence(unsigned int sequence) { _last_processed_sequence = sequence; }
|
||||||
unsigned int get_last_processed_sequence(void) const { return _last_processed_sequence; }
|
unsigned int get_last_processed_sequence(void) const { return _last_processed_sequence; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static unsigned int _next_player_id;
|
|
||||||
|
|
||||||
unsigned int _id;
|
|
||||||
float _x;
|
|
||||||
float _y;
|
|
||||||
float _yaw;
|
|
||||||
BettolaLib::Network::TCPSocket* _socket;
|
BettolaLib::Network::TCPSocket* _socket;
|
||||||
sockaddr_in _udp_addr;
|
sockaddr_in _udp_addr;
|
||||||
bool _has_udp_addr;
|
bool _has_udp_addr;
|
||||||
unsigned int _last_processed_sequence;
|
unsigned int _last_processed_sequence;
|
||||||
float _vx, _vy;
|
|
||||||
float _speed;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user