DK JS

by Sam Fereday

HTML

<div id="output"></div>

JavaScript

var op = document.getElementById("output");
var lair;

// Entity
var Entity = function(){};
Entity.prototype = {
  x: 0,
  y: 0,
  currentRoom: null,
  arrived: false,
  myDesire: null,
  hasLair: false,
  isHungry: false,
  isEating: false,
  needsPurpose: false,
  // Stats
  energy: 0,
  happiness: 100,
  knowledge: 0,
  // Update cycle for entity
  update: function(){
    
    this.howDoIFeel();
    
  },
  
  // A basic stack (will need more looking into)
  howDoIFeel: function(){
    
    // The pyramid of needs (in this example): sleep, eat, knowledge
    this.myDesire = this.getLargestStat();
    
    if(this.arrived) {
			this.setStat("happiness", 1);
      if(this.currentRoom.id === "lair") {
        this.setStat("energy", 1);
        console.log("Sleeping.");
      }
      return;
    }
    
    if(!this.hasLair) {
      // Find a lair in territory
      this.setStat("happiness", -1);
      this.goToDestination(lair);
      console.log("Going to make a bed.");
    	return;
    }
    
    if(this.isHungry && this.hasLair) {
      // Find a place to eat (if you've got a bed)
    	this.setStat("happiness", -1);
    	return;
    }
    
    if(this.needsPurpose) {
      // Find a place to learn something
      this.setStat("happiness", -1);
      return;
    }
    
  },
  
  getLargestStat: function() {
    
    var theStat = {};
    var prevStat;
    var newStat;
    
    for(var key in this.stats) {
      
      newStat = this.stats[key];
      
      if(prevStat)
      	newStat = this.stats[key] > prevStat ? this.stats[key] : prevStat;
      
      theStat = {
        val: newStat,
        name: key
      };
      
      prevStat = this.stats[key];
      
    };
    
    return theStat;
    
  },
  
  goToDestination: function(roomType) {
    
    console.log(this.lineDistance(roomType, this));
    
    if(this.lineDistance(roomType, this) > 1) {
      
      this.arrived = false;
      this.moveTowards(roomType);
      
    } else {
      
     ...