test.monkey2 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. Namespace test
  2. #Reflect test
  3. #Import "<std>"
  4. #Import "jsonifier"
  5. #Import "invocation"
  6. #Import "jsonifierexts"
  7. #Import "comparejson"
  8. Using std..
  9. Using jsonifier..
  10. Global editing:=True
  11. Global jsonifier:=New Jsonifier
  12. Class Component
  13. 'simple contructor
  14. Method New( entity:Entity )
  15. _entity=entity
  16. _entity.AddComponent( Self )
  17. End
  18. 'copy contructor
  19. Method New( component:Component,entity:Entity )
  20. Self.New( entity )
  21. End
  22. Property Entity:Entity()
  23. Return _entity
  24. End
  25. Protected
  26. Method OnCopy:Component( entity:Entity ) Abstract
  27. Method SaveInitialState()
  28. If editing jsonifier.AddInstance( Self,New Variant[]( _entity ) )
  29. End
  30. Method SaveInitialState( component:Component )
  31. If editing jsonifier.AddInstance( Self,New Variant[]( component,_entity ) )
  32. End
  33. Private
  34. Field _entity:Entity
  35. End
  36. Class Behaviour Extends Component
  37. 'simple contructor
  38. Method New( entity:Entity )
  39. Super.New( entity )
  40. Color=graphics.Color.White
  41. SaveInitialState()
  42. End
  43. 'copy constructor
  44. Method New( behaviour:Behaviour,entity:Entity )
  45. Super.New( entity )
  46. Color=behaviour.Color
  47. SaveInitialState( behaviour )
  48. End
  49. Property Color:Color()
  50. Return _color
  51. Setter( color:Color )
  52. _color=color
  53. End
  54. Internal
  55. Method OnCopy:Behaviour( entity:Entity ) Override
  56. Return New Behaviour( Self,entity )
  57. End
  58. Private
  59. Field _color:Color
  60. End
  61. Class Entity
  62. 'simple ctor
  63. Method New( parent:Entity )
  64. _parent=parent
  65. If _parent _parent._children.Add( Self )
  66. End
  67. 'copy ctor
  68. Method New( entity:Entity,parent:Entity )
  69. Self.New( parent )
  70. End
  71. Property Visible:Bool()
  72. Return _visible
  73. Setter( visible:Bool )
  74. _visible=visible
  75. End
  76. Method Copy:Entity( parent:Entity ) Virtual
  77. Local copy:=OnCopy( parent )
  78. CopyTo( copy )
  79. Return copy
  80. End
  81. Method AddComponent<T>:T()
  82. Local component:=New T( Self )
  83. Return component
  84. End
  85. Protected
  86. Method OnCopy:Entity( parent:Entity ) Virtual
  87. Return New Entity( Self,parent )
  88. End
  89. Method CopyTo( copy:Entity )
  90. For Local child:=Eachin _children
  91. child.CopyTo( child.OnCopy( copy ) )
  92. Next
  93. For Local c:=Eachin _components
  94. c.OnCopy( copy )
  95. Next
  96. End
  97. Method SaveInitialState()
  98. If editing jsonifier.AddInstance( Self,New Variant[]( _parent ) )
  99. End
  100. Method SaveInitialState( entity:Entity )
  101. If editing jsonifier.AddInstance( Self,New Variant[]( entity,_parent ) )
  102. End
  103. Private
  104. Field _parent:Entity
  105. Field _children:=New Stack<Entity>
  106. Field _visible:Bool
  107. Field _components:=New Stack<Component>
  108. Method AddComponent( component:Component )
  109. _components.Add( component )
  110. End
  111. End
  112. Class Camera Extends Entity
  113. Method New( parent:Entity )
  114. Super.New( parent )
  115. FOV=90
  116. SaveInitialState()
  117. Visible=True
  118. End
  119. Method New( camera:Camera,parent:Entity )
  120. Super.New( camera,parent )
  121. FOV=camera.FOV
  122. SaveInitialState( camera )
  123. Visible=True
  124. End
  125. Property FOV:Float()
  126. Return _fov
  127. Setter( fov:Float )
  128. _fov=fov
  129. End
  130. Method Copy:Camera( parent:Entity ) Override
  131. Local camera:=OnCopy( parent )
  132. CopyTo( camera )
  133. Return camera
  134. End
  135. Protected
  136. Method OnCopy:Camera( parent:Entity ) Override
  137. Return New Camera( Self,parent )
  138. End
  139. Private
  140. Field _fov:Float
  141. End
  142. Class Light Extends Entity
  143. Method New( parent:Entity )
  144. Super.New( parent )
  145. SaveInitialState()
  146. Visible=True
  147. End
  148. Method New( light:Light,parent:Entity )
  149. Super.New( parent )
  150. SaveInitialState( light )
  151. Visible=True
  152. End
  153. Method Copy:Light( parent:Entity ) Override
  154. Local light:=OnCopy( parent )
  155. CopyTo( light )
  156. Return light
  157. End
  158. Protected
  159. Method OnCopy:Light( parent:Entity ) Override
  160. Return New Light( Self,parent )
  161. End
  162. End
  163. Class Model Extends Entity
  164. Method New( parent:Entity )
  165. Super.New( parent )
  166. Mesh=""
  167. SaveInitialState()
  168. Visible=True
  169. End
  170. Method New( model:Model,parent:Entity )
  171. Super.New( parent )
  172. Mesh=model.Mesh
  173. SaveInitialState( model )
  174. Visible=True
  175. End
  176. Property Mesh:String()
  177. Return _mesh
  178. Setter( mesh:String )
  179. _mesh=mesh
  180. End
  181. Function Load:Model( path:String,parent:Entity )
  182. Local model:=New Model( parent,True )
  183. model.Mesh="<"+path+">"
  184. If editing jsonifier.AddInstance( model,"Load",New Variant[]( path,parent ) )
  185. Return model
  186. End
  187. Method Copy:Model( parent:Entity ) Override
  188. Local model:=OnCopy( parent )
  189. CopyTo( model )
  190. Return model
  191. End
  192. Protected
  193. Method OnCopy:Model( parent:Entity ) Override
  194. Return New Model( Self,parent )
  195. End
  196. Private
  197. Field _mesh:String
  198. Method New( parent:Entity,loading:Bool )
  199. Super.New( parent )
  200. End
  201. End
  202. Function CreateScene()
  203. Print "CreateScene"
  204. jsonifier=New Jsonifier
  205. Local model1:=Model.Load( "model1.png",Null )
  206. Local behavour1:=New Behaviour( model1 )
  207. Local model2:=model1.Copy( Null )
  208. End
  209. Function CreateScene2()
  210. Print "CreateScene"
  211. jsonifier=New Jsonifier
  212. Local camera:=New Camera( Null )
  213. Local light:=New Light( Null )
  214. Local root:=Model.Load( "blah.txt",Null )
  215. For Local i:=0 Until 3
  216. Local model:=New Model( root )
  217. Local component:=New Behaviour( model )
  218. Next
  219. Local copy:=root.Copy( Null )
  220. End
  221. Function SaveScene:JsonObject()
  222. Print "SaveScene"
  223. Local jobj:=jsonifier.JsonifyInstances()
  224. Return jobj
  225. End
  226. Function LoadScene( jobj:JsonObject )
  227. Print "LoadScene"
  228. jsonifier=New Jsonifier
  229. jsonifier.DejsonifyInstances( jobj )
  230. End
  231. Function Main()
  232. CreateScene()
  233. Local saved1:=SaveScene()
  234. LoadScene( saved1 )
  235. Local saved2:=SaveScene()
  236. If CompareJson( saved1,saved2 )=0
  237. Print saved1.ToJson()+"~nSuccess!"
  238. Else
  239. Print "saved1:~n"+saved1.ToJson()+"~nsaved2:~n"+saved2.ToJson()+"~nError!"
  240. Endif
  241. End