xxxxxxxxxx
326
/*
OMG PATCHNOTES
VERSION A.123984y6t7
UPDATE MONKE
SECRET GOD BUTTONS
BANAN LOSE HEALTH
REMOVED BULLETSPEED.z
*/
var game;
var BANANHEALTH = 10;
function setup() {
createCanvas(400, 400);
textAlign(CENTER);
game = new BananLasrMonke();
frameRate(60);
createButton('stronger bananas').mouseReleased((e) => BANANHEALTH+=1);
createButton('god bananas').mouseReleased((e) => BANANHEALTH=150);
}
class BananLasrMonke {
constructor() {
this.t = 0;
this.dt = 0.05;
this.shopBtns = [];
this.levels = new Map();
this.coin = 0;
this.maxBanans = 3;
this.bananValue = 0.2;
this.maxBananSpeed = 1;
this.bananSpawnRate = 2;
this.lastTimeBananSpawned = this.t;
this.banans = [];
this.cx = width / 2;
this.cy = width / 2;
this.shootingStrategy = "random";
this.bullets = [];
this.monke = new LasrMonke(this.t, 100, this.cx, this.cy);
this.setupShop();
this.spawnBanan();
}
setupShop() {
const btns = this.shopBtns;
/* todo improve this hhhhhhhh
*/
const m = this.monke;
createButton("buy damage").mouseReleased((e) => (m.damage += 0.1));
createButton("buy range").mouseReleased((e) => (m.range += 1));
createButton("buy speed").mouseReleased((e) => (m.speed += 0.0005));
createButton("buy shootSpeed").mouseReleased((e) => (m.shootSpeed -= 0.05));
createButton("buy laserVelocity").mouseReleased(
(e) => (m.laserVelocity += 0.1)
);
createButton("increase accuracy").mouseReleased((e) => this.monke.accuracy=max(this.monke.accuracy-1, 1));
createButton("spawn Banan").mouseReleased((e) => this.spawnBanan());
createButton("godmonke debug").mouseReleased(function (e) {
m.shootSpeed = 0.001;
m.range = 400;
m.laserVelocity = 8;
});
}
spawnBanan() {
// spawn banans somewhere near an edge and make them walk somewhere
// through the centre
const sides = [
[1, height],
[width, 1],
[0, 1],
[1, 0],
];
const [dx, dy] = sides[Math.floor(Math.random() * 4)];
var x = constrain(random(0, 400) * dx, 0, 400);
var y = constrain(random(0, 400) * dy, 0, 400);
var bananPos = createVector(x, y);
var btx = random(0.8, 1.2) * this.cx;
var bty = random(0.8, 1.2) * this.cy;
var bananTarget = createVector(btx, bty);
var maxBananSpeed = p5.Vector.sub(bananTarget, bananPos);
maxBananSpeed.setMag(random(this.maxBananSpeed));
this.banans.push(new Banan(bananPos, maxBananSpeed));
// print(`spawned a banan`)
}
getTarget(shootingStrategy) {
if (this.banans.length < 1) {
return null;
}
var target;
var tmp;
switch (shootingStrategy) {
case "manual":
case "random":
default:
tmp = this.banans[Math.floor(Math.random() * this.banans.length)].pos;
if (this.monke.accuracy==1){
target = createVector(tmp.x, tmp.y
);
} else {
target = createVector(
tmp.x + random(-1, 1) * this.monke.accuracy,
tmp.y + random(-1, 1) * this.monke.accuracy,
);
}
break;
}
return target;
}
updateBullets() {
for (var i = this.bullets.length - 1; i > -1; i--) {
var [b, bs, ls] = this.bullets[i];
b.add(bs);
ls.life -= this.monke.laserVelocity;
if (ls.life < 1) {
this.bullets.splice(i, 1);
} else if (b.x < 0 || b.x > width || b.y < 0 || b.y > height) {
this.bullets.splice(i, 1);
}
// UGLY UGLY O(n^)
for (var ban of this.banans) {
if (b.dist(ban.pos) < 5) {
ban.health -= 1;
// idk if i should update money here or in the other func ,
this.bullets.splice(i, 1);
}
}
}
if (this.t - this.monke.lastTimeShot > this.monke.shootSpeed) {
var target = this.getTarget();
if (target != null) {
const m = this.monke.pos;
var bulletPos;
if (random() > 0.5) {
bulletPos = createVector(m.x - 5, m.y - 30);
} else {
bulletPos= createVector(m.x - 15, m.y - 30);
}
var bulletSpeed = p5.Vector.sub(target, bulletPos);
bulletSpeed.setMag(this.monke.laserVelocity);
// store the lifespan of a bullet in the z component of its velocity
// must be done after setting magnitude
this.bullets.push([
bulletPos,
bulletSpeed,
{ life: this.monke.range },
]);
this.monke.shots++;
this.monke.lastTimeShot = this.t;
}
}
}
update() {
var t = this.t;
for (var i = this.banans.length - 1; i > -1; i--) {
var b = this.banans[i];
b.update();
if (b.health <= 0) {
this.banans.splice(i, 1);
print(`removed banan ${i} because dead`);
this.coin += this.bananValue;
text('💥',b.pos.x, b.pos.y)
} else if (
b.pos.x < 0 ||
b.pos.x > width ||
b.pos.y < 0 ||
b.pos.y > height
) {
this.banans.splice(i, 1);
print(`removed banan ${i} because gone`);
}
}
if (
this.t - this.lastTimeBananSpawned > this.bananSpawnRate &&
this.banans.length < this.maxBanans
) {
this.spawnBanan();
this.lastTimeBananSpawned = this.t;
}
this.updateBullets();
this.monke.update(this.t);
this.t += this.dt;
}
drawGUI() {
push();
textAlign(LEFT);
rect(0, 0, 112, 40);
text(`💲${this.coin.toFixed(1)}`, 4, 12);
text(`shooting ${this.shootingStrategy}ly`, 4, 23);
text(`acc ${100-this.monke.accuracy}`, 4, 36);
pop();
}
drawBullets() {
push()
stroke("red");
strokeWeight(2);
for (const [b, bs] of this.bullets) {
push();
translate(b.x, b.y);
line(0, 0, -bs.x, -bs.y);
pop();
}
pop();
}
draw() {
for (const b of this.banans) {
b.draw();
}
this.monke.draw();
this.drawBullets();
this.drawGUI();
}
}
class Banan {
constructor(pos, vel) {
this.health = BANANHEALTH;
this.pos = pos;
this.vel = vel;
this.t = 0;
this.dt = random(1, -1) / 100;
}
applyDamage(dmg) {
this.health -= dmg;
}
update() {
this.pos.add(this.vel);
this.t += this.dt;
}
draw() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.t);
textSize(this.health + 5);
text("🍌", 0, 0);
text(`${this.health}`, 0, this.health + 5);
pop();
}
}
class SuperLasrMonke {}
class LasrMonke {
constructor(t, R, cx, cy) {
this.damage = 0.01;
this.range = 100;
this.shots = 0;
this.accuracy = 50;
this.speed = 0.005;
this.shootSpeed = 1;
this.lastTimeShot = -this.shootSpeed;
this.laserVelocity = 1;
this.t = 0;
this.cx = cx;
this.cy = cy;
this.R = R;
var x = this.cx + cos(t) * this.R;
var y = this.cy + cos(t) * this.R;
this.pos = createVector(x, y);
}
setTarget() {}
update() {
this.pos.x = this.cx + cos(this.t) * this.R;
this.pos.y = this.cy + sin(this.t) * this.R;
this.t += this.speed;
}
draw() {
push();
textSize(50);
translate(this.pos.x, this.pos.y);
text("🐒", 0, 0);
pop();
}
}
function draw() {
background(220);
game.update();
game.draw();
}