xxxxxxxxxx
108
const spells = {
fire: 5,
ice: 3,
blast: 7,
decimate: 10,
}
const names = [
"brad",
"conner",
"brutus",
"phil",
"bob",
"barnaby",
"thomas",
"ewan",
];
let enemies = [];
let cmd = "";
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
if(frameCount % 120 === 0) {
spawnEnemy();
}
enemies.forEach(e => {
e.update();
e.draw();
});
text(cmd, 5, height - 5);
}
function keyReleased() {
if(validKey(keyCode)) {
cmd += key;
}
if(keyCode === BACKSPACE) {
cmd = cmd.slice(0, -1);
}
checkCmd();
}
function validKey(code) {
return code === 32 || (code >= 65 && code <= 90);
}
function checkCmd() {
spellParts = cmd.split(" ");
spell = spellParts[0];
target = spellParts[1];
dmg = 0;
if(spells[spell]) {
dmg = spells[spell];
}
spellUsed = false;
enemies = enemies.filter(e => {
if(e.name === target) {
spellUsed = true;
return e.damage(dmg);
}
return true;
});
if(spellUsed) {
cmd = "";
}
}
function spawnEnemy() {
const e = new Enemy(random(names), random(width));
enemies.push(e);
}
class Enemy {
constructor(name, x) {
this.name = name;
this.rad = 20;
this.x = x;
this.y = -this.rad/2;
this.hp = name.length;
}
update() {
this.y += 0.5;
}
damage(amt) {
this.hp -= amt;
return this.hp > 0;
}
draw() {
circle(this.x, this.y, this.rad);
text(this.name, this.x, this.y - this.rad);
text(this.hp, this.x, this.y + this.rad);
}
}