var GameObject = { textWarned: false, destroyed: false, create: function(template, x, y, image, layer, scene) { var newObj = $.extend(true, {}, template); GameObject.object.call(newObj, x, y, image, scene); layer = layer || 0; newObj.renderer.layerDepth = layer; if(!GameObject.allObjects) GameObject.allObjects = []; GameObject.allObjects.push(newObj); Game.reorderObjects(); return newObj; }, inherit: function(base, child) { var newObj = $.extend(true, {}, base); var newObj = $.extend(true, newObj, child); newObj.base = base; return newObj }, object: function(x, y, image, scene) //DO NOT use any functions with prefix g_ in your game! { this.active = true; this.name = "Object"; this.image = ""; this.ignoreCollisions = false; this.renderer = { image: image, subImage: 0, alpha: 1, visible: true, //used to correct NaN g_subImgOld: 0, g_layerDepth: 0, draw: function(x, y, w, h, rotation) { if(isNaN(this.subImage)) { this.subImage = this.g_subImgOld; } else { this.g_subImgOld = this.subImage; } var subImage = Math.round(this.subImage); subImage %= this.image.subImages; var c = subImage % this.image.subImagesPerRow; var r = Math.floor(subImage / this.image.subImagesPerRow); var sw = (this.image.g_image.width / this.image.subImagesPerRow); var sh = (this.image.g_image.height / Math.ceil(this.image.subImages / this.image.subImagesPerRow)); var sx = sw * c; var sy = sh * r; this.image.draw(sx, sy, sw, sh, x, y, w, h, rotation, this.alpha); } } Object.defineProperty(this.renderer, "layerDepth", { get: function() { return this.g_layerDepth; }, set: function(val) { if (this.g_layerDepth != val) { this.g_layerDepth = val; Game.reorderObjects(); } } }) this.transform = { position: new vector(x, y), velocity: new vector(0, 0), rotation: 0, width: image? image.width : 0, height: image? image.height: 0, scale: new vector(1, 1), pointTowards: function(point) { var delta = point.subtract(this.position); this.rotation = delta.toAngle(); }, left: function() { return this.g_dir(0); }, right: function() { return this.g_dir(180); }, forward: function() { return this.g_dir(90); }, back: function() { return this.g_dir(270); }, g_dir: function(offset) { var myAngleInRadians = (this.rotation + offset) * (Math.PI / 180); return angleVector = new vector( -Math.cos(myAngleInRadians), -Math.sin(myAngleInRadians) ); } }; this.g_destroy = false; this.g_init = function(scene) { if(scene == null) { Game.activeScene.addObject(this); } else { scene.addObject(this); } if (typeof this.start == 'function') { this.start(); } } /*this.g_toHTML = function() { return '' + this.text + ''; }*/ this.defaultDraw = function() { if(this.renderer.image) { var offX = GameWindow.cameraPosition.x; var offY = GameWindow.cameraPosition.y; this.renderer.draw(this.transform.position.x + offX, this.transform.position.y + offY, this.transform.width * this.transform.scale.x, this.transform.height * this.transform.scale.y, this.transform.rotation); } } this.g_draw = function() { if(!this.g_destroy) { if(this.draw) { this.draw(); } else { this.defaultDraw(); /*else //??? makes entire screen colored. { GameWindow.canvasContext.fillStyle = "rgb("+ this.transform.position.y+" ," + this.transform.position.x + ",0)"; // sets the color to fill in the rectangle with GameWindow.canvasContext.fillRect( this.transform.position.x - this.transform.width / 2, this.transform.position.y - this.transform.height / 2, this.transform.width, this.transform.height); }*/ } } } this.g_checkClick = function(thisFrame) { var hit = false; var clickPos = GameInput.mousePosition; //hw = halfWidth, hh = halfHeight var hw = this.transform.width / 2; var hh = this.transform.height / 2; if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw) { if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh) { hit = true; } } if (hit) { if (thisFrame) { this.onClick && this.onClick(); } else { this.onMouseHeld && this.onMouseHeld(); } } } this.g_updateStep = function(deltaTime) { if(this.g_destroy) { Game.activeScene.activeObjects.splice(Game.activeScene.activeObjects.indexOf(this), 1); delete this; } else { if (typeof this.update == 'function') { this.update(deltaTime); } this.transform.position.x += this.transform.velocity.x; this.transform.position.y += this.transform.velocity.y; //check for clicks if (GameInput.mousePressed()) { this.g_checkClick(true); } //check for clicks if (GameInput.mouseHeld()) { this.g_checkClick(false); } if(!this.ignoreCollisions && !this.destroyed) { for(var i = 0; i < Game.activeScene.activeObjects.length; i++) { if(Game.activeScene.activeObjects[i] != this && !Game.activeScene.activeObjects[i].ignoreCollisions) { var hit = false; var o2 = Game.activeScene.activeObjects[i]; //hw = halfWidth, hh = halfHeight var hw = (this.transform.width * this.transform.scale.x) / 2; var hh = (this.transform.height * this.transform.scale.y) / 2; var ohw = (o2.transform.width * o2.transform.scale.x) / 2; var ohh = (o2.transform.height * o2.transform.scale.y) / 2; //CHECK FROM OBJ PERSPECTIVE var p = [ [this.transform.position.x - hw, this.transform.position.y - hh], [this.transform.position.x + hw, this.transform.position.y - hh], [this.transform.position.x - hw, this.transform.position.y + hh], [this.transform.position.x + hw, this.transform.position.y + hh] ]; for(var pi = 0; pi < p.length; pi++) { if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw) { if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh) { hit = true; } } } //CHECK FROM OTHER PERSPECTIVE var p = [ [o2.transform.position.x - ohw, o2.transform.position.y - ohh], [o2.transform.position.x + ohw, o2.transform.position.y - ohh], [o2.transform.position.x - ohw, o2.transform.position.y + ohh], [o2.transform.position.x + ohw, o2.transform.position.y + ohh] ]; for(var pi = 0; pi < p.length; pi++) { if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw) { if(p[pi][1] > this.transform.position.y - hh && p[pi][1] < this.transform.position.y + hh) { hit = true; } } } if(hit) if (!this.destroyed && typeof this.onCollision == 'function') { this.onCollision(o2); } } } } } } this.destroy = function() { if(!this.g_destroy) { if (typeof this.onDestroy == 'function') { this.onDestroy(); } this.destroyed = true; this.g_destroy = true; GameObject.allObjects.remove(this); } } this.g_init(scene); } }