This commit is contained in:
aozhiwei 2019-03-21 16:37:55 +08:00
parent 122c6286a0
commit 89887d287a
11 changed files with 143 additions and 13 deletions

View File

@ -60,7 +60,7 @@ static void SavePerfLog()
void App::Init(int argc, char* argv[])
{
#if 1
#if 0
{
Vector2D v1(1, 1);
Vector2D v2 = v1.Rotate(-0.25);
@ -359,13 +359,11 @@ void App::ProcessGameGateMsg(f8::MsgHdr& hdr)
break;
case HID_Player:
{
#if 0
Player* hum = PlayerMgr::Instance()->GetPlayerBySocket(hdr.socket_handle);
if (hum) {
hdr.hum = hum;
ProcessNetMsg(handler, hum, hdr);
}
#endif
}
break;
}

View File

@ -45,8 +45,6 @@ class Human : public Entity
HumanFrameData frame_data;
bool moving = false;
std::set<Human*> my_seen_players;
std::set<Human*> seen_me_players;
std::set<Human*> new_players;

View File

@ -6,6 +6,7 @@
#include "room.h"
#include "metamgr.h"
#include "movement.h"
#include "bullet.h"
const int F_del_objids = 2;
const int F_full_objects = 3;
@ -51,7 +52,13 @@ void Player::Initialize()
void Player::Update(int delta_time)
{
movement->Update(delta_time);
if (moving) {
UpdateMove();
}
if (updated_times % 2 == 0) {
if (shot_start || shot_hold) {
UpdateShot();
}
MakeUpdateMsg();
SendNotifyMsg(*update_msg);
{
@ -62,9 +69,93 @@ void Player::Update(int delta_time)
}
}
void Player::UpdateMove()
{
++moved_frames;
if (moved_frames > 4) {
moving = false;
moved_frames = 0;
return;
}
}
void Player::UpdateShot()
{
if (shot_start) {
shot_start = false;
Shot();
return;
}
if (shot_hold) {
++series_shot_frames;
if (series_shot_frames > 4) {
shot_hold = false;
series_shot_frames = 0;
Shot();
}
}
}
void Player::Shot()
{
if (!weapon_meta) {
return;
}
{
cs::MFShot* shot = room->frame_data.shots.Add();
shot->set_player_id(entity_uniid);
shot->set_weapon_id(weapon_meta->i->id());
shot->set_offhand(true);
shot->set_bullskin(10001);
}
{
cs::MFBullet* bullet = room->frame_data.bullets.Add();
bullet->set_player_id(entity_uniid);
bullet->set_bullet_id(weapon_meta->i->use_bullet());
pos.ToPB(bullet->mutable_pos());
attack_dir.ToPB(bullet->mutable_dir());
bullet->set_bulletskin(10001);
}
{
Bullet* bullet = new Bullet();
bullet->player = this;
bullet->room = room;
bullet->gun_meta = weapon_meta;
bullet->meta = MetaMgr::Instance()->GetEquip(weapon_meta->i->use_bullet());
bullet->pos = pos;
bullet->dir = attack_dir;
bullet->born_pos = pos;
bullet->born_dir = attack_dir;
bullet->entity_uniid = bullet->room->AllocUniid();
bullet->Initialize();
room->AddBullet(bullet);
}
}
void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg)
{
if (msg.has_move_dir()) {
move_dir.FromPB(&msg.move_dir());
move_dir.Normalize();
}
moving = msg.has_move_dir();
if (msg.has_attack_dir()) {
attack_dir.FromPB(&msg.attack_dir());
attack_dir.Normalize();
} else {
if (moving) {
attack_dir = move_dir;
}
}
if (moving) {
moved_frames = 0;
}
shot_start = msg.shot_start();
shot_hold = msg.shot_hold();
if (shot_hold) {
series_shot_frames = 0;
}
}
void Player::_CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg)
@ -84,7 +175,18 @@ void Player::_CMSpectate(f8::MsgHdr& hdr, const cs::CMSpectate& msg)
void Player::_CMVoice(f8::MsgHdr& hdr, const cs::CMVoice& msg)
{
cs::SMVoiceNotify notifymsg;
notifymsg.set_mode(msg.mode());
notifymsg.set_account_id(account_id);
notifymsg.set_msg(msg.msg());
auto send_func = [] (Player* hum, a8::XParams& param)
{
cs::SMVoiceNotify* msg = (cs::SMVoiceNotify*)param.sender.GetUserData();
hum->SendNotifyMsg(*msg);
};
room->TouchPlayerList(a8::XParams()
.SetSender(&notifymsg),
send_func);
}
void Player::FillMFPlayerData(cs::MFPlayerData* player_data)

View File

@ -28,6 +28,13 @@ class Player : public Human
bool use_touch = false;
std::string avatar_url;
bool moving = false;
int moved_frames = 0;
bool shot_start = false;
bool shot_hold = false;
int series_shot_frames = 0;
template <typename T>
void SendNotifyMsg(T& msg)
{
@ -38,6 +45,9 @@ class Player : public Human
virtual ~Player() override;
virtual void Initialize() override;
virtual void Update(int delta_time) override;
void UpdateMove();
void UpdateShot();
void Shot();
void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg);
void _CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg);

View File

@ -19,7 +19,7 @@ void PlayerMgr::Update()
}
Player* PlayerMgr::GetPlayerSocket(int socket)
Player* PlayerMgr::GetPlayerBySocket(int socket)
{
auto itr = socket_hash_.find(socket);
return itr != socket_hash_.end() ? itr->second : nullptr;

View File

@ -20,7 +20,7 @@ class PlayerMgr : public a8::Singleton<PlayerMgr>
void UnInit();
void Update();
Player* GetPlayerSocket(int socket);
Player* GetPlayerBySocket(int socket);
Player* CreatePlayerByCMJoin(unsigned short obj_uniid, const cs::CMJoin& msg);
private:

View File

@ -247,3 +247,16 @@ void Room::ClearDeletedObjects()
}
}
}
void Room::TouchPlayerList(a8::XParams param,
std::function<void (Player*, a8::XParams&)> func)
{
if (!func) {
return;
}
for (auto& pair : accountid_hash_) {
if (pair.second) {
func(pair.second, param);
}
}
}

View File

@ -51,6 +51,8 @@ public:
void AddDeletedObject(unsigned short obj_uniid);
void FillSMJoinedNotify(Player* self_hum, cs::SMJoinedNotify& msg);
void ResetFrameData();
void TouchPlayerList(a8::XParams param,
std::function<void (Player*, a8::XParams&)> func);
private:
void ClearDeletedObjects();

View File

@ -16,6 +16,12 @@ void Vector2D::ToPB(cs::MFVector2D* pb_obj)
pb_obj->set_y(y);
}
void Vector2D::FromPB(const cs::MFVector2D* pb_obj)
{
x = pb_obj->x();
y = pb_obj->y();
}
Vector2D& Vector2D::Normalize()
{
Eigen::Vector2f v(x, y);

View File

@ -22,6 +22,7 @@ struct Vector2D
Vector2D(float _x = 0.0f, float _y = 0.0f):x(_x), y(_y) {};
void ToPB(cs::MFVector2D* pb_obj);
void FromPB(const cs::MFVector2D* pb_obj);
Vector2D& Normalize();
bool operator == (const Vector2D& b) const;

View File

@ -479,8 +479,8 @@ message CMMove
optional MFVector2D move_dir = 24; //-
optional MFVector2D attack_dir = 20; //()
optional bool shoot_start = 6; //-
optional bool shoot_hold = 7; //-
optional bool shot_start = 6; //-
optional bool shot_hold = 7; //-
optional bool reload = 8; //
optional bool equip_primary = 10; //1
@ -530,7 +530,7 @@ message CMSpectate
message CMVoice
{
optional int32 mode = 1; //
optional string msg = 2; //
optional bytes msg = 2; //
}
//endcmmsg
@ -634,5 +634,5 @@ message SMVoiceNotify
{
optional int32 mode = 1; //
optional string account_id = 2; //id
optional string msg = 3; //
optional bytes msg = 3; //
}