entity.monkey2 11 KB

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