Bat.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var glmatrix = require("gl-matrix");
  2. var vec2 = glmatrix.vec2;
  3. var game = Atomic.game;
  4. var node = self.node;
  5. var animationSet = game.cache.getResource("AnimationSet2D", "Sprites/Bat/Bat.scml");
  6. var sprite = node.createComponent("AnimatedSprite2D");
  7. sprite.setAnimation(animationSet, "Fly");
  8. sprite.setLayer(100);
  9. node.scale2D = [.5, .5];
  10. var cwaypoint = -1;
  11. var light = null;
  12. function start() {
  13. if (!Platformer.daytime) {
  14. light = node.createComponent("PointLight2D");
  15. light.color = [1, 1, 1, 1];
  16. light.radius = 4;
  17. Platformer.lightGroup.addLight(light);
  18. node.createJSComponent("LightFlicker");
  19. }
  20. }
  21. var time = Math.random() * 10000;
  22. function update(timestep) {
  23. time += timestep * 4;
  24. if (light)
  25. light.color = [.5 + Math.sin(time), .5 + Math.cos(time), .5 + Math.cos(-time), 1];
  26. var waypoints = Platformer.batWaypoints;
  27. var pos = node.position2D;
  28. if (cwaypoint == -1 || vec2.distance(pos, waypoints[cwaypoint]) < .5) {
  29. cwaypoint = Math.round(Math.random() * (waypoints.length - 1));
  30. return;
  31. }
  32. var dir = vec2.create();
  33. var goal = waypoints[cwaypoint];
  34. vec2.subtract(dir, goal, pos);
  35. vec2.normalize(dir, dir);
  36. vec2.scale(dir, dir, timestep * 2);
  37. if (dir[0] < 0)
  38. sprite.flipX = true;
  39. else
  40. sprite.flipX = false;
  41. vec2.add(pos, pos, dir);
  42. node.position2D = pos;
  43. }