germancin
1/11/2018 - 11:31 PM

Game prototype inherit, constructor functions

Game prototype inherit, constructor functions

function GameObject(gameAttr) {
  this.createdAt = gameAttr.createdAt;
  this.dimensions = gameAttr.dimensions;
}

GameObject.prototype.destroy = function destroy() {
  return 'Game object was removed from the game.';
};

function NPC(npcAttr) {
  GameObject.call(this, npcAttr);
  this.hp = npcAttr.hp;
  this.name = npcAttr.name;
}

NPC.prototype = Object.create(GameObject.prototype);

NPC.prototype.takeDamage = function takeDamage() {
  return `${this.name} took damage.`;
};

function Humanoid(humAttr) {
  NPC.call(this, humAttr);
  this.faction = humAttr.faction;
  this.weapons = humAttr.weapons;
  this.language = humAttr.language;
}

Humanoid.prototype = Object.create(NPC.prototype);

Humanoid.prototype.greet = function greet() {
  return `${this.name} offers a greeting in ${this.language}.`;
};

module.exports = {
  GameObject,
  NPC,
  Humanoid,
};