This was SVN commit r1734.
This commit is contained in:
parent
25fdbfd931
commit
6b64327a19
@ -2,12 +2,53 @@
|
||||
|
||||
#include "Player.h"
|
||||
#include "Network/NetMessage.h"
|
||||
#include "Entity.h"
|
||||
#include "EntityManager.h"
|
||||
#include "scripting/JSCollection.h"
|
||||
|
||||
CPlayer::CPlayer(uint playerID):
|
||||
m_PlayerID(playerID)
|
||||
{}
|
||||
{
|
||||
AddReadOnlyProperty( L"id", &m_PlayerID );
|
||||
AddProperty( L"controlled", (IJSObject::GetFn)GetControlledEntities_JS );
|
||||
AddProperty( L"name", &m_Name );
|
||||
AddProperty( L"colour", &m_Colour );
|
||||
}
|
||||
|
||||
bool CPlayer::ValidateCommand(CNetMessage *pMsg)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ControllerPredicate( CEntity* entity, void* userdata )
|
||||
{
|
||||
return( entity->m_player == userdata );
|
||||
}
|
||||
|
||||
std::vector<HEntity>* CPlayer::GetControlledEntities()
|
||||
{
|
||||
return( g_EntityManager.matches( ControllerPredicate, this ) );
|
||||
}
|
||||
|
||||
jsval CPlayer::ToString( JSContext* cx, uintN argc, jsval* argv )
|
||||
{
|
||||
wchar_t buffer[256];
|
||||
swprintf( buffer, 256, L"[object Player: %ls]", m_Name.c_str() );
|
||||
buffer[255] = 0;
|
||||
utf16string str16(buffer, buffer+wcslen(buffer));
|
||||
return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) );
|
||||
}
|
||||
|
||||
jsval CPlayer::GetControlledEntities_JS()
|
||||
{
|
||||
std::vector<HEntity>* controlledSet = GetControlledEntities();
|
||||
jsval vp = OBJECT_TO_JSVAL( EntityCollection::Create( *controlledSet ) );
|
||||
delete( controlledSet );
|
||||
return( vp );
|
||||
}
|
||||
|
||||
void CPlayer::ScriptingInit()
|
||||
{
|
||||
AddMethod<jsval, &CPlayer::ToString>( "toString", 0 );
|
||||
CJSObject<CPlayer>::ScriptingInit( "Player" );
|
||||
}
|
@ -2,21 +2,40 @@
|
||||
#define _Player_H
|
||||
|
||||
#include "CStr.h"
|
||||
#include "scripting/ScriptableObject.h"
|
||||
#include "scripting/ScriptCustomTypes.h"
|
||||
#include "EntityHandles.h"
|
||||
|
||||
class CNetMessage;
|
||||
|
||||
class CPlayer
|
||||
typedef SColour SPlayerColour;
|
||||
|
||||
class CPlayer : public CJSObject<CPlayer>
|
||||
{
|
||||
// FIXME: These shouldn't be public (need load-from-attributes method in game.cpp)
|
||||
public:
|
||||
|
||||
CStrW m_Name;
|
||||
uint m_PlayerID;
|
||||
|
||||
SPlayerColour m_Colour;
|
||||
|
||||
public:
|
||||
CPlayer(uint playerID);
|
||||
CPlayer( uint playerID );
|
||||
|
||||
bool ValidateCommand(CNetMessage *pMsg);
|
||||
|
||||
inline const CStrW &GetName() const
|
||||
{ return m_Name; }
|
||||
|
||||
// Caller frees...
|
||||
std::vector<HEntity>* GetControlledEntities();
|
||||
|
||||
// Scripting functions
|
||||
|
||||
jsval ToString( JSContext* context, uintN argc, jsval* argv );
|
||||
jsval GetControlledEntities_JS();
|
||||
|
||||
static void ScriptingInit();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user