2010-05-29 07:25:38 +00:00
|
|
|
// copyright (c) 2007 magnus auvinen, see licence.txt for more info
|
|
|
|
#include <game/generated/protocol.h>
|
|
|
|
#include <game/server/gamecontext.h>
|
|
|
|
#include "laser.h"
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CLaser::CLaser(CGameWorld *pGameWorld, vec2 Pos, vec2 Direction, float StartEnergy, int Owner)
|
|
|
|
: CEntity(pGameWorld, NETOBJTYPE_LASER)
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_Pos = Pos;
|
|
|
|
m_Owner = Owner;
|
|
|
|
m_Energy = StartEnergy;
|
|
|
|
m_Dir = Direction;
|
|
|
|
m_Bounces = 0;
|
|
|
|
m_EvalTick = 0;
|
|
|
|
GameWorld()->InsertEntity(this);
|
|
|
|
DoBounce();
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
bool CLaser::HitCharacter(vec2 From, vec2 To)
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 At;
|
|
|
|
CCharacter *OwnerChar = GameServer()->GetPlayerChar(m_Owner);
|
|
|
|
CCharacter *Hit = GameServer()->m_World.IntersectCharacter(m_Pos, To, 0.f, At, OwnerChar);
|
|
|
|
if(!Hit)
|
2008-08-14 18:25:44 +00:00
|
|
|
return false;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_From = From;
|
|
|
|
m_Pos = At;
|
|
|
|
m_Energy = -1;
|
|
|
|
Hit->TakeDamage(vec2(0.f, 0.f), GameServer()->Tuning()->m_LaserDamage, m_Owner, WEAPON_RIFLE);
|
2008-08-14 18:25:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLaser::DoBounce()
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_EvalTick = Server()->Tick();
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_Energy < 0)
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
GameServer()->m_World.DestroyEntity(this);
|
2008-08-14 18:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 To = m_Pos + m_Dir * m_Energy;
|
|
|
|
vec2 OrgTo = To;
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(GameServer()->Collision()->IntersectLine(m_Pos, To, 0x0, &To))
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!HitCharacter(m_Pos, To))
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
|
|
|
// intersected
|
2010-05-29 07:25:38 +00:00
|
|
|
m_From = m_Pos;
|
|
|
|
m_Pos = To;
|
2009-01-11 10:26:17 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 TempPos = m_Pos;
|
|
|
|
vec2 TempDir = m_Dir * 4.0f;
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
GameServer()->Collision()->MovePoint(&TempPos, &TempDir, 1.0f, 0);
|
|
|
|
m_Pos = TempPos;
|
|
|
|
m_Dir = normalize(TempDir);
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_Energy -= distance(m_From, m_Pos) + GameServer()->Tuning()->m_LaserBounceCost;
|
|
|
|
m_Bounces++;
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_Bounces > GameServer()->Tuning()->m_LaserBounceNum)
|
|
|
|
m_Energy = -1;
|
2008-08-14 18:25:44 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
GameServer()->CreateSound(m_Pos, SOUND_RIFLE_BOUNCE);
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!HitCharacter(m_Pos, To))
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_From = m_Pos;
|
|
|
|
m_Pos = To;
|
|
|
|
m_Energy = -1;
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLaser::Reset()
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
GameServer()->m_World.DestroyEntity(this);
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLaser::Tick()
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Server()->Tick() > m_EvalTick+(Server()->TickSpeed()*GameServer()->Tuning()->m_LaserBounceDelay)/1000.0f)
|
|
|
|
DoBounce();
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CLaser::Snap(int SnappingClient)
|
2008-08-14 18:25:44 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(NetworkClipped(SnappingClient))
|
2008-08-14 18:25:44 +00:00
|
|
|
return;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
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;
|
|
|
|
pObj->m_FromX = (int)m_From.x;
|
|
|
|
pObj->m_FromY = (int)m_From.y;
|
|
|
|
pObj->m_StartTick = m_EvalTick;
|
2008-08-14 18:25:44 +00:00
|
|
|
}
|