Skip to content

Commit

Permalink
added sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickGibson committed Apr 29, 2020
1 parent 0d8bfd5 commit 752237c
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 54 deletions.
11 changes: 7 additions & 4 deletions err.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ MAYA
ohinek

anička
speed boost
speed boost
zpomaleni rotace u velkych aut

yannick
kill camera when he kills him self # lol wtf
kill counter
bumper eats food too
√ kill camera when he kills him self # lol wtf # (screen shake brother)
√ kill counter
√ bumper eats food too
√ not drawing the car bug when u spawn next to it
fix camera shake boost/kill
add food pop sounds
3 changes: 3 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def on_join(data):

car['color'] = random_color()
car['score'] = 0

car['rot'] = random.random() * (math.pi * 2)

car['acc'] = 0
car['turn'] = 0

Expand Down
10 changes: 10 additions & 0 deletions static/js/libraries/pixi-sound.js

Large diffs are not rendered by default.

152 changes: 102 additions & 50 deletions static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ const fpsStyle = new PIXI.TextStyle({
fill: ['#000000']
});

let focused = true;

window.onfocus = function() {
focused = true;
};
window.onblur = function() {
console.log("blurred");
focused = false;
if (inGame){

socket.emit("focusout");
Expand All @@ -57,13 +50,12 @@ window.onblur = function() {
PIXI.utils.skipHello();



let isCameraShaking = false;
let shakeCamTime = 0;
const maxShakeCamTime = 200;
let shakeCamIntensity;
let killShakeIntensity = 300;
let boostShakeIntensity = 20;
let boostShakeIntensity = 50;
const MAX_BOOSTBAR_WIDTH = 100;


Expand All @@ -84,7 +76,8 @@ var app = new PIXI.Application(
resolution: window.devicePixelRatio || 1,
antialias: true
});

app.renderer.view.style.width = 100 + "%";
app.renderer.view.style.height = 100+ "%";
document.body.appendChild(app.view);


Expand Down Expand Up @@ -175,10 +168,9 @@ let isGoingForward = false;
let isGoingBackward = false;
let isPressingEnter = false;

const loader = PIXI.Loader.shared;
const textures = {};

loader
const sounds = {};
PIXI.Loader.shared
.add("car", "static/images/vehicles/basic.png")
.add("bumper", 'static/images/bumpers/basic.png')
.add("lights", 'static/images/lights/basic.png')
Expand All @@ -189,19 +181,22 @@ loader
.add("minimap", 'static/images/gui/minimap.png')
.add("whitePixel", 'static/images/gui/white_pixel.png')
.add("killCountEmoji", 'static/images/gui/kill_count_emoji.png')
.add("foodSound", 'static/sounds/blop.mp3')
.add("crashSound", 'static/sounds/crash.mp3')
.add("boostSound", 'static/sounds/boost.mp3')
;

let scoreboard;
let playerPoint;
let bestPlayerPoint;
let boostCharge;

loader.onProgress.add((e) => {
//console.log(e.progress + " - " + new Date().getMilliseconds());
PIXI.Loader.shared.onProgress.add((e) => {
console.log(e.progress + " - " + new Date().getMilliseconds());
});
loader.load( (loader, resources) =>
let loaded = false;
PIXI.Loader.shared.load( (loader, resources) =>
{
console.log("loaded");
textures.car = resources.car.texture;
textures.bumper = resources.bumper.texture;
textures.lights = resources.lights.texture;
Expand Down Expand Up @@ -263,6 +258,17 @@ loader.load( (loader, resources) =>
boostContainer.addChild(boostCharge);


//
///
//// SOUNDS
///
//
PIXI.sound.add('food', resources.foodSound);
resources.crashSound.volume = 0.07;
PIXI.sound.add('crash', resources.crashSound);

sounds.boost = resources.boostSound;



// Listen for animate update
Expand Down Expand Up @@ -417,6 +423,9 @@ loader.load( (loader, resources) =>

vanishingFood.push(food[foodId]);


PIXI.sound.play("food");

//console.log("Ham");
socket.emit("eat", foodId);
}
Expand Down Expand Up @@ -471,7 +480,6 @@ loader.load( (loader, resources) =>

});



socket.on('heartBeat', (data)=>
{
Expand Down Expand Up @@ -565,8 +573,11 @@ loader.load( (loader, resources) =>
}
}
});

loaded = true;
});



function calcScreenSize(){

}
Expand Down Expand Up @@ -609,6 +620,13 @@ function constrain(val, min, max){
}
}

//
///
//////
///////// START CAR
//////
///
//

class Car
{
Expand Down Expand Up @@ -748,6 +766,8 @@ class Car
this.turbo = new Turbo(container, color);
this.boostIsOn = false;
this.particles = particles;

this.turboSound = null;
}
get width(){
return this.carSprite.width;
Expand Down Expand Up @@ -874,10 +894,25 @@ class Car
boostOn(){
this.boostIsOn = true;
this.accSpeed = Car.BOOST_SPEED;

if (this.turboSound != null && this.turboSound.volume == 0){
this.turboSound.stop();
this.turboSound = null;
}
if (this.turboSound == null){
this.turboSound = PIXI.sound.Sound.from(sounds.boost);
this.turboSound.play();
}

}
boostOff(){
this.boostIsOn = false;
this.accSpeed = Car.NORMAL_SPEED;

if (this.turboSound != null)
{
this.turboSound.volume = 0.0;
}
}
doesKill(otherCar){
const myVertices = Car.getVertices(this.bumperSprite);
Expand Down Expand Up @@ -938,6 +973,13 @@ class Car
}
}

//
///
//////
///////// START TURBO
//////
///
//

class Turbo{
static PARTICLES_PER_TICK = 15;
Expand Down Expand Up @@ -1186,6 +1228,7 @@ class Food{
}
}


function moveCamera(){
container.pivot.x = lerp(container.pivot.x, car.x, 0.2);
container.pivot.y = lerp(container.pivot.y, car.y, 0.2);
Expand Down Expand Up @@ -1213,7 +1256,6 @@ function shakeCam(intensity, force = false){
function stopCameraShake(){
isCameraShaking = false;
shakeCamTime = 0 ;
console.log("stop");
}


Expand All @@ -1232,7 +1274,8 @@ socket.on("join", initData =>{



mouseControls = mouseCheckbox.checked;
mouseControls = mouseCheckbox.checked;

mouseAngle = startRot;
playerPoint.tint = initData.color;

Expand All @@ -1251,12 +1294,16 @@ socket.on("join", initData =>{
});


// Kill response from server
socket.on("u killed", iKilled);
function iKilled(data)
{
console.log("kill auth");
killCountText.text = ++killCount;
shakeCam(killShakeIntensity, true);
displayVictimText(data['dead']);
killCountText.text = ++killCount;

PIXI.sound.play("crash");

shakeCam(killShakeIntensity, true);
displayVictimText(data['dead']);
}
function displayVictimText(name){
victimText.alpha = 1;
Expand All @@ -1276,14 +1323,15 @@ function displayVictimText(name){
}, 30)
}

socket.on("u killed", iKilled);

socket.on("dead", (data)=>{

inGame = false;
console.log("you died");
shakeCam(killShakeIntensity, true);

PIXI.sound.play("crash");

deathText.text = "You were killed by " + data.killer;

// Delete car
Expand Down Expand Up @@ -1312,7 +1360,7 @@ socket.on("dead", (data)=>{
i++;
},30)
});
app.stage.interactive = true;

app.stage.on("pointermove", (e)=>
{

Expand All @@ -1327,8 +1375,8 @@ app.stage.on("pointermove", (e)=>
mouseAngle = Math.atan2(b, a);
mouseAngle = (mouseAngle + Math.PI * 2.5) % (Math.PI * 2) // Offset

if (inGame)
{
if(inGame){
// cis the distance from mouse to center of my car
const c = Math.sqrt(a * a + b * b);
if (c < 40){
car.acc = 0;
Expand All @@ -1337,21 +1385,32 @@ app.stage.on("pointermove", (e)=>
car.acc = 1;
}
}

}
});
app.stage.interactive = true;
app.view.addEventListener("pointerdown", ()=>
{
isPressingEnter = true;
shakeCam(boostShakeIntensity);
if (mouseControls)
{
isPressingEnter = true;
if (inGame){
shakeCam(boostShakeIntensity);
}
}

});
app.view.addEventListener("pointerup", ()=>
{
isPressingEnter = false;
isBoostEnabled = true;
if (inGame)
if (mouseControls)
{
car.boostOff();
stopCameraShake();
isPressingEnter = false;
if (inGame)
{
isBoostEnabled = true;
car.boostOff();
stopCameraShake();
}
}
});
app.view.addEventListener("mouseout", ()=>
Expand Down Expand Up @@ -1430,9 +1489,9 @@ document.onkeyup = e =>

case 32: // Space
isPressingEnter = false;
isBoostEnabled = true;
if (inGame)
{
isBoostEnabled = true;
car.boostOff();
}
break;
Expand All @@ -1442,18 +1501,8 @@ document.onkeyup = e =>



let w = window.innerWidth;
var h = window.innerHeight;
// Resize function window
window.addEventListener('resize', resize);

var maxWidth = 1920;
let normalRatio = 16/9;
var windowRatio = window.innerWidth / window.innerHeight;
let screenDifferenceRatio = windowRatio / normalRatio;

app.renderer.view.style.width = 100 + "%";
app.renderer.view.style.height = 100+ "%";
function resize()
{
const myScreenRatio = window.innerWidth / window.innerHeight;
Expand Down Expand Up @@ -1495,7 +1544,7 @@ function resize()
deathText.y = appHeight / 2 + 150;


console.log(`appWidth: ${appWidth}`);
//console.log(`appWidth: ${appWidth}`);
app.renderer.resize(appWidth, appHeight);

}
Expand All @@ -1505,8 +1554,11 @@ resize();

$(document).ready(()=>{
$("#nameButton").click(()=>{
$name = $("#name-input").val();
socket.emit("join", {name: $name});
if (loaded)
{
$name = $("#name-input").val();
socket.emit("join", {name: $name});
}
});
$('#name-input').keypress( (e) => {
if (e.which == 13) {
Expand Down
Binary file added static/sounds/blop.mp3
Binary file not shown.
Binary file added static/sounds/boost.mp3
Binary file not shown.
Binary file added static/sounds/crash.mp3
Binary file not shown.
Loading

0 comments on commit 752237c

Please sign in to comment.