PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Engine/src/org/flashIso/engine/core/IsoObjectBase.as

http://flashiso.googlecode.com/
ActionScript | 413 lines | 309 code | 54 blank | 50 comment | 43 complexity | ae8cb2cd28b04fc35bae013eac97a082 MD5 | raw file
  1. package org.flashIso.engine.core {
  2. import flash.display.MovieClip;
  3. import flash.display.Shape;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.utils.getQualifiedClassName;
  7. import org.flashIso.engine.base.IsoPerspective;
  8. import org.flashIso.engine.events.IsoEvent;
  9. import org.flashIso.engine.objects.IsoImage;
  10. [Exclude(name="addChildAt", kind="method")]
  11. [Exclude(name="removeChild", kind="method")]
  12. [Exclude(name="removeChildAt", kind="method")]
  13. [Exclude(name="setChildIndex", kind="method")]
  14. [Exclude(name="swapChildren", kind="method")]
  15. [Exclude(name="swapChildrenAt", kind="method")]
  16. [Event(name="positionChange", type="org.flashIso.engine.events.IsoEvent")]
  17. [Event(name="draw", type="org.flashIso.engine.events.IsoEvent")]
  18. /**
  19. * Base class for all isometric elements
  20. **/
  21. public class IsoObjectBase extends ValidateableSprite {
  22. private var _position : Point3D = new Point3D();
  23. private var _size : Point3D = new Point3D(50, 50, 50);
  24. private var _delta : Point3D = new Point3D();
  25. private var _fills : FillList = new FillList();
  26. private var _drawShape : Boolean = true;
  27. private var _shape : Sprite;
  28. private var _drawHeight : Boolean = false;
  29. private var _rotation : Number = 0;
  30. private var _useImageFramesWhenRotating : Boolean = false;
  31. private var _image : IsoImage = new IsoImage();
  32. private var _hitAreaType : String;
  33. private var _customProperties : Object = new Object();
  34. private var _content:Sprite;
  35. private var _container:Sprite = new Sprite();
  36. public function IsoObjectBase() {
  37. super();
  38. _fills.addEventListener(Event.CHANGE, triggerValidation, false, 0, true);
  39. _delta.addEventListener(Event.CHANGE, triggerValidation, false, 0, true);
  40. _size.addEventListener(Event.CHANGE, sizeChangeHandler, false, 0, true);
  41. _position.addEventListener(Event.CHANGE, positionChangeHandler, false, 0, true);
  42. mouseChildren = false;
  43. addChild(_container);
  44. _container.addChild(_image);
  45. _container.name = "container";
  46. _image.addEventListener(Event.CHANGE, imageChange, false, 0, true);
  47. }
  48. protected function positionChangeHandler(e:Event) : void {
  49. dispatchEvent(new IsoEvent(IsoEvent.POSITION_CHANGE));
  50. }
  51. protected function sizeChangeHandler(e:Event = null) : void {
  52. dispatchEvent(new IsoEvent(IsoEvent.SIZE_CHANGE));
  53. triggerValidation();
  54. }
  55. /**
  56. * Size
  57. **/
  58. public function get size() : Point3D {
  59. return _size;
  60. }
  61. public function set size(point3D : Point3D) : void {
  62. if (!point3D.equals(_size)){
  63. _size.autoValidation = false;
  64. _size.x = point3D.x;
  65. _size.y = point3D.y;
  66. _size.z = point3D.z;
  67. _size.autoValidation = true;
  68. sizeChangeHandler();
  69. triggerValidation();
  70. }
  71. }
  72. /**
  73. * CustomProperties object
  74. **/
  75. public function get customProperties() : Object{
  76. return _customProperties;
  77. }
  78. public function set customProperties(obj:Object) : void{
  79. _customProperties = obj;
  80. }
  81. public function get content() : Sprite {
  82. if (_content == null){
  83. _content = new Sprite();
  84. _content.name = "content";
  85. _container.addChild(_content);
  86. }
  87. return _content;
  88. }
  89. public function getProperty(property:String) : String{
  90. return _customProperties[property];
  91. }
  92. public function get image() : IsoImage {
  93. return _image;
  94. }
  95. public function set image(value:IsoImage) : void {
  96. if (_image != value){
  97. var level:int = numChildren;
  98. if (_image != null) {
  99. level = _container.getChildIndex(_image);
  100. _container.removeChild(_image);
  101. _image.removeEventListener(Event.CHANGE, imageChange);
  102. }
  103. _image = value;
  104. if (_image != null) {
  105. _container.addChildAt(_image, level);
  106. _image.addEventListener(Event.CHANGE, imageChange, false, 0, true);
  107. }
  108. imageChange();
  109. }
  110. }
  111. private function imageChange(e:Event = null) : void {
  112. dispatchEvent(new IsoEvent(IsoEvent.DRAW));
  113. }
  114. public function get fills() : FillList{
  115. return _fills;
  116. }
  117. public function set fills(list:FillList) : void {
  118. _fills = list;
  119. }
  120. /**
  121. * Flag indicating wheather the shape should be drawn
  122. **/
  123. public function set drawShape(value : Boolean) : void {
  124. if (_drawShape != value) {
  125. _drawShape = value;
  126. triggerValidation();
  127. }
  128. }
  129. public function get drawShape() : Boolean {
  130. return _drawShape;
  131. }
  132. /**
  133. * <p>Setter for hitArea type.</p>
  134. * <p>Accepted values are: shapeAsMask, shapeAsHitArea, none</p>
  135. */
  136. public function set hitAreaType(value : String) : void {
  137. if (_hitAreaType != value) {
  138. _hitAreaType = value;
  139. triggerValidation();
  140. }
  141. }
  142. public function get hitAreaType() : String {
  143. return _hitAreaType;
  144. }
  145. private function setHitAreaType() : void {
  146. switch((_hitAreaType+"").toLowerCase()) {
  147. case HitAreaType.SHAPE_AS_MASK.toLowerCase():
  148. _container.mask = _shape;
  149. this.hitArea = null;
  150. break;
  151. case HitAreaType.SHAPE_AS_HIT_AREA.toLowerCase():
  152. _container.mask = null;
  153. this.hitArea = _shape;
  154. break;
  155. default:
  156. _container.mask = null;
  157. this.hitArea = null;
  158. break;
  159. }
  160. }
  161. /**
  162. * Flag for height draw.
  163. * This should be use only for development purposes.
  164. **/
  165. public function set drawHeight(value : Boolean) : void {
  166. if (_drawHeight != value) {
  167. if (_drawHeight){
  168. _position.removeEventListener(Event.CHANGE, triggerValidation);
  169. }
  170. _drawHeight = value;
  171. if (_drawHeight){
  172. _position.addEventListener(Event.CHANGE, triggerValidation, false, 0, true);
  173. }
  174. triggerValidation();
  175. }
  176. }
  177. public function get drawHeight() : Boolean {
  178. return _drawHeight;
  179. }
  180. /**
  181. * 3D possition. When the object a child of an IsoMap, changing
  182. * position object determines changing the x,y sprite coordinates
  183. **/
  184. public function get position() : Point3D {
  185. return _position;
  186. }
  187. public function set position(point : Point3D) : void {
  188. if (!_position.equals(point)) {
  189. _position.x = point.x;
  190. _position.y = point.y;
  191. _position.z = point.z;
  192. }
  193. }
  194. /**
  195. * Use this object to move the sprite's origin point.
  196. * In order to avoid tiles to overlap objects,
  197. * set the origin point to upper corner for tiles
  198. * and to down corner for objects.
  199. *
  200. *
  201. **/
  202. public function get delta() : Point3D {
  203. return _delta;
  204. }
  205. public function set delta(point : Point3D) : void {
  206. if (!_delta.equals(point)) {
  207. _delta.x = point.x;
  208. _delta.y = point.y;
  209. _delta.z = point.z;
  210. triggerValidation();
  211. }
  212. }
  213. override public function set rotation (value:Number) : void {
  214. if (_rotation != value){
  215. _rotation = value;
  216. while (_rotation < 0){
  217. _rotation += 360;
  218. }
  219. while (_rotation > 360){
  220. _rotation -= 360;
  221. }
  222. triggerValidation();
  223. setImageFrame();
  224. }
  225. }
  226. /**
  227. *
  228. * //to do: a better explanation
  229. *
  230. * If the loaded object is a swf, it's frame can be set here.
  231. *
  232. * Ignored if image object doesn't contain a swf file loaded.
  233. **/
  234. private function setImageFrame() : void {
  235. if (_image.loader != null && _image.loader.content is MovieClip){
  236. if (_useImageFramesWhenRotating){
  237. var n:uint = (_image.loader.content as MovieClip).totalFrames;
  238. var frame:Number = (Math.floor(((_rotation * n) / 360) + 0.5) % n) + 1;
  239. (_image.loader.content as MovieClip).gotoAndStop(frame);
  240. }else{
  241. }
  242. }
  243. }
  244. override public function get rotation() : Number{
  245. return _rotation;
  246. }
  247. /**
  248. * <p>Flag indicating if the loaded swf IsoImage</p>
  249. * should circle through image frames while rotating the object.
  250. *
  251. * <p>Ignored if the image doesn't contain a swf file loaded.</p>
  252. **/
  253. public function set useImageFramesWhenRotating (value:Boolean) : void {
  254. if (_useImageFramesWhenRotating != value){
  255. _useImageFramesWhenRotating = value;
  256. }
  257. }
  258. public function get useImageFramesWhenRotating() : Boolean{
  259. return _useImageFramesWhenRotating;
  260. }
  261. public function draw(perspective : IsoPerspective) : void {
  262. if (_shape != null) {
  263. _shape.graphics.clear();
  264. }
  265. var p2d : Point2D;
  266. var p3d : Point3D;
  267. graphics.clear();
  268. if (_drawHeight){
  269. graphics.lineStyle(1, 0xff0000, 1);
  270. graphics.beginFill(0xff0000, 0.2);
  271. p2d = perspective.getProjection(new Point3D(8,2,-_position.z));
  272. graphics.moveTo(p2d.x, p2d.y);
  273. p2d = perspective.getProjection(new Point3D(-8,2,-_position.z));
  274. graphics.lineTo(p2d.x, p2d.y);
  275. p2d = perspective.getProjection(new Point3D(-8,-2,-_position.z));
  276. graphics.lineTo(p2d.x, p2d.y);
  277. p2d = perspective.getProjection(new Point3D(8,-2,-_position.z));
  278. graphics.lineTo(p2d.x, p2d.y);
  279. graphics.endFill();
  280. graphics.beginFill(0xff0000, 0.2);
  281. p2d = perspective.getProjection(new Point3D(2,8,-_position.z));
  282. graphics.moveTo(p2d.x, p2d.y);
  283. p2d = perspective.getProjection(new Point3D(-2,8,-_position.z));
  284. graphics.lineTo(p2d.x, p2d.y);
  285. p2d = perspective.getProjection(new Point3D(-2,-8,-_position.z));
  286. graphics.lineTo(p2d.x, p2d.y);
  287. p2d = perspective.getProjection(new Point3D(2,-8,-_position.z));
  288. graphics.lineTo(p2d.x, p2d.y);
  289. graphics.endFill();
  290. graphics.beginFill(0xff0000, 0.2);
  291. p2d = perspective.getProjection(new Point3D(8,2,0));
  292. graphics.moveTo(p2d.x, p2d.y);
  293. p2d = perspective.getProjection(new Point3D(-8,2,0));
  294. graphics.lineTo(p2d.x, p2d.y);
  295. p2d = perspective.getProjection(new Point3D(-8,-2,0));
  296. graphics.lineTo(p2d.x, p2d.y);
  297. p2d = perspective.getProjection(new Point3D(8,-2,0));
  298. graphics.lineTo(p2d.x, p2d.y);
  299. graphics.endFill();
  300. graphics.beginFill(0xff0000, 0.2);
  301. p2d = perspective.getProjection(new Point3D(2,8,0));
  302. graphics.moveTo(p2d.x, p2d.y);
  303. p2d = perspective.getProjection(new Point3D(-2,8,0));
  304. graphics.lineTo(p2d.x, p2d.y);
  305. p2d = perspective.getProjection(new Point3D(-2,-8,0));
  306. graphics.lineTo(p2d.x, p2d.y);
  307. p2d = perspective.getProjection(new Point3D(2,-8,0));
  308. graphics.lineTo(p2d.x, p2d.y);
  309. graphics.endFill();
  310. graphics.lineStyle(2, 0xff0000, 1);
  311. p2d = perspective.getProjection(new Point3D(0,0,0));
  312. graphics.moveTo(p2d.x, p2d.y);
  313. p2d = perspective.getProjection(new Point3D(0,0,-_position.z));
  314. graphics.lineTo(p2d.x, p2d.y);
  315. }
  316. if (_drawShape) {
  317. // trace("------- draw ------");
  318. if (_shape == null){
  319. _shape = new Sprite();
  320. _shape.name = "shape";
  321. addChild(_shape);
  322. }
  323. _shape.graphics.clear();
  324. var i : uint;
  325. for (i = 0; i < _fills.length; i++) {
  326. // trace("------- new fill ------");
  327. var fill : FillDescriptor = _fills.getItemAt(i);
  328. var j : uint;
  329. _shape.graphics.lineStyle(fill.lineThickness, fill.lineColor, fill.lineAlpha);
  330. _shape.graphics.beginFill(fill.color, fill.alpha);
  331. p3d = fill.pointsList.getPointAt(fill.pointsList.length - 1).clone(_delta.x, _delta.y, _delta.z);
  332. if (_rotation != 0){
  333. p3d = RotationMatrix.rotatePoint(p3d, _rotation);
  334. }
  335. p2d = perspective.getProjection(p3d);
  336. _shape.graphics.moveTo(p2d.x, p2d.y);
  337. // trace("moveTo: " + p2d.toString());
  338. for (j = 0;j < fill.pointsList.length;j++) {
  339. p3d = fill.pointsList.getPointAt(j).clone(_delta.x, _delta.y, _delta.z);
  340. if (_rotation != 0){
  341. p3d = RotationMatrix.rotatePoint(p3d, _rotation);
  342. }
  343. p2d = perspective.getProjection(p3d);
  344. // trace("timeTo: " + p2d.toString());
  345. _shape.graphics.lineTo(p2d.x, p2d.y);
  346. }
  347. _shape.graphics.endFill();
  348. }
  349. }else{
  350. if (_shape != null){
  351. removeChild(_shape);
  352. _shape = null;
  353. }
  354. }
  355. setHitAreaType();
  356. dispatchEvent(new IsoEvent(IsoEvent.DRAW));
  357. }
  358. }
  359. }