entity.monkey2 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. Namespace mojo3d
  2. #rem monkeydoc The Entity class.
  3. #end
  4. Class Entity Extends DynamicObject Abstract
  5. #rem monkeydoc Copied signal.
  6. Invoked after an entity is copied.
  7. #end
  8. Field Copied:Void( copy:Entity )
  9. #rem monkeydoc Destroyed signal.
  10. Invoked after an entity is destroyed.
  11. #end
  12. Field Destroyed:Void()
  13. #rem monkeydoc Hidden signal.
  14. Invoked after an entity is hidden.
  15. #end
  16. Field Hidden:Void()
  17. #rem monkeydoc Shown signal.
  18. Invoked after an entity is shown.
  19. #end
  20. Field Shown:Void()
  21. #rem monkeydoc Collided signal.
  22. After after an entity has collided with a rigidbody.
  23. #end
  24. Field Collided:Void( rigidBody:RigidBody )
  25. #rem monkeydoc Creates a new entity.
  26. #end
  27. Method New( parent:Entity=Null )
  28. _parent=parent
  29. If _parent
  30. _scene=_parent._scene
  31. _parent._children.Add( Self )
  32. Else
  33. _scene=Scene.GetCurrent()
  34. _scene.RootEntities.Add( Self )
  35. Endif
  36. Invalidate()
  37. End
  38. #rem monkeydoc Creates a copy of the entity.
  39. #end
  40. Method Copy:Entity( parent:Entity=Null ) Virtual
  41. Local copy:=OnCopy( parent )
  42. CopyTo( copy )
  43. Return copy
  44. End
  45. #rem monkeydoc Sequence id.
  46. The sequence id is an integer that is incremented whenever the entity's matrix is modified.
  47. #end
  48. Property Seq:Int()
  49. Return _seq
  50. End
  51. #rem monkeydoc Entity name.
  52. #end
  53. [jsonify=1]
  54. Property Name:String()
  55. Return _name
  56. Setter( name:String )
  57. _name=name
  58. End
  59. #rem monkeydoc @hidden
  60. #end
  61. Property Scene:Scene()
  62. Return _scene
  63. End
  64. #rem monkeydoc Parent entity.
  65. #end
  66. [jsonify=1]
  67. Property Parent:Entity()
  68. Return _parent
  69. Setter( parent:Entity )
  70. Assert( Not parent Or parent._scene=_scene )
  71. Local matrix:AffineMat4f=parent ? LocalMatrix Else Matrix
  72. If _parent
  73. _parent._children.Remove( Self )
  74. Else
  75. matrix=Matrix
  76. _scene.RootEntities.Remove( Self )
  77. Endif
  78. _parent=parent
  79. If _parent
  80. _parent._children.Add( Self )
  81. LocalMatrix=matrix
  82. Else
  83. _scene.RootEntities.Add( Self )
  84. Matrix=matrix
  85. Endif
  86. UpdateVisibility()
  87. End
  88. #rem monkeydoc Number of child entities.
  89. #end
  90. Property NumChildren:Int()
  91. Return _children.Length
  92. End
  93. #rem monkeydoc Array of child entities.
  94. #end
  95. Property Children:Entity[]()
  96. Return _children.ToArray()
  97. End
  98. #rem
  99. #rem monkeydoc Number of attached components.
  100. #end
  101. Property NumComponents:Int()
  102. Return _components.Length
  103. End
  104. #end
  105. #rem monkeydoc Array of attached components.
  106. #end
  107. Property Components:Component[]()
  108. Return _components.ToArray()
  109. End
  110. #rem monkeydoc Visibility flag.
  111. #end
  112. [jsonify=1]
  113. Property Visible:Bool()
  114. Return _visible
  115. Setter( visible:Bool )
  116. If visible=_visible Return
  117. _visible=visible
  118. UpdateVisibility()
  119. End
  120. #rem monkeydoc True if entity and all parents are visible.
  121. #end
  122. Property ReallyVisible:Bool()
  123. Return _rvisible
  124. End
  125. #rem monkeydoc Last copy.
  126. #end
  127. Property LastCopy:Entity()
  128. Return _lastCopy
  129. End
  130. #rem monkeydoc Master color.
  131. #end
  132. [jsonify=1]
  133. Property Color:Color()
  134. Return _color
  135. Setter( color:Color )
  136. _color=color
  137. End
  138. #rem monkeydoc Master alpha.
  139. #end
  140. [jsonify=1]
  141. Property Alpha:Float()
  142. Return _alpha
  143. Setter( alpha:Float )
  144. _alpha=alpha
  145. End
  146. '***** World space properties *****
  147. #rem monkeydoc World space transformation matrix.
  148. The world matrix combines the world position, basis matrix and scale of the entity into a single affine 3x4 matrix.
  149. #end
  150. Property Matrix:AffineMat4f()
  151. If _dirty & Dirty.W
  152. _W=_parent ? _parent.Matrix * LocalMatrix Else LocalMatrix
  153. _dirty&=~Dirty.W
  154. Endif
  155. Return _W
  156. Setter( matrix:AffineMat4f )
  157. Local scale:=matrix.m.GetScaling()
  158. Basis=matrix.m.Scale( 1/scale.x,1/scale.y,1/scale.z )
  159. Position=matrix.t
  160. Scale=scale
  161. End
  162. #rem monkeydoc Inverse world space transformation matrix.
  163. #end
  164. Property InverseMatrix:AffineMat4f()
  165. If _dirty & Dirty.IW
  166. _IW=-Matrix
  167. _dirty&=~Dirty.IW
  168. Endif
  169. Return _IW
  170. End
  171. #rem monkeydoc World space position.
  172. #end
  173. Property Position:Vec3f()
  174. Return Matrix.t
  175. Setter( position:Vec3f )
  176. _t=_parent ? _parent.InverseMatrix * position Else position
  177. Invalidate()
  178. End
  179. #rem monkeydoc World space basis matrix.
  180. A basis matrix is a 3x3 matrix representation of an orientation.
  181. A basis matrix is orthogonal (ie: the i,j,k members are perpendicular to each other) and normalized (ie: the i,j,k members all have unit length).
  182. #end
  183. Property Basis:Mat3f()
  184. Return _parent ? _parent.Basis * _r Else _r
  185. Setter( basis:Mat3f )
  186. _r=_parent ? ~_parent.Basis * basis Else basis
  187. Invalidate()
  188. End
  189. #rem monkeydoc World space scale.
  190. #end
  191. Property Scale:Vec3f()
  192. Return _parent ? _s * _parent.Scale Else _s
  193. Setter( scale:Vec3f )
  194. _s=_parent ? scale / _parent.Scale Else scale
  195. Invalidate()
  196. End
  197. '***** Local space properties *****
  198. #rem monkeydoc Local space transformation matrix.
  199. The local matrix combines the local position, orientation and scale of the entity into a single affine 4x4 matrix.
  200. #end
  201. [jsonify=1]
  202. Property LocalMatrix:AffineMat4f()
  203. If _dirty & Dirty.M
  204. _M=New AffineMat4f( _r.Scale( _s ),_t )
  205. _dirty&=~Dirty.M
  206. Endif
  207. Return _M
  208. Setter( matrix:AffineMat4f )
  209. Local scale:=matrix.m.GetScaling()
  210. LocalBasis=matrix.m.Scale( 1/scale.x,1/scale.y,1/scale.z )
  211. LocalPosition=matrix.t
  212. LocalScale=scale
  213. Invalidate()
  214. End
  215. #rem monkeydoc Local space position.
  216. #end
  217. Property LocalPosition:Vec3f()
  218. Return _t
  219. Setter( position:Vec3f )
  220. _t=position
  221. Invalidate()
  222. End
  223. #rem monkeydoc Local space basis matrix.
  224. A basis matrix is a 3x3 matrix representation of an orientation.
  225. A basis matrix is orthogonal (ie: the i,j,k members are perpendicular to each other) and normalized (ie: the i,j,k members all have unit length).
  226. #end
  227. Property LocalBasis:Mat3f()
  228. Return _r
  229. Setter( basis:Mat3f )
  230. _r=basis
  231. Invalidate()
  232. End
  233. #rem monkeydoc Local space scale.
  234. #end
  235. Property LocalScale:Vec3f()
  236. Return _s
  237. Setter( scale:Vec3f )
  238. _s=scale
  239. Invalidate()
  240. End
  241. #rem monkeydoc Finds an entity with the given name.
  242. #end
  243. Method Find:Entity( name:String )
  244. If _name=name Return Self
  245. For Local child:=Eachin _children
  246. Local found:=child.Find( name )
  247. If found Return found
  248. Next
  249. Return Null
  250. End
  251. #rem monkeydoc Destroys the entity and all of its children.
  252. #end
  253. Method Destroy()
  254. While Not _children.Empty
  255. _children.Top.Destroy()
  256. Wend
  257. While Not _components.Empty
  258. _components.Top.Destroy()
  259. Wend
  260. _visible=False
  261. UpdateVisibility()
  262. If _parent
  263. _parent._children.Remove( Self )
  264. Else
  265. _scene.RootEntities.Remove( Self )
  266. Endif
  267. _parent=Null
  268. _scene=Null
  269. Destroyed()
  270. End
  271. #rem monkeydoc Gets the number of components of a given type attached to the entity.
  272. #end
  273. Method NumComponents<T>:Int() Where T Extends Component
  274. Local n:=0
  275. For Local c:=Eachin _components
  276. If Cast<T>( c ) n+=1
  277. Next
  278. Return n
  279. End
  280. #rem monkeydoc Gets a component of a given type attached to the entity.
  281. If there is more than one component of the given type attached, the first is returned.
  282. #end
  283. Method GetComponent<T>:T() Where T Extends Component
  284. For Local c:=Eachin _components
  285. Local t:=Cast<T>( c )
  286. If t Return t
  287. Next
  288. Return Null
  289. End
  290. Method GetComponents<T>:T[]() Where T Extends Component
  291. Local cs:=New Component[NumComponents<T>()],i:=0
  292. For Local c:=Eachin _components
  293. Local t:=Cast<T>( c )
  294. If Not t Continue
  295. cs[i]=t
  296. i+=1
  297. Next
  298. Return cs
  299. End
  300. #rem monkeydoc Attaches a component to the entity.
  301. #end
  302. Method AddComponent<T>:T() Where T Extends Component
  303. Local c:=New T( Self )
  304. Return c
  305. End
  306. Protected
  307. #rem monkeydoc Copy constructor
  308. #end
  309. Method New( entity:Entity,parent:Entity )
  310. Self.New( parent )
  311. _name="Copy of "+entity._name
  312. _t=entity._t
  313. _r=entity._r
  314. _s=entity._s
  315. Invalidate()
  316. End
  317. Method OnCopy:Entity( parent:Entity ) Virtual
  318. RuntimeError( "Cannot copy Entity" )
  319. Return Null
  320. End
  321. #rem monkeydoc Invoked when entity transitions from hidden->visible.
  322. #end
  323. Method OnShow() Virtual
  324. End
  325. #rem monkeydoc Invoked when entity transitions from visible->hidden.
  326. #end
  327. Method OnHide() Virtual
  328. End
  329. #rem monkeydoc Helper method for copying an entity.
  330. 1) Recursively copies all child entities.
  331. 2) Invokes OnCopy for each component attached to this entity.
  332. 3) Copies visibility.
  333. 4) Invokes Copied signal.
  334. #end
  335. Method CopyTo( copy:Entity )
  336. _lastCopy=copy
  337. For Local child:=Eachin _children
  338. child.CopyTo( child.OnCopy( copy ) )
  339. Next
  340. 'should really be different pass...ie: ALL entities should be copied before ANY components?
  341. For Local c:=Eachin _components
  342. c.OnCopy( copy )
  343. Next
  344. copy.Visible=Visible
  345. copy.Alpha=Alpha
  346. Copied( copy )
  347. End
  348. Internal
  349. Method AddInstance()
  350. If _scene.Editing _scene.Jsonifier.AddInstance( Self,New Variant[]( _parent ) )
  351. End
  352. Method AddInstance( entity:Entity )
  353. If _scene.Editing _scene.Jsonifier.AddInstance( Self,New Variant[]( entity,_parent ) )
  354. End
  355. Method AddInstance( args:Variant[] )
  356. If _scene.Editing _scene.Jsonifier.AddInstance( Self,args )
  357. End
  358. Method AddComponent( c:Component )
  359. Local type:=c.Type
  360. For Local i:=0 Until _components.Length
  361. If type.Flags & ComponentTypeFlags.Singleton And _components[i].Type=type
  362. RuntimeError( "Duplicate component" )
  363. Endif
  364. If type.Priority>_components[i].Type.Priority
  365. _components.Insert( i,c )
  366. Return
  367. Endif
  368. Next
  369. _components.Add( c )
  370. End
  371. Method RemoveComponent( c:Component )
  372. _components.Remove( c )
  373. End
  374. 'bottom up
  375. Method BeginUpdate()
  376. For Local e:=Eachin _children
  377. e.BeginUpdate()
  378. Next
  379. For Local c:=Eachin _components
  380. c.OnBeginUpdate()
  381. Next
  382. End
  383. 'top down
  384. Method Update( elapsed:Float )
  385. For Local c:=Eachin _components
  386. c.OnUpdate( elapsed )
  387. End
  388. For Local e:=Eachin _children
  389. e.Update( elapsed )
  390. Next
  391. End
  392. Method Collide( body:RigidBody )
  393. For Local c:=Eachin _components
  394. c.OnCollide( body )
  395. Next
  396. Collided( body )
  397. End
  398. Private
  399. Enum Dirty
  400. M=1
  401. W=2
  402. IW=4
  403. All=7
  404. End
  405. Field _name:String
  406. Field _scene:Scene
  407. Field _parent:Entity
  408. Field _children:=New Stack<Entity>
  409. Field _components:=New Stack<Component>
  410. Field _lastCopy:Entity
  411. Field _rvisible:Bool
  412. Field _visible:Bool
  413. Field _color:Color=std.graphics.Color.White
  414. Field _alpha:Float=1
  415. Field _t:Vec3f=New Vec3f
  416. Field _r:Mat3f=New Mat3f
  417. Field _s:Vec3f=New Vec3f(1)
  418. Field _seq:Int=1
  419. Field _dirty:Dirty=Dirty.All
  420. Field _M:AffineMat4f
  421. Field _W:AffineMat4f
  422. Field _IW:AffineMat4f
  423. Method InvalidateWorld()
  424. If _dirty & Dirty.W Return
  425. _dirty|=Dirty.W|Dirty.IW
  426. For Local child:=Eachin _children
  427. child.InvalidateWorld()
  428. Next
  429. _seq+=1
  430. End
  431. Method Invalidate()
  432. _dirty|=Dirty.M
  433. InvalidateWorld()
  434. End
  435. Method UpdateVisibility()
  436. Local rvisible:=_visible And (Not _parent Or _parent._rvisible)
  437. If rvisible=_rvisible Return
  438. _rvisible=rvisible
  439. If _rvisible
  440. OnShow()
  441. For Local c:=Eachin _components
  442. c.OnShow()
  443. Next
  444. Else
  445. OnHide()
  446. For Local c:=Eachin _components
  447. c.OnHide()
  448. Next
  449. Endif
  450. For Local child:=Eachin _children
  451. child.UpdateVisibility()
  452. Next
  453. End
  454. End