2004-06-11 00:24:03 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "JSInterface_Entity.h"
|
|
|
|
#include "scripting/JSInterface_BaseEntity.h"
|
|
|
|
#include "scripting/JSInterface_Vector3D.h"
|
|
|
|
#include "EntityHandles.h"
|
|
|
|
#include "Entity.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "BaseEntityCollection.h"
|
|
|
|
#include "CConsole.h"
|
|
|
|
|
|
|
|
JSClass JSI_Entity::JSI_class = {
|
|
|
|
"Entity", JSCLASS_HAS_PRIVATE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JSI_Entity::getProperty, JSI_Entity::setProperty,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, JSI_Entity::finalize,
|
|
|
|
NULL, NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec JSI_Entity::JSI_props[] =
|
|
|
|
{
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec JSI_Entity::JSI_methods[] =
|
|
|
|
{
|
|
|
|
{ "toString", JSI_Entity::toString, 0, 0, 0 },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool JSI_Entity::getProperty( JSContext* cx, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
|
|
|
HEntity* e = (HEntity*)JS_GetPrivate( cx, obj );
|
|
|
|
if( !e )
|
|
|
|
{
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
CStrW propName = g_ScriptingHost.ValueToUCString( id );
|
2004-06-11 00:24:03 +02:00
|
|
|
|
|
|
|
if( (*e)->m_properties.find( propName ) != (*e)->m_properties.end() )
|
|
|
|
{
|
2004-07-20 21:30:35 +02:00
|
|
|
*vp = (*e)->m_properties[propName]->tojsval();
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-07-20 21:30:35 +02:00
|
|
|
else
|
2004-08-03 01:14:54 +02:00
|
|
|
JS_ReportError( cx, "No such property on %ls: %ls", (const wchar_t*)((*e)->m_name), (const wchar_t*)propName );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool JSI_Entity::setProperty( JSContext* cx, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
|
|
|
HEntity* e = (HEntity*)JS_GetPrivate( cx, obj );
|
2004-08-03 01:14:54 +02:00
|
|
|
CStrW propName = g_ScriptingHost.ValueToUCString( id );
|
2004-06-11 00:24:03 +02:00
|
|
|
|
|
|
|
if( (*e)->m_properties.find( propName ) != (*e)->m_properties.end() )
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
(*e)->m_properties[propName]->fromjsval( *vp );
|
2004-06-11 00:24:03 +02:00
|
|
|
(*e)->rebuild( propName );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-07-20 21:30:35 +02:00
|
|
|
else
|
2004-08-03 01:14:54 +02:00
|
|
|
JS_ReportError( cx, "No such property on %ls: %ls", (const wchar_t*)((*e)->m_name), (const wchar_t*)propName );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
JSBool JSI_Entity::construct( JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
assert( argc >= 2 );
|
2004-07-22 18:18:12 +02:00
|
|
|
CBaseEntity* baseEntity = NULL;
|
2004-06-11 00:24:03 +02:00
|
|
|
CVector3D position;
|
|
|
|
float orientation = 0.0f;
|
|
|
|
JSObject* jsBaseEntity = JSVAL_TO_OBJECT( argv[0] );
|
2004-08-03 01:14:54 +02:00
|
|
|
CStrW templateName;
|
2004-07-20 21:30:35 +02:00
|
|
|
|
2004-07-22 18:18:12 +02:00
|
|
|
if( !JSVAL_IS_OBJECT( argv[0] ) || !( baseEntity = (CBaseEntity*)JS_GetInstancePrivate( cx, jsBaseEntity, &JSI_BaseEntity::JSI_class, NULL ) ) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2004-06-11 00:24:03 +02:00
|
|
|
try
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
templateName = g_ScriptingHost.ValueToUCString( argv[0] );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
*rval = JSVAL_NULL;
|
2004-07-20 21:30:35 +02:00
|
|
|
JS_ReportError( cx, "Invalid template identifier" );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
baseEntity = g_EntityTemplateCollection.getTemplate( templateName );
|
|
|
|
}
|
|
|
|
if( !baseEntity )
|
|
|
|
{
|
|
|
|
*rval = JSVAL_NULL;
|
2004-08-03 01:14:54 +02:00
|
|
|
JS_ReportError( cx, "No such template: %ls", (const wchar_t*)templateName );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-07-22 18:18:12 +02:00
|
|
|
JSI_Vector3D::Vector3D_Info* jsVector3D = NULL;
|
|
|
|
if( JSVAL_IS_OBJECT( argv[1] ) && ( jsVector3D = (JSI_Vector3D::Vector3D_Info*)JS_GetInstancePrivate( cx, JSVAL_TO_OBJECT( argv[1] ), &JSI_Vector3D::JSI_class, NULL ) ) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
2004-07-22 18:18:12 +02:00
|
|
|
position = *( jsVector3D->vector );
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
if( argc >= 3 )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
orientation = (float)g_ScriptingHost.ValueToDouble( argv[2] );
|
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
// TODO: Net-safe random for this parameter.
|
2004-06-11 00:24:03 +02:00
|
|
|
orientation = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HEntity* handle = new HEntity( g_EntityManager.create( baseEntity, position, orientation ) );
|
2004-07-11 14:49:44 +02:00
|
|
|
CMessage message( CMessage::EMSG_INIT );
|
|
|
|
(*handle)->dispatch( &message );
|
2004-06-11 00:24:03 +02:00
|
|
|
JSObject* entity = JS_NewObject( cx, &JSI_Entity::JSI_class, NULL, NULL );
|
|
|
|
JS_SetPrivate( cx, entity, handle );
|
|
|
|
*rval = OBJECT_TO_JSVAL( entity );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSI_Entity::finalize( JSContext* cx, JSObject* obj )
|
|
|
|
{
|
2004-07-10 14:56:17 +02:00
|
|
|
delete( (HEntity*)JS_GetPrivate( cx, obj ) );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSI_Entity::init()
|
|
|
|
{
|
|
|
|
g_ScriptingHost.DefineCustomObjectType( &JSI_class, construct, 2, JSI_props, JSI_methods, NULL, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool JSI_Entity::toString( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval )
|
|
|
|
{
|
|
|
|
HEntity* e = (HEntity*)JS_GetPrivate( cx, obj );
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
wchar_t buffer[256];
|
|
|
|
_snwprintf( buffer, 256, L"[object Entity: \"%ls\" (%ls)]", (const wchar_t*)(*e)->m_name, (const wchar_t*)(*e)->m_base->m_name );
|
2004-06-11 00:24:03 +02:00
|
|
|
buffer[255] = 0;
|
2004-08-03 01:14:54 +02:00
|
|
|
*rval = STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, buffer ) );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
2004-06-18 20:49:12 +02:00
|
|
|
}
|