123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- var Mobile = require('../mobile');
- function GrugNpc(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;
- this.mobile.name = "Grug";
- this.mobile.sprite = "orc";
- 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 = parseInt(50 * Math.random());
- 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 = ["Grug say hi!", "*grunt grunt*", "Me punching you!"][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 = GrugNpc;
|