Commit ff07ebc0 authored by Raphaël Bastide's avatar Raphaël Bastide

Bonuses in json

parent 901b61bc
......@@ -84,13 +84,29 @@
},
{
"name": "buildUp",
"type": "buildPlace",
"char": "1",
"verticalShift": "500"
"verticalShift": -500
},
{
"name": "buildDown",
"type": "buildPlace",
"char": "0",
"verticalShift": "-500"
"verticalShift": 500
},
{
"name": "bonus1",
"type": "bonus",
"posX": 3300,
"posY": 500,
"unlocks":"boost"
},
{
"name": "bonus2",
"type": "bonus",
"posX": 2300,
"posY": 500,
"unlocks":"jump"
}
]
}
......@@ -49,15 +49,23 @@ function launch(units){
// Building common objects
var car = Composites.car(150, 100, 150, 30, 40)
var ball = Bodies.circle(100, 100, 100, { friction:1, frictionAir:.003, restitution: 0.2})
var bonus = Bodies.circle(1800, 0, 10, {name:"bonus",isStatic:true, isSensor:true})
bonus.render.fillStyle ="red"
// var bonus = Bodies.circle(1800, 0, 10, {name:"bonus",isStatic:true, isSensor:true})
// bonus.render.fillStyle ="red"
ball.render.sprite.texture = 'img/ball.svg'
// var bonuses = findObjectByKey(data.units, 'type', 'bonus')
const bonuses = findObjectsByKey(data.units, 'type', 'bonus')
console.log(bonuses);
for (var i = 0; i < bonuses.length; i++) {
var bonus = bonuses[i]
var toBuild = Bodies.circle(bonus.posX, bonus.posY, 20,{isStatic:true, isSensor:true})
var toUnLock = findObjectsByKey(data.units, 'name', bonus.unlocks)[0]
var bonusColor = toUnLock.color
toBuild.render.fillStyle = bonusColor
World.add(world,toBuild);
}
// Adding common objects to the world
World.add(world, [
bonus,
ball
]);
......@@ -78,14 +86,11 @@ function launch(units){
// Building terrain from the character array
function buildUnit(char, nextPos){
var unit = findObjectByKey(data.units, 'char', char);
if (unit.name == "buildUp") {
nextPos.x += 0
nextPos.y += -500
}else if (unit.name == "buildDown") {
var unit = findObjectsByKey(data.units, 'char', char)[0];
if (unit.type == "buildPlace") {
nextPos.x += 0
nextPos.y += 500
}else {
nextPos.y += unit.verticalShift
}else{
if (unit.verticalShift == 1) {
var posX = nextPos.x
var posY = nextPos.y + (unit.angle * 550) - 6
......@@ -96,7 +101,7 @@ function launch(units){
var posX = nextPos.x
var posY = nextPos.y + (unit.angle * 550) + 6
}
w = w + w * Math.round(Math.abs(unit.angle))
w += w * Math.round(Math.abs(unit.angle))
var toBuild = Bodies.rectangle(posX, posY, w, h, {isStatic:unit.isStatic,angle: Math.PI * unit.angle})
toBuild.isLocked = unit.isLocked
toBuild.name = unit.name
......@@ -132,29 +137,23 @@ function launch(units){
function unlock(unit){
unit.isLocked = false
}
function findObjectByKey(array, key, value) {
for (var i = 0; i < array.length; i++) {
if (array[i][key] === value) {
return array[i];
}
function findObjectsByKey(array, key, value) {
var result = []
for (var i = 0; i < array.length; i++) {
if (array[i][key] === value) {
result.push(array[i]);
}
return null;
}
return result
// return null;
}
// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
// World.add(world, mouseConstraint);
mouseConstraint = MouseConstraint.create(engine, {mouse: mouse});
World.add(world, mouseConstraint);
render.mouse = mouse;
// Render after each Tick
Events.on( engine, 'afterTick', (e)=> {
if (bulletTime) {
......@@ -196,7 +195,6 @@ function launch(units){
var pairs = event.pairs;
for (var i = 0, j = pairs.length; i != j; ++i) {
var pair = pairs[i];
console.log(pair);
var col = pair.bodyB.render.fillStyle
if (col !== "#ffffff00") { // If not transparent, highlight the color when collision
col = col.substring(0, col.length - 2);
......@@ -208,7 +206,7 @@ function launch(units){
}else if(pair.bodyB.name === 'bulletTime') {
ball.render.sprite.texture = 'img/ball-bullettime.svg'
bulletTime = true
}else if(pair.bodyB.name === 'bonus') {
}else if(pair.bodyB.type === 'bonus') {
bonusTime = true
bonus.render.fillStyle = "#ffffff00"
ball.render.sprite.texture ="img/bonus.svg"
......@@ -224,8 +222,6 @@ function launch(units){
var currentCollided = pair.bodyB.id; // avoid repeated collision
if ( lastCollided != currentCollided) {
if (isBackInTime) {
// console.log('back',checkPoint);
console.log(ball.position.x, ball.position.y);
Body.setPosition(ball,{x:checkPoint.x, y:checkPoint.y})
isBackInTime = false
}else {
......
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