AI.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* global SpaceGame */
  2. 'atomic component';
  3. exports.component = function(self) {
  4. var game = Atomic.game;
  5. var node = self.node;
  6. self.canMove = false;
  7. self.allowShoot = true;
  8. self.shootDelta = 0;
  9. self.start = function() {
  10. };
  11. self.update = function(timeStep) {
  12. if (SpaceGame.gameOver)
  13. return;
  14. var pos = node.worldPosition2D;
  15. var ppos = SpaceGame.playerNode.worldPosition2D;
  16. if (self.canMove) {
  17. if (Math.abs(pos[0] - ppos[0]) > .25) {
  18. if (pos[0] < ppos[0])
  19. pos[0] += timeStep * .95;
  20. else
  21. pos[0] -= timeStep * .95;
  22. node.position2D = pos;
  23. }
  24. }
  25. if (self.shootDelta > 0) {
  26. self.shootDelta -= timeStep;
  27. if (self.shootDelta < 0)
  28. self.shootDelta = 0;
  29. return;
  30. }
  31. if (Math.abs(pos[0] - ppos[0]) < .25) {
  32. self.shootDelta = 0.5;
  33. if (Math.random() > .1)
  34. return;
  35. var pos = node.worldPosition2D;
  36. pos[1] -= .25;
  37. SpaceGame.spawnBullet(pos, false);
  38. }
  39. };
  40. };