#ifndef BLANK_CLIENT_INTERACTIVESTATE_HPP_
#define BLANK_CLIENT_INTERACTIVESTATE_HPP_
+#include "../app/IntervalTimer.hpp"
#include "../app/State.hpp"
#include "../io/WorldSave.hpp"
#include "../model/Skeletons.hpp"
Interface interface;
ChunkRenderer chunk_renderer;
Skeletons skeletons;
+ IntervalTimer update_timer;
};
world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
)
, chunk_renderer(*interface.GetPlayer().chunks)
-, skeletons() {
+, skeletons()
+, update_timer(16) {
TextureIndex tex_index;
master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
skeletons.Load();
// TODO: better solution for initializing HUD
interface.SelectNext();
+ update_timer.Start();
}
void InteractiveState::OnEnter() {
world.Update(dt);
chunk_renderer.Update(dt);
+ update_timer.Update(dt);
+
Entity &player = *interface.GetPlayer().entity;
- master.GetClient().SendPlayerUpdate(player);
+ if (update_timer.Hit()) {
+ master.GetClient().SendPlayerUpdate(player);
+ }
glm::mat4 trans = player.Transform(player.ChunkCoords());
glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
#define BLANK_NET_CLIENT_HPP_
#include "Connection.hpp"
-#include "../app/IntervalTimer.hpp"
#include <string>
#include <SDL_net.h>
std::uint16_t SendPing();
std::uint16_t SendLogin(const std::string &);
std::uint16_t SendPart();
- // this may not send the update at all, in which case it returns -1
- int SendPlayerUpdate(const Entity &);
+ std::uint16_t SendPlayerUpdate(const Entity &);
private:
void HandlePacket(const UDPpacket &);
Connection conn;
UDPsocket client_sock;
UDPpacket client_pack;
- IntervalTimer update_timer;
};
Client::Client(const Config &conf)
: conn(client_resolve(conf.host.c_str(), conf.port))
, client_sock(client_bind(0))
-, client_pack{ -1, nullptr, 0 }
-, update_timer(16) {
+, client_pack{ -1, nullptr, 0 } {
client_pack.data = new Uint8[sizeof(Packet)];
client_pack.maxlen = sizeof(Packet);
// establish connection
SendPing();
- update_timer.Start();
}
Client::~Client() {
}
void Client::Update(int dt) {
- update_timer.Update(dt);
conn.Update(dt);
if (conn.ShouldPing()) {
SendPing();
return conn.Send(client_pack, client_sock);
}
-int Client::SendPlayerUpdate(const Entity &player) {
- // don't send all too many updates
- if (!update_timer.Hit()) return -1;
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
pack.WritePlayer(player);
return conn.Send(client_pack, client_sock);