Skip to content

Commit

Permalink
score works now, -50 for hitting a roid, +100 for shooting it
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed May 2, 2015
1 parent ab768e5 commit d01064c
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 22 deletions.
9 changes: 8 additions & 1 deletion control/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
<div class="viewport">
<div class="flipsnap"></div>
</div>
<button class="continue" onclick="window.location.href = '/control';"> Play </button>
<button class="continue" data-next="wizard-04" onclick="continueWizard(event)"> Continue </button>
</div>

<div id="wizard-04" style="display:none;">
<button class="back" data-next="wizard-03" onclick="continueWizard(event)"> Back </button>
<div class="ship-select"> Input your initials </div>
<input class="initials" placeholder="initials" type="text"/>
<button class="continue" onclick="confirmInitials(); window.location.href = '/control';"> Play </button>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions control/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ setShipColor = function(color){
localStorage.setItem('color',color);
}

confirmInitials = function() {
localStorage.setItem('initials', document.querySelector('.initials').value);
}

generateColors = function(){
// generates all the possible colors
var c = ['dd','77','22'];
Expand Down
2 changes: 1 addition & 1 deletion control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ fullscreenToggle = function(){

window.onload = function(){
var socket = io();
socket.emit('add plane',localStorage.getItem('color')||'0x777777');
socket.emit('add plane',localStorage.getItem('color')||'0x777777', localStorage.getItem('initials'));
}
11 changes: 11 additions & 0 deletions control/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ canvas{
font-weight: bold;
font-size: 2em;
}
.initials {
position:fixed;
bottom:0px;
left:0px;


width: calc(50% - 10px);
height: calc(50% - 10px);
margin: 5px;

}
/* attack screen */

#fire {
Expand Down
6 changes: 6 additions & 0 deletions host/game/damageinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var DamageInfo = function(owner, amt) {
this.owner = owner;
this.amt = amt;
}

module.exports = DamageInfo;
7 changes: 4 additions & 3 deletions host/game/entity/bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ var THREE = require('../libs/three');
var CANNON = require('../libs/cannon');
var global = require('../global');

var Bullet = function(position, angle) {
var Bullet = function(owner, position, vel) {
Entity.call(this);
this.owner = owner;
this.setGeometry(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshPhongMaterial({ color: 0x666666 })
Expand All @@ -13,8 +14,7 @@ var Bullet = function(position, angle) {
var shape = new CANNON.Box(new CANNON.Vec3(1, 1, 1));
var body = new CANNON.Body({mass: 10});
body.addShape(shape);
var dir = (new THREE.Vector3()).subVectors(global.game.camera.lookAtPos,global.game.camera.position).normalize().multiplyScalar(3);
body.velocity.set(dir.x, dir.y, dir.z);
body.velocity.set(vel.x, vel.y, vel.z);
body.angularDamping = 0.0;
this.setPhysicsBody(body);
this.setCollisionGroup(global.cgroup.BULLET);
Expand All @@ -36,6 +36,7 @@ Bullet.prototype.onCollide = function(e) {
if (e.body && e.body.entity && e.body.entity.applyDamage) {
console.log('Bullet colliding with: ', e.body.entity);
e.body.entity.applyDamage(Bullet.IMPACT_DAMAGE);
this.owner.score.add(100);
}
this.remove();
};
Expand Down
2 changes: 1 addition & 1 deletion host/game/entity/enemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Enemy.prototype.fire = function(){

Enemy.prototype.fireProjectile = function(){
// fires the projectile once
new Bullet(this.getPos(), new THREE.Vector3(this.motion.x,this.motion.y,5).normalize());
new Bullet(this, this.getPos(), new THREE.Vector3(this.motion.x,this.motion.y,5).normalize());
}


Expand Down
15 changes: 9 additions & 6 deletions host/game/entity/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ var THREE = require('../libs/three');
var CANNON = require('../libs/cannon');
/*jshint -W079 */ var global = require('../global');
var Bullet = require('./bullet');
var Score = require('../score');

var Player = function(color) {
var Player = function(color, initials) {
Damageable.call(this, Player.MAX_HEALTH);
// base square ... not in use anymore?
this.setGeometry(
Expand All @@ -30,6 +31,7 @@ var Player = function(color) {

// starting relative position in xy space. 0,0 is the left bottom; 1,1 is right top.
this.relativePos = {y:0,z:0};
this.score = new Score(initials, 0);
};

Player.prototype = Object.create( Damageable.prototype );
Expand Down Expand Up @@ -62,7 +64,11 @@ Player.prototype.fire = function(){

Player.prototype.fireProjectile = function(){
// fires the projectile once
new Bullet(this.getPos(), this.Forward);
new Bullet(
this,
this.getPos(),
(new THREE.Vector3()).subVectors(global.game.camera.lookAtPos,global.game.camera.position).normalize().multiplyScalar(3)
);
};

Player.prototype.applyMotion = function(motion) {
Expand All @@ -77,15 +83,12 @@ Player.prototype.applyMotion = function(motion) {
this.motion = motion;
};

function nameof(obj){
return obj.constructor.toString();
}

Player.prototype.onCollide = function(e) {
if (e.body && e.body.entity && e.body.entity.applyDamage) {
console.log('Player colliding with: ', e.body.entity);
e.body.entity.applyDamage(200);
this.applyDamage(50);
this.score.add(-50);
}
};

Expand Down
10 changes: 5 additions & 5 deletions host/game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ var hook = require('./hook');
var players = require('./player-store');
var Asteroid = require('./entity/asteroid');
var global = require('./global');

var Score = function(initials, score){
this.initials = initials;
this.score = score;
}
var Score = require('./score');

var Game = function() {
this.initPhysics();
Expand Down Expand Up @@ -41,6 +37,10 @@ Game.prototype.betweenLevel = function() {
var self = this;
var pos = self.level.points[global.LEVEL_SEGMENTS -1];
this.level.remove();
players.players.forEach(function(ply) {
self.addScore(ply.score.initials, ply.score.score);
ply.score.reset();
});
var highscoreDiv = document.getElementById('highscores');
highscoreDiv.innerHTML = "";
highscoreDiv.style.display = "";
Expand Down
4 changes: 2 additions & 2 deletions host/game/player-store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Player = require('./entity/player');
var store = { players: [] };

store.create = function(color) {
var p = new Player(color);
store.create = function(color, initials) {
var p = new Player(color, (initials || 'AAA').toUpperCase().substring(0,3));
store.players.push(p);
document.getElementById("title-gif").style.display = "none";
document.getElementById("qr_code_play").style.display = "none";
Expand Down
18 changes: 18 additions & 0 deletions host/game/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var Score = function(initials, score){
this.initials = initials;
this.score = score;
};

Score.prototype.add = function(num) {
this.score += num;
};

Score.prototype.reset = function() {
this.score = 0;
};

Score.prototype.setInitials = function(initials) {
this.initials = initials;
};

module.exports = Score;
3 changes: 2 additions & 1 deletion host/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ var THREE = require('./game/libs/three');
var socket = io();

socket.on('add plane', function(player){
console.log(player);
var color = Number(player.color);
console.log(color);
localStorage[player.playerId] = player.color;
var ply = players.create(color);
var ply = players.create(color, player.initials);
ply.userId = player.playerId;
console.log("%c ADDED PLAYER "+ply.userId,"color: #"+color.toString(16));
ply.setPos(new THREE.Vector3(0,0,0));
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ app.use(express.static(path.join(__dirname, 'static')));
var playerCount = 0;
io.on('connection', function(socket){
var playerId = ++playerCount;
socket.on('add plane', function(color){
socket.on('add plane', function(color, initials){
console.log(playerId + " joined");
var player = {playerId:playerId,color:color};
var player = {playerId:playerId,color:color,initials:initials};
io.emit('add plane',player);
});
socket.on('disconnect', function(){
Expand Down

0 comments on commit d01064c

Please sign in to comment.