Bat.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. var light = node.createComponent("PointLight2D");
  14. light.color = [1, 1, 1, 1];
  15. light.radius = 4;
  16. lightGroup.addLight(light);
  17. node.createJSComponent("LightFlicker");
  18. var time = Math.random() * 10000;
  19. function update(timestep) {
  20. time += timestep * 4;
  21. light.color = [ .5 + Math.sin(time), .5 + Math.cos(time), .5 + Math.cos(-time), 1];
  22. var waypoints = TheLevel.batWaypoints;
  23. var pos = node.position2D;
  24. if (cwaypoint == -1 || vec2.distance(pos, waypoints[cwaypoint]) < .5) {
  25. cwaypoint = Math.round(Math.random() * (waypoints.length - 1));
  26. return;
  27. }
  28. var dir = vec2.create();
  29. var goal = waypoints[cwaypoint];
  30. vec2.subtract(dir, goal, pos);
  31. vec2.normalize(dir, dir);
  32. vec2.scale(dir, dir, timestep * 2);
  33. if (dir[0] < 0)
  34. sprite.flipX = true;
  35. else
  36. sprite.flipX = false;
  37. vec2.add(pos, pos, dir);
  38. node.position2D = pos;
  39. }