Bat.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // FIXME
  2. keepAlive = typeof(keepAlive) == "undefined" ? [] : keepAlive;
  3. keepAlive.push(self);
  4. var glmatrix = require("gl-matrix");
  5. var vec2 = glmatrix.vec2;
  6. var node = self.node;
  7. var animationSet = cache.getResource("AnimationSet2D", "Sprites/Bat/Bat.scml");
  8. var sprite = node.createComponent("AnimatedSprite2D");
  9. sprite.setAnimation(animationSet, "Fly");
  10. sprite.setLayer(100);
  11. node.scale2D = [.5, .5];
  12. var cwaypoint = -1;
  13. function update(timestep) {
  14. var waypoints = TheLevel.batWaypoints;
  15. var pos = node.position2D;
  16. if (cwaypoint == -1 || vec2.distance(pos, waypoints[cwaypoint]) < .5) {
  17. cwaypoint = Math.round(Math.random() * (waypoints.length - 1));
  18. return;
  19. }
  20. var dir = vec2.create();
  21. var goal = waypoints[cwaypoint];
  22. vec2.subtract(dir, goal, pos);
  23. vec2.normalize(dir, dir);
  24. vec2.scale(dir, dir, timestep * 2);
  25. if (dir[0] < 0)
  26. sprite.flipX = true;
  27. else
  28. sprite.flipX = false;
  29. vec2.add(pos, pos, dir);
  30. node.position2D = pos;
  31. }