game2005/server/gameserver/roomobstacle.cc
aozhiwei 83bb518b11 1
2021-07-06 11:57:24 +00:00

447 lines
12 KiB
C++

#include "precompile.h"
#include "metamgr.h"
#include "room.h"
#include "collider.h"
#include "building.h"
#include "human.h"
#include "app.h"
#include "typeconvert.h"
#include "bullet.h"
#include "mapservice.h"
#include "roomobstacle.h"
#include "explosion.h"
RoomObstacle::RoomObstacle():Obstacle()
{
}
RoomObstacle::~RoomObstacle()
{
for (auto& itr : colliders_) {
ColliderComponent* collider = itr;
#ifdef DEBUG
#if 0
a8::UdpLog::Instance()->Debug("OnRemoveCollider %d %d %d",
{
room->GetRoomIdx(),
GetUniId(),
(long long)collider
});
#endif
#endif
room->map_service->RemoveCollider(collider);
}
if (!detached_) {
if (master.Get()) {
master.Get()->SlaveOnRemove(this);
}
detached_ = true;
}
if (!grid_list_) {
A8_SAFE_DELETE(grid_list_);
}
}
void RoomObstacle::Initialize()
{
Obstacle::Initialize();
xtimer_attacher.xtimer = &room->xtimer;
}
void RoomObstacle::RecalcSelfCollider()
{
if (!Throughable()){
switch (meta->i->type()) {
case 1:
{
if (!self_collider_) {
self_collider_ = new CircleCollider();
self_collider_->owner = this;
AddEntityCollider(self_collider_);
}
self_collider_->pos = a8::Vec2();
self_collider_->rad = meta->i->height() / 2.0;
room->map_service->AddCollider(self_collider_);
}
break;
case 2:
{
if (!self_collider2_) {
self_collider2_ = new AabbCollider();
self_collider2_->owner = this;
AddEntityCollider(self_collider2_);
}
self_collider2_->_min = a8::Vec2(meta->i->width() / -2.0f, meta->i->height() / -2.0f);
self_collider2_->_max = a8::Vec2(meta->i->width() / 2.0f, meta->i->height() / 2.0f);
room->map_service->AddCollider(self_collider2_);
}
break;
}
}
if (!self_collider_) {
#ifdef DEBUG
#if 0
a8::UdpLog::Instance()->Debug("OnAddCollider %d %d %d",
{
room->GetRoomIdx(),
GetUniId(),
(long long)self_collider_
});
#endif
#endif
}
for (auto collider : colliders_) {
collider->tag = collider_tag;
collider->param1 = collider_param1;
collider->param2 = collider_param2;
if (IsHalfWallCollider()) {
a8::SetBitFlag(collider->tag, kHalfWallTag);
}
}
}
void RoomObstacle::ActiveTimerFunc()
{
if (!master.Get()) {
room->xtimer.DeleteTimer(room->xtimer.GetRunningTimer());
return;
}
if (!grid_list_) {
grid_list_ = new std::set<GridCell*>();
room->grid_service->GetAllCellsByXy(room, GetPos().x, GetPos().y, *grid_list_);
}
bool has_hum = false;
room->grid_service->TraverseAllLayerHumanList
(room->GetRoomIdx(),
*grid_list_,
[this, &has_hum] (Human* hum, bool& stop)
{
if (master.Get()->team_id == hum->team_id) {
if (TestCollision(room, hum)) {
has_hum = true;
stop = true;
}
}
}
);
if (!has_hum) {
room->xtimer.DeleteTimer(room->xtimer.GetRunningTimer());
}
}
void RoomObstacle::UpdateTimerFunc()
{
if (!grid_list_) {
grid_list_ = new std::set<GridCell*>();
room->grid_service->GetAllCellsByXy(room, GetPos().x, GetPos().y, *grid_list_);
}
if (grid_list_ && master.Get() && !IsDead(room)) {
std::set<Human*> human_list;
room->grid_service->TraverseAllLayerHumanList
(room->GetRoomIdx(),
*grid_list_,
[this, &human_list] (Human* hum, bool& stop)
{
if (master.Get()->team_id != hum->team_id && TestCollision(room, hum)) {
human_list.insert(hum);
}
}
);
if (!human_list.empty()) {
for (Human* hum : human_list) {
for (int buff_id : meta->buff_list) {
MetaData::Buff* buff_meta = MetaMgr::Instance()->GetBuff(buff_id);
if (buff_meta) {
hum->AddBuff(master.Get(),
buff_meta,
1);
}
}
}
Die(room);
ProcDieExplosion(room);
BroadcastFullState(room);
if (room->xtimer.GetRunningTimer()) {
room->xtimer.DeleteTimer(room->xtimer.GetRunningTimer());
}
}
} else {
room->xtimer.DeleteTimer(room->xtimer.GetRunningTimer());
}
}
void RoomObstacle::SpecExplosion()
{
++explosion_times_;
if (meta->i->damage_dia() > 0.01f) {
if (!grid_list_) {
grid_list_ = new std::set<GridCell*>();
room->grid_service->GetAllCellsByXy(room, GetPos().x, GetPos().y, *grid_list_);
}
a8::Vec2 bomb_born_offset = a8::Vec2::UP;
bomb_born_offset.Rotate(a8::RandAngle());
bomb_born_offset = bomb_born_offset * a8::RandEx(1, std::max(2, meta->i->explosion_float()));
a8::Vec2 bomb_pos = GetPos() + bomb_born_offset;
if (room->grid_service->CanAdd(bomb_pos.x, bomb_pos.y)) {
Explosion explosion;
explosion.exclude_uniid = GetUniId();
explosion.IndifferenceAttack(
room,
bomb_pos,
meta->i->damage_dia(),
meta->i->explosion_effect(),
meta->i->damage()
);
}
}
int total_explosion_times = meta->i->explosion_times();
if (context_ability && context_ability->GetAttrAbs(kHAT_WeaponExplosionTime) > 0.001f) {
total_explosion_times += context_ability->GetAttrAbs(kHAT_WeaponExplosionTime) * 1000 /
meta->i->explosion_interval();
}
if (explosion_times_ >= total_explosion_times) {
if (room->xtimer.GetRunningTimer()) {
room->xtimer.DeleteTimer(room->xtimer.GetRunningTimer());
}
Die(room);
BroadcastFullState(room);
}
}
void RoomObstacle::Active()
{
switch (meta->i->thing_type()) {
case kObstacleSelfExplosion:
{
ActiveSelfExplosion();
}
break;
case kObstacleMine:
{
#if 0
ActiveMine();
#endif
}
break;
case kObstacleTrap:
{
#if 0
ActiveTrap();
#endif
}
break;
case kObstaclePosionGas:
{
ActivePosionGas();
}
break;
case kObstacleSpring:
{
ActiveSpring();
}
break;
case kObstacleHideHouse:
{
ActiveHideHouse();
}
break;
case kObstacleGully:
{
ActiveGully();
}
break;
case kObstacleAirDropBox:
{
ActiveAirDrop();
}
break;
default:
{
}
break;
}
}
void RoomObstacle::ActiveSelfExplosion()
{
room->xtimer.AddDeadLineTimerAndAttach
(
meta->i->time() / FRAME_RATE_MS,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->room->xtimer.AddRepeatTimerAndAttach
(
obstacle->meta->i->explosion_interval() / FRAME_RATE_MS,
a8::XParams()
.SetSender(obstacle),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->SpecExplosion();
},
&obstacle->xtimer_attacher.timer_list_
);
},
&xtimer_attacher.timer_list_
);
}
void RoomObstacle::ActiveMine()
{
room->xtimer.AddRepeatTimerAndAttach
(
1,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->UpdateTimerFunc();
},
&xtimer_attacher.timer_list_
);
}
void RoomObstacle::ActiveTrap()
{
room->xtimer.AddRepeatTimerAndAttach
(
SERVER_FRAME_RATE,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->UpdateTimerFunc();
},
&xtimer_attacher.timer_list_
);
}
void RoomObstacle::ActivePosionGas()
{
room->xtimer.AddDeadLineTimerAndAttach
(
meta->i->time() / FRAME_RATE_MS,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->room->xtimer.AddRepeatTimerAndAttach
(
obstacle->meta->i->explosion_interval() / FRAME_RATE_MS,
a8::XParams()
.SetSender(obstacle),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->SpecExplosion();
},
&obstacle->xtimer_attacher.timer_list_
);
},
&xtimer_attacher.timer_list_
);
}
void RoomObstacle::DetachFromMaster()
{
if (!detached_) {
detached_ = true;
if (master.Get()) {
xtimer_attacher.ClearTimerList();
master.Get()->SlaveOnRemove(this);
room->grid_service->DelRoomEntity(room, this);
if (!IsDead(room)) {
Die(room);
BroadcastFullState(room);
}
room->RemoveObjectLater(this);
}
}
}
void RoomObstacle::Die(Room* room)
{
if (!IsDead(room)) {
Obstacle::Die(room);
DetachFromMaster();
}
}
void RoomObstacle::ActiveSpring()
{
}
void RoomObstacle::ActiveHideHouse()
{
}
void RoomObstacle::ActiveGully()
{
}
void RoomObstacle::ActiveAirDrop()
{
room->xtimer.AddDeadLineTimerAndAttach
(
meta->int_param2 / FRAME_RATE_MS,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->SummonAirDropBox(obstacle->meta->int_param1);
},
&xtimer_attacher.timer_list_
);
room->xtimer.AddDeadLineTimerAndAttach
(
meta->int_param2 / FRAME_RATE_MS * 1,
a8::XParams()
.SetSender(this),
[] (const a8::XParams& param)
{
RoomObstacle* obstacle = (RoomObstacle*)param.sender.GetUserData();
obstacle->room->frame_event.AddAirDrop(obstacle->meta->int_param2 / 1000,
obstacle->meta->int_param1,
obstacle->GetPos());
obstacle->Die(obstacle->room);
obstacle->BroadcastFullState(obstacle->room);
},
&xtimer_attacher.timer_list_
);
}
bool RoomObstacle::DoInteraction(Human* sender)
{
if (Obstacle::DoInteraction(sender)) {
return true;
}
return true;
}
void RoomObstacle::SummonAirDropBox(int box_id)
{
RoomObstacle* obstacle = room->CreateObstacle
(
box_id,
GetPos().x,
GetPos().y
);
}
Entity* RoomObstacle::GetRealObject(Room* room)
{
return room->GetEntityByUniId(real_object_uniid);
}