Commit 2f8aec10 authored by Raphaël Bastide's avatar Raphaël Bastide

Bonus compas

parent 7e9186bc
......@@ -8,3 +8,7 @@ A game you play in the URL
- [Matter.js](http://brm.io/matter-js/), physics engine
- [Version Skeletor](http://www.xavierklein.eu/versions/), libre font by Xavier Klein
# License
[GNU General Public License](https://www.gnu.org/licenses/gpl.html)
......@@ -5,7 +5,7 @@ var d = document,
function preloadImgs() {
var images = ['img/burl-ball.svg','img/burl-bonus.svg'];
var images = ['burl-ball.svg','burl-bonus.svg'];
for (var i = 0; i < arguments.length; i++) {
images[i] = new Image();
images[i].src = preload.arguments[i];
......
......@@ -31,6 +31,8 @@ function launch(units, isRefresh){
timeScaleTarget = 1,
zoom = 500,
zoomTarget = 500,
bonusList = [],
bonusCompasList = [],
counterBonus = counterTime = counterZoom = collisionBuffer = 0,
bulletTime, bonusTime, isTeleported, checkPoint, lastCollided = false
......@@ -88,10 +90,11 @@ function launch(units, isRefresh){
// Building common objects
var ball = Bodies.circle(100, 100, 100, { friction:1, frictionAir:0.000001, restitution: 0.4})
ball.render.sprite.texture = 'img/burl-ball.svg'
const bonuses = findObjectsByKey(data.units, 'role', 'bonus')
var bonuses = findObjectsByKey(data.units, 'role', 'bonus')
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 bonusCompas = Bodies.circle(-1000, -1000, 3,{isStatic:true, isSensor:true})
var toUnLock = findObjectsByKey(data.units, 'name', bonus.unlocks)[0]
var bonusColor = toUnLock.color
// toUnLock.isLocked = false
......@@ -101,7 +104,13 @@ function launch(units, isRefresh){
toBuild.name = bonus.name
toBuild.role = bonus.role
toBuild.unlocks = bonus.unlocks
World.add(world,toBuild);
bonusCompas.targets = bonus.name
// bonusCompas.render.fillStyle = bonusColor
// bonusCompas.render.lineWidth = 5
// bonusCompas.render.strokeStyle = "black"
bonusList.push(toBuild)
bonusCompasList.push(bonusCompas)
World.add(world,[toBuild, bonusCompas]);
}
// Adding common objects to the world
......@@ -163,9 +172,9 @@ function launch(units, isRefresh){
}
}
var allBodies = Composite.allBodies(world)
function unlockFromBonus(bonus){
var toUnLock = findObjectsByKey(data.units, 'name', bonus.unlocks)[0]
var allBodies = Composite.allBodies(world)
for (var i = 0; i < allBodies.length; i++) {
var unit = allBodies[i]
if (unit.name == toUnLock.name) {
......@@ -174,7 +183,11 @@ function launch(units, isRefresh){
unit.render.fillStyle = toUnLock.color
unit.render.lineWidth = strokeWidth
}
if (unit.targets == bonus.name) {
World.remove(world, unit)
}
}
World.remove(world, bonus)
var message = '['+toUnLock.char+'] '+toUnLock.name+' unlocked!'
updateInventory(toUnLock.char)
......@@ -187,8 +200,23 @@ function launch(units, isRefresh){
World.add(world, mouseConstraint);
render.mouse = mouse;
// Render after each Tick
Events.on( engine, 'afterTick', (e)=> {
Events.on(engine, 'afterTick', (e)=> {
var ballCenter = ball.position
for (var i = 0; i < bonusList.length; i++) {
var bonusCenter = bonusList[i].position
var diffX = bonusCenter.x - ballCenter.x
var diffY = bonusCenter.y - ballCenter.y
var angle = Math.atan2(diffY, diffX)
var compasX = ballCenter.x + 140 * Math.cos(angle)
var compasY = ballCenter.y + 140 * Math.sin(angle)
for (var j = 0; j < bonusCompasList.length; j++) {
if (bonusCompasList[j].targets == bonusList[i].name) {
Body.setPosition(bonusCompasList[j],{x:compasX, y:compasY})
}
}
}
if (collisionBuffer >= 1000) {
togglePause()
collisionBuffer = 0
......@@ -236,10 +264,6 @@ function launch(units, isRefresh){
var b1 = pair.bodyB
var b2 = pair.bodyA // can differ accordins to World.add order
var col = b2.render.fillStyle
// if (col !== "#ffffff00") { // If not transparent, highlight the color when collision
// col = col.substring(0, col.length - 2);
// b2.render.fillStyle = col+"ff"
// }
if (!b2.isLocked) {
if (b2.name === 'boost' && !b1.inactiveBuffer) {
b1.render.fillStyle = '#00FF00';
......
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