glendanpc.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var Mobile = require('../mobile');
  2. function GlendaNpc(game) {
  3. var hunger = 0;
  4. var tired = 0;
  5. this.inventory = [];
  6. this.maxInventory = 10;
  7. this.id = 0;
  8. this.mobile = null;
  9. this.init = function(mobileId, npcData) {
  10. this.mobile = new Mobile();
  11. this.mobile.id = mobileId;
  12. this.id = mobileId;
  13. this.mobile.name = "Glenda";
  14. this.mobile.sprite = "woman3";
  15. for(var key in this.mobile) {
  16. if(npcData.hasOwnProperty(key)) {
  17. this.mobile[key] = npcData[key];
  18. }
  19. }
  20. this.mobile.position = game.getRandomPosition();
  21. this.mobile.targetPosition = game.getRandomPosition();
  22. this.mobile.type = "npc";
  23. hunger = 0;
  24. tired = parseInt(50 * Math.random());
  25. this.inventory = [];
  26. return this.mobile;
  27. };
  28. this.takeOver = function(player) {
  29. this.mobile = player.mobile;
  30. this.id = player.mobile.id;
  31. this.inventory = player.inventory;
  32. this.maxInventory = player.maxInventory;
  33. this.mobile.type = "npc";
  34. }
  35. this.update = function(delta, game) {
  36. //hunger++;
  37. /*if(hunger > 50) {
  38. this.lookForFood();
  39. return;
  40. }*/
  41. switch(this.state) {
  42. case "sleeping":
  43. tired--;
  44. if(tired <= 0) {
  45. //console.log("well rested");
  46. this.state = "idle";
  47. }
  48. return;
  49. break;
  50. case "wandering":
  51. if(this.closeEnough(game)) {
  52. this.state = "idle";
  53. }
  54. break;
  55. case "idle":
  56. default:
  57. break;
  58. }
  59. tired++;
  60. if(tired > 50) {
  61. //console.log("getting sleepy");
  62. this.state = "sleeping";
  63. return;
  64. }
  65. if(this.state == "idle") {
  66. this.idle(game);
  67. }
  68. //if close to item, pick it up
  69. };
  70. this.closeEnough = function(game) {
  71. if( this.mobile.targetPosition.x == this.mobile.position.x && this.mobile.targetPosition.y == this.mobile.position.y) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. this.idle = function(game) {
  77. this.randomChat(game);
  78. this.wander();
  79. this.state = "wandering";
  80. };
  81. this.randomChat = function(game) {
  82. var roll = parseInt(20 * Math.random() + 1);
  83. if(roll > 18) {
  84. var message = ["Heavens!", "My, that looks a treat.", "Oh hello, dearie."][parseInt(3 * Math.random())];
  85. game.sendChat(this.mobile, message);
  86. }
  87. }
  88. this.lookForFood = function() {
  89. //check inventory for food
  90. //if holding food, eat one
  91. //get list of foods
  92. //find closest food
  93. //set status "moving towards food"
  94. //set targetposition at food
  95. };
  96. this.wander = function() {
  97. this.mobile.targetPosition.x = parseInt(500 * Math.random() - 250);
  98. this.mobile.targetPosition.y = parseInt(500 * Math.random() - 250);
  99. };
  100. };
  101. module.exports = GlendaNpc;