entityexts.monkey2 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. Namespace mojo3d
  2. Private
  3. Const DegreesToRadians:=Pi/180.0
  4. Const RadiansToDegrees:=180.0/Pi
  5. Public
  6. #rem monkeydoc Utility extension methods for entities.
  7. #end
  8. Class Entity Extension
  9. #rem monkeydoc The rigid body attached to the entity.
  10. #end
  11. Property RigidBody:RigidBody()
  12. Return GetComponent<RigidBody>()
  13. End
  14. #rem monkeydoc The collider attached to the entity.
  15. #end
  16. Property Collider:Collider()
  17. Return GetComponent<Collider>()
  18. End
  19. #rem monkeydoc The animator attached to the entity.
  20. #end
  21. Property Animator:Animator()
  22. Return GetComponent<Animator>()
  23. End
  24. #rem monkeydoc World space rotation in degrees.
  25. #end
  26. Property Rotation:Vec3f()
  27. Return Basis.GetRotation() * RadiansToDegrees
  28. Setter( rotation:Vec3f )
  29. Basis=Mat3f.Rotation( rotation * DegreesToRadians )
  30. End
  31. #rem monkeydoc Local space rotation in degrees.
  32. #end
  33. Property LocalRotation:Vec3f()
  34. Return LocalBasis.GetRotation() * RadiansToDegrees
  35. Setter( rotation:Vec3f )
  36. LocalBasis=Mat3f.Rotation( rotation * DegreesToRadians )
  37. End
  38. #Rem monkeydoc World space rotation around the X axis in degrees.
  39. #End
  40. Property Rx:Float()
  41. Return Rotation.x
  42. Setter( rx:Float )
  43. Local r:=Rotation
  44. Rotation=New Vec3f( rx,r.y,r.z )
  45. End
  46. #Rem monkeydoc World space rotation around the Y axis in degrees.
  47. #End
  48. Property Ry:Float()
  49. Return Rotation.y
  50. Setter( ry:Float )
  51. Local r:=Rotation
  52. Rotation=New Vec3f( r.x,ry,r.z )
  53. End
  54. #Rem monkeydoc World space rotation around the Z axis in degrees.
  55. #End
  56. Property Rz:Float()
  57. Return Rotation.z
  58. Setter( rz:Float )
  59. Local r:=Rotation
  60. Rotation=New Vec3f( r.x,r.y,rz )
  61. End
  62. #Rem monkeydoc Local space rotation around the X axis in degrees.
  63. #End
  64. Property LocalRx:Float()
  65. Return LocalRotation.x
  66. Setter( rx:Float )
  67. Local r:=LocalRotation
  68. LocalRotation=New Vec3f( rx,r.y,r.z )
  69. End
  70. #Rem monkeydoc Local space rotation around the Y axis in degrees.
  71. #End
  72. Property LocalRy:Float()
  73. Return LocalRotation.y
  74. Setter( ry:Float )
  75. Local r:=LocalRotation
  76. LocalRotation=New Vec3f( r.x,ry,r.z )
  77. End
  78. #Rem monkeydoc Local space rotation around the Z axis in degrees.
  79. #End
  80. Property LocalRz:Float()
  81. Return LocalRotation.z
  82. Setter( rz:Float )
  83. Local r:=LocalRotation
  84. LocalRotation=New Vec3f( r.x,r.y,rz )
  85. End
  86. #rem monkeydoc World space X coordinate.
  87. #end
  88. Property X:Float()
  89. Return Position.x
  90. Setter( x:Float )
  91. Local v:=Position
  92. Position=New Vec3f( x,v.y,v.z )
  93. End
  94. #rem monkeydoc World space Y coordinate.
  95. #end
  96. Property Y:Float()
  97. Return Position.y
  98. Setter( y:Float )
  99. Local v:=Position
  100. Position=New Vec3f( v.x,y,v.z )
  101. End
  102. #rem monkeydoc World space Z coordinate.
  103. #end
  104. Property Z:Float()
  105. Return Position.z
  106. Setter( z:Float )
  107. Local v:=Position
  108. Position=New Vec3f( v.x,v.y,z )
  109. End
  110. #rem monkeydoc Local space X coordinate.
  111. #end
  112. Property LocalX:Float()
  113. Return LocalPosition.x
  114. Setter( x:Float )
  115. Local v:=LocalPosition
  116. LocalPosition=New Vec3f( x,v.y,v.z )
  117. End
  118. #rem monkeydoc Local space Y coordinate.
  119. #end
  120. Property LocalY:Float()
  121. Return LocalPosition.y
  122. Setter( y:Float )
  123. Local v:=LocalPosition
  124. LocalPosition=New Vec3f( v.x,y,v.z )
  125. End
  126. #rem monkeydoc Local space Z coordinate.
  127. #end
  128. Property LocalZ:Float()
  129. Return LocalPosition.z
  130. Setter( z:Float )
  131. Local v:=LocalPosition
  132. LocalPosition=New Vec3f( v.x,v.y,z )
  133. End
  134. #rem monkeydoc World space scale on the X axis.
  135. #end
  136. Property Sx:Float()
  137. Return Scale.x
  138. Setter( sx:Float )
  139. Local s:=Scale
  140. Scale=New Vec3f( sx,s.y,s.z )
  141. End
  142. #rem monkeydoc World space scale on the Y axis.
  143. #end
  144. Property Sy:Float()
  145. Return Scale.y
  146. Setter( sy:Float )
  147. Local s:=Scale
  148. Scale=New Vec3f( s.x,sy,s.z )
  149. End
  150. #rem monkeydoc World space scale on the Z axis.
  151. #end
  152. Property Sz:Float()
  153. Return Scale.z
  154. Setter( sz:Float )
  155. Local s:=Scale
  156. Scale=New Vec3f( s.x,s.y,sz )
  157. End
  158. #rem monkeydoc Local space scale on the X axis.
  159. #end
  160. Property LocalSx:Float()
  161. Return LocalScale.x
  162. Setter( sx:Float )
  163. Local s:=LocalScale
  164. LocalScale=New Vec3f( sx,s.y,s.z )
  165. End
  166. #rem monkeydoc Local space scale on the Y axis.
  167. #end
  168. Property LocalSy:Float()
  169. Return LocalScale.y
  170. Setter( sy:Float )
  171. Local s:=LocalScale
  172. LocalScale=New Vec3f( s.x,sy,s.z )
  173. End
  174. #rem monkeydoc Local space scale on the Z axis.
  175. #end
  176. Property LocalSz:Float()
  177. Return LocalScale.z
  178. Setter( sz:Float )
  179. Local s:=LocalScale
  180. LocalScale=New Vec3f( s.x,s.y,sz )
  181. End
  182. #rem monkeydoc Sets entity position in local or world space.
  183. #end
  184. Method SetPosition( position:Vec3f,localSpace:Bool=False )
  185. If localSpace LocalPosition=position Else Position=position
  186. End
  187. Method SetPosition( x:Float,y:Float,z:Float,localSpace:Bool=False )
  188. SetPosition( New Vec3f( x,y,z ),localSpace )
  189. End
  190. #rem monkeydoc Gets entity position in local or world space.
  191. #end
  192. Method GetPosition:Vec3f( localSpace:Bool=False )
  193. Return localSpace ? LocalPosition Else Position
  194. End
  195. #rem monkeydoc Sets entity basis matrix in local or world space.
  196. #end
  197. Method SetBasis( basis:Mat3f,localSpace:Bool=False )
  198. If localSpace LocalBasis=basis Else Basis=basis
  199. End
  200. #rem monkeydoc Gets entity basis matrix in local or world space.
  201. #end
  202. method GetBasis:Mat3f( localSpace:Bool=False )
  203. Return localSpace ? LocalBasis Else Basis
  204. End
  205. #rem monkeydoc Sets entity rotation in euler angles in local or world space.
  206. #end
  207. Method SetRotation( rotation:Vec3f,localSpace:Bool=False )
  208. Local basis:=Mat3f.Rotation( rotation * DegreesToRadians )
  209. If localSpace LocalBasis=basis Else Basis=basis
  210. End
  211. Method SetRotation( rx:Float,ry:Float,rz:Float,localSpace:Bool=False )
  212. SetRotation( New Vec3f( rx,ry,rz ),localSpace )
  213. End
  214. #rem monkeydoc Gets entity rotation in euler angles in local or world space.
  215. #end
  216. Method GetRotation:Vec3f( localSpace:Bool=False )
  217. Local basis:=localSpace ? LocalBasis Else Basis
  218. Return basis.GetRotation() * RadiansToDegrees
  219. End
  220. #rem monkeydoc Sets entity scale in local or world space.
  221. #end
  222. Method SetScale( scale:Vec3f,localSpace:Bool=False )
  223. If localSpace LocalScale=scale Else Scale=scale
  224. End
  225. Method SetScale( sx:Float,sy:Float,sz:Float,localSpace:Bool=False )
  226. SetScale( New Vec3f( sx,sy,sz ),localSpace )
  227. End
  228. #rem monkeydoc Gets entity scale in local or world space.
  229. #end
  230. Method GetScale:Vec3f( localSpace:Bool=False )
  231. Return localSpace ? LocalScale Else Scale
  232. End
  233. #rem monkeydoc Moves the entity.
  234. Moves the entity relative to its current orientation.
  235. #end
  236. Method Move( tv:Vec3f,localSpace:Bool=False )
  237. If localSpace LocalPosition+=tv Else Position+=Basis * tv
  238. End
  239. Method Move( tx:Float,ty:Float,tz:Float )
  240. Move( New Vec3f( tx,ty,tz ) )
  241. End
  242. #rem monkeydoc Moves the entity on the X axis.
  243. Moves the entity relative to its current orientation.
  244. #end
  245. Method MoveX( tx:Float,localSpace:Bool=False )
  246. If localSpace LocalX+=tx Else Position+=Basis.i * tx
  247. End
  248. #rem monkeydoc Moves the entity on the Y axis.
  249. Moves the entity relative to its current orientation.
  250. #end
  251. Method MoveY( ty:Float,localSpace:Bool=False )
  252. If localSpace LocalY+=ty Else Position+=Basis.j * ty
  253. End
  254. #rem monkeydoc Moves the entity on the Z axis.
  255. Moves the entity relative to its current orientation.
  256. #end
  257. Method MoveZ( tz:Float,localSpace:Bool=False )
  258. If localSpace LocalZ+=tz Else Position+=Basis.k * tz
  259. End
  260. #rem monkeydoc Rotates the entity.
  261. Rotates the entity.
  262. If `localSpace` is false, the rotation is applied after the entity's world rotation.
  263. If `localSpace` is true, the rotation is applied before the entity's local rotation.
  264. #end
  265. Method Rotate( rv:Vec3f,localSpace:Bool=False )
  266. Local basis:=Mat3f.Rotation( rv * DegreesToRadians )
  267. If localSpace LocalBasis*=basis Else Basis=basis*Basis
  268. End
  269. Method Rotate( rx:Float,ry:Float,rz:Float,localSpace:Bool=False )
  270. Rotate( New Vec3f( rx,ry,rz ),localSpace )
  271. End
  272. #rem monkeydoc Rotates the entity around the X axis.
  273. #end
  274. Method RotateX( rx:Float,localSpace:Bool=False )
  275. Local basis:=Mat3f.Pitch( rx * DegreesToRadians )
  276. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  277. End
  278. #rem monkeydoc Rotates the entity around the Y axis.
  279. #end
  280. Method RotateY( ry:Float,localSpace:Bool=False )
  281. Local basis:=Mat3f.Yaw( ry * DegreesToRadians )
  282. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  283. End
  284. #rem monkeydoc Rotates the entity around the Z axis.
  285. #end
  286. Method RotateZ( rz:Float,localSpace:Bool=False )
  287. Local basis:=Mat3f.Roll( rz * DegreesToRadians )
  288. If localSpace LocalBasis=basis*LocalBasis Else Basis*=basis
  289. End
  290. #rem monkeydoc Points the entity at a target.
  291. #end
  292. Method PointAt( target:Vec3f,up:Vec3f=New Vec3f( 0,1,0 ) )
  293. Local k:=(target-Position).Normalize()
  294. Local i:=up.Cross( k ).Normalize()
  295. Local j:=k.Cross( i )
  296. Basis=New Mat3f( i,j,k )
  297. End
  298. Method PointAt( target:Entity,up:Vec3f=New Vec3f( 0,1,0 ) )
  299. PointAt( target.LocalPosition )
  300. End
  301. End