JSFiddle - React, Tailwind, and code Playground
OOP Test
by black strings
HTML
<div>
<button id='nH'>nH</button>
<button id='pickHero'>H</button>
</div>
<div>
<button id='nextItem'>nI</button>
<button id='pItem'>pI</button>
<button id='useItem'>UI</button>
</div>
CSS
body {
background-color: #000;
}
JavaScript
class Util {
static random(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
}
class Character{
constructor(id, name){
this.id = id;
this.name = name;
this.items = [];
this.activeItem;
this.activeWeapon;
}
useItem(){
if(this.activeItem && this.activeItem instanceof Weapon) {
return this.activeItem.getRandomDmg();
}
}
addItem(i) {
if(i) {
this.items.push(i);
}
if(!this.activeItem) {
this.activeItem = i;
}
}
equipWeapon(id){
if(id) {
for(const w in this.items) {
if(w.id === id) {
this.activeWeapon = w;
break;
}
}
} else {
for(const w in this.items) {
if(w instanceof Weapon) {
this.activeWeapon = w;
break;
}
}
}
if(!this.activeWeapon) {
console.log('no weap found');
}
}
}
class DMG{
constructor(min, max) {
this.min = min;
this.max = max;
}
random() {
return Util.random(this.min, this.max);
}
}
class Weapon{
constructor(id, name, min, max, speed) {
this.id = id;
this.name = name;
this.speed = speed;
this.dmg = new DMG(min, max);
}
getRandomDmg(){
debugger;
return this.dmg.random();
}
}
class Factory{
static createCharacter(id, name) {
return new Character(id, name);
}
static createWeapon(id, name, min, max, speed) {
return new Weapon(id, name, min, max, speed);
}
}
class World {
constructor(){
this.currentHeroSelectId;
this.currentHero;
this.currentItemSelectId;
this.itemLibrary = new Map();
this.heroLibrary = new Map();
this.populate();
}
populate(){
this.populateItems();
this.populateHeroes();
}
populateItems(){
const items = [
{id: 100, name: 'Small Ham', min: 1, max: 3, speed: 100},
{id: 101, name: 'Small Stick', min: 1, max: 4, speed: 1000},
{id: 102, name: 'Bronze Bar', min: 3, max: 6, speed: 1000},
];
...