I have this code for my game (there is more, but this is the important part):
function BaseGameObject() {}
BaseGameObject.prototype.geometry = {
shape: 'none'
};
BaseGameObject.prototype.type = null;
BaseGameObject.prototype.texture = 'black';
function GameObject(x, y) {
this.geometry.x = x;
this.geometry.y = y;
}
GameObject.inheritsFrom(BaseGameObject);
function Rectangle(x, y, width, height) {//todo solve reverse inheritance in geometry bject
this.geometry.x = x;
this.geometry.y = y;
this.geometry.w = width;
this.geometry.h = height;
}
Rectangle.inheritsFrom(GameObject);
Rectangle.prototype.geometry.shape = 'rectangle';
function Square(x, y, size) {
this.geometry.x = x;
this.geometry.y = y;
this.geometry.w = size;
this.geometry.h = size;
}
Square.inheritsFrom(Rectangle);
InheritsFrom looks like this:
Function.prototype.inheritsFrom = function (parentClassOrObject) {
if (parentClassOrObject.constructor == Function) {
//Normal Inheritance
this.prototype = new parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject.prototype;
} else {
//Pure Virtual Inheritance
this.prototype = parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject;
}
return this;
};
My problem is, when I do new Square(10, 500, 20)
, geometry object gets changed even for BaseGameObject prototype, because the object is stored as reference.
My question is, can I somehow have separate geometry object for each object, that inherits geometry object from parent prototype, but changes aren't reflected the other way?