Skip to content

Instantly share code, notes, and snippets.

@bnolan
Created June 3, 2010 21:07
Show Gist options
  • Save bnolan/424490 to your computer and use it in GitHub Desktop.
Save bnolan/424490 to your computer and use it in GitHub Desktop.
var Item = function(world, id){
var self = this;
var position, scale, asset,
properties = {};
this.say = function(message){
$LOGGER.log("<b>Item</b>: " + message);
var p = position.subtract(asset.getMenuAnchor());
var div = $("<div />");
div.addClass('speech');
div.css({
left : p.x + 20,
top : p.y + 50
});
div.appendTo('#playfield');
div.delay(800 + message.split(" ").length * 200).animate({top : p.y - 50, opacity: 0}, function(){
div.remove();
});
var span = $("<span />");
span.text(message);
span.appendTo(div);
span.css({
top : 110 - span.height() / 2
})
}
this.redraw = function(){
if(asset){
asset.redraw();
}
}
this.setId = function(i){
id = i;
}
this.getId = function(){
return id;
}
this.getWorld = function(){
return world;
}
this.setWorld = function(w){
if(w instanceof World){
world = w;
}else{
throw "Not a World object";
}
}
this.remove = function(){
if(asset){
asset.remove();
}
world.removeItem(id);
}
this.setAsset = function(a){
asset = a;
}
this.getAsset = function(){
return asset;
}
this.setPosition = function(v){
if(v instanceof Vector){
position = new Vector(v);
this.redraw();
}else{
throw "Not a Vector object"
}
}
this.getPosition = function(){
return new Vector(position);
}
this.getProperties = function(){
return {
id : id,
type : 'Item',
position : position ? position.toJSON() : null,
asset : asset ? asset.toJSON() : null
};
}
this.getProperty = function(name){
return properties[name];
}
// todo - make it trigger any redraw events etc
this.setProperty = function(name, value){
if("id" == name){
id = value;
return;
}
if("position" == name){
position = Instantiate(Vector, value);
}
if("asset" == name){
asset = Instantiate(Asset, value);
asset.setItem(this);
}
properties[name] = value;
}
this.setProperties = function(obj){
// We need to normalize ids..
var key;
for(key in obj){
this.setProperty(key, obj[key]);
}
}
this.notifyChange = function(){
$SOCKET.send($.toJSON(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment