PageRenderTime 4108ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/editor/GameObject.as

http://github.com/MattThorson/Ogmo-Editor
ActionScript | 429 lines | 339 code | 71 blank | 19 comment | 54 complexity | 6dc18fca3a7fe7f1a7e45f09919d16dc MD5 | raw file
  1. package editor
  2. {
  3. import editor.ui.*;
  4. import editor.definitions.*;
  5. import flash.display.BitmapData;
  6. import flash.display.DisplayObject;
  7. import flash.display.Sprite;
  8. import flash.events.Event;
  9. import flash.events.KeyboardEvent;
  10. import flash.events.MouseEvent;
  11. import flash.geom.Point;
  12. import flash.geom.Rectangle;
  13. public class GameObject extends Sprite
  14. {
  15. private var _selected:Boolean;
  16. private var draw:Sprite;
  17. private var layer:ObjectLayer;
  18. private var holder:Sprite;
  19. private var bg:Sprite;
  20. public var selBox:SelBox;
  21. public var grabbed:Point;
  22. public var resizing:Boolean;
  23. public var definition:ObjectDefinition;
  24. public var objWidth:int;
  25. public var objHeight:int;
  26. public var values:Vector.<Value>;
  27. public var nodes:Sprite;
  28. public var lines:Sprite;
  29. public function GameObject( layer:ObjectLayer, objDef:ObjectDefinition = null )
  30. {
  31. this.layer = layer;
  32. grabbed = null;
  33. _selected = false;
  34. //The colored background drawn behind selected objects
  35. bg = new Sprite;
  36. addChild( bg );
  37. //Holds the actual drawn object image, rotates
  38. holder = new Sprite;
  39. addChild( holder );
  40. //The actual drawn object image
  41. draw = new Sprite;
  42. holder.addChild( draw );
  43. //The colored hollow rectangle around each object
  44. selBox = new SelBox( 8, 8, 1, SelBox.OBJECT_NOTSELECTED );
  45. addChild( selBox );
  46. addChild( nodes = new Sprite );
  47. addChild( lines = new Sprite );
  48. if (objDef)
  49. init( objDef );
  50. }
  51. private function init( objDef:ObjectDefinition ):void
  52. {
  53. definition = objDef;
  54. setSize( objDef.width, objDef.height );
  55. draw.x = -definition.originX;
  56. draw.y = -definition.originY;
  57. bg.x = -definition.originX;
  58. bg.y = -definition.originY;
  59. selBox.x = -definition.originX;
  60. selBox.y = -definition.originY;
  61. //Get values
  62. if (objDef.values)
  63. {
  64. values = new Vector.<Value>;
  65. for each ( var vd:ValueDefinition in objDef.values )
  66. values.push( vd.getValue() );
  67. }
  68. //Enforce object limits
  69. if (definition.limit > 0)
  70. {
  71. var amount:int = layer.getAmountType( definition.name );
  72. if (amount >= definition.limit)
  73. layer.removeType( definition.name, amount + 1 - definition.limit );
  74. }
  75. }
  76. private function drawBG():void
  77. {
  78. bg.graphics.clear();
  79. bg.graphics.beginFill( 0xFFFFFF, 0.2 );
  80. bg.graphics.drawRect( 0, 0, objWidth, objHeight );
  81. bg.graphics.endFill();
  82. }
  83. private function clearBG():void
  84. {
  85. bg.graphics.clear();
  86. }
  87. public function move( h:int, v:int ):void
  88. {
  89. var oldX:int = x;
  90. var oldY:int = y;
  91. x = Math.max( 0, Math.min( x + h, Ogmo.level.levelWidth - objWidth + definition.originX ) );
  92. y = Math.max( 0, Math.min( y + v, Ogmo.level.levelHeight - objHeight + definition.originY ) );
  93. }
  94. public function setSize( width:int, height:int ):void
  95. {
  96. width = Math.max( definition.width, width );
  97. width = Math.min( Ogmo.level.levelWidth - x, width );
  98. if (!definition.resizableX)
  99. width = definition.width;
  100. height = Math.max( definition.height, height );
  101. height = Math.min( Ogmo.level.levelHeight - y, height );
  102. if (!definition.resizableY)
  103. height = definition.height;
  104. objWidth = width;
  105. objHeight = height;
  106. draw.graphics.clear();
  107. draw.graphics.beginBitmapFill( definition.bitmapData );
  108. if (definition.tile)
  109. {
  110. draw.graphics.drawRect( 0, 0, objWidth, objHeight );
  111. }
  112. else
  113. {
  114. draw.graphics.drawRect( 0, 0, definition.imgWidth, definition.imgHeight );
  115. draw.scaleX = objWidth / definition.imgWidth;
  116. draw.scaleY = objHeight / definition.imgHeight;
  117. }
  118. draw.graphics.endFill();
  119. selBox.setSize( objWidth, objHeight );
  120. drawBG();
  121. refreshNodes();
  122. }
  123. public function setAngle( to:Number ):void
  124. {
  125. if (definition.rotatable)
  126. {
  127. var go:Number = Math.round( to / definition.rotationPrecision ) * definition.rotationPrecision;
  128. angle = go;
  129. angle = angle % 360;
  130. }
  131. }
  132. public function rotate( dir:int = 1 ):void
  133. {
  134. if (definition.rotatable)
  135. angle = (angle + 360 + (definition.rotationPrecision * dir)) % 360;
  136. }
  137. public function collidesWithPoint( x:int, y:int ):Boolean
  138. {
  139. return (rect.contains( x, y ));
  140. }
  141. public function collidesWithObject( other:GameObject ):Boolean
  142. {
  143. return (rect.intersects( other.rect ));
  144. }
  145. public function collidesWithRectangle( other:Rectangle ):Boolean
  146. {
  147. return (rect.intersects( other ));
  148. }
  149. /* ========================== NODES ========================== */
  150. public function addNode( node:Node ):void
  151. {
  152. nodes.addChild( node );
  153. node.x -= x;
  154. node.y -= y;
  155. refreshLines();
  156. }
  157. public function removeNode( node:Node ):void
  158. {
  159. nodes.removeChild( node );
  160. refreshLines();
  161. }
  162. public function removeAllNodes():void
  163. {
  164. while (nodes.numChildren > 0)
  165. nodes.removeChildAt( 0 );
  166. refreshLines();
  167. }
  168. public function removeFirstNode( times:uint = 1 ):void
  169. {
  170. for ( var i:int = 0; i < times; i++ )
  171. nodes.removeChildAt( 0 );
  172. refreshLines();
  173. }
  174. public function getAmountOfNodes():uint
  175. {
  176. return nodes.numChildren;
  177. }
  178. public function hasNodeAt( x:int, y:int ):Boolean
  179. {
  180. for ( var i:int = 0; i < nodes.numChildren; i++ )
  181. {
  182. if (nodes.getChildAt( i ).x == x - this.x && nodes.getChildAt( i ).y == y - this.y)
  183. return true;
  184. }
  185. return false;
  186. }
  187. public function removeNodeAt( x:int, y:int ):void
  188. {
  189. for ( var i:int = 0; i < nodes.numChildren; i++ )
  190. {
  191. if (nodes.getChildAt( i ).x == x - this.x && nodes.getChildAt( i ).y == y - this.y)
  192. {
  193. nodes.removeChildAt( i );
  194. refreshLines();
  195. return;
  196. }
  197. }
  198. }
  199. private function refreshNodes():void
  200. {
  201. for ( var i:int = 0; i < nodes.numChildren; i++ )
  202. {
  203. (nodes.getChildAt( i ) as Node).updateImage();
  204. }
  205. refreshLines();
  206. }
  207. private function refreshLines():void
  208. {
  209. if (definition.nodesDefinition == null || definition.nodesDefinition.lineMode == NodesDefinition.NONE)
  210. return;
  211. lines.graphics.clear();
  212. var color:uint = definition.nodesDefinition.color;
  213. var n:Node;
  214. var i:int;
  215. lines.graphics.lineStyle( 1, color );
  216. if (definition.nodesDefinition.lineMode == NodesDefinition.PATH || definition.nodesDefinition.lineMode == NodesDefinition.CIRCUIT)
  217. {
  218. lines.graphics.moveTo( 0, 0 );
  219. for ( i = 0; i < nodes.numChildren; i++ )
  220. {
  221. n = nodes.getChildAt( i ) as Node;
  222. lines.graphics.lineTo( n.x, n.y );
  223. }
  224. if (nodes.numChildren > 0 && definition.nodesDefinition.lineMode == NodesDefinition.CIRCUIT)
  225. lines.graphics.lineTo( 0, 0 );
  226. }
  227. else if (definition.nodesDefinition.lineMode == NodesDefinition.FAN)
  228. {
  229. for ( i = 0; i < nodes.numChildren; i++ )
  230. {
  231. n = nodes.getChildAt( i ) as Node;
  232. lines.graphics.moveTo( 0, 0 );
  233. lines.graphics.lineTo( n.x, n.y );
  234. }
  235. }
  236. }
  237. /* ========================== GETS/SETS ========================== */
  238. public function deepCopy():GameObject
  239. {
  240. var o:GameObject = new GameObject( layer, definition );
  241. o.x = x;
  242. o.y = y;
  243. o.setAngle( angle );
  244. o.setSize( objWidth, objHeight );
  245. var i:int;
  246. //Copy the values
  247. if (values)
  248. {
  249. for ( i = 0; i < values.length; i++ )
  250. o.values[ i ].value = values[ i ].value;
  251. }
  252. //Copy the nodes
  253. for ( i = 0; i < nodes.numChildren; i++ )
  254. {
  255. o.addNode( new Node( o, nodes.getChildAt( i ).x, nodes.getChildAt( i ).y ) );
  256. }
  257. return o;
  258. }
  259. public function get angle():Number
  260. {
  261. return holder.rotation;
  262. }
  263. public function set angle( to:Number ):void
  264. {
  265. holder.rotation = to;
  266. }
  267. public function set selected( to:Boolean ):void
  268. {
  269. _selected = to;
  270. if (to)
  271. {
  272. nodes.alpha = 1;
  273. lines.alpha = 1;
  274. selBox.setColor( SelBox.OBJECT_SELECTED );
  275. drawBG();
  276. }
  277. else
  278. {
  279. nodes.alpha = 0.4;
  280. lines.alpha = 0.4;
  281. selBox.setColor( SelBox.OBJECT_NOTSELECTED );
  282. clearBG();
  283. }
  284. }
  285. public function get selected():Boolean
  286. {
  287. return _selected;
  288. }
  289. public function get xml():XML
  290. {
  291. var xml:XML = <object/>;
  292. //basics
  293. xml.setName( definition.name );
  294. xml.@x = x;
  295. xml.@y = y;
  296. //Size if resizable
  297. if (definition.resizableX)
  298. xml.@width = objWidth;
  299. if (definition.resizableY)
  300. xml.@height = objHeight;
  301. //Angle if rotatable
  302. if (definition.rotatable)
  303. {
  304. if (definition.exportRadians)
  305. xml.@angle = Utils.degToRad( angle );
  306. else
  307. xml.@angle = angle;
  308. }
  309. //values
  310. Reader.writeValues( xml, values );
  311. //nodes
  312. for ( var i:int = 0; i < nodes.numChildren; i++ )
  313. {
  314. var node:XML = (nodes.getChildAt( i ) as Node).xml;
  315. xml.appendChild( node );
  316. }
  317. return xml;
  318. }
  319. public function set xml( to:XML ):void
  320. {
  321. var o:ObjectDefinition = Ogmo.project.getObjectDefinitionByName( to.name().localName );
  322. if (o)
  323. init( o );
  324. else
  325. throw new Error( "Object not defined: \"" + to.name().localName + "\"" );
  326. x = (int)(to.@x);
  327. y = (int)(to.@y);
  328. //Set the size
  329. var w:int, h:int;
  330. if (definition.resizableX)
  331. w = to.@width;
  332. else
  333. w = definition.width;
  334. if (definition.resizableY)
  335. h = to.@height;
  336. else
  337. h = definition.height;
  338. setSize( w, h );
  339. //Angle if rotatable
  340. if (definition.rotatable)
  341. {
  342. if (definition.exportRadians)
  343. angle = Utils.radToDeg( Number( to.@angle ) );
  344. else
  345. angle = Number( to.@angle );
  346. }
  347. //set the values
  348. Reader.readValues( to, values );
  349. //create the nodes
  350. for each ( var n:XML in to.node )
  351. addNode( new Node( this, n.@x, n.@y ) );
  352. }
  353. public function get rect():Rectangle
  354. {
  355. return new Rectangle( x - definition.originX, y - definition.originY, objWidth, objHeight );
  356. }
  357. }
  358. }