var Mobile = require('./mobile'); function NonPlayerCharacter(game) { var hunger = 0; var tired = 0; this.inventory = []; this.maxInventory = 10; this.id = 0; this.mobile = null; this.init = function(mobileId, npcData) { this.mobile = new Mobile(); this.mobile.id = mobileId; this.id = mobileId; for(var key in this.mobile) { if(npcData.hasOwnProperty(key)) { this.mobile[key] = npcData[key]; } } this.mobile.position = game.getRandomPosition(); this.mobile.targetPosition = game.getRandomPosition(); this.mobile.type = "npc"; hunger = 0; tired = 0; this.inventory = []; return this.mobile; }; this.takeOver = function(player) { this.mobile = player.mobile; this.id = player.mobile.id; this.inventory = player.inventory; this.maxInventory = player.maxInventory; this.mobile.type = "npc"; } this.update = function(delta, game) { //hunger++; /*if(hunger > 50) { this.lookForFood(); return; }*/ switch(this.state) { case "sleeping": tired--; if(tired <= 0) { //console.log("well rested"); this.state = "idle"; } return; break; case "wandering": if(this.closeEnough(game)) { this.state = "idle"; } break; case "idle": default: break; } tired++; if(tired > 50) { //console.log("getting sleepy"); this.state = "sleeping"; return; } if(this.state == "idle") { this.idle(game); } //if close to item, pick it up }; this.closeEnough = function(game) { if( this.mobile.targetPosition.x == this.mobile.position.x && this.mobile.targetPosition.y == this.mobile.position.y) { return true; } return false; } this.idle = function(game) { this.randomChat(game); this.wander(); this.state = "wandering"; }; this.randomChat = function(game) { var roll = parseInt(20 * Math.random() + 1); if(roll > 18) { var message = ["Hello!", "Hi!", "Is anyone there?"][parseInt(3 * Math.random())]; game.sendChat(this.mobile, message); } } this.lookForFood = function() { //check inventory for food //if holding food, eat one //get list of foods //find closest food //set status "moving towards food" //set targetposition at food }; this.wander = function() { this.mobile.targetPosition.x = parseInt(500 * Math.random() - 250); this.mobile.targetPosition.y = parseInt(500 * Math.random() - 250); }; }; module.exports = NonPlayerCharacter;