JSFiddle - React, Tailwind, and code Playground

by Brian Wendt

JavaScript

class Creature {

  constructor() {
    this.health = 0;
    this.dots = []; //empty array
    this.effects = []; //empty array
  }

  addDot(dot) {
    this.dots.push(dot)
  }

  /* On Tick Event */
  applyDots() {
    this.dots.filter(dot => {
      return dot.apply(Creature)
    })
  }

  applyDamage(value, damage_type) {
  	damage_type(Creature, value);
  }

  modifyHealth(value) {
    this.health += value;
  }

  reduceHealth(value) {
    if (value > 0) {
      this.modifyHealth(value * -1);
    }
  }

}

const DamageTypes = {
  blunt: function(Creature, value) {
    Creature.reduceHealth(value - Creature.resitances.blunt);
  },

  fire: function(Creature, value) {
    Creature.reduceHealth(value - Creature.resitances.fire);
    if(value > Creature.resitances.fire){
    	Creature.addDot(DamageTypes.dots.fire());
    }
  },

  dots: {
    // Dots are attached to the Creature object so they are independent on the event that created the dot.
    fire: function() {
      return new DotFire();
    }
  }
}

class Dot {
  constructor() {
    this.ticks = 0
    this.ticksStep = 0
  }

  apply(Creature) {
    if (this.ticks > 0) {
    	if(this.ticks % this.ticksStep == 0){
      this.applyDamage(Creature);
      this.applyEffect(Creature);
      }
      this.ticks--;
      return true;
    } else {
      return false;
    }
  }

  applyEffect() {

  }

  applyDamage() {

  }
}

class DotFire {
  constructor() {
    this.ticks = 60
    this.ticksStep = 10;
  }

  applyDamage(Creature) {
    Creature.reduceHealth(2);
  }

  applyEffect(Creature) {
    Creature.addEffect(Effects.creature.onFire);
  }


}