Skip to content

Instantly share code, notes, and snippets.

@mscandal
Created July 27, 2014 20:33
Show Gist options
  • Select an option

  • Save mscandal/61fad5fef4b560e3349b to your computer and use it in GitHub Desktop.

Select an option

Save mscandal/61fad5fef4b560e3349b to your computer and use it in GitHub Desktop.
var ship, bullet;
window.onload = function() {
var angle;
var button1 = 'up';
var button2 = 'up';
ship = (function() {
var xVel = 10;
var yVel = 10;
var vel = 0;
var x = 100;
var y = 100;
var el = document.getElementById('ship');
el.style.height = '100px';
el.style.width = '100px';
el.style.border = '1px solid black';
el.style.borderRight = '5px solid black';
el.style.position = 'fixed';
el.style.top = y + 'px';
el.style.left = x + 'px';
return {
update: function() {
if(button1 == 'down') {
vel = vel + 2;
if(vel > 20) {
vel = 20;
}
}
if(button2 == 'down') {
bullet.fire(x, y, angle);
}
var rad = 2 * Math.PI * angle / 360;
yVel += vel * Math.sin(rad);
xVel += vel * Math.cos(rad)
y += yVel;
x += xVel;
el.style.top = y + 'px';
el.style.left = x + 'px';
el.style.webkitTransform = 'rotate(' + angle + 'deg)';
xVel = xVel * 0.9;
yVel = yVel * 0.9;
vel = vel * 0.7;
}
};
})();
bullet = (function() {
var xVel = 0;
var yVel = 0;
var vel = 50;
var x = 100;
var y = 100;
var el = document.getElementById('bullet');
el.style.width = '10px';
el.style.height = '10px';
el.style.webkitBorderRadius = '5px';
el.style.border = '1px solid black'
el.style.position = 'fixed';
return {
update: function() {
y += yVel;
x += xVel;
el.style.top = y + 'px';
el.style.left = x + 'px';
},
fire: function(newX, newY, angle) {
console.log('FIRE');
x = newX;
y = newY;
var rad = 2 * Math.PI * angle / 360;
yVel = vel * Math.sin(rad);
xVel = vel * Math.cos(rad);
console.log(xVel, yVel);
}
};
})();
setInterval(function() {
ship.update();
bullet.update();
}, 50);
var socket = io('http://localhost');
socket.on('command', function (data) {
if(data.button1) {
button1 = data.button1;
} else if(data.button2) {
button2 = data.button2;
} else if(data.wheel) {
angle = data.wheel * 2;
console.log('wheel', data.wheel);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment