2004-08-16 17:19:17 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "Player.h"
|
|
|
|
#include "Network/NetMessage.h"
|
2005-01-18 11:48:51 +01:00
|
|
|
#include "Entity.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "scripting/JSCollection.h"
|
2004-08-16 17:19:17 +02:00
|
|
|
|
|
|
|
CPlayer::CPlayer(uint playerID):
|
2005-01-19 23:32:25 +01:00
|
|
|
m_PlayerID(playerID),
|
|
|
|
m_Name(CStrW(L"Player #")+CStrW(playerID)),
|
|
|
|
m_Colour(0.7f, 0.7f, 0.7f),
|
|
|
|
m_UpdateCB(0)
|
2005-01-18 11:48:51 +01:00
|
|
|
{
|
2005-01-19 23:32:25 +01:00
|
|
|
AddSynchedProperty( L"name", &m_Name );
|
2005-02-21 18:13:31 +01:00
|
|
|
// HACK - since we have to use setColour to update this, we don't want to
|
|
|
|
// expose a colour property. Meanwhile, we want to have a property "colour"
|
|
|
|
// available to be able to use the update/sync system.
|
|
|
|
// So, this is only added to the SynchedProperties list and not also passed
|
|
|
|
// to CJSObject's list
|
|
|
|
ISynchedJSProperty *prop=new CSynchedJSProperty<SPlayerColour>(L"colour", &m_Colour, this);
|
|
|
|
m_SynchedProperties[L"colour"]=prop;
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
}
|
|
|
|
|
2005-02-27 23:11:26 +01:00
|
|
|
CPlayer::~CPlayer()
|
|
|
|
{
|
|
|
|
// Side-effect of HACK - since it's not passed to CJSObject's list, it
|
|
|
|
// doesn't get freed automatically
|
|
|
|
delete m_SynchedProperties[L"colour"];
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
void CPlayer::ScriptingInit()
|
|
|
|
{
|
|
|
|
AddMethod<jsval, &CPlayer::JSI_ToString>( "toString", 0 );
|
|
|
|
AddMethod<jsval, &CPlayer::JSI_SetColour>( "setColour", 1);
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-04-22 09:12:55 +02:00
|
|
|
AddProperty( L"id", &CPlayer::m_PlayerID, true );
|
2005-03-30 18:14:19 +02:00
|
|
|
// MT: Work out how this fits with the Synched stuff...
|
|
|
|
|
|
|
|
// AddClassProperty( L"name", &CPlayer::m_Name );
|
|
|
|
// AddClassProperty( L"colour", &CPlayer::m_Colour );
|
2005-05-18 07:32:09 +02:00
|
|
|
AddProperty( L"controlled", (IJSObject::GetFn)&CPlayer::JSI_GetControlledEntities );
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
CJSObject<CPlayer>::ScriptingInit( "Player" );
|
2005-01-19 23:32:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPlayer::Update(CStrW name, ISynchedJSProperty *prop)
|
|
|
|
{
|
|
|
|
if (m_UpdateCB)
|
|
|
|
m_UpdateCB(name, prop->ToString(), this, m_UpdateCBData);
|
2005-01-18 11:48:51 +01:00
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2005-01-29 17:51:13 +01:00
|
|
|
void CPlayer::SetValue(CStrW name, CStrW value)
|
|
|
|
{
|
|
|
|
ISynchedJSProperty *prop=GetSynchedProperty(name);
|
|
|
|
if (prop)
|
|
|
|
{
|
|
|
|
prop->FromString(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
bool CPlayer::ValidateCommand(CNetMessage *pMsg)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2005-01-18 11:48:51 +01:00
|
|
|
|
|
|
|
static bool ControllerPredicate( CEntity* entity, void* userdata )
|
|
|
|
{
|
2005-01-23 23:21:37 +01:00
|
|
|
return( entity->GetPlayer() == userdata );
|
2005-01-18 11:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HEntity>* CPlayer::GetControlledEntities()
|
|
|
|
{
|
|
|
|
return( g_EntityManager.matches( ControllerPredicate, this ) );
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
jsval CPlayer::JSI_ToString( JSContext* cx, uintN argc, jsval* argv )
|
2005-01-18 11:48:51 +01:00
|
|
|
{
|
|
|
|
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() ) ) );
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
jsval CPlayer::JSI_GetControlledEntities()
|
2005-01-18 11:48:51 +01:00
|
|
|
{
|
|
|
|
std::vector<HEntity>* controlledSet = GetControlledEntities();
|
|
|
|
jsval vp = OBJECT_TO_JSVAL( EntityCollection::Create( *controlledSet ) );
|
|
|
|
delete( controlledSet );
|
|
|
|
return( vp );
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
jsval CPlayer::JSI_SetColour( JSContext* cx, uintN argc, jsval* argv )
|
2005-01-18 11:48:51 +01:00
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
if (argc != 1)
|
|
|
|
return JSVAL_NULL;
|
|
|
|
|
|
|
|
m_Colour=*( ToNative<SPlayerColour>(argv[0]) );
|
|
|
|
ISynchedJSProperty *prop=GetSynchedProperty(L"colour");
|
|
|
|
Update(L"colour", prop);
|
2005-02-21 20:49:20 +01:00
|
|
|
|
|
|
|
// Return something that isn't null, so users can check whether this function succeeded
|
|
|
|
return argv[0];
|
2005-01-19 23:32:25 +01:00
|
|
|
}
|