entity add initialize function

This commit is contained in:
aozhiwei 2019-03-15 11:12:27 +08:00
parent 971bc22b85
commit 6fc58d553a
9 changed files with 78 additions and 2 deletions

View File

@ -1,3 +1,14 @@
#include "precompile.h"
#include "android.h"
#include "metamgr.h"
Android::Android()
{
entity_subtype = EST_Android;
}
void Android::Initialize()
{
health = meta->i->health();
}

View File

@ -4,5 +4,8 @@
class Android : public Human
{
public:
Android();
virtual void Initialize() override;
};

View File

@ -20,17 +20,26 @@ enum EntityType_e
ET_Smoke = 9
};
enum EntitySubType_e
{
EST_None = 0,
EST_Player = 1,
EST_Android = 2,
};
class Room;
class Entity
{
public:
unsigned short entity_uniid = 0;
EntityType_e entity_type = ET_None;
EntitySubType_e entity_subtype = EST_None;
Room* room = nullptr;
Vector2D pos;
Vector2D dir;
int updated_times = 0;
virtual void Initialize() {};
virtual void Update(int delta_time) {};
virtual void FillMFObjectPart(cs::MFObjectPart* part_data) {};
virtual void FillMFObjectFull(cs::MFObjectFull* full_data) {};

View File

@ -2,10 +2,16 @@
#include "entity.h"
namespace MetaData
{
struct Player;
}
class Human : public Entity
{
public:
std::string team_uniid;
MetaData::Player* meta = nullptr;
float health = 0.0;
bool dead = false;

View File

@ -4,6 +4,7 @@
#include "player.h"
#include "cs_proto.pb.h"
#include "room.h"
#include "metamgr.h"
const int F_del_objids = 2;
const int F_full_objects = 3;
@ -21,6 +22,16 @@ const int F_explosions = 22;
const int F_emotes = 23;
const int F_ack = 24;
Player::Player()
{
entity_subtype = EST_Player;
}
void Player::Initialize()
{
health = meta->i->health();
}
void Player::Update(int delta_time)
{
if (updated_times % 2 == 0) {

View File

@ -34,6 +34,8 @@ class Player : public Human
GGListener::Instance()->SendToClient(socket_handle, 0, msg);
}
Player();
virtual void Initialize() override;
virtual void Update(int delta_time) override;
void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg);

View File

@ -4,6 +4,8 @@
#include "player.h"
#include "cs_proto.pb.h"
#include "room.h"
#include "android.h"
#include "metamgr.h"
const int ROOM_MAX_PLAYER_NUM = 50;
@ -38,7 +40,10 @@ Player* Room::GetPlayerByAccountId(const std::string& accountid)
Player* Room::GetPlayerByUniId(unsigned short uniid)
{
auto itr = uniid_hash_.find(uniid);
return itr != uniid_hash_.end() ? itr->second : nullptr;
return itr != uniid_hash_.end() &&
itr->second->entity_type == ET_Player &&
itr->second->entity_subtype == EST_Player ?
(Player*)itr->second : nullptr;
}
int Room::AliveCount()
@ -59,3 +64,22 @@ unsigned short Room::AllocUniid()
{
return ++current_uniid;
}
void Room::ShuaAndroid()
{
MetaData::Player* hum_meta = MetaMgr::Instance()->GetPlayer(40002);
assert(hum_meta);
if (!hum_meta) {
abort();
}
for (int i = 0; i < 30; ++i) {
Android* hum = new Android();
hum->meta = hum_meta;
hum->entity_uniid = AllocUniid();
hum->pos.x = 100 + rand() % 400;
hum->pos.y = 200 + rand() % 500;
hum->room = this;
hum->Initialize();
uniid_hash_[hum->entity_uniid] = hum;
}
}

View File

@ -12,6 +12,7 @@ namespace MetaData
struct Map;
}
class Entity;
class Player;
class Room
{
@ -27,6 +28,7 @@ public:
Player* GetPlayerByUniId(unsigned short uniid);
void AddPlayer(Player* hum);
unsigned short AllocUniid();
void ShuaAndroid();
private:
unsigned short current_uniid = 0;
@ -34,5 +36,5 @@ public:
int elapsed_time_ = 0;
std::map<std::string, Player*> accountid_hash_;
std::map<unsigned short, Player*> uniid_hash_;
std::map<unsigned short, Entity*> uniid_hash_;
};

View File

@ -28,6 +28,11 @@ void RoomMgr::Update(int delta_time)
void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
{
MetaData::Player* hum_meta = MetaMgr::Instance()->GetPlayer(40001);
assert(hum_meta);
if (!hum_meta) {
abort();
}
Room* room = GetJoinableRoom(msg.account_id());
if (!room) {
room = new Room();
@ -37,11 +42,14 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
abort();
}
room->map_meta = MetaMgr::Instance()->GetMap(1001);
room->ShuaAndroid();
room_hash_[room->room_uuid] = room;
}
unsigned short new_uniid = room->AllocUniid();
Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(new_uniid, msg);
hum->meta = hum_meta;
hum->socket_handle = hdr.socket_handle;
hum->Initialize();
room->AddPlayer(hum);
{