Skip to content

Commit

Permalink
latest
Browse files Browse the repository at this point in the history
  • Loading branch information
jinpyojeon committed May 10, 2017
1 parent 7d45799 commit 52b5d5b
Show file tree
Hide file tree
Showing 71 changed files with 6,203 additions and 28 deletions.
5 changes: 2 additions & 3 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
<!-- script type="text/javascript" src="src/loaders/LoadingManager.js"></script-->
<script type="text/javascript" src="src/loaders/OBJLoader.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src='src/threex.grassground.js'></script>
<script src="src/loaders/MTLLoader.js"></script>
<script src="src/loaders/DDSLoader.js"></script>
</head>

<body>
<div id="score">0</div>
<div id="distance">0</div>
</body>

</html>
262 changes: 237 additions & 25 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
var scene, camera, renderer;
var mousePos;
var mousePos = { x: 0, y: 0 };

var figure;
var sun;
var player, floor, sun, sky;

var game = {
finished: false,
points: 0,
speed: 1
};

var game;
var deltaTime = 0;
var newTime = new Date().getTime();
var oldTime = new Date().getTime();

var obstacles = [];
var coins = [];

var Colors = {
red: 0xf25346,
white: 0xd8d0d1,
brown: 0x59332e,
pink: 0xF5986E,
brownDark: 0x23190f,
blue: 0x68c3c0,
gold: 0xffdf00
};

init();
Expand All @@ -27,18 +34,23 @@ function init() {
camera.position.z = 10;
camera.position.y = 2;

var ambientLight = new THREE.AmbientLight(0x404040); // soft white light
var ambientLight = new THREE.AmbientLight(Colors.white); // soft white light
scene.add(ambientLight);

var geomSun = new THREE.BoxGeometry(1, 1, 1, 1, 1, 1);
var matSun = new THREE.MeshPhongMaterial({ color: Colors.red, shading: THREE.FlatShading })
var geomSun = new THREE.TetrahedronGeometry(8, 2);
var matSun = new THREE.MeshPhongMaterial({
color: Colors.red,
shininess: 0,
specular: 0xffffff,
shading: THREE.FlatShading
});
sun = new THREE.Mesh(geomSun, matSun);
sun.position.set(5, 5, -1);
sun.scale.set(0.25, 0.25, 0.25);
sun.position.set(5, 20, -10);
scene.add(sun);

var light = new THREE.DirectionalLight(Colors.red, 1, 10);
light.position.set(sun.position);
light.position.set(0, 1, 0); //default; light shining from top
var light = new THREE.DirectionalLight(Colors.red, 1);
light.position.set(0, 1, 0);
scene.add(light);

var listener = new THREE.AudioListener();
Expand All @@ -59,41 +71,118 @@ function init() {

var loader = new THREE.TextureLoader();

// var groundMesh = new THREEx.GrassGround();
// scene.add(groundMesh);
var grassTexture = loader.load('src/images/grasslight-small.jpg', function(texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set(0, 0);
texture.repeat.set(1, 512);
});

floor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100000, 8, 8),
new THREE.MeshBasicMaterial({
map: grassTexture
}));
floor.rotation.x -= Math.PI / 2;
floor.position.y -= 1
scene.add(floor);

var skyTexture = loader.load('src/images/sky.jpg', function(texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set(0, 0);
texture.repeat.set(1, 1);
});

sky = new THREE.Mesh(
new THREE.PlaneGeometry(10000, 1000, 8, 8),
new THREE.MeshBasicMaterial({
map: skyTexture
}));
sky.position.z = -60;
scene.add(sky);


renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
// renderer.setClearColor(0xEEEEEE);

var loader = new THREE.OBJLoader();

// var ratamahatta = new THREEx.MD2CharacterRatmahatta();
// scene.add(ratamahatta.character.object3d);

loader.load('models/Stick_Figure_by_Swp.OBJ',
function(object) {
object.position.set(1, 1, 0);
object.rotation.set(1, 1, 180);
figure = object;
scene.add(figure);
object.position.set(1, 0, 8);
object.scale.set(0.5, 0.5, 0.5);
// object.rotation.set(0, -Math.PI / 2, 1);
player = object;
scene.add(player);
}
);

// var mtlLoader = new THREE.MTLLoader();
// mtlLoader.setPath('models/');
// mtlLoader.load('Oak_Dark_01.mtl', function(materials) {

// materials.preload();

// var objLoader = new THREE.OBJLoader();
// loader.setMaterials(materials);
// loader.setPath('models');
// lader.load('Oak_Dark_01.obj', function(object) {

// object.position.y = -95;
// scene.add(object);

// }, onProgress, onError);

// });

window.onload = function() {
document.body.appendChild(renderer.domElement);
render();
}

document.addEventListener("mousemove", handleMouseMove, false);

document.body.addEventListener('keydown', function(event) {
var inputs = player.input;
if (event.keyCode === 37) inputs.left = true
if (event.keyCode === 39) inputs.right = true
});

document.body.addEventListener('keyup', function(event) {
var inputs = player.input;
if (event.keyCode === 37) inputs.left = false
if (event.keyCode === 39) inputs.right = false
});
}

function render() {
// camera.rotation.z -= 0.1;
// figure.position.z += 0.01;
if (sun) figure.position.z -= 0.01;
if (figure) figure.position.set(mousePos.x * 10, mousePos.y * 10, 1);
renderer.render(scene, camera);

requestAnimationFrame(render);
if (!game.finished) {
if (player) player.position.set(mousePos.x * 10, 0, 8);
floor.position.z += 0.3 * game.speed;
sky.position.x += 0.1;
updateSun();
procGenerateRocks();
// procGenerateCoins();
moveObstacles();
moveCoins();
detectCollision();
game.speed += 0.0001;
renderer.render(scene, camera);
requestAnimationFrame(render);
}

}

function updateSun() {
if (sun) {
sun.rotation.z += Math.sin(0.1);
sun.rotation.y += Math.sin(0.1);
sun.position.y *= Math.PI * Math.sin(sun.position.y);
}
}

function handleMouseMove(event) {
Expand All @@ -102,6 +191,129 @@ function handleMouseMove(event) {
mousePos = { x: tx, y: ty };
}

function procGenerateCubes() {
function updatePlayer() {
if (player.input) {
if (player.input.left) player.position.x += 0.1;
if (player.input.right) player.position.x -= 0.1;
}
}

function procGenerateRocks() {
newTime = new Date().getTime();

if (newTime - oldTime > 2000) {
oldTime = new Date().getTime();

var spawnNum = Math.round(Math.random() * 3);
var spawnedObs;
for (var i = 0; i < spawnNum; i++) {
spawnedObs = new Rock();
spawnedObs.mesh.position.x = -10 + Math.random() * 20;
spawnedObs.mesh.position.z = -20 - Math.random() * 10;
spawnedObs.mesh.scale.set(0.1, 0.1, 0.1);
obstacles.push(spawnedObs);
scene.add(spawnedObs.mesh);
}

var spawnNum = Math.round(Math.random() * 3);
var spawnedObs;
for (var i = 0; i < spawnNum; i++) {
spawnedObs = new Coin();
spawnedObs.mesh.position.x = -10 + Math.random() * 20;
spawnedObs.mesh.position.z = -20 - Math.random() * 10;
spawnedObs.mesh.scale.set(0.1, 0.1, 0.1);
coins.push(spawnedObs);
scene.add(spawnedObs.mesh);
}
}
}


// function procGenerateCoins() {
// newTime = new Date().getTime();

// if (newTime - oldTime > 2000) {
// oldTime = new Date().getTime();

// }
// }

function moveObstacles() {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].mesh.position.z += 0.3;
if (obstacles[i].mesh.position.z > 6) {
scene.remove(obstacles[i].mesh);
obstacles.splice(i, 1);
}
}
}

function moveCoins() {

for (var i = 0; i < coins.length; i++) {
coins[i].mesh.position.z += 0.3 * game.speed;
if (coins[i].mesh.position.z > 9) {
scene.remove(coins[i].mesh);
coins.splice(i, 1);
}
}
}

function detectCollision() {
for (var i = 0; i < obstacles.length; i++) {
console.log(player.position.distanceTo(obstacles[i].mesh.position));
if (player.position.distanceTo(obstacles[i].mesh.position) < 3) {
scene.remove(obstacles[i].mesh);
obstacles.splice(i, 1);
initiateCollision("obstacle");
game.finished = true;
}
}

for (var i = 0; i < coins.length; i++) {
if (player.position.distanceTo(coins[i].mesh.position) < 3) {
scene.remove(coins[i].mesh);
coins.splice(i, 1);
initiateCollision("coin");
}
}
}

function initiateCollision(obj) {
if (obj == "obstacle") console.log("Encountered obs");
else if (obj == "coin") {
game.points += 1;
console.log(game.points);
}
}

function Coin() {
var geom = new THREE.TetrahedronGeometry(8, 2);
var mat = new THREE.MeshPhongMaterial({
color: Colors.gold,
shininess: 0,
specular: 0xffffff,
shading: THREE.FlatShading
});
this.mesh = new THREE.Mesh(geom, mat);
this.mesh.scale.set(0.5, 0.5, 0.5);
this.mesh.castShadow = true;
this.angle = 0;
this.dist = 0;
}

function Rock() {
var geom = new THREE.TetrahedronGeometry(8, 2);
var mat = new THREE.MeshPhongMaterial({
color: Colors.brownDark,
shininess: 0,
specular: 0xffffff,
shading: THREE.FlatShading
});
this.mesh = new THREE.Mesh(geom, mat);
this.mesh.scale.set(0.8, 0.8, 0.8);
this.mesh.castShadow = true;
this.angle = 0;
this.dist = 0;
//*/
}
14 changes: 14 additions & 0 deletions models/Mushroom_Brown_01.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
## Alias OBJ Material File
# Exported from SketchUp, (c) 2000-2012 Trimble Navigation Limited

newmtl Wood
Ka 0.000000 0.000000 0.000000
Kd 0.666667 0.545098 0.356863
Ks 0.330000 0.330000 0.330000

newmtl FrontColor
Ka 0.000000 0.000000 0.000000
Kd 1.000000 1.000000 1.000000
Ks 0.330000 0.330000 0.330000

Loading

0 comments on commit 52b5d5b

Please sign in to comment.