1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/source/Entity/Sprite.cpp
Sandu Liviu Catalin 70e5f0ba21 Migrated the host module to C++ exceptions as well.
Also enabled the latest C++ revision in the project.
Replaced the Random library with the one provided by C++11.
Implemented a simple AES256 encryption class.
Various other fixes and improvements.
2016-03-10 05:57:13 +02:00

531 lines
20 KiB
C++

// ------------------------------------------------------------------------------------------------
#include "Entity/Sprite.hpp"
#include "Entity/Player.hpp"
#include "Base/Vector2i.hpp"
#include "Core.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
const Int32 CSprite::Max = SQMOD_SPRITE_POOL;
// ------------------------------------------------------------------------------------------------
SQInteger CSprite::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqSprite");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
CSprite::CSprite(Int32 id)
: m_ID(VALID_ENTITYGETEX(id, SQMOD_SPRITE_POOL))
, m_Tag(ToStrF("%d", id))
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
CSprite::~CSprite()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Int32 CSprite::Cmp(const CSprite & o) const
{
if (m_ID == o.m_ID)
return 0;
else if (m_ID > o.m_ID)
return 1;
else
return -1;
}
// ------------------------------------------------------------------------------------------------
const String & CSprite::ToString() const
{
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
const String & CSprite::GetTag() const
{
return m_Tag;
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetTag(CSStr tag)
{
m_Tag.assign(tag);
}
// ------------------------------------------------------------------------------------------------
Object & CSprite::GetData()
{
// Validate the managed identifier
Validate();
// Return the requested information
return m_Data;
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetData(Object & data)
{
// Validate the managed identifier
Validate();
// Apply the specified value
m_Data = data;
}
// ------------------------------------------------------------------------------------------------
bool CSprite::Destroy(Int32 header, Object & payload)
{
// Validate the managed identifier
Validate();
// Perform the requested operation
return _Core->DelSprite(m_ID, header, payload);
}
// ------------------------------------------------------------------------------------------------
void CSprite::BindEvent(Int32 evid, Object & env, Function & func) const
{
// Validate the managed identifier
Validate();
// Obtain the function instance called for this event
Function & event = _Core->GetSpriteEvent(m_ID, evid);
// Is the specified callback function null?
if (func.IsNull())
event.Release(); // Then release the current callback
// Assign the specified environment and function
else
event = Function(env.GetVM(), env, func.GetFunc());
}
// ------------------------------------------------------------------------------------------------
void CSprite::ShowAll() const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowSprite(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
void CSprite::ShowFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->ShowSprite(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CSprite::ShowRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then show this textdraw on his client
_Func->ShowSprite(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideAll() const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideSprite(m_ID, -1);
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideFor(CPlayer & player) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->HideSprite(m_ID, player.GetID());
}
// ------------------------------------------------------------------------------------------------
void CSprite::HideRange(Int32 first, Int32 last) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then hide this textdraw on his client
_Func->HideSprite(m_ID, first);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionAll(const Vector2i & pos) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, -1, pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionAllEx(Int32 x, Int32 y) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, -1, x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionFor(CPlayer & player, const Vector2i & pos) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, player.GetID(), pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionForEx(CPlayer & player, Int32 x, Int32 y) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->MoveSprite(m_ID, player.GetID(), x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetPositionRange(Int32 first, Int32 last, const Vector2i & pos) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then move this textdraw on his client
_Func->MoveSprite(m_ID, first, pos.x, pos.y);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterAll(const Vector2i & pos) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, -1, pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterAllEx(Int32 x, Int32 y) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, -1, x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterFor(CPlayer & player, const Vector2i & pos) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, player.GetID(), pos.x, pos.y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterForEx(CPlayer & player, Int32 x, Int32 y) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteCenter(m_ID, player.GetID(), x, y);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetCenterRange(Int32 first, Int32 last, const Vector2i & pos) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then center this textdraw on his client
_Func->SetSpriteCenter(m_ID, first, pos.x, pos.y);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationAll(Float32 rot) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotateSprite(m_ID, -1, rot);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationFor(CPlayer & player, Float32 rot) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->RotateSprite(m_ID, player.GetID(), rot);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetRotationRange(Int32 first, Int32 last, Float32 rot) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then rotate this textdraw on his client
_Func->RotateSprite(m_ID, first, rot);
}
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaAll(Uint8 alpha) const
{
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteAlpha(m_ID, -1, alpha);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaFor(CPlayer & player, Uint8 alpha) const
{
// Is the specified player even valid?
if (!player.IsActive())
SqThrowF("Invalid player argument: null");
// Validate the managed identifier
Validate();
// Perform the requested operation
_Func->SetSpriteAlpha(m_ID, player.GetID(), alpha);
}
// ------------------------------------------------------------------------------------------------
void CSprite::SetAlphaRange(Int32 first, Int32 last, Uint8 alpha) const
{
// Validate the specified range
if (first > last)
SqThrowF("Invalid player range: %d > %d", first, last);
// Validate the managed identifier
Validate();
// Perform the requested operation
for (; first <= last; ++first)
{
// Is the currently processed player even connected?
if (_Func->IsPlayerConnected(first))
// Then colorize this textdraw on his client
_Func->SetSpriteAlpha(m_ID, first, alpha);
}
}
// ------------------------------------------------------------------------------------------------
const String & CSprite::GetFilePath() const
{
// Validate the managed identifier
Validate();
// Return the requested information
return _Core->GetSprite(m_ID).mPath;
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(-1, file, xp, yp, xr, yr, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(-1, file, xp, yp, xr, yr, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(index, file, xp, yp, xr, yr, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_CreateEx(Int32 index, CSStr file, Int32 xp, Int32 yp, Int32 xr, Int32 yr,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(index, file, xp, yp, xr, yr, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(-1, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(-1, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel, header, payload);
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel)
{
return _Core->NewSprite(index, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel,
SQMOD_CREATE_DEFAULT, NullObject());
}
// ------------------------------------------------------------------------------------------------
static Object & Sprite_Create(Int32 index, CSStr file, const Vector2i & pos, const Vector2i & rot,
Float32 angle, Int32 alpha, bool rel, Int32 header, Object & payload)
{
return _Core->NewSprite(index, file, pos.x, pos.y, rot.x, rot.y, angle, alpha, rel, header, payload);
}
// ================================================================================================
void Register_CSprite(HSQUIRRELVM vm)
{
RootTable(vm).Bind(_SC("SqSprite"),
Class< CSprite, NoConstructor< CSprite > >(vm, _SC("SqSprite"))
// Metamethods
.Func(_SC("_cmp"), &CSprite::Cmp)
.SquirrelFunc(_SC("_typename"), &CSprite::Typename)
.Func(_SC("_tostring"), &CSprite::ToString)
// Static values
.SetStaticValue(_SC("MaxID"), CSprite::Max)
// Core Properties
.Prop(_SC("ID"), &CSprite::GetID)
.Prop(_SC("Tag"), &CSprite::GetTag, &CSprite::SetTag)
.Prop(_SC("Data"), &CSprite::GetData, &CSprite::SetData)
.Prop(_SC("Active"), &CSprite::IsActive)
// Core Functions
.Func(_SC("Bind"), &CSprite::BindEvent)
// Core Overloads
.Overload< bool (CSprite::*)(void) >(_SC("Destroy"), &CSprite::Destroy)
.Overload< bool (CSprite::*)(Int32) >(_SC("Destroy"), &CSprite::Destroy)
.Overload< bool (CSprite::*)(Int32, Object &) >(_SC("Destroy"), &CSprite::Destroy)
// Properties
.Prop(_SC("Path"), &CSprite::GetFilePath)
// Functions
.Func(_SC("ShowAll"), &CSprite::ShowAll)
.Func(_SC("ShowTo"), &CSprite::ShowFor)
.Func(_SC("ShowFor"), &CSprite::ShowFor)
.Func(_SC("ShowRange"), &CSprite::ShowRange)
.Func(_SC("HideAll"), &CSprite::HideAll)
.Func(_SC("HideFor"), &CSprite::HideFor)
.Func(_SC("HideFrom"), &CSprite::HideFor)
.Func(_SC("HideRange"), &CSprite::HideRange)
.Func(_SC("SetPositionRange"), &CSprite::SetPositionRange)
.Func(_SC("SetCenterRange"), &CSprite::SetCenterRange)
.Func(_SC("SetRotationAll"), &CSprite::SetRotationAll)
.Func(_SC("SetRotationFor"), &CSprite::SetRotationFor)
.Func(_SC("SetRotationRange"), &CSprite::SetRotationRange)
.Func(_SC("SetAlphaAll"), &CSprite::SetAlphaAll)
.Func(_SC("SetAlphaFor"), &CSprite::SetAlphaFor)
.Func(_SC("SetAlphaRange"), &CSprite::SetAlphaRange)
// Overloads
.Overload< void (CSprite::*)(const Vector2i &) const >
(_SC("SetPositionAll"), &CSprite::SetPositionAll)
.Overload< void (CSprite::*)(Int32, Int32) const >
(_SC("SetPositionAll"), &CSprite::SetPositionAllEx)
.Overload< void (CSprite::*)(CPlayer &, const Vector2i &) const >
(_SC("SetPositionFor"), &CSprite::SetPositionFor)
.Overload< void (CSprite::*)(CPlayer &, Int32, Int32) const >
(_SC("SetPositionFor"), &CSprite::SetPositionForEx)
.Overload< void (CSprite::*)(const Vector2i &) const >
(_SC("SetCenterAll"), &CSprite::SetCenterAll)
.Overload< void (CSprite::*)(Int32, Int32) const >
(_SC("SetCenterAll"), &CSprite::SetCenterAllEx)
.Overload< void (CSprite::*)(CPlayer &, const Vector2i &) const >
(_SC("SetCenterFor"), &CSprite::SetCenterFor)
.Overload< void (CSprite::*)(CPlayer &, Int32, Int32) const >
(_SC("SetCenterFor"), &CSprite::SetCenterForEx)
// Static Overloads
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(Int32, CSStr, Int32, Int32, Int32, Int32, Float32, Int32, bool rel, Int32, Object &) >
(_SC("CreateEx"), &Sprite_CreateEx)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool) >
(_SC("Create"), &Sprite_Create)
.StaticOverload< Object & (*)(Int32, CSStr, const Vector2i &, const Vector2i &, Float32, Int32, bool, Int32, Object &) >
(_SC("Create"), &Sprite_Create)
);
}
} // Namespace:: SqMod