main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // This script is the main entry point of the game
  2. var halfWidth = Atomic.graphics.width * Atomic.PIXEL_SIZE * 0.5;
  3. var halfHeight = Atomic.graphics.height * Atomic.PIXEL_SIZE * 0.5;
  4. var maxX = halfWidth;
  5. var minX = -halfWidth;
  6. var maxY = halfHeight;
  7. var minY = -halfHeight;
  8. Atomic.player.loadScene("Scenes/Scene.scene");
  9. var scene = Atomic.player.currentScene;
  10. var sheet = Atomic.cache.getResource("SpriteSheet2D", "Sprites/bunnys_sheet.xml");
  11. var bunny1 = sheet.getSprite("bunny1");
  12. var bunny2 = sheet.getSprite("bunny2");
  13. var bunny3 = sheet.getSprite("bunny3");
  14. var bunny4 = sheet.getSprite("bunny4");
  15. var bunny5 = sheet.getSprite("bunny5");
  16. var bunnyTextures = [bunny1, bunny2, bunny3, bunny4, bunny5];
  17. var bunnyType = 2;
  18. var currentTexture = bunnyTextures[bunnyType];
  19. var bunnys = [];
  20. var count = 0;
  21. var amount = 3;
  22. var gravity = -0.5;
  23. // TODO: we hold a reference to the node in script, otherwise it is GC'd
  24. // and the object rewrapped every time bunny.node is accessed!
  25. var nodes = [];
  26. var isAdding = false;
  27. scene.subscribeToEvent(Atomic.MouseButtonDownEvent(function() {
  28. isAdding = true;
  29. }));
  30. scene.subscribeToEvent(Atomic.MouseButtonUpEvent(function() {
  31. isAdding = false;
  32. bunnyType++;
  33. bunnyType %= 5;
  34. currentTexture = bunnyTextures[bunnyType];
  35. }));
  36. exports.update = function() {
  37. var scale = [0, 0];
  38. if (isAdding) {
  39. if (count < 200000) {
  40. for (var i = 0; i < amount; i++) {
  41. var node = scene.createChild();
  42. nodes.push(node);
  43. var bunny = node.createComponent("StaticSprite2D");
  44. bunny.blendMode = Atomic.BlendMode.BLEND_ALPHA;
  45. bunny.sprite = currentTexture;
  46. bunny.position = [minX, maxY];
  47. bunny.speedX = Math.random() * 10;
  48. bunny.speedY = (Math.random() * 10) - 5;
  49. //bunny.anchor.y = 1;
  50. bunnys.push(bunny);
  51. scale[0] = scale[1] = (0.5 + Math.random() * 0.5);
  52. bunny.scale2D = scale;
  53. bunny.rotation2D = (Math.random() - 0.5);
  54. count++;
  55. }
  56. }
  57. }
  58. var len = bunnys.length;
  59. for (var i = 0; i < len; i++) {
  60. var bunny = bunnys[i];
  61. var p = bunny.position;
  62. var px = p[0];
  63. var py = p[1];
  64. var speedX = bunny.speedX;
  65. var speedY = bunny.speedY;
  66. px += speedX * .002;
  67. py += speedY * .002;
  68. if (px > maxX) {
  69. speedX *= -1;
  70. px = maxX;
  71. } else if (px < minX) {
  72. speedX *= -1;
  73. px = minX;
  74. }
  75. if (py > maxY) {
  76. speedY = 0;
  77. py = maxY;
  78. } else if (py < minY) {
  79. speedY *= -0.95;
  80. if (Math.random() > 0.5) {
  81. speedY -= Math.random() * 6;
  82. }
  83. py = minY;
  84. }
  85. bunny.speedX = speedX;
  86. bunny.speedY = speedY + gravity;
  87. p[0] = px;
  88. p[1] = py;
  89. nodes[i].position2D = p;
  90. }
  91. };
  92. createInstructions();
  93. function createInstructions() {
  94. var view = new Atomic.UIView();
  95. // Create a layout, otherwise child widgets won't know how to size themselves
  96. // and would manually need to be sized
  97. var layout = new Atomic.UILayout();
  98. // specify the layout region
  99. layout.rect = view.rect;
  100. view.addChild(layout);
  101. // we're laying out on the X axis so "position" controls top and bottom alignment
  102. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_RIGHT_BOTTOM;
  103. // while "distribution" handles the Y axis
  104. layout.layoutDistributionPosition = Atomic.UI_LAYOUT_DISTRIBUTION_POSITION.UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM;
  105. var fd = new Atomic.UIFontDescription();
  106. fd.id = "Vera";
  107. fd.size = 18;
  108. var scoreText = new Atomic.UIEditField();
  109. scoreText.fontDescription = fd;
  110. scoreText.readOnly = true;
  111. scoreText.multiline = true;
  112. scoreText.adaptToContentSize = true;
  113. scoreText.text = "BunnyMark\nLeft Click - Spawn Bunnies";
  114. layout.addChild(scoreText);
  115. }