ddnet/src/game/server/entities/door.cpp

103 lines
2.3 KiB
C++
Raw Normal View History

#include <game/generated/protocol.h>
#include <game/server/gamecontext.h>
#include <engine/shared/config.h>
#include "door.h"
CDoor::CDoor(CGameWorld *pGameWorld, vec2 Pos, float Rotation, int Length, bool Opened)
: CEntity(pGameWorld, NETOBJTYPE_LASER)
{
m_Pos = Pos;
for (int i = 0; i < MAX_CLIENTS; ++i) {
m_Opened[i] = false;
}//TODO: Check this
2010-09-25 16:39:52 +00:00
m_Length = Length;
2010-09-25 16:39:52 +00:00
m_Direction = vec2(sin(Rotation), cos(Rotation));
vec2 To = Pos + normalize(m_Direction) * m_Length;
GameServer()->Collision()->IntersectNoLaser(Pos, To, &this->m_To, 0);
2010-09-25 16:39:52 +00:00
ResetCollision();
GameWorld()->InsertEntity(this);
}
void CDoor::Open(int Tick, bool ActivatedTeam[])
{
for (int i = 0; i < MAX_CLIENTS; ++i) {
if(ActivatedTeam[i]) m_EvalTick[i] = Tick;
m_Opened[i] = ActivatedTeam[i];
2010-09-25 16:39:52 +00:00
if(ActivatedTeam[i]) Open(i);
}
}
2010-09-25 16:39:52 +00:00
void CDoor::ResetCollision()
{
2010-09-25 16:39:52 +00:00
for(int i=0;i<m_Length;i++)
{
GameServer()->Collision()->SetDCollisionAt(m_Pos.x + (m_Direction.x * i), m_Pos.y + (m_Direction.y * i), TILE_STOPA, 99);
}
}
2010-09-25 16:39:52 +00:00
void CDoor::Open(int Team)
{
2010-09-25 16:39:52 +00:00
m_Opened[Team] = true;
for(int i=0;i<m_Length;i++)
{
2010-09-25 16:39:52 +00:00
GameServer()->Collision()->SetDTile(m_Pos.x + (m_Direction.x * i), m_Pos.y + (m_Direction.y * i), Team, false);
}
}
2010-09-25 16:39:52 +00:00
void CDoor::Close(int Team)
{
//if (Team < 0 || Team > (MAX_CLIENTS - 1) && Team !=99) return;
2010-09-25 16:39:52 +00:00
m_Opened[Team] = false;
for(int i=0;i<m_Length;i++)
{
GameServer()->Collision()->SetDTile(m_Pos.x + (m_Direction.x * i), m_Pos.y + (m_Direction.y * i), Team, true);
}
}
void CDoor::Reset()
{
for (int i = 0; i < MAX_CLIENTS; ++i) {
m_Opened[i] = false;
2010-09-25 16:39:52 +00:00
Close(i);
}
}
void CDoor::Tick()
{
for (int i = 0; i < MAX_CLIENTS; ++i) {
if (m_EvalTick[i] + 10 < Server()->Tick() && m_Opened[i]) {
2010-09-01 10:22:15 +00:00
Close(i);
}
}
}
void CDoor::Snap(int SnappingClient)
{
if(NetworkClipped(SnappingClient, m_Pos) && NetworkClipped(SnappingClient, m_To))
return;
CNetObj_Laser *pObj = static_cast<CNetObj_Laser *>(Server()->SnapNewItem(NETOBJTYPE_LASER, m_Id, sizeof(CNetObj_Laser)));
pObj->m_X = (int)m_Pos.x;
pObj->m_Y = (int)m_Pos.y;
CCharacter * Char = GameServer()->GetPlayerChar(SnappingClient);
if(Char == 0) return;
if (!m_Opened[Char->Team()])
{
pObj->m_FromX = (int)m_To.x;
pObj->m_FromY = (int)m_To.y;
}
else
{
pObj->m_FromX = (int)m_Pos.x;
pObj->m_FromY = (int)m_Pos.y;
}
pObj->m_StartTick = Server()->Tick();
}