Commit 5e0916da by ogomez92

engine

parent 30b6d903
'use strict';
import {EngineSound} from './engineSound';
import {son} from './sonoObject';
import {GameUtils} from './utilities';
import {speech} from './tts';
var utils=new GameUtils();
class Car {
constructor(player,type, gears, maxAccelerate,maxSpeed) {
constructor(player,name,gears,maxSpeed=200,maxRate=1.5,minRate=0.5) {
this.maxRate=maxRate;
this.minRate=minRate;
this.speedMod=0.01;
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();
}
this.speed=0;
this.maxSpeed=maxSpeed;
this.numberOfGears=gears;
this.speedPerGear=this.maxSpeed/this.numberOfGears;
this.velocity=0;
this.name = name;
this.currentGear = 0;
this.turboMod=0;
this.accelerating=false;
this.decelerating=false;
this.sndEngine=son.create("car_loop");
this.sndIdle=son.create("car_idle");
this.sndEngine.loop=true;
}
getGear() {
let result= Math.floor((this.speed/this.maxSpeed)*this.numberOfGears)+1;
if (result>this.numberOfGears) result=this.numberOfGears;
return result;
}
update() {
if (this.accelerating) {
if (this.sndIdle.playing) this.sndIdle.stop();
if (!this.sndEngine.playing) this.sndEngine.play();
this.velocity+=1/1000;
this.speed+=this.velocity;
if (this.speed>=this.maxSpeed) {
this.speed=this.maxSpeed+this.turboMod;
this.velocity-=1/1000;
}
if (this.currentGear==0) this.currentGear=1;
if (this.getGear()>this.currentGear) this.currentGear=this.getGear();
this.sndEngine.playbackRate=utils.progressPitch(this.speed/this.currentGear,this.speedPerGear);
this.player.speed=this.speed*0.0004;
this.player.move();
return;
}
if (!this.accelerating) {
if (this.speed==0) {
if (!this.sndIdle.playing) this.sndIdle.play();
this.velocity=0;
if (this.sndEngine.playing) this.sndEngine.stop();
this.accelerating=false;
return;
}
//decelerate code
this.velocity-=1/1500;
if (this.decelerating) {
this.velocity-=1/1500;
this.speed-=this.velocity;
}
if (this.velocity<1/1000) this.velocity=1/1500;
this.speed-=this.velocity;
if (this.speed<0) {
this.speed=0;
return;
}
if (this.currentGear==0) this.currentGear=1;
if (this.getGear()<this.currentGear) this.currentGear=this.getGear();
this.sndEngine.playbackRate=utils.progressPitch(this.speed/this.currentGear,this.speedPerGear);
this.player.speed=this.speed*0.0004;
this.player.move();
}
}
}
export {Car}
\ No newline at end of file
......@@ -2,13 +2,14 @@ import uniqueRandom from 'unique-random';
import {speech} from './tts';
class Cone {
constructor(world,x,z) {
console.log("cone made");
this.world=world;
this.x =x;
this.y =0;
this.z =z;
this.sound=this.world.player.sounds.play("loop_cone");
this.sound.sound.pos(this.x,this.y,this.z);
this.sound.sound.sound.sound.loop=true;
this.sound.sound.pos(this.x,this.y,this.z);
this.sound.sound.sound.loop=true;
}
collide() {
speech.speak("collide");
......
......@@ -51,14 +51,11 @@ class EngineSound {
}
startEngine() {
this.sndStart.play();
this.sndStart.sound.once("ended",()=> {
this.sndStart.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;
}
});
}
......@@ -105,6 +102,7 @@ class EngineSound {
}
handleAccelerating() {
console.log(this.sndAccelerate[this.currentAccelerateSound].progress);
if (this.sndAccelerate[this.currentAccelerateSound].progress > 0.99 || this.sndAccelerate[this.currentAccelerateSound].ended) {
this.max();
}
......
......@@ -27,14 +27,24 @@ class Game {
update() {
this.world.update();
if (this.input.isJustPressed(KeyEvent.DOM_VK_UP))
speech.speak(Math.round(this.world.player.x)+", "+Math.round(this.world.player.z));
if (this.input.isJustPressed(KeyEvent.DOM_VK_DOWN))
this.world.player.movetest(2);
if (this.input.isJustPressed(KeyEvent.DOM_VK_UP)) {
this.world.player.car.accelerating=true;
}
if (this.input.isJustReleased(KeyEvent.DOM_VK_UP)) {
this.world.player.car.accelerating=false;
}
if (this.input.isJustPressed(KeyEvent.DOM_VK_DOWN)) {
this.world.player.car.accelerating=false;
this.world.player.car.decelerating=true;
}
if (this.input.isJustReleased(KeyEvent.DOM_VK_DOWN)) {
this.world.player.car.decelerating=false;
}
if (this.input.isJustPressed(KeyEvent.DOM_VK_RIGHT))
this.world.player.movetest(3);
speech.speak("velocity: "+this.world.player.car.velocity+" gear: "+this.world.player.car.currentGear+" speed: "+this.world.player.car.speed+" getGear: "+this.world.player.car.getGear());
if (this.input.isJustPressed(KeyEvent.DOM_VK_LEFT))
this.world.player.movetest(4);
speech.speak(this.world.player.z);
}
......
export var gameID="";
export var cars={
default:{
name:"default",
gears:5,
maxSpeed:500,
},
}
import {SoundSource} from './sonoSource';
import {Game} from './game';
export var version="1.0";
......
import {cars} from './main';
import uniqueRandom from 'unique-random';
import {speech} from './tts';
import {Car} from './car';
......@@ -20,7 +21,7 @@ export default class Player {
this.x=this.world.size/2;
this.z=this.world.size/2;
this.y=0;
this.car=new Car(this,1,5,1,2);
this.car=new Car(this,cars.default.name,cars.default.gears,cars.default.maxSpeed);
}
get position() {
......@@ -36,11 +37,8 @@ export default class Player {
}
update() {
if (this.moving) {
this.move();
}
}
this.car.update();
}
move() {
this.position = [this.x + (Math.sin(this.alpha) * (this.speed)), 0, this.z + (Math.cos(this.alpha) * (this.speed))];
this.stepCounter += this.speed;
......@@ -52,7 +50,7 @@ i.collide();
}
if (this.stepCounter >= 1) {
this.stepCounter = 0;
this.sounds.playStatic('tick');
//this.sounds.playStatic('tick',false);
}
}
checkWalls() {
......
......@@ -127,7 +127,7 @@ class SoundObject {
//returnObject = new SoundObjectItem(sono.utils.cloneBuffer(found.sound.data), () => { this.doneLoading(); });
returnObject = new SoundObjectItem(found.sound.data, () => { this.doneLoading(); });
}
return returnObject;
return returnObject.sound;
}
enqueue(file) {
......
......@@ -16,13 +16,12 @@ maxDistance:50,
* @return {object} A Sound Source object
*/
export class SoundSource {
constructor(file, x = 0, y = 0, z = 0,loop=true) {
constructor(file, x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
this.loop = loop;
this.sound = son.create(file);
this.pan = this.sound.sound.effects.add(panner());
this.sound = son.create(file);
this.pan = this.sound.effects.add(panner());
this.pos(this.x, this.y, this.z);
this.rate = 1;
this.speed = 0;
......@@ -32,7 +31,9 @@ export class SoundSource {
this.rateShiftSpeed = 0.015;
this.sound.currentPosition = 0;
}
set loop(v) {
this.sound.loop=v;
}
/** Plays the sound source */
play() {
// This.sound.seek(0);
......@@ -49,8 +50,7 @@ export class SoundSource {
this.y = y;
this.z = z;
this.pan.setPosition(x, y, z);
console.log("pan: "+this.x+" "+this.z);
}
}
/** Empty update method */
update() {
......
......@@ -118,7 +118,10 @@ class SoundItem {
this.sound = son.create(file);
}
}
set loop(v) {
if (this.threeD) this.sound.sound.sound.sound.loop=true;
if (!this.threeD) this.sound.sound.loop=true;
}
destroy() {
this.sound.destroy();
}
......
import SoundSource from './sonoSource';
import panner from 'sono/effects/panner';
import {son} from './sonoObject';
import Player from './player';
......@@ -6,6 +7,7 @@ import {Cone} from './cone';
export default class World {
constructor(game) {
this.game = game;
this.contents = {
static: [],
dynamic: []
......@@ -14,21 +16,22 @@ export default class World {
panner.defaults.maxDistance=this.size;
this.player = new Player(this);
this.sides=son.create("beep_sides");
this.sidesPan=this.sides.sound.effects.add(panner());
this.sidesPan=this.sides.effects.add(panner());
this.front=son.create("beep_front");
this.frontPan=this.front.sound.effects.add(panner());
this.frontPan=this.front.effects.add(panner());
this.back=son.create("beep_back");
this.backPan=this.back.sound.effects.add(panner());
this.backPan=this.back.effects.add(panner());
this.sidesPan.set(-1);
this.frontPan.set(0);
this.backPan.set(0);
}
this.level=1;
}
set level(v) {
this.contents.static.push(new Cone(this,25,49));
//this.contents.static.push(new Cone(this,25,25));
}
update() {
this.player.update();
for (const i of this.contents.dynamic) {
for (const i of this.contents.dynamic) {
i.update();
}
}
......
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