movement.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. function vecLen(x,y) {
  2. return Math.hypot(x,y);
  3. }
  4. function abs(a) {
  5. return Math.abs(a);
  6. }
  7. function norm(x,y) {
  8. var l = vecLen(x,y);
  9. if(l == 0) return {x:0, y:0};
  10. return {
  11. x: x / l,
  12. y: y / l,
  13. };
  14. }
  15. Systems.urge = function(te) {
  16. var moves = game.components.movable;
  17. var positions = game.components.position;
  18. var velocities = game.components.velocity;
  19. // var accelerations = game.components.acceleration;
  20. var urges = game.components.urge;
  21. for(var eid in moves) {
  22. if(moves.hasOwnProperty(eid) &&
  23. positions.hasOwnProperty(eid) &&
  24. velocities.hasOwnProperty(eid) &&
  25. // accelerations.hasOwnProperty(eid) &&
  26. urges.hasOwnProperty(eid)) {
  27. var move = moves[eid];
  28. var pos = positions[eid];
  29. var vel = velocities[eid];
  30. var urge = urges[eid];
  31. var nextpos = game.getComp(eid, 'nextpos');
  32. if(urge.acc) {
  33. nextpos.x = pos.x + urge.x * te * urge.acc;
  34. nextpos.y = pos.y + urge.y * te * urge.acc;
  35. }
  36. }
  37. }
  38. }
  39. // Systems.userControl = function() {
  40. //
  41. // var ii = game.input;
  42. //
  43. //
  44. // var movers = game.components.controllable;
  45. //
  46. // for(var eid in movers) { if(movers.hasOwnProperty(eid)) { var mover = movers[eid];
  47. //
  48. // var urge = game.getComp(eid, 'urge');
  49. // urge.acc = 0;
  50. // urge.x = 0;
  51. // urge.y = 0;
  52. //
  53. // if(ii.down[37]) {
  54. // //console.log(ii);
  55. // urge.x = -1;
  56. // urge.acc = 1;
  57. // }
  58. // if(ii.down[39]) {
  59. // //console.log(ii);
  60. // urge.x = 1;
  61. // urge.acc = 1;
  62. // }
  63. // if(ii.down[38]) {
  64. // //console.log(ii);
  65. // urge.y = 1;
  66. // urge.acc = 1;
  67. // }
  68. // if(ii.down[40]) {
  69. // //console.log(ii);
  70. // urge.y = -1;
  71. // urge.acc = 1;
  72. // }
  73. //
  74. // // debug
  75. // // urge.x = -1;
  76. // // urge.acc = 2;
  77. //
  78. // }};
  79. //
  80. //
  81. // }
  82. Systems.userControl = function() {
  83. var ii = game.input;
  84. var comps = ['controllable'];
  85. runSystem(game.c, comps, function(ent, eid) {
  86. if(ii.clicked !== null) {
  87. game.addComponent(eid, 'goTo', ptc(ii.clicked));
  88. }
  89. });
  90. }
  91. Systems.mapFollows = function() {
  92. var follows = game.components.mapFollows;
  93. var edges = game.map.getEdges(game.mapCenter);
  94. for(var eid in follows) { if(follows.hasOwnProperty(eid)) { var follow = follows[eid];
  95. var pos = game.getComp(eid, 'position');
  96. if(pos.y > edges.t - follow.margin) {
  97. // move the map up
  98. game.mapCenter.y += pos.y - (edges.t - follow.margin);
  99. }
  100. if(pos.y < edges.b + follow.margin) {
  101. // move the map down
  102. game.mapCenter.y -= edges.b + follow.margin - pos.y;
  103. }
  104. if(pos.x > edges.r - follow.margin) {
  105. // move the map up
  106. game.mapCenter.x += pos.x - (edges.r - follow.margin);
  107. }
  108. if(pos.x < edges.l + follow.margin) {
  109. // move the map down
  110. game.mapCenter.x -= edges.l + follow.margin - pos.x;
  111. }
  112. break; // only follow the first guy. really bad temp hack, but whatever
  113. }}
  114. }
  115. Systems.goTo = function() {
  116. var comps = ['goTo', 'velocity', 'position', 'maxSpeed', 'acceleration', 'state'];
  117. var epsilon = .1;
  118. runSystem(game.components, comps, function(ent) {
  119. if(ent.goTo == null) return;
  120. if(vDist(ent.goTo, ent.position) < epsilon) {
  121. ent.acceleration.x = 0;
  122. ent.acceleration.y = 0;
  123. ent.velocity.x = 0;
  124. ent.velocity.y = 0;
  125. // neet to set some state for the ai here...
  126. ent.state.loco = 'standing';
  127. }
  128. else {
  129. var a = ent.maxSpeed * 8;
  130. var dir = norm(ent.goTo.x - ent.position.x, ent.goTo.y - ent.position.y);
  131. ent.acceleration.x = a * dir.x;
  132. ent.acceleration.y = a * dir.y;
  133. ent.state.loco = 'going';
  134. }
  135. });
  136. };
  137. Systems.acceleration = function(te) {
  138. var comps = ['acceleration', 'velocity', 'maxSpeed'];
  139. runSystem(game.components, comps, function(ent) {
  140. var x = ent.velocity.x + ent.acceleration.x * te;
  141. var y = ent.velocity.y + ent.acceleration.y * te;
  142. var speed = vecLen(x,y);
  143. // need better integrator
  144. if(abs(speed) < ent.maxSpeed) {
  145. ent.velocity.x = x;
  146. ent.velocity.y = y;
  147. }
  148. else {
  149. var dir = norm(x,y)
  150. ent.velocity.x = dir.x * ent.maxSpeed;
  151. ent.velocity.y = dir.y * ent.maxSpeed;
  152. }
  153. });
  154. };
  155. Systems.velocity = function(te) {
  156. var comps = ['velocity', 'position', 'nextpos'];
  157. runSystem(game.components, comps, function(ent) {
  158. // need better integrator
  159. ent.nextpos.x = ent.position.x + te * ent.velocity.x;
  160. ent.nextpos.y = ent.position.y + te * ent.velocity.y;
  161. });
  162. };
  163. Systems.lookAt = function() {
  164. var comps = ['lookAt', 'sprite'];
  165. runSystem(game.components, comps, function(ent, eid) {
  166. var dir = game.getComp(eid, ent.lookAt);
  167. if(dir.x == 0 && dir.y == 0) return;
  168. dir = ptNorm(dir);
  169. // need better integrator
  170. ent.sprite.angle = atan2(
  171. dir.x,
  172. dir.y
  173. );
  174. });
  175. };
  176. // you can add your own trip timers in there
  177. Systems.odometer = function(te) {
  178. var comps = ['nextpos', 'position', 'odometer'];
  179. runSystem(game.components, comps, function(ent) {
  180. // need better integrator
  181. var len = vDist(ent.nextpos, ent.position);
  182. for(var i in ent.odometer) {
  183. ent.odometer[i] += len;
  184. };
  185. });
  186. };
  187. Systems.finalizeMove = function(te) {
  188. //return;
  189. var movers = game.components.velocity;
  190. for(var eid in movers) { if(movers.hasOwnProperty(eid)) { var vel = movers[eid];
  191. var pos = game.getComp(eid, 'position');
  192. var nextpos = game.getComp(eid, 'nextpos');
  193. // references...
  194. pos.x = nextpos.x;
  195. pos.y = nextpos.y;
  196. }};
  197. }