0ad/source/ps/Player.cpp
olsner 7bcc12373b - Created JSMap
- Changed player colour JS interface: setColour(...) instead of a colour
property
- Introduced a network log and replaced most network LOG() calls with
NET_LOG()
- Moved to a slot-based system for Pre-Game and extended a lot of the JS
APIs to networking stuff
- A bit of cleanup in the low-level network code (Unix parts)
- Clients now keep track of all other connected clients on the server
(And exposes this info to JS)
- Split out GameAttributes to its own file
- Removed unused class AttributeMap
- Changed CJSObject to use T* pointers in JS_SetPrivate (needed to make
ToScript work with multiple inheritance)

This was SVN commit r1929.
2005-02-21 17:13:31 +00:00

90 lines
2.4 KiB
C++
Executable File

#include "precompiled.h"
#include "Player.h"
#include "Network/NetMessage.h"
#include "Entity.h"
#include "EntityManager.h"
#include "scripting/JSCollection.h"
CPlayer::CPlayer(uint playerID):
m_PlayerID(playerID),
m_Name(CStrW(L"Player #")+CStrW(playerID)),
m_Colour(0.7f, 0.7f, 0.7f),
m_UpdateCB(0)
{
AddReadOnlyProperty( L"id", &m_PlayerID );
AddProperty( L"controlled", (GetFn)&CPlayer::JSI_GetControlledEntities);
AddSynchedProperty( L"name", &m_Name );
// 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;
}
void CPlayer::ScriptingInit()
{
AddMethod<jsval, &CPlayer::JSI_ToString>( "toString", 0 );
AddMethod<jsval, &CPlayer::JSI_SetColour>( "setColour", 1);
CJSObject<CPlayer>::ScriptingInit( "Player" );
}
void CPlayer::Update(CStrW name, ISynchedJSProperty *prop)
{
if (m_UpdateCB)
m_UpdateCB(name, prop->ToString(), this, m_UpdateCBData);
}
void CPlayer::SetValue(CStrW name, CStrW value)
{
ISynchedJSProperty *prop=GetSynchedProperty(name);
if (prop)
{
prop->FromString(value);
}
}
bool CPlayer::ValidateCommand(CNetMessage *pMsg)
{
return true;
}
static bool ControllerPredicate( CEntity* entity, void* userdata )
{
return( entity->GetPlayer() == userdata );
}
std::vector<HEntity>* CPlayer::GetControlledEntities()
{
return( g_EntityManager.matches( ControllerPredicate, this ) );
}
jsval CPlayer::JSI_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::JSI_GetControlledEntities()
{
std::vector<HEntity>* controlledSet = GetControlledEntities();
jsval vp = OBJECT_TO_JSVAL( EntityCollection::Create( *controlledSet ) );
delete( controlledSet );
return( vp );
}
jsval CPlayer::JSI_SetColour( JSContext* cx, uintN argc, jsval* argv )
{
if (argc != 1)
return JSVAL_NULL;
m_Colour=*( ToNative<SPlayerColour>(argv[0]) );
ISynchedJSProperty *prop=GetSynchedProperty(L"colour");
Update(L"colour", prop);
}