Permalink
Browse files

fixed a bug in ObjectManager that was causing the engine to freeze if…

… an object was removed, then added again. Also got it so Entity::update now calls Entity.onUpdate in the javascript vm. Lastly, tried to get some properties binded, but the compiler is giving me some problems.
  • Loading branch information...
1 parent 88f5d7d commit 964eb0d6de6a85ebf9888b28ea75072f4ebc62ab Will Riley committed Jan 21, 2011
Showing with 72 additions and 11 deletions.
  1. +3 −3 Makefile
  2. +18 −1 data/game/main.js
  3. +4 −2 src/Entity.cpp
  4. +0 −1 src/Entity.h
  5. +4 −1 src/ObjectManager.cpp
  6. +40 −3 src/Scripting.cpp
  7. +3 −0 src/Scripting.h
View
@@ -42,9 +42,9 @@ $(JUICE_DIRS): FORCE
finish:
@echo ""
- @mv -vf ./src/blackpyre .
- @mv -vf ./support/v8-juice/src/lib/juice/libv8-juice.so ./lib
- @mv -vf ./support/v8/libv8.so ./lib
+ @cp -vf ./src/blackpyre .
+ @cp -vf ./support/v8-juice/src/lib/juice/libv8-juice.so ./lib
+ @cp -vf ./support/v8/libv8.so ./lib
@chmod +x ./blackpyre
@echo ""
-ls --color ./blackpyre ./lib
View
@@ -1,5 +1,22 @@
for(var i=0; i<360; i+=45){
print("Making entity at angle "+i);
var f = new Entity(300, 200, 100, 100, i, "bullet.png");
- if(i==0) setTimeout(function(){ f.destroy(); }, 4000);
+ f.onUpdate = function(){
+ var tss = 2000; //f.timeSinceSpawn();
+ var time = tss*2;
+ while(time > 6000){
+ time -= 6000;
+ }
+ //f.angle = (tss/5)+f.initialAngle;
+ //f.posX = f.initialPosX + (time*Math.sin((f.initialAngle+(time/10))*(3.14/180))/17);
+ //f.posY = f.initialPosY + (time*Math.cos((f.initialAngle+(time/10))*(3.14/180))/17);
+ //f.posX++;
+ //f.posY++;
+ f.posX = 300;
+ f.posY = 200;
+ };
+ if(i==0){
+ setTimeout(function(){ f.hide(); }, 2000);
+ setTimeout(function(){ f.show(); }, 4000);
+ }
}
View
@@ -11,6 +11,7 @@
#include "GlUtil.h"
#include "ResourceManager.h"
#include "ObjectManager.h"
+#include "Scripting.h"
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
@@ -71,7 +72,8 @@ void Entity::render(){
}
void Entity::update(){
- //uint32_t time = fabs(sin((timeSinceSpawn()/22.22)*(3.14/180)))*4000;
+ Scripting::callUpdateFunction(this);
+ /*//uint32_t time = fabs(sin((timeSinceSpawn()/22.22)*(3.14/180)))*4000;
uint32_t time = timeSinceSpawn()*2;
while(time > 6000){
time -= 6000;
@@ -80,7 +82,7 @@ void Entity::update(){
// posX = initialPosX+(time*sin(initialAngle*(3.14/180))/15);
// posY = initialPosY+(time*cos(initialAngle*(3.14/180))/15);
posX = initialPosX + (time*sin((initialAngle+(time/10))*(3.14/180))/17);
- posY = initialPosY + (time*cos((initialAngle+(time/10))*(3.14/180))/17);
+ posY = initialPosY + (time*cos((initialAngle+(time/10))*(3.14/180))/17);*/
}
void Entity::show(){
View
@@ -32,7 +32,6 @@ class Entity : public GameObject{
void show();
void hide();
-
private:
int objectmanagerId;
};
View
@@ -10,6 +10,8 @@
#include "ObjectManager.h"
#include "GameObject.h"
+#include <stdio.h>
+
GameObject *ObjectManager::rootObject = 0;
ObjectManager *ObjectManager::instance = 0;
int ObjectManager::idCounter = 0;
@@ -40,9 +42,10 @@ int ObjectManager::addObject(GameObject *object){
}
listObj->next = object;
object->prev = listObj;
+ object->next = 0; //set a default incase it was added already
}
object->id = ++idCounter;
- object->setSpawnTime();
+ object->setSpawnTime(); //TODO: maybe move this somewhere else since it would screw up some animations if the object was removed then added again
return (int) object->id;
}
View
@@ -22,8 +22,16 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string>
+// Some magic so we can get JS objects from our native objects
+ #define CLASSWRAP_BOUND_TYPE Entity
+ #define CLASSWRAP_BOUND_TYPE_NAME "Entity"
+ // OPTIONAL: #define CLASSWRAP_BOUND_TYPE_INHERITS BoundBaseClass
+// ^^^^^ required if MyType sublcasses another bound native!
+#include <v8/juice/ClassWrap_TwoWay.h>
+
Scripting *Scripting::instance = 0;
v8::Handle<v8::Context> Scripting::context;
@@ -98,6 +106,20 @@ v8::Handle<v8::ObjectTemplate> Scripting::getObjectTemplate(){
return global;
}
+void Scripting::callUpdateFunction(Entity* obj){
+ v8::Locker tlock;
+ v8::HandleScope handle_scope;
+ namespace cv = ::v8::juice::convert;
+
+ v8::Handle<v8::Object> jsObj = v8::Handle<v8::Object>::Cast(cv::CastToJS(obj));
+
+ v8::Local<v8::Value> func = jsObj->Get(v8::String::New("onUpdate"));
+ if (func->IsFunction()){ //&& !func->isEmpty()){
+ v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(func);
+ f->Call(jsObj, 0, 0);
+ }
+}
+
// ======== Setup v8 juice's bindable functions ========
void Scripting::setupTimeFunctions(v8::Handle<v8::Object> dest){
@@ -134,11 +156,25 @@ v8::Handle<v8::Object> Scripting::setupEntityClass(v8::Handle<v8::Object> dest){
typedef cw::ClassWrap<Entity> CW;
CW & cw( CW::Instance() );
-
+
+ cw.BindMemVar<float, &Entity::posX>( "posX" );
+ cw.BindMemVar<float, &Entity::posY>( "posY" );
+ cw.BindMemVar<float, &Entity::angle>( "angle" );
+ cw.BindMemVar<float, &Entity::initialPosX>( "initialPosX" );
+ cw.BindMemVar<float, &Entity::initialPosY>( "initialPosY" );
+ cw.BindMemVar<float, &Entity::initialAngle>( "initialAngle" );
+
+
+ typedef convert::MemFuncInvocationCallbackCreator<Entity>
+ ICM; // typing-saver
+ cw.Set( "show", ICM::M0::Invocable<void,&Entity::show> );
+ cw.Set( "hide", ICM::M0::Invocable<void,&Entity::hide> );
+ cw.Set( "timeSinceSpawn", ICM::M0::Invocable<uint32_t,&Entity::timeSinceSpawn> );
cw.Set( "destroy", CW::DestroyObject );
+
cw.Seal(); // ends the binding process
- cw.AddClassTo( dest ); // installs BoundNative class in dest
+ cw.AddClassTo( dest );
return cw.CtorTemplate()->GetFunction();
}
@@ -169,6 +205,7 @@ namespace v8 { namespace juice { namespace convert {
// Optional:
static const NativeToJS<float> FloatToJS = NativeToJS<float>();
static const JSToNative<float> JSToFloat = JSToNative<float>();
+
}}}
namespace v8 { namespace juice { namespace cw {
@@ -184,7 +221,7 @@ namespace v8 { namespace juice { namespace cw {
}}}
//define JS-side name for classes
-JUICE_CLASSWRAP_CLASSNAME(Entity,"Entity")
+//JUICE_CLASSWRAP_CLASSNAME(Entity,"Entity")
// ======== Runtime ========
View
@@ -12,6 +12,7 @@
#define Scripting_h
class Entity;
+class GameObject;
class Scripting {
public:
@@ -25,6 +26,8 @@ class Scripting {
static v8::Handle<v8::Value> func_quit(const v8::Arguments& args);
static v8::Handle<v8::Value> func_print(const v8::Arguments& args);
+
+ static void callUpdateFunction(Entity* obj);
private:
static Scripting *instance;

0 comments on commit 964eb0d

Please sign in to comment.