topical media & game development
mobile-query-game-jquery.gamequery.ext.js / js
gameQuery.ext
Copyright (c) 2010 Rob Britton
licensed under the MIT (MIT-LICENSE.txt)
.extend(
QuadTree
A space-partitioning tree useful for efficient collision detection.
.gameQuery.QuadTree = function (width, height){
this.width = width; this.height = height;
this.root = null;
}
.gameQuery.QuadTreeNode.prototype.add = function(obj, x, y){
if (this.child){
if (this.objects.length == this.splitSize){
this.split();
this.add(obj, x, y);
}else{
this.objects.push({obj: obj, x: x, y: y});
}
}else{
var cx = x - this.x;
var cy = y - this.y;
if (cx < this.width / 2){
if (cy < this.height / 2){
this.children.tl.add(obj, x, y);
}else{
this.children.bl.add(obj, x, y);
}
}else{
if (cy < this.height / 2){
this.children.tr.add(obj, x, y);
}else{
this.children.br.add(obj, x, y);
}
}
}
}
.gameQuery.QuadTreeNode(this.x, this.y, this.width / 2, this.height / 2),
tr: new .gameQuery.QuadTreeNode(this.x, this.y + this.height / 2, this.width / 2, this.height / 2),
br: new .gameQuery.QuadTreeNode.prototype.get = function(x, y, width, height){
if (this.child){
if (!.map(this.objects, function(obj, i) { return obj.obj; });
}
}else{
var ret = [];
var next;
for (var i in this.children){
if ((next = this.children[i].get(x, y, width, height)) !== null)
.gameQuery.QuadTree.prototype.add = function(obj, x, y){
if (!this.root){
this.root = new .gameQuery.QuadTree.prototype.get = function(x, y, width, height){
if (!this.root)
return [];
return this.root.get(x, y, width, height);
}
.gameQuery.keyDown = function(what){
return .gameQuery.keycodes[what]];
}
.gameQuery.keycodes[String.fromCharCode(i)] = i;
}
for (var i = 65; i <= 90; i++){
.gameQuery.keycodes["num" + i] = i + 96;
}
for (var i = 1; i <= 12; i++){
.gameQuery.getTimeElapsed = function (){
if (.gameQuery.getTimeElapsed.lastFrame = new Date();
var currentTime = new Date();
var gap = new Date();
gap.setTime(currentTime.getTime() - .gameQuery.getTimeElapsed.lastFrame = currentTime;
return gap.getMilliseconds();
}
View classes for handling scrolling backgrounds
parameter: viewport The jQuery element where the view is displayed
parameter: background The jQuery element to be scrolled around in the viewport
.extend({
width: background.width(),
height: background.height()
}, options);
background.css(
{
backgroundRepeat: "repeat",
backgroundPosition: "0px 0px",
backgroundImage: "url(" + options.imageURL + ")",
width: options.width + "px",
height: options.height + "px"
}
);
return background;
}
.gameQuery.View.prototype.scroll = function(dx, dy){
if (this.background === null){
return;
}
var pos = this.background.position();
var x = dx - pos.left;
var y = dy - pos.top;
this.anchor(x, y);
}
.gameQuery.LockedView = function(target, viewport, background, options){
.gameQuery.LockedView.prototype = .gameQuery.LockedView.prototype.frame = function(timeStep){
var left = this.target.position().left;
var top = this.target.position().top;
this.anchor(left - Math.floor(this.viewport.width() / 2), top - Math.floor(this.viewport.height() / 2));
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.