begin-pickup-cargo.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var floatingItems = [];
  2. sets.push({
  3. id: "begin-pickup-cargo",
  4. setup: () => {
  5. if(!setsLoaded['begin-pickup-cargo']) {
  6. buildBeginPickup();
  7. setsLoaded['begin-pickup-cargo'] = true;
  8. }
  9. },
  10. update: (delta) => {
  11. updateBeginPickup(delta);
  12. },
  13. draw: (context, delta) => {
  14. drawBeginPickup(context, delta);
  15. },
  16. });
  17. function buildBeginPickup() {
  18. floatingItems = [];
  19. floatingItems.push({
  20. sprite: `pill_red.png`,
  21. x: -50,
  22. y: 50,
  23. angle: Math.floor(360 * Math.random()),
  24. rotationSpeed: (4 * Math.random()) - 2,
  25. velocity: {x: 0, y: 0},
  26. });
  27. floatingItems.push({
  28. sprite: `pill_green.png`,
  29. x: 130,
  30. y: 75,
  31. angle: Math.floor(360 * Math.random()),
  32. rotationSpeed: (4 * Math.random()) - 2,
  33. velocity: {x: 0, y: 0},
  34. });
  35. floatingItems.push({
  36. sprite: `pill_yellow.png`,
  37. x: 0,
  38. y: -100,
  39. angle: Math.floor(360 * Math.random()),
  40. rotationSpeed: (4 * Math.random()) - 2,
  41. velocity: {x: 0, y: 0},
  42. });
  43. }
  44. function updateBeginPickup(delta) {
  45. for(let i = 0; i < floatingItems.length; i++) {
  46. let pickup = floatingItems[i];
  47. pickup.angle += pickup.rotationSpeed;
  48. pickup.angle %= 360;
  49. pickup.x += pickup.velocity.x;
  50. pickup.y += pickup.velocity.y;
  51. }
  52. }
  53. function drawBeginPickup(context, delta) {
  54. for (let i = 0; i < floatingItems.length; i++) {
  55. let pickup = floatingItems[i];
  56. context.save();
  57. context.translate(pickup.x, pickup.y);
  58. context.rotate(pickup.angle * Math.PI / 180);
  59. atlas.drawCentered(context, pickup.sprite, { x: 0, y: 0 });
  60. context.restore();
  61. }
  62. }