main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This script is the main entry point of the game
  2. // create the start ui programmatically, we could also
  3. // use a ui template
  4. //create main view
  5. var view = new Atomic.UIView();
  6. //create a window
  7. var window = new Atomic.UIWindow();
  8. //disable tile bard and make it non resizeable
  9. window.settings = Atomic.UI_WINDOW_SETTINGS.UI_WINDOW_SETTINGS_TITLEBAR;
  10. window.text = "Physics Platformer";
  11. // Create a layout, otherwise child widgets won't know how to size themselves
  12. // and would manually need to be sized
  13. var layout = new Atomic.UILayout();
  14. layout.rect = view.rect;
  15. // give ourselves a little more spacing
  16. layout.spacing = 18;
  17. //axis to y
  18. layout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  19. //add ours layout to window
  20. window.addChild(layout);
  21. //create a text field
  22. var text = new Atomic.UITextField();
  23. text.text = "Please select the time of day:";
  24. layout.addChild(text);
  25. // Buttons layout
  26. var buttonLayout = new Atomic.UILayout();
  27. buttonLayout.axis = Atomic.UI_AXIS.UI_AXIS_X;
  28. layout.addChild(buttonLayout);
  29. var buttonDaytime = new Atomic.UIButton();
  30. buttonDaytime.text = "Play Daytime";
  31. buttonDaytime.onClick = function () {
  32. run(true);
  33. //we need to return value here, otherwise we will be GC'ed
  34. return true;
  35. };
  36. buttonLayout.addChild(buttonDaytime);
  37. var buttonNightTime = new Atomic.UIButton();
  38. buttonNightTime.text = "Play Nighttime";
  39. buttonNightTime.onClick = function () {
  40. run(false);
  41. //we need to return value here, otherwise we will be GC'ed
  42. return true;
  43. };
  44. buttonLayout.addChild(buttonNightTime);
  45. window.resizeToFitContent();
  46. // add to the root view and center
  47. view.addChild(window);
  48. window.center();
  49. var dayTime;
  50. function run(daytime) {
  51. //ok, then remove ours window
  52. view.removeChild(window);
  53. //require GlobalVariables module, and set its dayTime value to the current daytime
  54. require("GlobalVariables").dayTime = daytime;
  55. //load main scene!
  56. var scene = Atomic.player.loadScene("Scenes/Scene.scene");
  57. //if we are running ours game on android
  58. if(Atomic.platform == "Android" || Atomic.platform == "iOS") {
  59. //requiring a dpad module
  60. var DPad = require("DPad");
  61. //create a new view
  62. var uiView = new Atomic.UIView();
  63. //creating a new DPad
  64. var dpad = new DPad();
  65. //adding horizontal and vertical buttons
  66. dpad.addAll();
  67. //ok, then we could init ours dpad
  68. dpad.init(uiView);
  69. //create a jump button
  70. var jumpButton = new Atomic.UIButton();
  71. //unset its skin, because we will use UIImageWidget
  72. jumpButton.skinBg = "";
  73. //create ours jump button image
  74. var jumpButtonImage = new Atomic.UIImageWidget();
  75. //load image
  76. jumpButtonImage.setImage("UI/jumpButton.png");
  77. //resize ours image by 2.2x
  78. var jumpButtonWidth = jumpButtonImage.imageWidth*2.2;
  79. var jumpButtonHeight = jumpButtonImage.imageHeight*2.2;
  80. //calculate position
  81. var posX = Atomic.graphics.width - Atomic.graphics.width/8-jumpButtonWidth/2;
  82. var posY = Atomic.graphics.height - Atomic.graphics.height/4-jumpButtonHeight/2;
  83. //sets jumpButton rect, specify position and end position
  84. jumpButton.rect = [posX, posY, posX+jumpButtonWidth, posY+jumpButtonHeight];
  85. //sets jumpButtonImage rect, we specify there only end position
  86. jumpButtonImage.rect = [0, 0, jumpButtonWidth, jumpButtonHeight];
  87. //adds image to jumpButton
  88. jumpButton.addChild(jumpButtonImage);
  89. //adds jumpButton to the dpad view
  90. dpad.view.addChild(jumpButton);
  91. //sets jumpButton capturing to false, because we wanna make it multitouchable
  92. jumpButton.setCapturing(false);
  93. //binds jumpButton to KEY_SPACE
  94. jumpButton.emulationButton = Atomic.KEY_SPACE;
  95. }
  96. }