/Nd2d-example/tests/CameraTest.as

https://bitbucket.org/HopeSky/mars_nd2d · ActionScript · 121 lines · 72 code · 19 blank · 30 comment · 2 complexity · 6559c1782d4a1d62f206f81b2fa8a553 MD5 · raw file

  1. /*
  2. * ND2D - A Flash Molehill GPU accelerated 2D engine
  3. *
  4. * Author: Lars Gerckens
  5. * Copyright (c) nulldesign 2011
  6. * Repository URL: http://github.com/nulldesign/nd2d
  7. * Getting started: https://github.com/nulldesign/nd2d/wiki
  8. *
  9. *
  10. * Licence Agreement
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. package tests {
  31. import de.nulldesign.nd2d.display.Node2D;
  32. import de.nulldesign.nd2d.display.Scene2D;
  33. import de.nulldesign.nd2d.display.Sprite2D;
  34. import de.nulldesign.nd2d.display.TextField2D;
  35. import de.nulldesign.nd2d.materials.texture.Texture2D;
  36. import de.nulldesign.nd2d.utils.NumberUtil;
  37. import flash.events.Event;
  38. import flash.events.MouseEvent;
  39. import flash.text.TextFormatAlign;
  40. public class CameraTest extends Scene2D {
  41. [Embed(source="/assets/water_texture.jpg")]
  42. private var backgroundTexture:Class;
  43. [Embed(source="/assets/nd_logo.png")]
  44. private var spriteTexture:Class;
  45. private var back:Sprite2D;
  46. private var targetNode:Node2D;
  47. public function CameraTest() {
  48. back = new Sprite2D(Texture2D.textureFromBitmapData(new backgroundTexture().bitmapData));
  49. back.alpha = 0.5;
  50. addChild(back);
  51. addEventListener(Event.ADDED_TO_STAGE, addedToStage);
  52. }
  53. private function addedToStage(e:Event):void {
  54. removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
  55. var tex:Texture2D = Texture2D.textureFromBitmapData(new spriteTexture().bitmapData);
  56. var s:Sprite2D;
  57. for(var i:int = 0; i < 5; i++) {
  58. s = new Sprite2D(tex);
  59. s.x = NumberUtil.rndMinMax(100.0, stage.stageWidth - 100.0);
  60. s.y = NumberUtil.rndMinMax(100.0, stage.stageHeight - 100.0);
  61. s.rotation = NumberUtil.rndMinMax(0.0, 360.0);
  62. s.mouseEnabled = true;
  63. s.addEventListener(MouseEvent.CLICK, spriteClick);
  64. addChild(s);
  65. }
  66. // GUI layer test
  67. s = new Sprite2D(tex);
  68. s.x = s.width * 0.5;
  69. s.y = stage.stageHeight - s.height * 0.5;
  70. s.tint = 0xFF9900;
  71. s.mouseEnabled = true;
  72. s.addEventListener(MouseEvent.CLICK, guiLayerItemClick);
  73. sceneGUILayer.addChild(s);
  74. var txt:TextField2D = new TextField2D();
  75. txt.font = "Helvetica";
  76. txt.textColor = 0xFF9900;
  77. txt.size = 30.0;
  78. txt.align = TextFormatAlign.LEFT;
  79. txt.text = "GUI Layer";
  80. txt.x = 120.0;
  81. txt.y = stage.stageHeight - s.height * 0.5;
  82. sceneGUILayer.addChild(txt);
  83. }
  84. private function guiLayerItemClick(e:MouseEvent):void {
  85. trace("hello GUI");
  86. }
  87. private function spriteClick(e:MouseEvent):void {
  88. targetNode = Node2D(e.target);
  89. }
  90. override protected function step(elapsed:Number):void {
  91. back.x = camera.sceneWidth * 0.5;
  92. back.y = camera.sceneHeight * 0.5;
  93. back.width = camera.sceneWidth * 5.0;
  94. back.height = camera.sceneHeight * 5.0;
  95. if(targetNode) {
  96. camera.x += ((targetNode.x - camera.sceneWidth * 0.5) - camera.x) * 0.05;
  97. camera.y += ((targetNode.y - camera.sceneHeight * 0.5) - camera.y) * 0.05;
  98. camera.rotation += (-targetNode.rotation - camera.rotation) * 0.05;
  99. }
  100. }
  101. }
  102. }