this.boundingBox = this.boundingBox.resize(w, this.layerHeight);
var nb = this.negBleed.x
- , v = this.canvasWidth = w + nb + this.posBleed.x;
+ , v = this.canvasWidth = Math.ceil(w + nb + this.posBleed.x);
this.layer.width(w).css('margin-left', (-nb)+'px')
this.canvas.width(v);
// this.canvas.css({
this.boundingBox = this.boundingBox.resize(this.layerWidth, h);
var nb = this.negBleed.y
- , v = this.canvasHeight = h + nb + this.posBleed.y;
+ , v = this.canvasHeight = Math.ceil(h + nb + this.posBleed.y);
this.layer.height(h).css('margin-top', (-nb)+'px')
this.canvas.height(v);
// this.canvas.css({
['', '-moz-', '-webkit-'].reduce(
function(values, prefix){
values[prefix+'transform'] = trans;
- values[prefix+'origin'] = origin;
+ values[prefix+'transform-origin'] = origin;
return values;
}, {}) );
return this;
Circle = new Y.Class('Circle', Shape, {
_cssClasses : 'portal layer shape circle',
- init : function initCircle(radius){
+ init : function initCircle(radius, centerTL){
Layer.init.call(this);
var d = radius * 2;
- this.radius = this.negBleed.x = this.negBleed.y = radius;
+ this.radius = radius;
+ if (!centerTL)
+ this.negBleed.x = this.negBleed.y = radius;
this.width(d).height(d);
// .origin(radius,radius);
},
drawShape : function drawShape(ctx){
- var r = this.radius;
- ctx.arc(0,0, r, 0, Math.PI*2, false);
+ var r = this.radius, nb = this.negBleed;
+ ctx.arc(r-nb.x,r-nb.y, r, 0, Math.PI*2, false);
ctx.fill();
ctx.stroke();
}
setupUI();
// B = bullets.attr(0);
- // T = B.trajectory;
+ // R = B.trajectory;
}
function rand(a,b){ return a + Math.random()*(b-a); }
function randOpenLoc(){
+ var BP = Bullet.prototype
+ , w = BP.width, h = BP.height
+ , offX = BP.offsetX, offY = BP.offsetY
+ ;
do {
- var x = rand(0,COLUMNS*REF_SIZE)
- , y = rand(0,ROWS*REF_SIZE);
- } while ( LBT.pathmap.get(x,y, x+6,y+6).size() );
+ var x = rand(-offX, COLUMNS*REF_SIZE-w)
+ , y = rand(-offY, ROWS*REF_SIZE-h);
+ } while ( LBT.pathmap.get(x+offX,y+offY, x+w,y+h).size() );
return new math.Vec(x,y);
}
return obj;
},
- moveBlocked : function moveBlocked(agent, trj, to){
+ moveBlocked : function moveBlocked(agent, trj, to, bb){
+ bb = bb || agent.boundingBox;
var blockers, blocker, msg
, side = null
- , bb = agent.boundingBox
, bw = bb.width, bh = bb.height
, offX = agent.offsetX, offY = agent.offsetY
msg = blocker+' on the ';
var B = blocker.boundingBox;
- if (bb.x2 <= B.x1 && x2 > B.x1) {
+ if (bb.x2 <= B.x1 && x2 >= B.x1) {
msg += 'left';
side = B.sides.left;
to = trj.pointAtX(B.x1-bw-offX-1);
- } else if (bb.x1 >= B.x2 && x1 < B.x2) {
+ } else if (bb.x1 >= B.x2 && x1 <= B.x2) {
msg += 'right';
side = B.sides.right;
to = trj.pointAtX(B.x2-offX+1);
- } else if (bb.y2 <= B.y1 && y2 > B.y1) {
+ } else if (bb.y2 <= B.y1 && y2 >= B.y1) {
msg += 'top';
side = B.sides.top;
to = trj.pointAtY(B.y1-bh-offY-1);
- } else if (bb.y1 >= B.y2 && y1 < B.y2) {
+ } else if (bb.y1 >= B.y2 && y1 <= B.y2) {
msg += 'bottom';
side = B.sides.bottom;
to = trj.pointAtY(B.y2-offY+1);
this.elapsed += t;
_to = to = this.parametric(this.elapsed);
- test = this.pathmap.moveBlocked(o, this, to);
+ test = this.pathmap.moveBlocked(o, this, to, bb);
// Blocked! Reflect trajectory
if ( test ) {
side = test.side;
blockers = test.blockers;
+ if (!side) {
+ console.error('Null side! Something horrible has occurred!');
+ continue;
+ }
+
if ( blockers.length > 1 ) {
to = loc;
ng = this.p1; // XXX: recalculate?
+ console.log('corner!', this, 'to:', to, 'ng:', ng);
} else {
ng = math.reflect(this.p2, side);
}
- var og = this.p2;
- this.reset(to.x,to.y, ng.x,ng.y);
+ var x = to.x, y = to.y
+ , og = this.p2
+ ;
+ this.reset(x,y, ng.x,ng.y);
if (this.depth) {
console.log([
].join('\n'));
}
- bb = new Loc.Rect(to.x,to.y, ng.x,ng.y);
+ bb = new Loc.Rect(x,y, x+bw,y+bh);
ng = null;
this.owner.render(this.game.level);
this.owner = owner;
this.game = owner.game;
Thing.init.call(this, owner.align);
- var loc = owner.loc
- , x1 = loc.x
- , y1 = loc.y;
+ var loc = owner.getTurretLoc()
+ , x1 = loc.x, y1 = loc.y;
this.setLocation(x1,y1);
this.trajectory = new Trajectory(this, x1,y1, x2,y2, this.stats.move*REF_SIZE/1000);
blocking : true,
// Bounding box size
- width : REF_SIZE*0.7,
- height : REF_SIZE*0.6,
+ width : REF_SIZE*0.55,
+ height : REF_SIZE*0.55,
// Attributes
stats: {
return p;
},
+ getTurretLoc : function getTurretLoc(){
+ var loc = this.loc
+ , barrel = this.barrel, bb = barrel.boundingBox
+ , theta = barrel.transform.rotate, sin = Math.sin(theta), cos = Math.cos(theta)
+ , x0 = bb.x2-bb.x1, y0 = (bb.y2-bb.y1)/2
+ , x = loc.x + bb.x1 + x0*cos - y0*sin
+ , y = loc.y + bb.y1 + x0*sin + y0*cos
+ ;
+ // console.log('getTurretLoc()', 'loc:', loc, 'bb.(x2,y2):', [bb.x2,bb.y2], '(x,y):', [x,y]);
+ return new math.Vec(x,y);
+ },
+
/// Rendering Methods ///
render : function render( parent ){
if (this.shape) this.shape.remove();
- var w = this.width
- , h = this.height
- , tw = w / 4
- , th = h * 0.9
- , sw = w * 0.05
+ var w = this.width, w2 = w/2
+ , h = this.height, h2 = h/2
+
+ , r = w / 4
+ , cw = w * 0.75
+ , ch = h / 6
;
this.shape =
new Rect(w,h)
.position(this.loc.x, this.loc.y)
- .attr('fillStyle', '#E73075')
- .append(
- new Triangle(0,th, tw,th/2)
- .position(w-tw-sw, (h-th)/2)
- .attr('fillStyle', '#980011')
- )
+ .fill('#E73075')
.appendTo( parent );
+ this.cannon = new Circle(r, true)
+ .position(w2-r+1, h2-r+1)
+ .fill('#A72F5B')
+ .appendTo( this.shape )
+ ;
+
+ this.barrel = new Rect(cw,ch)
+ .position(w2-2, h2-ch/2)
+ .origin(2, ch/2)
+ .fill('#2E62C9')
+ .appendTo( this.shape )
+ ;
+
return this;
}
return this;
},
+ getTurretLoc : function getTurretLoc(){
+ return this.loc;
+ },
/// Rendering Methods ///
Key = {
+ fire : 32,
+
_dirFromKey : {
37: "left", 38: "up", 39: "right", 40: "down",
65: "left", 87: "up", 68: "right", 83: "down"
}
};
+
+
Player = new Y.Class('Player', {
activeKeys : null,
shift : false, ctrl : false, meta : false, alt : false,
leftMouse : false, middleMouse : false, rightMouse : false,
- action : null,
- target : null,
+ queue : null,
+
init : function init(game, tank){
Y.bindAll(this);
this.activeKeys = new Y.YArray();
+ this.queue = [];
this.tank = tank;
this.game = game;
.bind('keydown', this.keydown)
.bind('keyup', this.keyup)
.bind('mousedown', this.mousedown)
- .bind('mouseup', this.mouseup);
+ .bind('mouseup', this.mouseup)
+ .bind('mousemove', this.mousemove)
+ ;
},
updateMeta : function updateMeta(evt){
mousedown : function mousedown(evt){
switch (evt.which) {
- case 1: evt.leftMouse = true; break;
+ case 1: evt.leftMouse = true;
+ var off = this.game.viewport.offset();
+ this.queue.push({
+ type : 'fire',
+ x : evt.pageX - off.left,
+ y : evt.pageY - off.top
+ });
+ break;
case 2: evt.rightMouse = true; break;
case 3: evt.middleMouse = true; break;
}
this.updateMeta(evt);
},
+ mousemove : function mousemove(evt){
+ var shape = this.tank.shape
+ , cannon = this.tank.cannon
+ , barrel = this.tank.barrel
+ , off = shape.offset()
+ , w = shape.width(), h = shape.height()
+ , x = off.left + w/2 - evt.pageX
+ , y = off.top + h/2 - evt.pageY
+ , theta = Math.atan2(-y,-x);
+
+ barrel.rotate(theta);
+ },
+
+
+
act : function act(){
- if (this.tank.dead || !this.activeKeys.size() )
+ if (this.tank.dead)
return this.tank;
+ var action = this.queue.shift();
+
+ if (action && action.type === 'fire')
+ this.tank.fireProjectile(action.x, action.y);
+ else if ( this.activeKeys.size() )
+ this.move();
+
+ return this.tank;
+ },
+
+ move : function move(){
var dir = this.activeKeys
.unique()
.sort()
.join(' ');
- if (dir) this.move(dir);
+ if (!dir) return;
- // var active = this.activeKeys;
- // for (var key in Key._dirFromKey)
- // if ( active.has(key) ) {
- // this.move( Key._dirFromKey[key] );
- // return this.tank;
- // }
-
- return this.tank;
- },
-
- move : function move(dir){
var tank = this.tank
, toLoc = tank.loc.moveByDir(dir, (tank.stats.move * SQUARETH))