PageRenderTime 83ms CodeModel.GetById 27ms RepoModel.GetById 2ms app.codeStats 0ms

/src/de/nulldesign/nd2d/display/Scene2D.as

https://bitbucket.org/HopeSky/mars_nd2d
ActionScript | 152 lines | 76 code | 32 blank | 44 comment | 11 complexity | 293878f0eacb9f2c90cde35b96c856ad 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 de.nulldesign.nd2d.display {
  31. import flash.display.Stage;
  32. import flash.display3D.Context3D;
  33. import flash.events.Event;
  34. import flash.geom.Matrix3D;
  35. import flash.geom.Vector3D;
  36. import de.nulldesign.nd2d.utils.StatsObject;
  37. /**
  38. * <p>A scene that can contain 2D nodes. Such as Sprite2D.</p>
  39. * <p>The scene is meant to display a state of your game, such as the game screen, a highscore screen, etc. You can switch between the scenes by setting a new active scene in the World2D</p>
  40. *
  41. * Even if a scene has x,y, rotation etc. properties you can't modify a scene this way.
  42. * Use the built in camera instance to pan and zoom over your scene.
  43. *
  44. * <p>If you make use of the camera and still want to have non moving objects in your scene like GUI elements, attach them to the sceneGUILayer instead to the scene itself.</p>
  45. */
  46. public class Scene2D extends Node2D {
  47. internal var br:Number = 0.0;
  48. internal var bg:Number = 0.0;
  49. internal var bb:Number = 0.0;
  50. private var _backgroundColor:Number = 0x000000;
  51. public function get backgroundColor():Number {
  52. return _backgroundColor;
  53. }
  54. /**
  55. * @param The background color of your scene in RGB format
  56. */
  57. public function set backgroundColor(value:Number):void {
  58. _backgroundColor = value;
  59. br = (backgroundColor >> 16) / 255.0;
  60. bg = (backgroundColor >> 8 & 255) / 255.0;
  61. bb = (backgroundColor & 255) / 255.0;
  62. }
  63. protected var sceneGUICamera:Camera2D = new Camera2D(1, 1);
  64. protected var sceneGUILayer:Node2D = new Node2D();
  65. public function Scene2D() {
  66. super();
  67. mouseEnabled = true;
  68. }
  69. override public function handleDeviceLoss():void {
  70. super.handleDeviceLoss();
  71. if(sceneGUILayer)
  72. sceneGUILayer.handleDeviceLoss();
  73. }
  74. override internal function stepNode(elapsed:Number, timeSinceStartInSeconds:Number):void {
  75. this.timeSinceStartInSeconds = timeSinceStartInSeconds;
  76. for each(var child:Node2D in children) {
  77. child.stepNode(elapsed, timeSinceStartInSeconds);
  78. }
  79. // call step() after all nodes have finished updating their positions. removes camera stuttering issue
  80. step(elapsed);
  81. sceneGUILayer.stepNode(elapsed, timeSinceStartInSeconds);
  82. }
  83. override internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean, statsObject:StatsObject):void {
  84. for each(var child:Node2D in children) {
  85. child.drawNode(context, camera, false, statsObject);
  86. }
  87. // resize gui camera if needed
  88. if(sceneGUICamera.sceneWidth != camera.sceneWidth) {
  89. sceneGUICamera.resizeCameraStage(camera.sceneWidth, camera.sceneHeight);
  90. }
  91. // draw GUI layer
  92. sceneGUILayer.drawNode(context, sceneGUICamera, false, statsObject);
  93. }
  94. override internal function processMouseEvent(mousePosition:Vector3D, mouseEventType:String, cameraViewProjectionMatrix:Matrix3D, isTouchEvent:Boolean, touchPointID:int):Node2D {
  95. var node:Node2D = super.processMouseEvent(mousePosition, mouseEventType, cameraViewProjectionMatrix, isTouchEvent, touchPointID);
  96. var guiNode:Node2D = sceneGUILayer.processMouseEvent(mousePosition, mouseEventType, sceneGUICamera.getViewProjectionMatrix(), isTouchEvent, touchPointID);
  97. return guiNode ? guiNode : node;
  98. }
  99. override internal function setStageAndCamRef(value:Stage, cameraValue:Camera2D):void {
  100. super.setStageAndCamRef(value, cameraValue);
  101. if(camera) {
  102. _width = camera.sceneWidth;
  103. _height = camera.sceneHeight;
  104. }
  105. if(sceneGUILayer) {
  106. sceneGUILayer.setStageAndCamRef(value, sceneGUICamera);
  107. }
  108. }
  109. override protected function hitTest():Boolean {
  110. return (_mouseX >= 0.0 && _mouseX <= _width && _mouseY >= 0.0 && _mouseY <= _height);
  111. }
  112. override public function dispose():void {
  113. if(sceneGUILayer) {
  114. sceneGUILayer.dispose();
  115. sceneGUILayer = null;
  116. }
  117. super.dispose();
  118. }
  119. }
  120. }