Commit 30b6d903 authored by ogomez92's avatar ogomez92

engine

parent c7d6178a
'use strict';
import {EngineSound} from './engineSound';
class Car {
constructor(player,type, gears, maxAccelerate,maxSpeed) {
this.player=player;
this.state= 0;
this.maxAccelerate=maxAccelerate;
this.type = type;
this.gears = gears;
this.currentGear = 0;
this.maxSpeed=maxSpeed;
this.engine=new EngineSound(this,this.gears,this.maxAccelerate);
this.engine.startEngine();
}
}
export {Car}
\ No newline at end of file
'use strict';
import {GameUtils} from './utilities';
import {son} from './sonoObject';
import {speech} from './tts';
let utils=new GameUtils();
class EngineSound {
constructor(car, gears, maxAccelerate=1, minRate=0.7, speedFactor=0.0025) {
this.started = false;
this.car = car;
this.gears = gears;
this.currentGear = 0;
this.currentAccelerateSound = 0;
this.accelerateSounds = maxAccelerate;
this.rateFactor = 0.8;
this.minRate = 0.8;
this.maxRate = 1;
this.rate = this.maxRate;
this.idling=1;
this.state=0;
this.shiftTime = 5000;
this.timer = new Date();
this.time=this.timer.getMilliseconds();
this.states ={
idling:0,
accelerating:1,
decelerating:2,
shifting:3,
maxing:4
}
this.speedFactor = speedFactor;
this.maxVolume = 0.4;
this.sndAccelerate = new Array();
for (var i=0;i<this.accelerateSounds;i++) {
this.sndAccelerate.push(son.create("cars/"+this.car.type+"/accelerate"+(i+1)));
}
this.sndDecelerate = son.create("cars/"+this.car.type+"/decelerate");
this.sndIdle = son.create("cars/"+this.car.type+"/idle");
this.sndIdle.loop = true;
this.sndLoop = son.create("cars/"+this.car.type+"/high");
this.sndLoop.loop = true;
this.sndShift = son.create("cars/"+this.car.type+"/shift");
this.sndStart = son.create("cars/"+this.car.type+"/start");
this.sndStart.volume = this.maxVolume;
this.sndShift.volume = this.maxVolume;
this.sndDecelerate.volume = this.maxVolume;
}
startEngine() {
this.sndStart.play();
this.sndStart.sound.once("ended",()=> {
this.sndIdle.play();
this.currentGear = 0;
this.started = true;
this.accelerate();
if (this.car.player.world.level==0) {
this.car.player.world.level=1;
}
});
}
accelerate() {
if (this.started) {
if (this.state != this.states.accelerating) {
if (this.sndIdle.playing) this.sndIdle.stop();
if (this.sndDecelerate.playing) this.sndDecelerate.stop();
this.rateFactor = ((this.maxRate-this.currentGear/this.gears > this.minRate) ? this.maxRate-this.currentGear/this.gears : this.minRate);
this.rate = utils.getRandomArbitrary(this.rateFactor, this.rateFactor+0.1);
this.currentAccelerateSound = utils.randomInt(0, this.accelerateSounds-1);
this.sndAccelerate[this.currentAccelerateSound].playbackRate = this.rate;
this.sndAccelerate[this.currentAccelerateSound].volume = this.maxVolume;
this.sndAccelerate[this.currentAccelerateSound].play();
this.state = this.states.accelerating;
this.idling=0;
}
}
}
decelerate() {
if (this.started) {
if (this.state != this.states.decelerating) {
this.sndDecelerate.play();
this.sndAccelerate.forEach(function(sound) {
sound.stop();
});
this.sndLoop.stop();
this.state = this.states.decelerating;
}
}
}
update() {
if (this.started) {
if (this.state == this.states.accelerating) { this.handleAccelerating(); }
if (this.state == this.states.decelerating) { this.handleDecelerating(); }
if (this.state == this.states.shifting) { this.handleShifting(); }
if (this.state == this.states.maxing) { this.handlePitch(); }
}
}
handleAccelerating() {
if (this.sndAccelerate[this.currentAccelerateSound].progress > 0.99 || this.sndAccelerate[this.currentAccelerateSound].ended) {
this.max();
}
}
max() {
if (this.started) {
this.sndAccelerate[this.currentAccelerateSound].fade(0, 0.1);
this.sndLoop.volume = 0;
this.sndLoop.playbackRate = this.rate;
this.sndLoop.fade(this.maxVolume, 0.1);
this.state = this.states.maxing;
this.sndLoop.play();
}
}
shift() {
if (this.started) {
this.sndShift.play();
this.sndLoop.fade(0, 0.1);
this.sndLoop.stop();
this.sndAccelerate[this.currentAccelerateSound].stop();
this.state = this.states.shifting;
this.currentGear++;
if (this.speedFactor > this.minSpeedFactor) this.speedFactor -= 0.0005;
if (this.rateFactor > this.minRate) this.rateFactor -= 0.05;
}
}
handlePitch() {
if (this.sndLoop.playbackRate < this.maxRate) {
this.sndLoop.playbackRate += this.speedFactor;
} else {
if (this.currentGear < this.gears) {
this.shift();
}
}
}
handleShifting() {
if (this.sndShift.progress > 0.9 || this.sndShift.ended) {
this.accelerate();
}
}
handleDecelerating() {
if (this.currentGear == 0) {
this.idle();
}
if ((this.sndDecelerate.ended) && this.currentGear > 0) {
this.currentGear--;
this.rate = utils.getRandomArbitrary(this.rateFactor, this.rateFactor+0.1);
if (this.rateFactor > 0.2) this.rateFactor -= 0.1;
this.sndDecelerate.playbackRate = this.rate;
this.sndDecelerate.seek(0);
this.sndDecelerate.play();
}
}
idle() {
this.rateFactor = this.minRate+this.maxRate/2;
this.sndDecelerate.stop();
this.sndLoop.stop();
this.sndIdle.play();
this.state = this.states.idling;
}
stopEngine() {
this.started = false;
this.sndShift.stop();
this.sndDecelerate.stop();
this.sndAccelerate.forEach(function(sound) {
sound.stop();
});
this.sndLoop.stop();
this.sndIdle.stop();
this.state = -1;
}
destroy() {
this.sndAccelerate.forEach(function(sound) {
sound.destroy();
});
this.sndDecelerate.destroy();
this.sndIdle.destroy();
this.sndLoop.destroy();
this.sndShift.destroy();
this.sndStart.destroy();
}
}
export {EngineSound}
\ No newline at end of file
......@@ -9,7 +9,7 @@ import {KeyboardInput} from './input.js';
import {KeyEvent} from './keycodes.js';
import World from './world';
export default class Game {
class Game {
constructor() {
this.world = new World(this);
this.input=new KeyboardInput();
......@@ -38,4 +38,5 @@ speech.speak(Math.round(this.world.player.x)+", "+Math.round(this.world.player.z
}
}
\ No newline at end of file
}
export {Game}
\ No newline at end of file
export var gameID="";
import {SoundSource} from './sonoSource';
import Game from './game';
import {Game} from './game';
export var version="1.0";
export var version2="";
......
import uniqueRandom from 'unique-random';
import {speech} from './tts';
import {Car} from './car';
import {Ray} from './ray';
import {SoundHandler} from './soundHandler.js';
import {OldTimer} from './oldtimer';
......@@ -12,14 +13,14 @@ export default class Player {
this.z = 0;
this.alpha = 0;
this.speed =0.04;
this.moving = true;
this.sounds = new SoundHandler(true);
this.sounds = new SoundHandler(true);
this.stepCounter = 0;
this.nextStep = uniqueRandom(1, 1);
this.world=world;
this.x=this.world.size/2;
this.z=this.world.size/2;
this.y=0;
this.car=new Car(this,1,5,1,2);
}
get position() {
......@@ -30,6 +31,8 @@ export default class Player {
this.x = pos[0];
this.y = pos[1];
this.z = pos[2];
this.rx=Math.round(this.x);
this.rz=Math.round(this.z);
}
update() {
......@@ -41,16 +44,16 @@ export default class Player {
move() {
this.position = [this.x + (Math.sin(this.alpha) * (this.speed)), 0, this.z + (Math.cos(this.alpha) * (this.speed))];
this.stepCounter += this.speed;
if (this.stepCounter >= 1) {
this.stepCounter = 0;
this.sounds.playStatic('run_1');
this.checkWalls();
this.checkWalls();
for (const i of this.world.contents.static) {
if (Math.round(this.x)==i.x && Math.round(this.z)==i.z) {
if (this.rx==i.x && this.rz==i.z) {
i.collide();
}
}
}
}
if (this.stepCounter >= 1) {
this.stepCounter = 0;
this.sounds.playStatic('tick');
}
}
checkWalls() {
this.zWall=0;
......
......@@ -21,6 +21,9 @@ this.sound.data=file;
this.timeToLoad = performance.now();
this.tag = tag;
}
set loop(v) {
this.sound.loop=v;
}
checkProgress() {
if (this.sound.progress == 0) {
......
......@@ -93,4 +93,4 @@ return size;
}
}
export var utils = new GameUtils();
export {GameUtils}
\ No newline at end of file
......@@ -22,6 +22,8 @@ export default class World {
this.sidesPan.set(-1);
this.frontPan.set(0);
this.backPan.set(0);
}
set level(v) {
this.contents.static.push(new Cone(this,25,49));
}
update() {
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment