11"use strict" ;
22// Global variables
3- let camera , controls , scene , renderer , ship , sun ,
3+ let camera , controls , scene , renderer , ship , sun , arrow ,
44 planets = [ ] ,
55 gravity = - 0.007 ,
6- isPaused = false ;
6+ fuel = 1000 ,
7+ gameOver = false ,
8+ isPaused = false ,
9+ fuelIndicator = document . getElementById ( "fuel" ) ;
710
811// Required utils
912let clock = new THREE . Clock ( ) ,
@@ -282,7 +285,8 @@ function init() {
282285 } ) ;
283286
284287 ship = new THREE . Mesh ( shipGeometry , shipMaterials ) ;
285- ship . position . set ( 60 , 0 , 0 ) ;
288+ ship . position . set ( - 100 , 0 , 0 ) ;
289+ ship . name = "Ship" ;
286290 scene . add ( ship ) ;
287291
288292 //Space background is a large sphere
@@ -326,60 +330,83 @@ function move() {
326330 moveDistance = 10 * delta , // 0.1 pixels per second
327331 rotateAngle = Math . PI / 9 * delta , // pi/9 radians (20 degrees) per second
328332 shipPosition = ship . position . clone ( ) ,
329- distanceToSun = shipPosition . distanceTo ( sun . getInstance . position . clone ( ) ) ,
330333 array = [ ] ;
331334
332- if ( distanceToSun < 3 ) {
333- showDeadScreen ( ) ;
335+ if ( gameOver )
334336 return ;
335- }
336337
337338 array = planets . map ( ( planet ) => {
338339 return {
339340 p : planet ,
340- angle : shipPosition . angleTo ( planet . getInstance . position . clone ( ) ) ,
341341 distance : shipPosition . distanceTo ( planet . getInstance . position . clone ( ) )
342342 } ;
343343 } ) ;
344344
345- /* array.push({
345+ array . push ( {
346346 p : sun ,
347- angle: shipPosition.angleTo(sun.getInstance.position.clone()),
348347 distance : shipPosition . distanceTo ( sun . getInstance . position . clone ( ) )
349- });*/
348+ } ) ;
350349
351350 let planet = array . find ( ( planet ) => {
352351 return planet . distance == Math . min . apply ( null , array . map ( ( planet ) => planet . distance ) ) ;
353352 } ) ;
354- console . debug ( "Planets :" , array , ", the closest planet: " , planet ) ;
355353
356- let vector = new THREE . Vector3 ( 1 , 0 , 0 ) . clone ( ) . multiplyScalar ( gravity , gravity , gravity ) ;
354+ if ( planet . distance < 3 ) {
355+ showDeadScreen ( ) ;
356+ scene . remove ( scene . getObjectByName ( 'Ship' ) ) ;
357+ }
357358
358- vector . applyAxisAngle ( planet . p . getInstance . position . clone ( ) . normalize ( ) , planet . angle ) ;
359+ let direction = new THREE . Vector3 ( ) . subVectors (
360+ planet . p . getInstance . position . clone ( ) ,
361+ shipPosition
362+ ) . normalize ( ) ;
359363
360- console . debug ( "After" , vector ) ;
364+ arrow = new THREE . ArrowHelper ( direction , shipPosition , planet . distance , 0x884400 ) ;
365+ arrow . name = "arrow" ;
366+ scene . add ( arrow ) ;
361367
362- ship . position . x -= vector . x ;
363- ship . position . y -= vector . y ;
364- ship . position . z -= vector . z ;
368+ gravity = planet . p . radius / planet . distance / 2 ;
365369
366- gravity = planet . p . radius / planet . distance / 10 ;
370+ let vector = direction . clone ( ) . multiplyScalar ( gravity , gravity , gravity ) ;
367371
368- if ( keyboard . pressed ( "A" ) )
369- ship . rotation . z += rotateAngle ;
370- if ( keyboard . pressed ( "D" ) )
371- ship . rotation . z -= rotateAngle ;
372+ if ( fuel > 0 ) {
373+ if ( keyboard . pressed ( "A" ) ) {
374+ ship . rotation . z += rotateAngle ;
375+ fuel -= 1 ;
376+ }
377+ if ( keyboard . pressed ( "D" ) ) {
378+ ship . rotation . z -= rotateAngle ;
379+ fuel -= 1 ;
380+ }
381+ if ( keyboard . pressed ( "left" ) ) {
382+ vector . z -= moveDistance ;
383+ fuel -= 3 ;
384+ }
385+ if ( keyboard . pressed ( "right" ) ) {
386+ vector . z += moveDistance ;
387+ fuel -= 3 ;
388+ }
389+ if ( keyboard . pressed ( "up" ) ) {
390+ vector . x += moveDistance ;
391+ fuel -= 3 ;
392+ }
393+ if ( keyboard . pressed ( "down" ) ) {
394+ vector . x -= moveDistance ;
395+ fuel -= 3 ;
396+ }
397+ updateFuelIndicator ( fuel ) ;
398+ }
372399
373- if ( keyboard . pressed ( "left" ) )
374- ship . position . z += moveDistance ;
375- if ( keyboard . pressed ( "right" ) )
376- ship . position . z -= moveDistance ;
377- if ( keyboard . pressed ( "up" ) )
378- ship . position . x -= moveDistance ;
379- if ( keyboard . pressed ( "down" ) )
380- ship . position . x += moveDistance ;
400+ ship . position . x += vector . x ? vector . x : - vector . x ;
401+ ship . position . y += vector . y ;
402+ ship . position . z += vector . z ? vector . z : - vector . z ;
381403
382404 array = [ ] ;
405+ window . customUniforms . time . value += delta ;
406+
407+ setTimeout ( function ( ) {
408+ scene . remove ( scene . getObjectByName ( 'arrow' ) )
409+ } , 1000 / 60 )
383410}
384411
385412function onWindowResize ( ) {
@@ -389,39 +416,23 @@ function onWindowResize() {
389416}
390417
391418function update ( ) {
392- let originPoint = ship . position . clone ( ) ,
393- delta = clock . getDelta ( ) ;
394-
395419 // Update planets positions
396420 planets . forEach ( ( planet ) => {
397421 planet . rotate ( planet . rotateSpeed ) ;
398422 planet . spin ( planet . getSpinSpeed ) ;
399423 } ) ;
400424
401- window . customUniforms . time . value += delta ;
402- // collision detection:
403- // determines if any of the rays from the ship origin to each vertex
404- // intersects any face of a mesh in the array of target meshes
405- // for increased collision accuracy, add more vertices to the ship;
406- // HOWEVER: when the origin of the ray is within the target mesh, collisions do not occur
407- for ( let vertexIndex = 0 ; vertexIndex < ship . geometry . vertices . length ; vertexIndex ++ ) {
408- let localVertex = ship . geometry . vertices [ vertexIndex ] . clone ( ) ,
409- globalVertex = localVertex . applyMatrix4 ( ship . matrix ) ,
410- directionVector = globalVertex . sub ( ship . position ) ,
411- ray = new THREE . Raycaster ( originPoint , directionVector . clone ( ) . normalize ( ) ) ;
412-
413- let collisionResults = ray . intersectObjects ( planets . map ( ( planet ) => planet . getInstance ) ) ;
414- if ( collisionResults . length > 0 && collisionResults [ 0 ] . distance < directionVector . length ( ) ) {
415- console . log ( "dead" ) ;
416- }
417- }
425+ // Rotate sun
426+ sun . rotate ( 0.01 ) ;
427+
418428 move ( ) ;
419429}
420430
421431function render ( ) {
422432 renderer . render ( scene , camera ) ;
423433}
424434
435+ // Main game loop
425436function animate ( ) {
426437 controls . update ( ) ;
427438
@@ -443,6 +454,14 @@ function showDeadScreen() {
443454 let classList = document . getElementsByClassName ( 'background' ) [ 0 ] . classList ;
444455 if ( classList . contains ( 'disabled' ) )
445456 classList . remove ( 'disabled' ) ;
457+ gameOver = true ;
458+ }
459+
460+ function updateFuelIndicator ( fuel ) {
461+ fuelIndicator . style . height = fuel * 200 / 1000 + 'px' ;
462+ if ( fuel <= 0 ) {
463+ fuelIndicator . classList . add ( 'red' ) ;
464+ }
446465}
447466
448467( function ( ) {
0 commit comments