Commit 193d143f authored by ogomez92's avatar ogomez92

ok this steering system is weird

parent 66b8b2c4
......@@ -3,8 +3,9 @@ import {GameUtils} from './utilities';
import {speech} from './tts';
var utils=new GameUtils();
class Car {
constructor(player,name,gears,maxSpeed=200,maxRate=1.5,minRate=0.5) {
constructor(player,name,gears,maxSpeed=200,maxRate=1.5,minRate=0.5,steerFactor=5) {
this.maxRate=maxRate;
this.steerFactor=steerFactor;
this.minRate=minRate;
this.speedMod=0.01;
this.player=player;
......@@ -19,7 +20,9 @@ class Car {
this.accelerating=false;
this.decelerating=false;
this.sndEngine=son.create("car_loop");
this.sndEngine.volume-=0.5;
this.sndIdle=son.create("car_idle");
this.sndIdle.volume-=0.5;
this.sndEngine.loop=true;
}
getGear() {
......@@ -28,6 +31,7 @@ class Car {
return result;
}
update() {
if (this.steering) this.accelerating=false;
if (this.accelerating) {
if (this.sndIdle.playing) this.sndIdle.stop();
if (!this.sndEngine.playing) this.sndEngine.play();
......@@ -39,7 +43,7 @@ class Car {
}
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,1,this.speedPerGear,1,1.8);
this.sndEngine.playbackRate=utils.progressPitch(this.speed/this.currentGear,1,this.speedPerGear,this.minRate,this.maxRate);
this.player.speed=this.speed*0.0004;
this.player.move();
return;
......@@ -66,7 +70,7 @@ class Car {
}
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,0,this.speedPerGear,1,1.8);
this.sndEngine.playbackRate=utils.progressPitch(this.speed/this.currentGear,1,this.speedPerGear,this.minRate,this.maxRate);
this.player.speed=this.speed*0.0004;
this.player.move();
......
import uniqueRandom from 'unique-random';
import panner from 'sono/effects/panner';
import {utils} from './player';
import {speech} from './tts';
import {son} from './sonoObject';
class Cone {
constructor(world,x,z) {
constructor(world) {
console.log("cone made");
this.world=world;
this.x =x;
this.world=world;
let pos=this.findPosition();
this.x =pos.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.loop=true;
}
this.z =pos.z;
this.sound=son.create("loop_cone");
this.pan=this.sound.effects.add(panner({
panningModel: 'HRTF',
distanceModel: 'linear',
refDistance: 1,
maxDistance: 12,
rolloffFactor: 1,
coneInnerAngle: 360,
coneOuterAngle: 0,
coneOuterGain: 0
}));
this.pan.setPosition(this.x,this.y,this.z);
this.sound.loop=true;
this.sound.playbackRate=utils.getRandomArbitrary(0.8,1.2);
this.sound.play();
}
collide() {
speech.speak("collide");
this.world.player.sounds.playStatic("cone_knock"+utils.randomInt(1,11),false);
this.destroy();
}
get position() {
return [this.x, this.y, this.z];
}
findPosition() {
let cont=true;
let x=utils.randomInt(1,this.world.size-2);
let z=utils.randomInt(1,this.world.size-2);
if (this.world.contents.static.length==0) {
return {x:x,z:z};
}//if
while (cont) {
x=utils.randomInt(1,this.world.size);
z=utils.randomInt(1,this.world.size);
for (let i of this.world.contents.static) {
if (i.x==x && i.z==z) {
continue;
}//if
}//for
cont=false;
return {x:x,z:z};
}//function
}
destroy() {
this.sound.destroy();
}
}
export {Cone}
\ No newline at end of file
......@@ -27,7 +27,7 @@ class Game {
update() {
this.world.update();
if (this.input.isJustPressed(KeyEvent.DOM_VK_UP)) {
if (this.input.isDown(KeyEvent.DOM_VK_UP)) {
this.world.player.car.accelerating=true;
}
if (this.input.isJustReleased(KeyEvent.DOM_VK_UP)) {
......@@ -41,12 +41,18 @@ 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))
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))
speech.speak(this.world.player.z);
if (this.input.isDown(KeyEvent.DOM_VK_RIGHT))
this.world.player.steer(0+this.world.player.car.steerFactor);
if (this.input.isDown(KeyEvent.DOM_VK_LEFT))
this.world.player.steer(0-this.world.player.car.steerFactor);
if (this.input.isJustReleased(KeyEvent.DOM_VK_RIGHT))
this.world.player.car.steering=false;
if (this.input.isJustReleased(KeyEvent.DOM_VK_LEFT))
this.world.player.car.steering=false;
if (this.input.isJustPressed(KeyEvent.DOM_VK_D))
//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());
speech.speak(this.world.player.alpha);
}
}
export {Game}
\ No newline at end of file
......@@ -2,8 +2,11 @@ export var gameID="";
export var cars={
default:{
name:"default",
gears:5,
maxSpeed:500,
steerFactor: 0.1,
gears:4,
maxSpeed:450,
maxRate:1.80,
minRate:1.0,
},
}
import {SoundSource} from './sonoSource';
......
import {cars} from './main';
import {GameUtils} from './utilities';
export let utils=new GameUtils();
import uniqueRandom from 'unique-random';
import {speech} from './tts';
import {Car} from './car';
......@@ -7,9 +9,11 @@ import {SoundHandler} from './soundHandler.js';
import {OldTimer} from './oldtimer';
export default class Player {
constructor(world) {
this.coneRay=new Ray(this,false);
this.zWall=0;
this.xWall=0;
this.x = 0;
this.radars=[];
this.y = 0;
this.y = 0;
this.zTimer=new OldTimer();
this.z = 0;
this.alpha = 0;
......@@ -21,7 +25,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,cars.default.name,cars.default.gears,cars.default.maxSpeed);
this.car=new Car(this,cars.default.name,cars.default.gears,cars.default.maxSpeed,cars.default.maxRate,cars.default.minRate,cars.default.steerFactor);
}
get position() {
......@@ -38,7 +42,24 @@ export default class Player {
update() {
this.car.update();
}
this.coneRay.scan();
/*
let maxCones=0;
for (let i of this.world.contents.static) {
if (utils.distance(this.x,this.z,i.x,i.z)>5) {
i.sound.sound.sound.stop();
}
else {
if (maxCones<5) {
i.sound.sound.sound.play();
maxCones++;
i.sound.sound.sound.playbackRate=i.rate;
}
}
}
*/
}
move() {
this.position = [this.x + (Math.sin(this.alpha) * (this.speed)), 0, this.z + (Math.cos(this.alpha) * (this.speed))];
this.stepCounter += this.speed;
......@@ -54,21 +75,25 @@ i.collide();
}
}
checkWalls() {
this.frontRay=new Ray(this);
let result=this.frontRay.scanWall(this.world.size+1,this.alpha);
this.world.back.volume=utils.progressVolume(result.distance,1,20,1,0.1);
if (result.distance>25) {
this.zWall=0;
this.xWall=0;
this.wallMultiplier=50;
this.frontRay=new Ray(this);
let result=this.frontRay.scanWall(100,this.alpha);
//speech.speak(result.distance);
/*
if (this.zWall>2 && this.zWall<11) {
if (this.zTimer.elapsed>this.zWall*this.wallMultiplier) {
//this.frontPan.setPosition(this.x,0,1);
//this.front.play();
speech.speak("ok");
this.zTimer.restart();
}
else {
if (this.zWall==0 && this.xWall==0) {
this.xWall=result.x;
this.zWall=result.z;
}
*/
}
this.world.backPan.setPosition(this.xWall,0,this.zWall);
}
steer(dir) {
this.car.steering=true;
this.alpha+=dir;
if (this.alpha>359) this.alpha=0;
if (this.alpha<0) this.alpha=359;
}
}
import {son} from './sonoObject';
class Ray {
constructor(player) {
constructor(player,silent=true) {
this.result={
distance:0,
distance:0,
x:0,
y:0,
z:0,
......@@ -9,13 +10,37 @@ class Ray {
this.player=player;
this.x = this.player.x;
this.y = this.player.y;
this.sound=son.create("radar");
this.sound.loop=true;
this.z = this.player.z;
this.alpha = this.player.alpha;
}
get position() {
return [this.x, this.y, this.z];
}
scan() {
this.limit=12;
this.distanceToWall=0;
this.alpha=this.player.alpha;
let foundWall=false;
let count=0;
while (!foundWall && count<=12) { //let's not create unending loops
count++;
this.x=this.x + (Math.sin(this.alpha));
this.z=this.z + (Math.cos(this.alpha));
this.distanceToWall++;
//the following if condition checks if either x or z are more than 0 or max grid size.
if (this.x<1 || this.x>this.player.world.size || this.z<1 || this.z>this.player.world.size) {
foundWall=true;
this.sound.play();
}
else {
this.sound.stop();
}
}
return this.result;
}
set position(pos) {
this.x = pos[0];
this.y = pos[1];
......
......@@ -9,7 +9,7 @@ class SoundHandler {
this.maxDynamicSounds = 512;
this.currentStaticSound = 0;
this.maxStaticSounds = 512;
this.reuseSounds = true;
this.reuseSounds = false;
this.x = 0;
this.y = 0;
this.z = 0;
......@@ -87,6 +87,27 @@ class SoundHandler {
this.dynamicSounds[slot].sound.play();
return this.dynamicSounds[slot];
}
create(file,loop=true) {
let slot = 0,
reuse = 0;
if (this.reuseSounds) {
slot = this.findDynamicSound(file);
reuse = true;
}
if (slot == -1 || this.reuseSounds == false) {
slot = this.findFreeDynamicSlot();
reuse = false;
}
if (typeof this.dynamicSounds[slot] === 'undefined') {
if (reuse == false) {
this.dynamicSounds[slot] = new SoundItem(file, this.directional,loop);
}
} else if (reuse == false) {
this.dynamicSounds[slot].sound.destroy();
this.dynamicSounds[slot] = new SoundItem(file, this.directional,loop);
}
return this.dynamicSounds[slot];
}
update(position) {
if (this.directional == true) {
......
......@@ -4,11 +4,17 @@ progressPan(current, max) {
return ((current*200/max)-100)/100;
}
progressPitch(current,min,max,minPitch,maxPitch) {
if (current==0) console.log("current: "+current);
if (min==0) console.log("min:"+min);
if (max==0) console.log("max"+max);
return (current/(min+max))*(maxPitch-minPitch)+minPitch;
}
progressVolume(current,min,max,minVolume,maxVolume) {
if (current>max) return 0;
return (current/(min+max))*(maxVolume-minVolume)+minVolume;
}
getProportion(current,min,max,minVolume,maxVolume) {
if (current>max) return 0;
return (current/(min+max))*(maxVolume-minVolume)+minVolume;
}
distance3D(x1, y1, z1, x2, y2, z2) {
return Math.sqrt((x2 - x1) * (x2 - x1) +
(y2 - y1) * (y2 - y1) +
......
......@@ -16,18 +16,17 @@ export default class World {
panner.defaults.maxDistance=this.size;
this.player = new Player(this);
this.sides=son.create("beep_sides");
this.sidesPan=this.sides.effects.add(panner());
this.front=son.create("beep_front");
this.frontPan=this.front.effects.add(panner());
this.back=son.create("beep_back");
this.backPan=this.back.effects.add(panner());
this.sidesPan.set(-1);
this.frontPan.set(0);
this.backPan.set(0);
this.back=son.create("beep_back");
this.backPan=this.back.effects.add(panner());
this.back.loop=true;
this.back.play();
this.back.volume=0;
this.level=1;
}
set level(v) {
//this.contents.static.push(new Cone(this,25,25));
for (let i=1;i<=v*(this.size/2);i++) {
this.contents.static.push(new Cone(this));
}
}
update() {
this.player.update();
......@@ -38,8 +37,9 @@ set level(v) {
render() {
panner.setListenerPosition(this.player.x,this.player.y,this.player.z);
// panner.setListenerOrientation(Math.sin(this.player.alpha), 0, Math.cos(this.player.alpha));
for (const i of this.contents.dynamic) {
//panner.setListenerOrientation(Math.sin(this.player.alpha), 0, Math.cos(this.player.alpha));
panner.setListenerOrientation(Math.cos(this.player.alpha), 0, Math.sin(this.player.alpha),0,1,0);
for (const i of this.contents.dynamic) {
if (typeof i.sound !== undefined) {
i.sounds.update({x: i.x, y: i.y, z: i.z});
}
......
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