From e70e3b335e4dba9fc001db118ae060041d481c44 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 16 Mar 2021 16:52:06 +0800 Subject: [PATCH] 1 --- server/gameserver/entityfactory.cc | 9 ++++++ server/gameserver/entityfactory.h | 2 ++ server/gameserver/gridcell.cc | 11 +++++++ server/gameserver/gridcell.h | 4 +++ server/gameserver/gridservice.cc | 50 ++++++++++++++++++++++++++++++ server/gameserver/gridservice.h | 5 +++ server/gameserver/hero.h | 5 ++- server/gameserver/room.cc | 17 ++++++++++ server/gameserver/room.h | 6 ++++ 9 files changed, 108 insertions(+), 1 deletion(-) diff --git a/server/gameserver/entityfactory.cc b/server/gameserver/entityfactory.cc index 2f7bfef..63be9c5 100644 --- a/server/gameserver/entityfactory.cc +++ b/server/gameserver/entityfactory.cc @@ -9,6 +9,7 @@ #include "android.h" #include "player.h" #include "car.h" +#include "hero.h" void EntityFactory::Init() { @@ -87,3 +88,11 @@ Car* EntityFactory::MakeCar(int entity_uniid) hum->entity_type_ = ET_Car; return hum; } + +Hero* EntityFactory::MakeHero(int entity_uniid) +{ + Hero* hum = new Hero(); + hum->entity_uniid_ = entity_uniid; + hum->entity_type_ = ET_Hero; + return hum; +} diff --git a/server/gameserver/entityfactory.h b/server/gameserver/entityfactory.h index f2389db..aa60d60 100644 --- a/server/gameserver/entityfactory.h +++ b/server/gameserver/entityfactory.h @@ -8,6 +8,7 @@ class Bullet; class Android; class Player; class Car; +class Hero; class EntityFactory : public a8::Singleton { private: @@ -26,6 +27,7 @@ class EntityFactory : public a8::Singleton Android* MakeAndroid(int entity_uniid); Player* MakePlayer(int entity_uniid); Car* MakeCar(int entity_uniid); + Hero* MakeHero(int entity_uniid); private: }; diff --git a/server/gameserver/gridcell.cc b/server/gameserver/gridcell.cc index 30a9848..160cfa1 100644 --- a/server/gameserver/gridcell.cc +++ b/server/gameserver/gridcell.cc @@ -6,6 +6,7 @@ #include "human.h" #include "room.h" #include "car.h" +#include "hero.h" GridCell::GridCell() { @@ -83,6 +84,16 @@ void GridCell::RemoveCar(Car* car) car_list_[car->room->GetRoomIdx()].erase(car); } +void GridCell::AddHero(Hero* hero) +{ + hero_list_[hero->room->GetRoomIdx()].insert(hero); +} + +void GridCell::RemoveHero(Hero* hero) +{ + hero_list_[hero->room->GetRoomIdx()].erase(hero); +} + void GridCell::AddPermanentEntity(Entity* entity) { entity_list_[0].insert(entity); diff --git a/server/gameserver/gridcell.h b/server/gameserver/gridcell.h index f15e5db..3c56d08 100644 --- a/server/gameserver/gridcell.h +++ b/server/gameserver/gridcell.h @@ -5,6 +5,7 @@ class Human; class Bullet; class Room; class Car; +class Hero; class GridCell { @@ -26,6 +27,8 @@ public: void RemoveBullet(Bullet* bullet); void AddCar(Car* car); void RemoveCar(Car* car); + void AddHero(Hero* hero); + void RemoveHero(Hero* hero); void AddPermanentEntity(Entity* entity); void AddRoomEntity(Room* room, Entity* entity); void RemoveRoomEntity(Room* room, Entity* entity); @@ -44,4 +47,5 @@ private: std::vector> entity_list_; std::vector> bullet_list_; std::vector> car_list_; + std::vector> hero_list_; }; diff --git a/server/gameserver/gridservice.cc b/server/gameserver/gridservice.cc index 9031124..c455592 100644 --- a/server/gameserver/gridservice.cc +++ b/server/gameserver/gridservice.cc @@ -6,6 +6,7 @@ #include "room.h" #include "gridcell.h" #include "car.h" +#include "hero.h" /* 1 2 3 @@ -241,6 +242,55 @@ void GridService::DelCar(Car* car) cell.RemoveCar(car); } +void GridService::AddHero(Hero* hero) +{ + int x = (int)hero->GetX() + cell_width_; + int y = (int)hero->GetY() + cell_width_; + if (BroderOverFlow(x, y)) { + abort(); + } + hero->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_); + if (hero->GetGridId() == 0 || hero->GetGridId() > max_grid_id_) { + abort(); + } + cells_[hero->GetGridId()].AddHero(hero); + GetAllCells(hero->room, hero->GetGridId(), hero->GetGridList()); +} + +void GridService::MoveHero(Hero* hero) +{ + int new_x = (int)hero->GetX() + cell_width_; + int new_y = (int)hero->GetY() + cell_width_; + int new_grid_id = new_x/cell_width_ + (new_y/cell_width_) * cell_count_per_row_; + if (BroderOverFlow(new_x, new_y)) { + abort(); + } + if (new_grid_id == 0 || new_grid_id > max_grid_id_) { + abort(); + } + if (new_grid_id != hero->GetGridId()) { + std::set inc_grid_list; + std::set dec_grid_list; + std::set old_grid_list = hero->GetGridList(); + ComputeDiff(hero->room, + hero->GetGridId(), + new_grid_id, + hero->GetGridList(), + inc_grid_list, + dec_grid_list); + cells_[hero->GetGridId()].RemoveHero(hero); + cells_[new_grid_id].AddHero(hero); + hero->SetGridId(new_grid_id); + hero->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list); + } +} + +void GridService::DelHero(Hero* hero) +{ + GridCell& cell = cells_[hero->GetGridId()]; + cell.RemoveHero(hero); +} + void GridService::AddRoomEntity(Room* room, Entity* entity) { assert(!entity->IsEntityType(ET_Player)); diff --git a/server/gameserver/gridservice.h b/server/gameserver/gridservice.h index 6ccb8a4..7c03830 100644 --- a/server/gameserver/gridservice.h +++ b/server/gameserver/gridservice.h @@ -5,6 +5,7 @@ class Entity; class Room; class Bullet; class Car; +class Hero; class GridCell; class GridService { @@ -31,6 +32,10 @@ class GridService void MoveCar(Car* car); void DelCar(Car* car); + void AddHero(Hero* hero); + void MoveHero(Hero* hero); + void DelHero(Hero* hero); + void AddRoomEntity(Room* room, Entity* entity); void DelRoomEntity(Room* room, Entity* entity); diff --git a/server/gameserver/hero.h b/server/gameserver/hero.h index 515e46f..0f362f5 100644 --- a/server/gameserver/hero.h +++ b/server/gameserver/hero.h @@ -6,7 +6,7 @@ namespace MetaData { - struct Equip; + struct Player; } class Human; @@ -14,6 +14,9 @@ class Room; class Hero : public MoveableEntity { public: + Entity* master = nullptr; + MetaData::Player* meta = nullptr; + Hero(); virtual ~Hero() override; virtual void Initialize() override; diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index da3196e..add242d 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -20,6 +20,7 @@ #include "building.h" #include "loot.h" #include "car.h" +#include "hero.h" #include "roommgr.h" #include "app.h" #include "gamelog.h" @@ -568,6 +569,22 @@ Car* Room::CreateCar(Human* driver, return car; } +Hero* Room::CreateHero(Entity* master, + int hero_uniid, + MetaData::Player* meta, + const a8::Vec2& pos) +{ + Hero* hero = EntityFactory::Instance()->MakeHero(hero_uniid); + hero->meta = meta; + hero->room = this; + hero->SetPos(pos); + hero->Initialize(); + AddToEntityHash(hero); + grid_service->AddHero(hero); + hero->RefreshView(); + return hero; +} + void Room::RemoveObjectLater(RoomEntity* entity) { auto remove_func = diff --git a/server/gameserver/room.h b/server/gameserver/room.h index 7a1a8d9..d716c5d 100644 --- a/server/gameserver/room.h +++ b/server/gameserver/room.h @@ -14,6 +14,7 @@ namespace MetaData struct AirLine; struct MapTplThing; struct MapThing; + struct Player; } namespace metatable @@ -34,6 +35,7 @@ class Building; class AabbCollider; class Android; class Car; +class Hero; class Room { public: @@ -112,6 +114,10 @@ public: int car_uniid, MetaData::Equip* meta, const a8::Vec2& pos); + Hero* CreateHero(Entity* master, + int hero_uniid, + MetaData::Player* meta, + const a8::Vec2& pos); void OnHumanDie(Human* hum); void OnHumanRevive(Human* hum);