This commit is contained in:
aozhiwei 2021-03-16 16:52:06 +08:00
parent 933a39dfed
commit e70e3b335e
9 changed files with 108 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "android.h" #include "android.h"
#include "player.h" #include "player.h"
#include "car.h" #include "car.h"
#include "hero.h"
void EntityFactory::Init() void EntityFactory::Init()
{ {
@ -87,3 +88,11 @@ Car* EntityFactory::MakeCar(int entity_uniid)
hum->entity_type_ = ET_Car; hum->entity_type_ = ET_Car;
return hum; return hum;
} }
Hero* EntityFactory::MakeHero(int entity_uniid)
{
Hero* hum = new Hero();
hum->entity_uniid_ = entity_uniid;
hum->entity_type_ = ET_Hero;
return hum;
}

View File

@ -8,6 +8,7 @@ class Bullet;
class Android; class Android;
class Player; class Player;
class Car; class Car;
class Hero;
class EntityFactory : public a8::Singleton<EntityFactory> class EntityFactory : public a8::Singleton<EntityFactory>
{ {
private: private:
@ -26,6 +27,7 @@ class EntityFactory : public a8::Singleton<EntityFactory>
Android* MakeAndroid(int entity_uniid); Android* MakeAndroid(int entity_uniid);
Player* MakePlayer(int entity_uniid); Player* MakePlayer(int entity_uniid);
Car* MakeCar(int entity_uniid); Car* MakeCar(int entity_uniid);
Hero* MakeHero(int entity_uniid);
private: private:
}; };

View File

@ -6,6 +6,7 @@
#include "human.h" #include "human.h"
#include "room.h" #include "room.h"
#include "car.h" #include "car.h"
#include "hero.h"
GridCell::GridCell() GridCell::GridCell()
{ {
@ -83,6 +84,16 @@ void GridCell::RemoveCar(Car* car)
car_list_[car->room->GetRoomIdx()].erase(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) void GridCell::AddPermanentEntity(Entity* entity)
{ {
entity_list_[0].insert(entity); entity_list_[0].insert(entity);

View File

@ -5,6 +5,7 @@ class Human;
class Bullet; class Bullet;
class Room; class Room;
class Car; class Car;
class Hero;
class GridCell class GridCell
{ {
@ -26,6 +27,8 @@ public:
void RemoveBullet(Bullet* bullet); void RemoveBullet(Bullet* bullet);
void AddCar(Car* car); void AddCar(Car* car);
void RemoveCar(Car* car); void RemoveCar(Car* car);
void AddHero(Hero* hero);
void RemoveHero(Hero* hero);
void AddPermanentEntity(Entity* entity); void AddPermanentEntity(Entity* entity);
void AddRoomEntity(Room* room, Entity* entity); void AddRoomEntity(Room* room, Entity* entity);
void RemoveRoomEntity(Room* room, Entity* entity); void RemoveRoomEntity(Room* room, Entity* entity);
@ -44,4 +47,5 @@ private:
std::vector<std::set<Entity*>> entity_list_; std::vector<std::set<Entity*>> entity_list_;
std::vector<std::set<Bullet*>> bullet_list_; std::vector<std::set<Bullet*>> bullet_list_;
std::vector<std::set<Car*>> car_list_; std::vector<std::set<Car*>> car_list_;
std::vector<std::set<Hero*>> hero_list_;
}; };

View File

@ -6,6 +6,7 @@
#include "room.h" #include "room.h"
#include "gridcell.h" #include "gridcell.h"
#include "car.h" #include "car.h"
#include "hero.h"
/* /*
1 2 3 1 2 3
@ -241,6 +242,55 @@ void GridService::DelCar(Car* car)
cell.RemoveCar(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<GridCell*> inc_grid_list;
std::set<GridCell*> dec_grid_list;
std::set<GridCell*> 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) void GridService::AddRoomEntity(Room* room, Entity* entity)
{ {
assert(!entity->IsEntityType(ET_Player)); assert(!entity->IsEntityType(ET_Player));

View File

@ -5,6 +5,7 @@ class Entity;
class Room; class Room;
class Bullet; class Bullet;
class Car; class Car;
class Hero;
class GridCell; class GridCell;
class GridService class GridService
{ {
@ -31,6 +32,10 @@ class GridService
void MoveCar(Car* car); void MoveCar(Car* car);
void DelCar(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 AddRoomEntity(Room* room, Entity* entity);
void DelRoomEntity(Room* room, Entity* entity); void DelRoomEntity(Room* room, Entity* entity);

View File

@ -6,7 +6,7 @@
namespace MetaData namespace MetaData
{ {
struct Equip; struct Player;
} }
class Human; class Human;
@ -14,6 +14,9 @@ class Room;
class Hero : public MoveableEntity class Hero : public MoveableEntity
{ {
public: public:
Entity* master = nullptr;
MetaData::Player* meta = nullptr;
Hero(); Hero();
virtual ~Hero() override; virtual ~Hero() override;
virtual void Initialize() override; virtual void Initialize() override;

View File

@ -20,6 +20,7 @@
#include "building.h" #include "building.h"
#include "loot.h" #include "loot.h"
#include "car.h" #include "car.h"
#include "hero.h"
#include "roommgr.h" #include "roommgr.h"
#include "app.h" #include "app.h"
#include "gamelog.h" #include "gamelog.h"
@ -568,6 +569,22 @@ Car* Room::CreateCar(Human* driver,
return car; 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) void Room::RemoveObjectLater(RoomEntity* entity)
{ {
auto remove_func = auto remove_func =

View File

@ -14,6 +14,7 @@ namespace MetaData
struct AirLine; struct AirLine;
struct MapTplThing; struct MapTplThing;
struct MapThing; struct MapThing;
struct Player;
} }
namespace metatable namespace metatable
@ -34,6 +35,7 @@ class Building;
class AabbCollider; class AabbCollider;
class Android; class Android;
class Car; class Car;
class Hero;
class Room class Room
{ {
public: public:
@ -112,6 +114,10 @@ public:
int car_uniid, int car_uniid,
MetaData::Equip* meta, MetaData::Equip* meta,
const a8::Vec2& pos); const a8::Vec2& pos);
Hero* CreateHero(Entity* master,
int hero_uniid,
MetaData::Player* meta,
const a8::Vec2& pos);
void OnHumanDie(Human* hum); void OnHumanDie(Human* hum);
void OnHumanRevive(Human* hum); void OnHumanRevive(Human* hum);