Skip to content

Commit

Permalink
Merge pull request #18 from FAC-11/yztest
Browse files Browse the repository at this point in the history
Yztest
  • Loading branch information
polyccon authored Sep 11, 2017
2 parents f7ef1a7 + 3ad3c6b commit 8b24616
Show file tree
Hide file tree
Showing 8 changed files with 9,427 additions and 44 deletions.
9,267 changes: 9,267 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

Binary file added public/images/crate/healthpack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const start = (options) => {
pointerLockControls,
world,
timeStep,
health,
} = options;

let prevTime = performance.now();
Expand All @@ -32,6 +33,7 @@ const start = (options) => {
pointerLockControls,
world,
timeStep,
health,
);
prevTime = time;
}
Expand Down
4 changes: 2 additions & 2 deletions src/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const init = () => {
movements.canJump = false;
}
break;
case 17:
case 70:
movements.shooting = true;
break;
}
Expand All @@ -50,7 +50,7 @@ const init = () => {
case 68: // d
movements.right = false;
break;
case 17:
case 70:
movements.shooting = false;
break;
}
Expand Down
20 changes: 20 additions & 0 deletions src/cubes.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,33 @@ const getObj6 = () => {
return obj6;
};

const getObj7 = () => {
const textureLoader = new THREE.TextureLoader();
const crateTexture = textureLoader.load('images/crate/healthpack.png');
const obj7 = new THREE.Mesh(
new THREE.BoxGeometry(10, 10, 10),
new THREE.MeshPhongMaterial({
color: 0xffffff,
map: crateTexture,
wireframe: false,
}),
);
obj7.position.set(60, -5, 2);
obj7.receiveShadow = false;
obj7.castShadow = true;
return obj7;
};



module.exports = {
getObj1,
getObj2,
getObj3,
getObj4,
getObj5,
getObj6,
getObj7
};


Expand Down
17 changes: 15 additions & 2 deletions src/init/getFloor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const THREE = require('three');
const CANNON = require('cannon');
// const OBJLoader = require('three-obj-loader');
// const MTLLoader = require('three-mtl-loader');

Expand All @@ -11,11 +12,23 @@ const getFloor = () => {
geometry.applyMatrix(new THREE.Matrix4().makeRotationX(-Math.PI / 2));
const material = new THREE.MeshLambertMaterial({ map: floorTexture });
const floor = new THREE.Mesh(geometry, material);
floor.position.y = -25;
floor.position.y = -50;

floor.castShadow = false;
floor.receiveShadow = true;
return floor;


const groundShape = new CANNON.Plane();
groundShape.collisionResponse = true;
const groundBody = new CANNON.Body({ mass: 0 });
groundBody.addShape(groundShape);
groundBody.position.set(0, -14, 0);
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);


return {
floor, groundBody,
};
// const floor = new THREE.Mesh(
// new THREE.PlaneGeometry(2500, 2500, 10, 10),
// new THREE.MeshPhongMaterial({
Expand Down
67 changes: 49 additions & 18 deletions src/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,59 @@ const blocker = require('../blocker');
// turnSpeed: Math.PI * 0.02
// };

const bullet = () => {
const bullet = new THREE.Mesh(
new THREE.SphereGeometry(5, 8, 8),
new THREE.MeshBasicMaterial(),
);
bullet.alive = true;
setTimeout(() => {
bullet.alive = false;
scene.remove(bullet);
}, 1000);
scene.add(bullet);
};

// create the scene

const init = () => {

// attempt to create a HUD but need to know how to render in the DOM.
const hud = document.createElement("div");
hud.innerHTML = '<p>Health: <span id="health"></span><br />Score: <span id="score">0</span></p>'
document.body.appendChild(hud);


const timeStep = 1 / 60;

// Cannon init
const world = new CANNON.World();
world.gravity.set(0, -9.82, 0);
world.gravity.set(0, -20, 0);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
const shape = new CANNON.Box(new CANNON.Vec3(1, 1, 1));

// quaternians and performance
world.quatNormalizeSkip = 0;
world.quatNormalizeFast = false;

world.solver = new CANNON.SplitSolver(new CANNON.GSSolver());
// shape is shape of geometry/wireframe
const shape = new CANNON.Box(new CANNON.Vec3(10, 10, 10));
// body is it being effected by forces.
const body = new CANNON.Body({
mass: 1,
});
body.addShape(shape);
body.angularVelocity.set(0, 50, 0);
body.angularDamping = 0.5;
body.position.set(0, 50, 0);



// Create a slippery material (friction coefficient = 0.0)
var physicsMaterial = new CANNON.Material("slipperyMaterial");
var physicsContactMaterial = new CANNON.ContactMaterial(physicsMaterial,
physicsMaterial,
0.0, // friction coefficient
0.3 // restitution
);
// We must add the contact materials to the world
world.addContactMaterial(physicsContactMaterial);


world.addBody(body);




// const camera = new THREE.PerspectiveCamera(75, -50, 1, 1000);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
// let's create the scene
Expand All @@ -76,8 +97,15 @@ const init = () => {
const obj4 = cubes.getObj4();
const obj5 = cubes.getObj5();
const obj6 = cubes.getObj6();
// create health pack
const health = cubes.getObj7();



scene.add(obj1, obj2, obj3, obj4, obj5, obj6);



scene.add(obj1, obj2, obj3, obj4, obj5, obj6, health);


// objects
Expand All @@ -103,8 +131,10 @@ const init = () => {
// let's get the floor

const floor = getFloor();
scene.add(floor);
const objects = [floor];
world.add(floor.groundBody);
scene.add(floor.floor);

const objects = [floor.floor];

const renderer = getRenderer();
document.body.appendChild(renderer.domElement);
Expand All @@ -117,7 +147,7 @@ const init = () => {
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
console.log(scene.children[1]);

return {
camera,
scene,
Expand All @@ -127,6 +157,7 @@ const init = () => {
pointerLockControls,
world,
timeStep,
health
};
};

Expand Down
94 changes: 72 additions & 22 deletions src/letsMove.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const THREE = require('three');
const pointLockers = require('./pointLockers');

const CANNON = require('cannon');
// const shoot = require('./shoot.js');

const {
movements,
} = require('./controls');

const bullets = [];
const cannonBullets = [];
const velocity = new THREE.Vector3();
var lastHealthPickup = 0;
var life=30;


function distance(x1, y1, x2, y2) {
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}

module.exports = function (
camera,
Expand All @@ -20,8 +28,41 @@ module.exports = function (
pointerLockControls,
world,
timeStep,
health
) {

const date = Date.now() - lastHealthPickup ;

world.step(timeStep);

//make health bar rotate
// health.rotation.x += 0.004;
health.rotation.y += 0.008;

if (!document.getElementById("health").textContent) {
document.getElementById("health").textContent = life;
} ;

// console.log('datenow', Date.now());




if (date > 10000 ) {
health.material.wireframe = false;
}

if (distance(pointLockers().position.x, pointLockers().position.z, health.position.x, health.position.z) < 15 && life != 100 && date > 10000 ) {
life = Math.min(life + 50, 100);
document.getElementById("health").textContent = life ;
lastHealthPickup = Date.now();
health.material.wireframe = true;
// health.position.x = -300;
console.log('last health', lastHealthPickup);
}
console.log('date', date);


scene.children[1].position.copy(world.bodies[0].position);
scene.children[1].quaternion.copy(world.bodies[0].quaternion);

Expand All @@ -40,52 +81,61 @@ module.exports = function (
continue;
}
if (bullets[index].alive == false) {
cannonBullets.splice(index, 1);
bullets.splice(index, 1);
world.remove(cannonBullets[index]);
continue;
}
bullets[index].position.add(bullets[index].velocity);
// bullets[index].position.add(bullets[index].velocity);
bullets[index].position.copy(cannonBullets[index].position);
bullets[index].quaternion.copy(cannonBullets[index].quaternion);
}

if (movements.shooting) {
// shoot.bullet(scene);
const bullet = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 8, 8),
new THREE.SphereGeometry(1.3, 8, 8),
new THREE.MeshBasicMaterial(),
);

// const shape = new CANNON.Sphere(new CANNON.Vec3(0.5));
// const body = new CANNON.Body({
// mass: 1,
// });
// body.addShape(shape);
// body.angularVelocity.set(0, 50, 0);
// body.angularDamping = 0.5;
// body.position.set(
// raycaster.ray.origin.x,
// raycaster.ray.origin.y,
// raycaster.ray.origin.z,
// );
// world.addBody(body);

bullet.position.set(
const shape = new CANNON.Sphere(new CANNON.Vec3(1.3));
const body = new CANNON.Body({
mass: 5,
});
body.linearDamping = 0;
body.addShape(shape);
body.position.set(
raycaster.ray.origin.x,
raycaster.ray.origin.y,
raycaster.ray.origin.z,
);
bullet.velocity = new THREE.Vector3(
-Math.sin(pointerLockControls.getObject().rotation._y),
0, -Math.cos(pointerLockControls.getObject().rotation._y),
world.addBody(body);


// bullet.position.copy(world.bodies[world.bodies.length - 1].position);
// bullet.quaternion.copy(world.bodies[world.bodies.length - 1].quaternion);
// console.log(cannonBullets[cannonBullets.length - 1]);


bullet.velocity = new CANNON.Vec3(
-Math.sin(pointerLockControls.getObject().rotation._y),
0,
-Math.cos(pointerLockControls.getObject().rotation._y),
);

console.log('thing', world.bodies[world.bodies.length - 1]);
bullet.alive = true;
setTimeout(() => {
bullet.alive = false;
scene.remove(bullet);
}, 1000);
}, 3000);
cannonBullets.push(world.bodies[world.bodies.length - 1]);
bullets.push(bullet);
scene.add(bullet);
cannonBullets[cannonBullets.length - 1].velocity = bullet.velocity;
cannonBullets[cannonBullets.length - 1].velocity.x *= 150;
cannonBullets[cannonBullets.length - 1].velocity.y += 10;
cannonBullets[cannonBullets.length - 1].velocity.z *= 150;
}

if (movements.forward) { velocity.z -= 2000.0 * delta; }
Expand Down

0 comments on commit 8b24616

Please sign in to comment.