Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by alexandre

How would I go about writing a plugin module of utility functions--say for Box2D--something I could use like
var myDisc = Box2dUtils.makeDisc({x:100, y:100},20,1,0,0,myWorld);

I tried this:
ig.module (
	'game.box2d-utils'
)
.requires (
	'plugins.box2d.debug',
	'plugins.box2d.game',
	'plugins.box2d.entity'
)
.defines (function ()
{
	makeBox: function (pos, size, density, restitution, friction, world)
	{
		var bodyDef = new b2.BodyDef();
		bodyDef.position.Set (
			pos.x * b2.SCALE,
			pos.y * b2.SCALE
		);

		var body = world.CreateBody(bodyDef);

		var shapeDef = new b2.PolygonDef();
		shapeDef.SetAsBox (
			size.x / 2 * b2.SCALE,
			size.y / 2 * b2.SCALE
		);

		shapeDef.density = density;
		shapeDef.restitution = restitution;
		shapeDef.friction = friction;
		body.CreateShape(shapeDef);
		body.SetMassFromShapes();

		return body;
	},
  	
	makeDisc: function (pos, radius, density, restitution, friction, world)
	{
		var bodyDef = new b2.BodyDef();
		bodyDef.position.Set (
			pos.x * b2.SCALE,
			pos.y * b2.SCALE
		);

		var body = world.CreateBody(bodyDef);

		var shapeDef = new b2.CircleDef();
		shapeDef.radius = radius * b2.SCALE;

		shapeDef.density = density;
		shapeDef.restitution = restitution;
		shapeDef.friction = friction;
		body.CreateShape(shapeDef);
		body.SetMassFromShapes();

		return body;
	}
});

But nope. Seems like
makeBox: function (pos, size, density, restitution, friction, world)

triggers an "Unexpected identifier" error.

Obviously a JS misunderstanding on my part.

1 decade ago by gxxaxx

Hi Alexandre,

I'm not much of a javascript programmer yet either.
I just kinda poke the code with a stick until it works.

Based on some fine plugins others on the forum have produced, I use a skeleton for my plugins and utility classes like the following:

ig.module(
	'plugins.myfile'
)
.requires(
	'impact.impact'
)
.defines(function(){
ig.Myfile = ig.Class.extend({
	game: null,
	param1: 1,
	param2: 'string',
    
	init: function(theGame){
      		this.game = theGame;
	},
	
	mymethod:: function(var) {
		// do stuff with var;
	},

	myothermethod:function() {
	}

});

});


Then in main.js I include the following elements:

.requires(
	'......',
	'plugins.myfile',
	'......'
)
MyGame = ig.Game.extend({

	myFile: null,

	init: function() {
		code here;
		this.myFile = new ig.Myfile(this);
		code here;
	}
)

According to the way things go, there is sure to be a missing comma or one too many commas... but this might give you an idea.

Hope if helps

1 decade ago by alexandre

Hi Claude

Thanks for your help. I was looking at ways to create a global Box2D utility functions module (I know, I said the "g" word again), avoiding the need for this (per your example),

this.myFile = new ig.Myfile(this);

Not sure it's a good idea, but it sure seemed like it at the time. :)

Alex

1 decade ago by dominic

Alexandre, In your initial code you defined two global functions: makeBox() and makeDisc(). You should be able to call these from anywhere in your code:

// Note, no 'Box2dUtils' prefix!
makeDisc({x:100, y:100},20,1,0,0,myWorld);

If you want to have the Box2dUtils prefix, you could create a JS object with that name:
Box2dUtils = {
	makeBox: function (pos, size, density, restitution, friction, world) {
		...
	},

	makeDisc: function (pos, radius, density, restitution, friction, world) {
		...
	}
};

1 decade ago by alexandre

Thanks dominic. Works like a charm, and one more Javascript lesson learned.
Page 1 of 1
« first « previous next › last »