ai.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Systems.ai = Systems.ai || {};
  2. Systems.ai.followPaths = function() {
  3. var comps = ['ai', 'position', 'state'];
  4. runSystem(game.c, comps, function(ent, eid) {
  5. if(ent.state.loco == 'standing') {
  6. // get path node near spot.
  7. var at = game.aiPaths.getNodeNear(ent.position, .8);
  8. if(null === at) {
  9. console.log('ai is lost at [' + ent.position.x + ',' + ent.position.y + ']');
  10. }
  11. // choose random exit
  12. var to = game.aiPaths.followRandomEdge(at);
  13. if(!to) {
  14. console.log('ai is dead ended at [' + ent.position.x + ',' + ent.position.y + ']');
  15. }
  16. game.addComponent(eid, 'goTo', to);
  17. ent.state.loco = 'pending';
  18. }
  19. });
  20. };
  21. Systems.ai.thirst = function() {
  22. var comps = ['thirst', 'odometer'];
  23. runSystem(game.c, comps, function(ent, eid) {
  24. var t = ent.odometer.lastDrink || 0;
  25. game.setComp(eid, 'thirst', t > 50); // weird shit here, dunno what I was originally thinking
  26. game.addComponent(eid, 'thirsty', t > 50);
  27. console.log('thirst: ' + ent.thirst);
  28. // if(t > 50) {
  29. // console.log('person is thirsty');
  30. // }
  31. ent.odometer.lastDrink = t;
  32. });
  33. }
  34. Systems.ai.thirsty_LocateStand = function() {
  35. var comps = ['thirsty', 'position'];
  36. runSystem(game.c, comps, function(ent, eid) {
  37. var l = game.grid.rangeRadiusFast(ent.position, 2);
  38. console.log('looking for stand: ' + l.length);
  39. for(var i = 0; i < l.length; i++) {
  40. var ee = l[i];
  41. if(game.getComp(ee, 'sells')) {
  42. // this is a stand
  43. game.addComponent(eid, 'queuedAt', ee);
  44. console.log('queuing up at');
  45. var vel = game.getComp(eid, 'velocity');
  46. vel.x = 0;
  47. vel.y = 0;
  48. var state = game.getComp(eid, 'state');
  49. state.loco = 'standing';
  50. game.removeComp(eid, 'goTo');
  51. break;
  52. }
  53. }
  54. });
  55. }
  56. Systems.ai.buyDrink = function() {
  57. var comps = ['queuedAt'];
  58. runSystem(game.c, comps, function(ent, eid) {
  59. var stand = ent.queuedAt;
  60. var s_inv = game.getComp(stand, 'inventory');
  61. console.log('in queue');
  62. if(stand.lemonade > 0) {
  63. console.log('buying');
  64. // increment player cash
  65. var price = game.getComp(stand, 'prices');
  66. var pc = game.firstEntityWith('playerOwned');
  67. var cash = game.getComp(pc, 'cash');
  68. game.setComp(pc, 'cash', cash + price);
  69. s_inv.lemonade--;
  70. game.removeComp(eid, 'thirsty');
  71. game.removeComp(eid, 'queuedAt');
  72. var c_state = game.getComp(eid, 'state');
  73. c_state.loco = 'standing';
  74. game.setComp(eid, 'thirst', 0);
  75. var odo = game.getComp(eid, 'odometer');
  76. odo.lastDrink = 0;
  77. }
  78. });
  79. }