PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 31package tests {
 32
 33	import de.nulldesign.nd2d.display.Node2D;
 34	import de.nulldesign.nd2d.display.Scene2D;
 35	import de.nulldesign.nd2d.display.Sprite2D;
 36	import de.nulldesign.nd2d.display.TextField2D;
 37	import de.nulldesign.nd2d.materials.texture.Texture2D;
 38	import de.nulldesign.nd2d.utils.NumberUtil;
 39
 40	import flash.events.Event;
 41	import flash.events.MouseEvent;
 42	import flash.text.TextFormatAlign;
 43
 44	public class CameraTest extends Scene2D {
 45
 46		[Embed(source="/assets/water_texture.jpg")]
 47		private var backgroundTexture:Class;
 48
 49		[Embed(source="/assets/nd_logo.png")]
 50		private var spriteTexture:Class;
 51
 52		private var back:Sprite2D;
 53		private var targetNode:Node2D;
 54
 55		public function CameraTest() {
 56
 57			back = new Sprite2D(Texture2D.textureFromBitmapData(new backgroundTexture().bitmapData));
 58			back.alpha = 0.5;
 59			addChild(back);
 60
 61			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
 62		}
 63
 64		private function addedToStage(e:Event):void {
 65			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
 66
 67			var tex:Texture2D = Texture2D.textureFromBitmapData(new spriteTexture().bitmapData);
 68			var s:Sprite2D;
 69
 70			for(var i:int = 0; i < 5; i++) {
 71				s = new Sprite2D(tex);
 72				s.x = NumberUtil.rndMinMax(100.0, stage.stageWidth - 100.0);
 73				s.y = NumberUtil.rndMinMax(100.0, stage.stageHeight - 100.0);
 74				s.rotation = NumberUtil.rndMinMax(0.0, 360.0);
 75				s.mouseEnabled = true;
 76				s.addEventListener(MouseEvent.CLICK, spriteClick);
 77				addChild(s);
 78			}
 79
 80			// GUI layer test
 81			s = new Sprite2D(tex);
 82			s.x = s.width * 0.5;
 83			s.y = stage.stageHeight - s.height * 0.5;
 84			s.tint = 0xFF9900;
 85			s.mouseEnabled = true;
 86			s.addEventListener(MouseEvent.CLICK, guiLayerItemClick);
 87			sceneGUILayer.addChild(s);
 88
 89			var txt:TextField2D = new TextField2D();
 90			txt.font = "Helvetica";
 91			txt.textColor = 0xFF9900;
 92			txt.size = 30.0;
 93			txt.align = TextFormatAlign.LEFT;
 94			txt.text = "GUI Layer";
 95			txt.x = 120.0;
 96			txt.y = stage.stageHeight - s.height * 0.5;
 97			sceneGUILayer.addChild(txt);
 98		}
 99
100		private function guiLayerItemClick(e:MouseEvent):void {
101			trace("hello GUI");
102		}
103
104		private function spriteClick(e:MouseEvent):void {
105			targetNode = Node2D(e.target);
106		}
107
108		override protected function step(elapsed:Number):void {
109			back.x = camera.sceneWidth * 0.5;
110			back.y = camera.sceneHeight * 0.5;
111			back.width = camera.sceneWidth * 5.0;
112			back.height = camera.sceneHeight * 5.0;
113
114			if(targetNode) {
115				camera.x += ((targetNode.x - camera.sceneWidth * 0.5) - camera.x) * 0.05;
116				camera.y += ((targetNode.y - camera.sceneHeight * 0.5) - camera.y) * 0.05;
117				camera.rotation += (-targetNode.rotation - camera.rotation) * 0.05;
118			}
119		}
120	}
121}