nonplayercharacter.js 2.4 KB

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