完善协议

This commit is contained in:
aozhiwei 2019-03-14 13:52:42 +08:00
parent db26e044ca
commit e8320d9c5e
10 changed files with 168 additions and 63 deletions

View File

@ -0,0 +1,8 @@
#include "precompile.h"
#include "entity.h"
void Entity::Update(int delta_time)
{
}

View File

@ -0,0 +1,25 @@
#pragma once
enum EntityType_e
{
ET_Player = 1,
ET_Obstacle = 2,
ET_Building = 3,
ET_LootSpawner = 4,
ET_Loot = 5,
ET_DeadBody = 6,
ET_Decal = 7,
ET_Projectile = 8,
ET_Smoke = 9
};
class Entity
{
public:
unsigned short entity_uniid = 0;
Vector2D pos;
Vector2D dir;
int updated_times = 0;
virtual void Update(int delta_time);
};

View File

@ -3,10 +3,26 @@
#include "playermgr.h"
#include "player.h"
#include "cs_proto.pb.h"
#include "room.h"
void Player::Update(int delta_time)
{
if (updated_times % 2 == 0) {
cs::SMUpdate msg;
if (updated_times == 0) {
msg.set_active_player_id(entity_uniid);
msg.set_alive_count(room->AliveCount());
{
int data_flags32 = 0;
a8::SetBitFlag(data_flags32, 5);
a8::SetBitFlag(data_flags32, 15);
msg.set_data_flags32(data_flags32);
}
}
SendNotifyMsg(msg);
}
}
void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg)

View File

@ -1,5 +1,8 @@
#pragma once
#include "entity.h"
#include "GGListener.h"
namespace cs
{
class CMMove;
@ -10,12 +13,13 @@ namespace cs
}
class Room;
class Player
class Player : public Entity
{
public:
enum { HID = HID_Player };
public:
int socket_handle = 0;
Room* room = nullptr;
std::string account_id;
std::string team_uniid;
@ -25,7 +29,6 @@ class Player
bool use_touch = false;
std::string avatar_url;
unsigned short obj_uniid = 0;
float health = 0.0;
bool dead = false;
bool downed = false;
@ -41,7 +44,13 @@ class Player
int vip = 0;
int sdmg = 0;
void Update(int delta_time);
template <typename T>
void SendNotifyMsg(T& msg)
{
GGListener::Instance()->SendToClient(socket_handle, 0, msg);
}
virtual void Update(int delta_time) override;
void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg);
void _CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg);

View File

@ -28,7 +28,7 @@ Player* PlayerMgr::GetPlayerSocket(int socket)
Player* PlayerMgr::CreatePlayerByCMJoin(unsigned short obj_uniid, const cs::CMJoin& msg)
{
Player* hum = new Player();
hum->obj_uniid = obj_uniid;
hum->entity_uniid = obj_uniid;
hum->account_id = msg.account_id();
hum->health = 100;
hum->team_uniid = msg.team_uuid();

View File

@ -9,8 +9,13 @@ const int ROOM_MAX_PLAYER_NUM = 50;
void Room::Update(int delta_time)
{
for (auto& pair : uniid_hash_) {
pair.second->Update(delta_time);
elapsed_time_ += delta_time;
while (elapsed_time_ >= 50) {
for (auto& pair : uniid_hash_) {
pair.second->Update(50);
pair.second->updated_times++;
}
elapsed_time_ -= 50;
}
}
@ -36,10 +41,17 @@ Player* Room::GetPlayerByUniId(unsigned short uniid)
return itr != uniid_hash_.end() ? itr->second : nullptr;
}
int Room::AliveCount()
{
return accountid_hash_.size();
}
void Room::AddPlayer(Player* hum)
{
hum->pos.x = 100 + rand() % 100;
hum->pos.y = 200 + rand() % 200;
hum->room = this;
uniid_hash_[hum->obj_uniid] = hum;
uniid_hash_[hum->entity_uniid] = hum;
accountid_hash_[hum->account_id] = hum;
}

View File

@ -22,6 +22,7 @@ public:
void Update(int delta_time);
bool IsFull();
int GetPlayerNum();
int AliveCount();
Player* GetPlayerByAccountId(const std::string& accountid);
Player* GetPlayerByUniId(unsigned short uniid);
void AddPlayer(Player* hum);
@ -30,6 +31,7 @@ public:
private:
unsigned short current_uniid = 0;
RoomState_e state_ = RS_Inactive;
int elapsed_time_ = 0;
std::map<std::string, Player*> accountid_hash_;
std::map<unsigned short, Player*> uniid_hash_;

View File

@ -41,12 +41,13 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
}
unsigned short new_uniid = room->AllocUniid();
Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(new_uniid, msg);
hum->socket_handle = hdr.socket_handle;
room->AddPlayer(hum);
{
cs::SMJoinedNotify notifymsg;
notifymsg.set_team_mode(msg.team_mode());
notifymsg.set_player_id(hum->obj_uniid);
notifymsg.set_player_id(hum->entity_uniid);
notifymsg.set_started(false);
GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg);
}

View File

@ -9,9 +9,8 @@ struct PerfMonitor
long long read_count = 0;
};
#define PB_REPEATED_PTR(type) ::google::protobuf::RepeatedPtrField<type>
namespace GameData
struct Vector2D
{
}
float x = 0.0;
float y = 0.0;
};

View File

@ -24,7 +24,28 @@ package cs;
0x01 == 1<<0
0x02 == 1<<1
0x04 == 1<<2
*/
data_flags字段的意义
protobuf的二义性无法描述repeated字段是否有值(repeated的字段可以通过msg.xxx == null来a判断)
data_flags这样的字段用来描述字段是否有值
data_flags32id 1-31
data_flags64: id 31-63
data_flags128......
message MFRoleInfo
{
optional int32 role_id = 1;
optional int32 role_name = 2;
optional int32 role_level = 34;
optional uint32 data_flags32 = 256;
optional uint32 data_flags64 = 257;
}
role_id是否赋值 (data_flags32 & (1<<(1 - 1))) != 0
role_name是否赋值 (data_flags32 & (1<<(2 - 1))) != 0
role_level是否赋值 (data_flags64 & (1<<(34 - 33)) != 0
protobuf反射得到字段名和字段id的对应关系做到自动化判断
*/
//
message CMPing
@ -114,21 +135,24 @@ message MFPlayerPart
message MFPlayerFull
{
optional int32 obj_uniid = 1; //id
optional float health = 2; //
optional bool dead = 3; //
optional bool downed = 4; //
optional bool disconnected = 5; //
optional int32 anim_type = 6; //
optional int32 anim_seq = 7; //
optional int32 action_type = 8; //
optional int32 skin = 9; //id
optional MFVector2D pos = 2; //
optional MFVector2D dir = 3; //
optional float health = 6; //
optional bool dead = 7; //
optional bool downed = 8; //
optional bool disconnected = 9; //
optional int32 anim_type = 10; //
optional int32 anim_seq = 11; //
optional int32 action_type = 12; //
optional int32 skin = 13; //id
//backpack
optional int32 helmet = 10; //
optional int32 chest = 11; //
optional int32 weapon = 12; //
optional int32 energy_shield = 13; //
optional int32 vip = 14; //vip
optional int32 sdmg = 15;
optional int32 helmet = 16; //
optional int32 chest = 17; //
optional int32 weapon = 18; //
optional int32 energy_shield = 19; //
optional int32 vip = 20; //vip
optional int32 sdmg = 21;
}
//-
@ -143,19 +167,22 @@ message MFObstaclePart
message MFObstacleFull
{
optional int32 obj_uniid = 1; //id
optional int32 obstacle_id = 2; //id
optional float health = 3; //
optional bool dead = 4; //
optional MFVector2D pos = 2; //
optional float scale = 3; //
optional bool is_door = 5; //
optional int32 door_relative_ori = 6; //
optional bool door_can_use = 7; //
optional int32 door_seq = 8;
optional int32 obstacle_id = 6; //id
optional float health = 7; //
optional bool dead = 8; //
optional bool is_button = 10;
optional bool button_onoff = 11;
optional bool button_can_use = 12;
optional int32 button_seq = 13;
optional bool is_door = 9; //
optional int32 door_relative_ori = 10; //
optional bool door_can_use = 11; //
optional int32 door_seq = 12;
optional bool is_button = 15;
optional bool button_onoff = 16;
optional bool button_can_use = 17;
optional int32 button_seq = 18;
}
//-
@ -171,26 +198,27 @@ message MFBuildingPart
message MFBuildingFull
{
optional int32 obj_uniid = 1; //id
optional int32 building_id = 2; //id
optional MFVector2D pos = 3; //
optional int32 ori = 4;
optional bool ceiling_dead = 5;
optional MFVector2D pos = 2; //
optional int32 building_id = 3; //id
optional int32 ori = 4; //
optional bool ceiling_dead = 6;
}
//loot出生点-
message MFLootSpawnerPart
{
optional int32 obj_uniid = 1; //id
optional int32 loot_id = 2; //id
optional MFVector2D pos = 3; //
optional MFVector2D pos = 2; //
optional int32 loot_id = 3; //id
}
//loot出生点-
message MFLootSpawnerFull
{
optional int32 obj_uniid = 1; //id
optional int32 loot_id = 2; //id
optional MFVector2D pos = 3; //
optional MFVector2D pos = 2; //
optional int32 loot_id = 3; //id
}
//loot-
@ -204,9 +232,11 @@ message MFLootPart
message MFLootFull
{
optional int32 obj_uniid = 1; //id
optional string name = 2;
optional int32 count = 3;
optional int32 age_ms = 4;
optional MFVector2D pos = 2; //
optional string name = 6;
optional int32 count = 7;
optional int32 age_ms = 8;
}
//-
@ -220,24 +250,25 @@ message MFDeadBodyPart
message MFDeadBodyFull
{
optional int32 obj_uniid = 1; //id
optional int32 player_id = 2; //id
optional int32 inkjet = 3;
optional MFVector2D pos = 2; //
optional int32 inkjet = 6;
}
//decal-
message MFDecalPart
{
optional int32 obj_uniid = 1; //id
optional int32 decal_id = 2; //id
optional MFVector2D pos = 3; //
optional MFVector2D pos = 2; //
optional int32 decal_id = 3; //id
}
//decal-
message MFDecalFull
{
optional int32 obj_uniid = 1; //id
optional int32 decal_id = 2; //id
optional MFVector2D pos = 3; //
optional MFVector2D pos = 2; //
optional int32 decal_id = 3; //id
}
//-
@ -542,7 +573,7 @@ message CMVoice
message SMJoinedNotify
{
optional int32 team_mode = 1; // 0: 1:
optional int32 player_id = 2; //id
optional int32 player_id = 2; //id()
optional bool started = 3; //
repeated MFPlayerInfo player_infos = 4; //
@ -557,7 +588,7 @@ message SMMapInfo
optional int32 width = 2; //
optional int32 height = 3; //
optional int32 seed = 4; //
repeated MFPlace places = 5; //
repeated MFPlace places = 5; //
repeated MFMapObject objects = 6; //
}
@ -570,11 +601,11 @@ message SMPlayerInfo
//
message SMUpdate
{
repeated int32 del_objids = 1;
repeated MFObjectFull full_objects = 2;
repeated MFObjectPart part_objects = 3;
optional int32 active_player_id = 10; //id
optional MFPlayerData active_player_data = 11; //
repeated int32 del_objids = 2;
repeated MFObjectFull full_objects = 3;
repeated MFObjectPart part_objects = 4;
optional int32 active_player_id = 5; //id
optional MFPlayerData active_player_data = 6; //
optional int32 alive_count = 15; //
optional int32 gasT = 16;
optional MFGasData gas_data = 17;
@ -585,6 +616,8 @@ message SMUpdate
repeated MFExplosion explosions = 22;
repeated MFEmote emotes = 23;
optional int32 ack = 24;
optional uint32 data_flags32 = 256;
}
//xx