I recently started working on a text-based RPG in HTML and JS. I made a list of the different weapons (and a list of enemies, although it only contains 1 at the moment) for a couple reasons (they are all in the same place, easier to retrieve, etc) and made a function to get a weapon from the list by name. But when I run the function I made (getWeapon) to get a weapon from the list by name (2 times, one in the enemies list and one in the button.AddEventListener) it can't find the weapon. A lot of the things in the code are incomplete or don't work correctly, as I was just trying to get systems in place first. I'm planning on fixing them later. I tried switching the weapon to see if it was an issue with the particular weapon, but that didn't work either. I'm guessing there are some other issues with the code, but I don't know of any at the moment. Does anyone know what's happening?
const pg = document.getElementById('par');
const input = document.getElementById('input');
const button = document.getElementById('button');
let curr = 0; // Tells game what is happening right now
let pressed = false; // If button is being pressed
const def = { // Starting values for player stats
hp: 100, // Max HP
weapons: [],
luck: 20,
maxLuck: 100,
money: 0,
armor: 0,
}
const p = { // Player stats
inv: { // Inventory
money: 0,
items: [], // Items usable by player
weapons: [], // Unlocked weapons
weapon: null, // Equipped weapon
},
name: '',
hp: 0, // Current hp
luck: 0, // Increases chances of getting better loot and getting critical hits
maxHp: def.hp, // Max hp
armor: 0, // Dampens damage taken
setup() {
this.maxHp = def.hp;
this.hp = def.hp;
this.inv.weapons = def.weapons;
this.inv.weapon = null;
this.inv.money = def.money;
this.luck = def.luck;
this.armor = def.armor;
},
attack(enemy) {
this.weapon.attack(enemy);
},
gain(item) { // Sorts out what to give player when receiving something
if (typeof item == 'number') this.inv.money += item.num;
else if (item instanceof Weapon) this.inv.weapons.push(item);
//else if (item instanceof Item) Haven't added yet
else console.log(`
Item: $ {
item
}
was passed through p.gain() but did not match anything.
`); // [IMPORTANT] I see this in console after the button is pressed to submit the name.
}
};
p.setup();
function weightChoose(loot, luck = 0) { // Chooses an item from a loot pool with weight
const totalWeight = loot.reduce((total, entry) => total + entry[1]);
let rand = Math.random() * totalWeight;
for (let i of loot) {
rand -= luck ? luck - i[1] : i[1]; // Subtract item weight from total (if luck, subtract weight from luck to get new weight)
if (rand <= 0) return i;
}
}
class Weapon {
constructor(damage, acc, name, attacks = 1, special = []) {
this.damage = damage; // Damage
this.accuracy = acc; // Accuracy (more means less chance of weapon missing)
this.attacks = attacks; // Amount of time weapon attacks (e.g. more for a shotgun)
this.special = special; // Some weapons have more stuff that they do
}
attack(attacker, target, returnHit = false) {
for (let i = 0; i < this.attacks; i++) {
if (Math.random() < this.accuracy / 100) {
if (Math.random() > attacker.luck / 150) target.hp -= this.damage * 2; // 100 luck = approx. 66% crit chance
else target.hp -= this.damage;
if (returnHit) return true;
} else if (returnHit) return false;
}
}
}
class Enemy {
constructor(maxhp, luck, wep, dmgMult, armor, loot = []) {
this.maxHp = maxhp;
this.hp = this.maxHp;
this.luck = luck; // Affects crit chance (planning to add other stuff)
this.weapon = wep;
this.dmgMult = dmgMult; // Damage multiplier (from enemy)
this.armor = armor; // Reduces amount of damage taken by enemy
this.loot = loot; // Pool of possible loot to get (type as: [[loot, chance], [loot, chance]])
}
attack(target) {
this.weapon.attack(target);
}
}
const weapons = [ // List of weapons
// Melee (basic stuff, not fancy)
new Weapon(2, 100, 'Stick'), // Starter weapon; basically useless
new Weapon(30, 80, 'Sword'),
new Weapon(25, 70, 'Dagger', 3),
new Weapon(50, 55, 'Longsword'),
// Guns (AMERICAAAAAAAAAA)
new Weapon(10, 75, 'Glock'),
new Weapon(8, 15, 'Shotgun', 6),
new Weapon(3, 30, 'Minigun', 20),
new Weapon(10, 20, 'Revolver', 6),
// Special stuff (with abilites)
new Weapon(5, 85, 'Torch', 1, ['fire', 'watervuln']),
new Weapon(10, 10, 'Flamethrower', 10, ['fire', 'watervuln']),
new Weapon(20, 60, 'Shiny Rock', 1, ['confusion']), // Enemy is distracted by rock (sounded better in my head honestly)
new Weapon(3, 80, 'Water Gun', 5, ['water']),
];
function getWeapon(name) { // [IMPORTANT] weapons.find is returning undefined.
const w = weapons.find((w) => w.name == name);
if (w) return w;
else console.log(`
Tried to find weapon: $ {
name
}
but failed.
`);
}
const enemies = [
new Enemy(25, 20, getWeapon('Stick'), 1.1, 1, [[5, 10]]),
];
function write(txt) { // For simplifying display of text because I ain't writing allat
pg.innerHTML = txt;
}
write('Enter your name ^^^');
button.addEventListener('mousedown', () => {
pressed = true;
if (curr === 0) { // Just started game; get name
if (input.value != '') {
p.name = input.value;
button.textContent = 'ok';
curr = 1;
write('You just left Grandma\'s house with a cookie. You found a stick in the woods and thought it looked cool, so you are also carrying it around right now.');
p.gain(getWeapon('Stick'));
}
} else if (curr === 1) { // Story
if (pressed) curr = 2;
}
});
button.addEventListener('mouseup', () => pressed = false);
<h3>Input</h3>
<input type='text' id='input'></input>
<button type="button" id='button'>submit</button>
<p id='par'></p>