entity.monkey2 11 KB

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