jQuery addClass example

Test RPG

by black strings

CSS

table {
  border: thin solid red;
}
table td {
  text-align: center;
}
table .header {
  background-color:#00ff00;
}
body {
  background-color:#000;
}
button {
   background-color:#333;
   border:thin solid black;
}

JavaScript

class GO {
	constructor(name){
  	this.name = name;
  }
}

var EffectTypes = { POISON: 0 };

class Effect extends GO {
	constructor(name, effectType){
  	super(name);
    this.effectType = effectType;
  }
}

class Poison extends Effect {
	constructor(strength, maxTick){
  	super('poison', EffectTypes.POISON);
    this.remainingTick = maxTick;
    this.defaultStrength = strength;
  }
  apply(stats){
  	// poi only effects hp stats
  	var hp = stats.hp;	// grab the hp from stats
  	if(hp instanceof HP){
    
    	var finalPoisonValue = this.defaultStrength;
      if(stats.resistance && stats.resistance.poison){
      	finalPoisonValue -= stats.resistance.poison.value * finalPoisonValue;
      	finalPoisonValue = finalPoisonValue >= 0 ? finalPoisonValue : 0;	// prevent negative value
      }
    	hp.add(finalPoisonValue);
      this.remainingTick--;
    }
    
    if(this.remainingTick <= 0){
    	return true;	// indicates the the last remaining tick has been used
    }
    return false;
  }
}

class HP extends GO {
	constructor(max, name){
  	super(name + '_hp');
    this.max = max;
    this.value = this.max;
  }
  add(value){
  	this.value += value;
  }
}

class Player extends GO {
	constructor(name){
  	super(name + '_player');
    this.effects = [];
    this.stats = {
    	hp: new HP(100)
    };
  }
  addEffect(newEffect){
  	if(this.effects){
    
    	this._removeExistingOldEffect(newEffect);
      
      // add the new effect - may need more logic in the future
    	this.effects.push(newEffect);
    	
    } else {
    	console.error('cannot add, effects is null');
    }
  }
  removeEffect(effect){
  	var index = this.effects.indexOf(effect);
    if(index >= 0){
    	this.effects.splice(index,1);
    }
  }
  _removeExistingOldEffect(newEffect){
  	// currently you can only have one type of effect at a time applied
    if(this.effects.length){
      // remove any old effect of same type
      var oldEffect = this.effects.filter((effect) => effect.effectType ===...