@@ -0,0 +1,208 @@
const THREE = require('./ThreeHelpers');

const Utils = {
// Rotate an object around an arbitrary axis in object space
rotateAroundObjectAxis: function(object, axis, radians) {
var rotObjectMatrix = new THREE.Matrix4();
rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);
object.matrix.multiply(rotObjectMatrix);
object.rotation.setFromRotationMatrix(object.matrix);
},

// Rotate an object around an arbitrary axis in world space
rotateAroundWorldAxis: function(object, axis, radians) {
var rotWorldMatrix = new THREE.Matrix4();
rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
rotWorldMatrix.multiply(object.matrix); // pre-multiply
object.matrix = rotWorldMatrix;
object.rotation.setFromRotationMatrix(object.matrix);
},

GetDistance: function(v1, v2) {
var dx = v1.x - v2.x;
var dy = v1.y - v2.y;
var dz = v1.z - v2.z;
return Math.sqrt(dx*dx+dy*dy+dz*dz);
},

UniqueArr: function(a) {
var temp = {};
for (var i = 0; i < a.length; i++)
temp[a[i]] = true;
var r = [];
for (var k in temp)
r.push(k);
return r;
},

timeStamp: function() {
var now = new Date();
var date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ];
var time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];
time[0] = ( time[0] < 12 ) ? time[0] : time[0] - 12;
time[0] = time[0] || 12;
for ( var i = 1; i < 3; i++ ) {
if ( time[i] < 10 ) {
time[i] = "0" + time[i];
}
}
return date.join("/") + " " + time.join(":");
},

Log: function(msg) {

if(typeof(msg) != 'object') {
console.log("["+this.timeStamp()+"] "+msg);
} else {
console.log(msg);
}
},

MsgBoard: function(msg) {
$('#msgboard').fadeIn(1000);
$('#msgboard_msg').html("<font color='#FF0000'>"+msg+"</font>");
setTimeout(function() {
$('#msgboard').fadeOut(1000);
}, 2000);
},

// CreateBoundingBox2: function(obj) {
// var object3D = obj.mesh;
// var box = null;
// object3D.geometry.computeBoundingBox();
// box = geometry.boundingBox;


// var x = box.max.x - box.min.x;
// var y = box.max.y - box.min.y;
// var z = box.max.z - box.min.z;

// obj.bbox = box;

// var bcube = new THREE.Mesh( new THREE.BoxGeometry( x, y, z ),
// new THREE.MeshNormalMaterial({ visible: false, wireframe: true, color: 0xAA3333}) );

// game.scene.add(bcube);
// var bboxCenter = box.center();
// bcube.translateX(bboxCenter.x);
// bcube.translateY(bboxCenter.y);
// bcube.translateZ(bboxCenter.z);
// obj.bcube = bcube;
// object3D.add(bcube);

// bcube.that = obj.mesh.that;
// },

// CreateBoundingBox: function(obj) {
// var object3D = obj.mesh;
// var box = null;
// object3D.traverse(function (obj3D) {
// var geometry = obj3D.geometry;
// if (geometry === undefined) {
// return;
// }
// geometry.computeBoundingBox();
// if (box === null) {
// box = geometry.boundingBox;
// } else {
// box.union(geometry.boundingBox);
// }
// });


// var x = box.max.x - box.min.x;
// var y = box.max.y - box.min.y;
// var z = box.max.z - box.min.z;

// obj.bbox = box;

// var bcube = new THREE.Mesh( new THREE.BoxGeometry( x, y, z ),
// new THREE.MeshNormalMaterial({ visible: false, wireframe: true, color: 0xAA3333}) );
// var bboxCenter = box.center();
// bcube.translateX(bboxCenter.x);
// bcube.translateY(bboxCenter.y);
// bcube.translateZ(bboxCenter.z);
// obj.bcube = bcube;
// object3D.add(bcube);

// bcube.that = obj.mesh.that;

// game.targets.push(bcube);
// },

rgbToHex: function(r, g, b) {
if(r < 0) r = 0;
if(g < 0) g = 0;
return "0x" + this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
},

rgbToHex2: function(r, g, b) {
if(r < 0) r = 0;
if(g < 0) g = 0;
return "#" + this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
},

componentToHex: function(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
},


// GetWorldYVector: function(vector) {
// var world = game.terrain.GetNoise();
// var x = Math.round(vector.x/10)+world.length/2;
// var z = Math.round(vector.z/10)+world.length/2;
// var y = 0;
// if(x < world.length-1) {
// if(world[x] != undefined && z < world[x].length-1) {
// y = world[x][z]*200;
// }
// } else {
// y = 0;
// }
// return y;
// },


// GetWorldY: function(mesh) {
// var world = game.terrain.GetNoise();
// var x = Math.round(mesh.position.x/10)+world.length/2;
// var z = Math.round(mesh.position.z/10)+world.length/2;
// var y = 0;
// if(x < world.length-1) {
// if(world[x] != undefined && z < world[x].length-1) {
// y = world[x][z]*200;
// }
// } else {
// y = 0;
// }
// return y;
// },


ReleasePointer: function() {
var instructions = document.getElementsByTagName("body")[0];
instructions.removeEventListener( 'click', instrClick);
keys_enabled = 0;
document.exitPointerLock = document.exitPointerLock ||
document.mozExitPointerLock ||
document.webkitExitPointerLock;
document.exitPointerLock();

},

// http://www.html5rocks.com/en/tutorials/pointerlock/intro/
LockPointer: function() {
var instructions = document.getElementsByTagName("body")[0];
instructions.addEventListener( 'click', this.instrClick, false);
},

instrClick: function( event ) {
var element = document.body;
keys_enabled = 1;
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}
};

module.exports = Utils;
@@ -1,6 +1,6 @@
const util = require('util');
const Object3D = require('./Object3D');
const THREE = require('three');
const THREE = require('./ThreeHelpers');

function Water() {
Object3D.call(this);
@@ -17,15 +17,13 @@ function World() {
};

World.prototype.Load = function(filename, wallHeight, blockSize) {
debugger;
this.wallHeight = wallHeight;
this.blockSize = blockSize;
this.readWorld(filename);
this.readMap();
};

World.prototype.readMap = function() {
debugger;
if(this.map == undefined) {
var that = this;
setTimeout(function() {
@@ -35,16 +33,13 @@ World.prototype.readMap = function() {
return;
}

debugger; //KJZ DO WE EVER GET HERE?

game.worldMap = new Array(this.map.length);
for(var i = 0; i < game.worldMap.length; i++) {
game.worldMap[i] = new Array();
}
this.mapHeight = this.blockSize*this.map.length;
this.mapWidth = this.blockSize*this.map.length;

debugger;
for(var cy = 0; cy < this.map.length; cy+=this.chunkSize) {
var alpha = 0;
var total = 0;
@@ -87,7 +82,6 @@ World.prototype.readMap = function() {
};

World.prototype.readWorld = function(filename) {
debugger;
// Read png file binary and get color for each pixel
// one pixel = one block
// Read RGBA (alpha is height)
@@ -99,7 +93,6 @@ World.prototype.readWorld = function(filename) {
var ctx = document.createElement('canvas').getContext('2d');
var that = this;
image.onload = function() {
debugger;
ctx.canvas.width = image.width;
ctx.canvas.height = image.height;
ctx.drawImage(image, 0, 0);
@@ -124,7 +117,6 @@ World.prototype.readWorld = function(filename) {

console.log("Read world complete.");
game.chunkManager.maxChunks = (that.height / that.chunkSize)*(that.height/that.chunkSize);
debugger;

};
};
@@ -1,4 +1,3 @@
require('./utils');
const Game = require('./Game');
const game = new Game();
window.game = game;
@@ -1,4 +1,5 @@
const THREE = require('three');
const THREE = require('../ThreeHelpers');
const Utils = require('../Utils');

function Enemy() {
this.type = "enemy";
@@ -148,7 +149,7 @@ Enemy.prototype.Create = function(x, y ,z, shotType) {
};

Enemy.prototype.Draw = function(time, delta) {
var dist = GetDistance(this.mesh.position, game.player.mesh.position);
var dist = Utils.GetDistance(this.mesh.position, game.player.mesh.position);
if(dist > 20) {
// Optimization for performance, skipping frames when far away.
this.skipDraw = Math.floor(dist/4);
@@ -1,5 +1,6 @@
const util = require('util');
const Enemy = require('./Enemy');
const Utils = require('../Utils');

function Hula1() {
Enemy.call(this);
@@ -18,7 +19,7 @@ util.inherits(Hula1, Enemy);
Hula1.prototype.Draw = function(time, delta) {
Enemy.prototype.Draw.call(this);

var dist = GetDistance(this.mesh.position, game.player.mesh.position);
var dist = Utils.GetDistance(this.mesh.position, game.player.mesh.position);
if(dist < 5) {
this.Explode();
}
@@ -1,5 +1,6 @@
const util = require('util');
const Enemy = require('./Enemy');
const Utils = require('../Utils');

function Hula2() {
Enemy.call(this);
@@ -18,7 +19,7 @@ util.inherits(Hula2, Enemy);
Hula2.prototype.Draw = function(time, delta) {
Enemy.prototype.Draw.call(this);

var dist = GetDistance(this.mesh.position, game.player.mesh.position);
var dist = Utils.GetDistance(this.mesh.position, game.player.mesh.position);
if(dist < 5) {
this.Explode();
}
@@ -0,0 +1,35 @@
const util = require('util');
const Item = require('./Item');

function Bomb() {
Item.call(this);
this.type = "bomb";
this.speed = 10;
this.hit = false;
this.model = "bomb";
this.damage = 5;
}
util.inherits(Bomb, Item);

Bomb.prototype.Remove = function() {
this.Explode();
this.remove = 1;
game.soundLoader.PlaySound("explode", this.mesh.position, 300);
};

Bomb.prototype.Hit = function() {
this.hit = true;
};

Bomb.prototype.Draw = function(time, delta) {
if(this.hit) {
this.speed -= 0.1;
}
if(this.speed <= 1) {
this.Remove();
} else {
this.mesh.rotation.z = (time/this.speed);
}
};

module.exports = Bomb;
@@ -0,0 +1,23 @@
const util = require('util');
const Item = require('./Item');

function GodMode() {
Item.call(this);
this.speed = 5;
this.type = "godmode";
this.model = "godmode";
}
util.inherits(GodMode, Item)

GodMode.prototype.Remove = function() {
this.chunk.Explode(this.mesh.position);
game.soundLoader.PlaySound("crate_explode", this.mesh.position, 300);
this.remove = 1;
};

GodMode.prototype.Hit = function() {
game.player.godMode = true;
this.Remove();
};

module.exports = GodMode;
@@ -0,0 +1,23 @@
const util = require('util');
const Item = require('./Item');

function HealthBox() {
Item.call(this);
this.speed = 5;
this.type = "healthbox";
this.model = "healthbox";
}
util.inherits(HealthBox, Item);

HealthBox.prototype.Remove = function() {
this.remove = 1;
this.chunk.Explode(this.mesh.position);
game.soundLoader.PlaySound("health", this.mesh.position, 300);
};

HealthBox.prototype.Hit = function() {
this.Remove();
game.player.AddHealth();
};

module.exports = HealthBox;
@@ -0,0 +1,49 @@
const Utils = require('../Utils');

function Item() {
this.scale = 1;
this.remove = 0;
this.mesh = undefined;
this.type = "item";
this.model = undefined;
this.speed = 5;
this.chunk = undefined;
this.mesh = undefined;
this.skipDraw = 0;
}

Item.prototype.Explode = function() {
game.chunkManager.ExplodeBomb(this.mesh.position.x, this.mesh.position.z, this.damage, false);
this.chunk.Explode(this.mesh.position);
game.scene.remove(this.mesh);
};

Item.prototype.Remove = function() {
if(this.remove != 1) {
this.Explode();
this.remove = 1;
}
};

Item.prototype.Create = function(pos) {
this.chunk = game.voxLoader.GetModel(this.model);
this.mesh = this.chunk.mesh;
game.scene.add(this.mesh);
this.mesh.position.set(pos.x, pos.y, pos.z);
this.mesh.that = this;
game.targets.push(this.mesh);
};

Item.prototype.Draw = function(time, delta) {
if(game.player != undefined) {
var dist = Utils.GetDistance(this.mesh.position, game.player.mesh.position);
if(dist > 20) {
// Optimization for performance, skipping frames when far away.
this.skipDraw = Math.floor(dist/3);
}
}
this.mesh.rotation.z = (time/this.speed);
};

module.exports = Item;

@@ -0,0 +1,35 @@
const util = require('util');
const Item = require('./Item');

function WeaponBox() {
Item.call(this);
this.type = "bomb";
this.speed = 10;
this.hit = false;
this.model = "weaponbox";
this.damage = 10;
}
util.inherits(WeaponBox, Item);

WeaponBox.prototype.Remove = function() {
this.Explode();
this.remove = 1;
game.soundLoader.PlaySound("explode", this.mesh.position, 300);
};

WeaponBox.prototype.Hit = function() {
this.hit = true;
};

WeaponBox.prototype.Draw = function(time, delta) {
if(this.hit) {
this.speed -= 0.1;
}
if(this.speed <= 1) {
this.Remove();
} else {
this.mesh.rotation.z = (time/this.speed);
}
};

module.exports = WeaponBox;
@@ -1,4 +1,4 @@
const THREE = require('three');
const THREE = require('./ThreeHelpers');

//==============================================================================
// Author: Nergal

This file was deleted.