Dmg Test

by black strings

JavaScript

const WepType = { SWD:0, HAM:1, BOW:2 };
class Dmg {
  constructor(wepType, amt) { 
  	this.wepType = wepType;
    this.amt = amt;
  }
}
class Wep {
  constructor(wepType, atk) {
  	this.wepType = wepType;
    this.atk = atk;
  }
  action() {
    return new Dmg(this.wepType, this.atk);
  }
}
class DmgMang {
  constructor() {
  	this.wepWeakTypes = [];
    this.wepStrongTypes = [];
  }
  addWeakType(weakType) {
    this.wepWeakTypes.push(weakType);
  }
  addStrongType(strongType) {
    this.wepStrongTypes.push(strongType);
  }
  take(dmg) {
    var finalDmg = 0;
    if (dmg) {
      finalDmg = dmg.amt;
      if (this.isWeakAgainst(dmg)) {
        finalDmg *= 2;
      } else if (this.isStrongAgainst(dmg)) {
        finalDmg /= 2;
      }
    }
    return -finalDmg;
  }
  isWeakAgainst(dmg) {
    let isWeak = false;
    this.wepWeakTypes.forEach((wepType) => {
      if (wepType === dmg.wepType) {
        isWeak = true;
        return;
      }
    });
    return isWeak;
  }
  isStrongAgainst(dmg) {
    let isStrong = false;
    this.wepStrongTypes.forEach((wepType) => {
      if (wepType === dmg.wepType) {
        isStrong = true;
        return;
      }
    });
    return isStrong;
  }
}
class CharEntity {
  constructor(name, maxHp) {
  	this.name = name;
    this.hp = maxHp;
    this.isAlive = true;
    this.weps = [];
    this.dmgMang = new DmgMang();
  }
  addWepWeakness(wepWeakType) {
    this.dmgMang.addWeakType(wepWeakType);
  }
  addWepStrength(wepStrongType) {
    this.dmgMang.addStrongType(wepStrongType);
  }
  addWep(wep) {
    if (this.weps.length > 0) {
      this.weps = [];
      this.weps.push(wep);
    } else {
      this.weps.push(wep);
    }
  }
  removeWep() {
    this.weps = [];
  }
  attack(c) {
    c.takeDmg(this.weps[0].action());
  }
  takeDmg(dmg) {
    this.hp += this.dmgMang.take(dmg);
    if (this.hp <= 0) {
      this.isAlive = false;
    }
  }
  toString() {
    console.log('' + this.name + ' ' + this.hp);
  }
}


var w1 = new...