PageRenderTime 322ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/zombie-stick-level-editor-workspace/level-editor/src/com/jxl/zombiestick/rl/models/LevelModel.as

https://github.com/JesterXL/ZombieStick
ActionScript | 217 lines | 189 code | 26 blank | 2 comment | 26 complexity | e06fbba0910de4efc2fd1186ff8a6e3f MD5 | raw file
  1. package com.jxl.zombiestick.rl.models
  2. {
  3. import com.jxl.zombiestick.vo.GameObjectVO;
  4. import com.jxl.zombiestick.vo.LevelVO;
  5. import flash.events.Event;
  6. import flash.geom.Point;
  7. import mx.collections.ArrayCollection;
  8. import org.robotlegs.mvcs.Actor;
  9. public class LevelModel extends Actor
  10. {
  11. private var _level:LevelVO;
  12. private var _selections:ArrayCollection;
  13. private var _lastSelected:GameObjectVO;
  14. public function get level():LevelVO { return _level; }
  15. public function set level(value:LevelVO):void
  16. {
  17. if(value !== _level)
  18. {
  19. _level = value;
  20. dispatch(new Event("levelChanged"));
  21. }
  22. }
  23. public function get selections():ArrayCollection { return _selections; }
  24. public function set selections(value:ArrayCollection):void
  25. {
  26. if(_selections !== value)
  27. {
  28. var len:int;
  29. var gameObject:GameObjectVO;
  30. if(_selections)
  31. {
  32. len = _selections.length;
  33. while(len--)
  34. {
  35. gameObject = _selections[len] as GameObjectVO;
  36. gameObject.selected = false;
  37. }
  38. }
  39. _selections = value;
  40. if(_selections)
  41. {
  42. len = _selections.length;
  43. while(len--)
  44. {
  45. gameObject = _selections[len] as GameObjectVO;
  46. gameObject.selected = true;
  47. }
  48. }
  49. dispatch(new Event("selectionsChanged"));
  50. }
  51. }
  52. public function get lastSelected():GameObjectVO { return _lastSelected; }
  53. public function set lastSelected(value:GameObjectVO):void
  54. {
  55. if(value !== _lastSelected)
  56. {
  57. if(_lastSelected)
  58. _lastSelected.selected = false;
  59. _lastSelected = value;
  60. if(_lastSelected)
  61. _lastSelected.selected = true;
  62. dispatch(new Event("lastSelectedChanged"));
  63. }
  64. }
  65. public function LevelModel()
  66. {
  67. super();
  68. }
  69. public function setSelection(gameObject:GameObjectVO):void
  70. {
  71. lastSelected = gameObject;
  72. selections = new ArrayCollection([gameObject]);
  73. }
  74. public function clearAllSelections():void
  75. {
  76. selections = null;
  77. }
  78. public function addSelection(gameObject:GameObjectVO):void
  79. {
  80. if(selections)
  81. {
  82. if(selections.contains(gameObject) == false)
  83. {
  84. gameObject.selected = true;
  85. selections.addItem(gameObject);
  86. }
  87. }
  88. else
  89. {
  90. lastSelected = gameObject;
  91. selections = new ArrayCollection([gameObject]);
  92. }
  93. }
  94. public function setSelectedIndices(indices:Vector.<int>):void
  95. {
  96. lastSelected = null;
  97. if(level && level.events)
  98. {
  99. var newSelections:ArrayCollection = new ArrayCollection();
  100. var len:int = indices.length;
  101. while(len--)
  102. {
  103. var index:int = indices[len];
  104. var go:GameObjectVO = level.events[index];
  105. newSelections.addItem(go);
  106. }
  107. selections = newSelections;
  108. }
  109. }
  110. public function deleteSelected():void
  111. {
  112. if(selections)
  113. {
  114. var len:int = selections.length;
  115. while(len--)
  116. {
  117. var gameObject:GameObjectVO = selections[len] as GameObjectVO;
  118. var index:int = level.events.getItemIndex(gameObject);
  119. level.events.removeItemAt(index);
  120. }
  121. }
  122. lastSelected = null;
  123. }
  124. public function resetOriginalPoint():void
  125. {
  126. // NOTE: this was done to allow the objects to know where they should move in relation
  127. // to where you're draging + in relation to eachother.
  128. if(selections == null)
  129. return;
  130. var len:int = selections.length;
  131. while(len--)
  132. {
  133. var gameObject:GameObjectVO = selections[len] as GameObjectVO;
  134. gameObject.originalPoint = new Point(gameObject.x, gameObject.y);
  135. }
  136. }
  137. public function onDrag(offsetX:Number, offsetY:Number):void
  138. {
  139. if(selections == null)
  140. return;
  141. var len:int = _selections.length;
  142. while(len--)
  143. {
  144. var gameObject:GameObjectVO = selections[len] as GameObjectVO;
  145. gameObject.x = gameObject.originalPoint.x - offsetX;
  146. gameObject.y = gameObject.originalPoint.y - offsetY;
  147. }
  148. }
  149. public function onMove(x:Number, y:Number):void
  150. {
  151. if(selections)
  152. {
  153. var len:int = selections.length;
  154. while(len--)
  155. {
  156. var gameObject:GameObjectVO = selections[len] as GameObjectVO;
  157. gameObject.x += x;
  158. gameObject.y += y;
  159. }
  160. }
  161. }
  162. public function onResize(width:Number, height:Number):void
  163. {
  164. if(selections)
  165. {
  166. var len:int = selections.length;
  167. while(len--)
  168. {
  169. var gameObject:GameObjectVO = selections[len] as GameObjectVO;
  170. gameObject.width += width;
  171. gameObject.height += height;
  172. }
  173. }
  174. }
  175. public function duplicateSelectedObjects():void
  176. {
  177. if(selections == null || level == null || level.events == null)
  178. return;
  179. var len:int = selections.length;
  180. var newSelections:ArrayCollection = new ArrayCollection();
  181. for(var index:int = 0; index < len; index++)
  182. {
  183. var go:GameObjectVO = selections[index];
  184. var cloned:GameObjectVO = go.clone();
  185. level.events.addItem(cloned);
  186. newSelections.addItem(cloned);
  187. }
  188. selections = newSelections;
  189. }
  190. }
  191. }