drop weapon ok

This commit is contained in:
aozhiwei 2019-04-02 16:34:13 +08:00
parent 061a45c188
commit a72692b860
6 changed files with 76 additions and 62 deletions

View File

@ -46,7 +46,6 @@ void HandlerMgr::RegisterNetMsgHandlers()
RegisterNetMsgHandler(&ggmsghandler, &RoomMgr::_CMJoin);
RegisterNetMsgHandler(&ggmsghandler, &Player::_CMMove);
RegisterNetMsgHandler(&ggmsghandler, &Player::_CMDropItem);
RegisterNetMsgHandler(&ggmsghandler, &Player::_CMEmote);
RegisterNetMsgHandler(&ggmsghandler, &Player::_CMSpectate);
RegisterNetMsgHandler(&ggmsghandler, &Player::_CMVoice);

View File

@ -50,6 +50,8 @@ class Human : public Entity
std::vector<Weapon> weapons;
Weapon* curr_weapon = nullptr;
std::vector<int> inventory;
HumanFrameData frame_data;
std::set<Human*> my_seen_players;

View File

@ -85,6 +85,9 @@ void Player::Update(int delta_time)
if (select_weapon) {
UpdateSelectWeapon();
}
if (drop_weapon) {
UpdateDropWeapon();
}
MakeUpdateMsg();
SendNotifyMsg(*update_msg);
{
@ -348,76 +351,81 @@ void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg)
select_weapon = true;
selected_weapon_idx = msg.select_weapon();
}
if (msg.has_drop_weapon()) {
drop_weapon = true;
drop_weapon_idx = msg.drop_weapon();
}
}
void Player::_CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg)
void Player::UpdateDropWeapon()
{
if (msg.has_weapon_idx()) {
if (msg.weapon_idx() >= 0 && msg.weapon_idx() < weapons.size()) {
bool drop_ok = false;
Weapon* weapon = &weapons[msg.weapon_idx()];
int weapon_id = weapon->weapon_id;
if (weapon->weapon_id != 0) {
if (weapon->weapon_idx == 0) {
if (weapon->weapon_id != default_weapon.weapon_id) {
drop_ok = true;
*weapon = default_weapon;
}
} else if (weapon->weapon_idx == GUN_SLOT1) {
if (drop_weapon_idx >= 0 && drop_weapon_idx < weapons.size()) {
bool drop_ok = false;
Weapon* weapon = &weapons[drop_weapon_idx];
int weapon_id = weapon->weapon_id;
if (weapon->weapon_id != 0) {
if (weapon->weapon_idx == 0) {
if (weapon->weapon_id != default_weapon.weapon_id) {
drop_ok = true;
*weapon = Weapon();
weapon->weapon_idx = msg.weapon_idx();
if (curr_weapon == weapon) {
if (weapons[GUN_SLOT2].weapon_id != 0) {
curr_weapon = &weapons[GUN_SLOT2];
} else {
curr_weapon = &weapons[0];
*weapon = default_weapon;
}
} else if (weapon->weapon_idx == GUN_SLOT1) {
drop_ok = true;
*weapon = Weapon();
weapon->weapon_idx = drop_weapon_idx;
if (curr_weapon == weapon) {
if (weapons[GUN_SLOT2].weapon_id != 0) {
curr_weapon = &weapons[GUN_SLOT2];
} else {
curr_weapon = &weapons[0];
}
}
} else if (weapon->weapon_idx == GUN_SLOT2) {
drop_ok = true;
*weapon = Weapon();
weapon->weapon_idx = drop_weapon_idx;
if (curr_weapon == weapon) {
if (weapons[GUN_SLOT1].weapon_id != 0) {
curr_weapon = &weapons[GUN_SLOT1];
} else {
curr_weapon = &weapons[0];
}
}
}
if (drop_ok) {
#if 1
{
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(weapon_id);
if (equip_meta) {
Loot* entity = new Loot();
entity->room = room;
entity->meta = equip_meta;
entity->entity_uniid = room->AllocUniid();
{
Vector2D dir = Vector2D::UP;
dir.Rotate(a8::RandAngle());
entity->pos = pos + dir * (5 + rand() % 50);
}
}
} else if (weapon->weapon_idx == GUN_SLOT2) {
drop_ok = true;
*weapon = Weapon();
weapon->weapon_idx = msg.weapon_idx();
if (curr_weapon == weapon) {
if (weapons[GUN_SLOT1].weapon_id != 0) {
curr_weapon = &weapons[GUN_SLOT1];
} else {
curr_weapon = &weapons[0];
entity->item_id = weapon_id;
entity->count = 1;
entity->Initialize();
room->uniid_hash_[entity->entity_uniid] = entity;
for (auto& pair : room->human_hash_) {
pair.second->new_objects.insert(entity);
pair.second->part_objects.insert(entity);
}
}
}
if (drop_ok) {
#if 1
{
MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(weapon_id);
if (equip_meta) {
Loot* entity = new Loot();
entity->room = room;
entity->meta = equip_meta;
entity->entity_uniid = room->AllocUniid();
{
Vector2D dir = Vector2D::UP;
dir.Rotate(a8::RandAngle());
entity->pos = pos + dir * (5 + rand() % 50);
}
entity->item_id = weapon_id;
entity->count = 1;
entity->Initialize();
room->uniid_hash_[entity->entity_uniid] = entity;
for (auto& pair : room->human_hash_) {
pair.second->new_objects.insert(entity);
pair.second->part_objects.insert(entity);
}
}
}
#endif
for (auto& pair : room->human_hash_) {
pair.second->new_objects.insert(this);
}
#endif
need_sync_active_player = true;
for (auto& pair : room->human_hash_) {
pair.second->new_objects.insert(this);
}
}
}
}
drop_weapon = false;
drop_weapon_idx;
}
void Player::_CMEmote(f8::MsgHdr& hdr, const cs::CMEmote& msg)
@ -454,6 +462,9 @@ void Player::FillMFActivePlayerData(cs::MFActivePlayerData* player_data)
auto p = player_data->add_weapons();
weapon.ToPB(p);
}
for (auto& num : inventory) {
player_data->add_inventory(num);
}
}
void Player::FillMFGasData(cs::MFGasData* gas_data)

View File

@ -42,6 +42,9 @@ class Player : public Human
bool select_weapon = false;
int selected_weapon_idx = 0;
bool drop_weapon = false;
int drop_weapon_idx = 0;
bool need_sync_active_player = false;
::google::protobuf::RepeatedField< ::google::protobuf::int32 > interaction_objids;
@ -59,13 +62,13 @@ class Player : public Human
void UpdateMove();
void UpdateShot();
void UpdateSelectWeapon();
void UpdateDropWeapon();
void Shot();
void ProcInteraction();
void ObstacleInteraction(Obstacle* entity);
void LootInteraction(Loot* entity);
void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg);
void _CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg);
void _CMEmote(f8::MsgHdr& hdr, const cs::CMEmote& msg);
void _CMSpectate(f8::MsgHdr& hdr, const cs::CMSpectate& msg);
void _CMVoice(f8::MsgHdr& hdr, const cs::CMVoice& msg);
@ -76,7 +79,6 @@ class Player : public Human
private:
cs::SMUpdate* update_msg = nullptr;
long long last_sync_gas_frameno = 0;
std::vector<int> inventory;
void MakeUpdateMsg();
};

View File

@ -7,7 +7,6 @@ enum CMMessageId_e
_CMJoin = 103;
_CMMove = 201;
_CMDropItem = 203;
_CMEmote = 204;
_CMSpectate = 205;
_CMVoice = 206;

View File

@ -518,6 +518,7 @@ message CMMove
optional bool reload = 8; //
optional int32 select_weapon = 10; //()
optional int32 drop_weapon = 11; //
optional bool cancel_action = 15; //zz
//optional bool edit_mode = 16; //