Skip to content

Instantly share code, notes, and snippets.

@sergiogarciadev
Last active December 14, 2016 15:04
Show Gist options
  • Select an option

  • Save sergiogarciadev/b544dd47100f05709c94eb2136ebc2da to your computer and use it in GitHub Desktop.

Select an option

Save sergiogarciadev/b544dd47100f05709c94eb2136ebc2da to your computer and use it in GitHub Desktop.
// https://buch415.github.io/game-off-2016/
// 648
setup: function () {
this.canonPosition = 48;
this.maxEnemyPosition = 790;
this.maxShootPosition = 700;
this.minShootPosition = 400;
this.currentEnemy = null;
this.enemies = [];
/*
this.setCannonAngle(cannonMinAngle);
var angle = cannonMinAngle;
while (angle < cannonMaxAngle) {
console.log(angle, this.calcShootDistance(angle));
angle += 0.01;
}
*/
},
calcShootDistance: function (angle) {
angle = angle || this.getCannonAngle();
var distance = (Math.pow(cannonShootSpeed, 2) * Math.sin(2 * angle)) / g;
distance += this.canonPosition;
return distance;
},
calcShootAngle: function (R) {
R = R - this.canonPosition;
return Math.asin( (R*g) / (Math.pow(cannonShootSpeed, 2) * 2) );
},
updateEnemies: function () {
this.enemies.forEach(enemy => enemy.killed = true);
this.getEnemies().forEach((e, enemyIndex) => {
var found = false;
this.enemies.forEach(enemy => {
if (Math.abs(e[0] - enemy.x) < 5) {
found = true;
enemy.index = enemyIndex;
enemy.x = e[0];
enemy.killed = false;
}
}, this);
if (!found) {
console.log('New enemy!');
this.enemies.push({
index: enemyIndex,
x: e[0],
y: e[1]
});
}
}, this);
this.enemies.forEach((enemy, index) => {
if (enemy.killed) {
console.log('Enemy killed!');
this.enemies.splice(index, 1);
}
}, this);
},
update: function () {
this.updateEnemies();
//if (this.hp() < 8 && this.ready()) {
// this.repair();
//}
if (this.enemies.length == 0) {
return;
}
var enemies = this.enemies.filter(e => !e.shot);
if (enemies.length == 0) {
return;
}
this.currentEnemy = enemies[0];
this.nextEnemy = enemies.length >= 2 ? enemies[1] : null
if (this.currentEnemy.x < this.minShootPosition) {
return;
}
if (!this.currentEnemy.marked) {
console.log('Mark!', this.currentEnemy.index);
this.mark(this.currentEnemy.index);
this.currentEnemy.marked = true;
}
var bulletDelay = this.currentEnemy.x / 10;
if ((this.calcShootDistance() - this.currentEnemy.x + bulletDelay) < 1) {
if (this.ready() && this.currentEnemy.x < this.maxShootPosition) {
console.log('Shoot: ', this.calcShootDistance(), this.currentEnemy.x);
this.shoot();
this.currentEnemy.shot = true;
if (this.nextEnemy && this.nextEnemy.x - 30 < this.currentEnemy.x) {
this.nextEnemy.shot = true;
}
return;
}
}
this.setCannonAngle(this.calcShootAngle(this.currentEnemy.x - bulletDelay));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment