From 4ac5881160dcdc8d5cb2651c0aafc05f172e10c7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 24 Apr 2019 15:28:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84create=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/gameserver/building.cc | 15 --- server/gameserver/constant.h | 2 + server/gameserver/room.cc | 204 ++++++++++++++++------------------ server/gameserver/room.h | 11 +- 4 files changed, 105 insertions(+), 127 deletions(-) diff --git a/server/gameserver/building.cc b/server/gameserver/building.cc index 150a5ff..67c8400 100644 --- a/server/gameserver/building.cc +++ b/server/gameserver/building.cc @@ -32,21 +32,6 @@ void Building::RecalcSelfCollider() obj.y() + obj.height()/2.0 - meta->i->tileheight()/2.0); colliders.push_back(collider); } - for (size_t i = 0; i < meta->doors.size(); ++i) { - room->CreateDoor(this, i); - } - for (auto& obj : meta->i->lootobj()) { - room->CreateHouseObstacle(this, obj.id(), obj.x(), obj.y()); - } - for (auto& obj : meta->i->dropobj()) { - room->CreateLoot(obj.id(), - Vector2D( - pos.x + obj.x() - meta->i->tilewidth() / 2.0, - pos.y + obj.y() - meta->i->tileheight() / 2.0 - ), - 1 - ); - } } void Building::FillMFObjectPart(cs::MFObjectPart* part_data) diff --git a/server/gameserver/constant.h b/server/gameserver/constant.h index dc2e815..1b86bd3 100755 --- a/server/gameserver/constant.h +++ b/server/gameserver/constant.h @@ -110,3 +110,5 @@ const int SMOKE_SLOT = 4; const int MAP_HEIGHT = 8192; const int MAP_WIDTH = 8192; const int MAP_CELL_WIDTH = 64 * 5; + +const int DOOR_THING_ID = 61701; diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index a9df5d8..9cc0a84 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -341,25 +341,9 @@ void Room::CreateThings() MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id); if (thing_meta) { if (thing_meta->i->is_house()) { - MetaData::Building* building_meta = MetaMgr::Instance()->GetBuilding(thing_meta->i->house_id()); - if (building_meta) { - Building* entity = new Building(); - entity->room = this; - entity->meta = building_meta; - entity->building_id = thing_id; - entity->entity_uniid = AllocUniid(); - entity->pos = Vector2D(thing_tpl.i->x(), thing_tpl.i->y()); - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; - } + CreateBuilding(thing_id, thing_tpl.i->x(), thing_tpl.i->y()); } else { - Obstacle* entity = new Obstacle(); - entity->room = this; - entity->meta = thing_meta; - entity->entity_uniid = AllocUniid(); - entity->pos = Vector2D(thing_tpl.i->x(), thing_tpl.i->y()); - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; + CreateObstacle(thing_id, thing_tpl.i->x(), thing_tpl.i->y()); } } } @@ -400,97 +384,80 @@ void Room::FillSMMapInfo(cs::SMMapInfo& map_info) void Room::DropItem(Vector2D pos, int item_id, int item_count) { - MetaData::Equip* equip_meta = MetaMgr::Instance()->GetEquip(item_id); - if (equip_meta) { - Loot* entity = new Loot(); - entity->room = this; - entity->meta = equip_meta; - entity->entity_uniid = AllocUniid(); - { - Vector2D dir = Vector2D::UP; - dir.Rotate(a8::RandAngle()); - entity->pos = pos + dir * (25 + rand() % 50); - } - entity->item_id = equip_meta->i->id(); - entity->count = item_count; - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); - } + { + Vector2D dir = Vector2D::UP; + dir.Rotate(a8::RandAngle()); + pos = pos + dir * (25 + rand() % 50); } + CreateLoot(item_id, pos, item_count); } -void Room::CreateDoor(Building* building, int door_idx) +void Room::CreateBuilding(int thing_id, float building_x, float building_y) { - if (door_idx >= 0 && door_idx < building->meta->doors.size()) { - MetaData::Building::Door* door_meta = &building->meta->doors[door_idx]; - MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(61701); - if (thing) { - Obstacle* entity = new Obstacle(); - entity->room = this; - entity->meta = thing; - entity->entity_uniid = AllocUniid(); - entity->is_door = true; - entity->door_id = door_meta->door_id; - entity->door_state = DoorStateClose; - entity->building = building; - entity->door_house_uniid = building->entity_uniid; - entity->door_state0 = door_meta->state0; - entity->door_state1 = door_meta->state1; - entity->pos = Vector2D(building->pos.x + entity->door_state0->x() - building->meta->i->tilewidth() / 2.0, - building->pos.y + entity->door_state0->y() - building->meta->i->tileheight() / 2.0); - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); + MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id); + if (!thing_meta) { + return; + } + MetaData::Building* building_meta = MetaMgr::Instance()->GetBuilding(thing_meta->i->house_id()); + if (building_meta) { + Building* building = new Building(); + building->room = this; + building->meta = building_meta; + building->building_id = thing_id; + building->entity_uniid = AllocUniid(); + building->pos = Vector2D(building_x, building_y); + building->Initialize(); + uniid_hash_[building->entity_uniid] = building; + { + std::set inc_grid_list; + grid_service.AddEntity(building, inc_grid_list); + } + for (size_t door_idx = 0; door_idx < building_meta->doors.size(); ++door_idx) { + if (door_idx >= 0 && door_idx < building->meta->doors.size()) { + MetaData::Building::Door* door_meta = &building->meta->doors[door_idx]; + float x = building->pos.x + door_meta->state0->x() - building->meta->i->tilewidth() / 2.0; + float y = building->pos.y + door_meta->state0->y() - building->meta->i->tileheight() / 2.0; + InternalCreateObstacle(DOOR_THING_ID, x, y, + [building, door_meta] (Obstacle* entity) + { + entity->building = building; + entity->is_door = true; + entity->door_id = door_meta->door_id; + entity->door_state = DoorStateClose; + entity->building = building; + entity->door_house_uniid = building->entity_uniid; + entity->door_state0 = door_meta->state0; + entity->door_state1 = door_meta->state1; + }); } } - } -} - -void Room::CreateHouseObstacle(Building* building, int id, float x, float y) -{ - MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id); - if (thing) { - Obstacle* entity = new Obstacle(); - entity->room = this; - entity->meta = thing; - entity->building = building; - entity->entity_uniid = AllocUniid(); - entity->pos = Vector2D(building->pos.x + x - building->meta->i->tilewidth() / 2.0, - building->pos.y + y - building->meta->i->tileheight() / 2.0); - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); + for (auto& obj : building_meta->i->lootobj()) { + float x = building->pos.x + obj.x() - building->meta->i->tilewidth() / 2.0; + float y = building->pos.y + obj.y() - building->meta->i->tileheight() / 2.0; + InternalCreateObstacle(obj.id(), x, y, + [building] (Obstacle* entity) + { + entity->building = building; + }); + } + for (auto& obj : building_meta->i->dropobj()) { + CreateLoot(obj.id(), + Vector2D( + building->pos.x + obj.x() - building_meta->i->tilewidth() / 2.0, + building->pos.y + obj.y() - building_meta->i->tileheight() / 2.0 + ), + 1 + ); } } } -void Room::CreateObstacle(int id, float x, float y) +Obstacle* Room::CreateObstacle(int id, float x, float y) { - #if 0 - a8::XPrintf("createobstacle %d %f %f\n", {id, x, y}); - #endif - MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id); - if (thing) { - Obstacle* entity = new Obstacle(); - entity->room = this; - entity->meta = thing; - entity->building = nullptr; - entity->entity_uniid = AllocUniid(); - entity->pos = Vector2D(x, y); - entity->Initialize(); - uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); - } - } + return InternalCreateObstacle(id, x, y, + [] (Obstacle*) + { + }); } void Room::CreateLoot(int equip_id, Vector2D pos, int count) @@ -528,9 +495,7 @@ void Room::CreateBullet(Human* hum, MetaData::Equip* gun_meta, bullet->fly_distance = fly_distance; bullet->entity_uniid = AllocUniid(); bullet->Initialize(); - #if 0 - be_added_hash_[bullet->entity_uniid] = bullet; - #endif + AddObjectLater(bullet); } void Room::RemoveObjectLater(Entity* entity) @@ -931,13 +896,6 @@ void Room::AirDrop(int appear_time, int box_id) if (thing_meta) { Vector2D dir = Vector2D::UP; dir.Rotate(a8::RandAngle()); - #if 0 - a8::XPrintf("gas_data.pos_new %f %f\n", - { - gas_data.pos_new.x, - gas_data.pos_new.y - }); - #endif Vector2D box_pos = gas_data.pos_new + dir * (500 + rand() % 300); frame_event.AddAirDrop(appear_time, box_id, box_pos); xtimer.AddDeadLineTimerAndAttach(SERVER_FRAME_RATE * appear_time / 1000.f, @@ -983,3 +941,33 @@ void Room::ShuaPlane() } } } + +Obstacle* Room::InternalCreateObstacle(int id, float x, float y, + std::function on_precreate) +{ + MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id); + if (thing) { + Obstacle* entity = new Obstacle(); + entity->room = this; + entity->meta = thing; + entity->building = nullptr; + entity->entity_uniid = AllocUniid(); + entity->pos = Vector2D(x, y); + entity->Initialize(); + if (on_precreate) { + on_precreate(entity); + } + uniid_hash_[entity->entity_uniid] = entity; + { + std::set inc_grid_list; + grid_service.AddEntity(entity, inc_grid_list); + } + return entity; + } + return nullptr; +} + +void Room::AddObjectLater(Entity* entity) +{ + +} diff --git a/server/gameserver/room.h b/server/gameserver/room.h index 05e8fd0..12a393e 100644 --- a/server/gameserver/room.h +++ b/server/gameserver/room.h @@ -22,6 +22,7 @@ struct RoomProfile struct timer_list; class Entity; +class Obstacle; class Bullet; class Human; class Player; @@ -74,10 +75,7 @@ public: void ScatterDrop(Vector2D center, int drop_id); void DropItem(Vector2D pos, int item_id, int item_count); - void CreateThings(); - void CreateDoor(Building* building, int door_idx); - void CreateObstacle(int id, float x, float y); - void CreateHouseObstacle(Building* building, int id, float x, float y); + Obstacle* CreateObstacle(int id, float x, float y); void CreateLoot(int equip_id, Vector2D pos, int count); void CreateBullet(Human* hum, MetaData::Equip* gun_meta, Vector2D pos, Vector2D dir, float fly_distance); @@ -97,6 +95,11 @@ private: void AirDrop(int appear_time, int box_id); void ShuaPlane(); int NewTeam(); + void CreateThings(); + void CreateBuilding(int thing_id, float building_x, float building_y); + Obstacle* InternalCreateObstacle(int id, float x, float y, + std::function on_precreate); + void AddObjectLater(Entity* entity); private: timer_list* stats_timer_ = nullptr;