castle.monkey2 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. Namespace myapp
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "<mojo3d>"
  5. #Import "<mojo3d-loaders>"
  6. #Import "assets/castle/@/castle"
  7. #Import "../../mojo3d/tests/assets/terrain_256.png"
  8. #Import "../../mojo3d/tests/assets/mossy.pbr@/mossy.pbr"
  9. '#Import "../../mojo3d/tests/assets/miramar-skybox.jpg"
  10. #Import "util"
  11. Using std..
  12. Using mojo..
  13. Using mojo3d..
  14. Struct QResult
  15. Field position:Vec3f
  16. Field hitground:Bool
  17. Field hitwall:Bool
  18. End
  19. Function QCollide:QResult( collider:ConvexCollider,src:Vec3f,dst:Vec3f,moving:Bool )
  20. Local margin:=collider.Margin
  21. Local world:=collider.Entity.Scene.World
  22. Local start:=src
  23. Local plane0:Planef,plane1:Planef,state:=0,casts:=0
  24. Local qresult:QResult
  25. Local debug:=""
  26. Repeat
  27. If src.Distance( dst )<.00001
  28. dst=src
  29. Exit
  30. Endif
  31. casts+=1
  32. Local cresult:=world.ConvexSweep( collider,src,dst,1 )
  33. If Not cresult Exit
  34. ' debug+=", "
  35. If cresult.normal.y>.7'9
  36. qresult.hitground=True
  37. Endif
  38. If cresult.normal.y<.1
  39. qresult.hitwall=True
  40. Endif
  41. Local plane:=New Planef( cresult.point,cresult.normal )
  42. plane.d-=margin
  43. Local d0:=plane.Distance( src ),d1:=plane.Distance( dst )
  44. Local tline:=New Linef( src,dst-src )
  45. Local t:=plane.TIntersect( tline )
  46. If t>0
  47. src=tline * t
  48. Endif
  49. If Not moving Or t>=1
  50. dst=src
  51. Exit
  52. Endif
  53. Select state
  54. Case 0
  55. Local tdst:=plane.Nearest( dst )
  56. If plane.n.y<.1 And tdst.y>src.y
  57. dst=src
  58. Exit
  59. Local t:=(tdst.y-src.y)/(tdst-src).Length
  60. tdst=(tdst-src)*t+src
  61. Print "Here!"
  62. Endif
  63. dst=tdst
  64. plane0=plane
  65. state=1
  66. Case 1
  67. Local v:=plane0.n.Cross( plane.n )
  68. If v.Length>.00001
  69. Local groove:=New Linef( src,v )
  70. dst=groove.Nearest( dst )
  71. plane1=plane
  72. state=2
  73. Else
  74. Print "QCollide OOPS2"
  75. dst=src
  76. Exit
  77. Endif
  78. Case 2
  79. dst=src
  80. Exit
  81. End
  82. Forever
  83. If casts>3 Print debug.Slice( 2 )+"QCOLLIDE OOPS3 casts="+casts
  84. qresult.position=dst
  85. Return qresult
  86. End
  87. Class CharacterController Extends Behaviour
  88. Method New( entity:Entity )
  89. Super.New( entity )
  90. End
  91. Property OnGround:Bool()
  92. Return _onground
  93. End
  94. Property OnWall:Bool()
  95. Return _onwall
  96. End
  97. Property StepDown:Float()
  98. Return _stepDown
  99. Setter( stepDown:Float )
  100. _stepDown=stepDown
  101. End
  102. Protected
  103. Field _jumping:Bool
  104. Field _stepDown:Float=.5 '50 cms
  105. Field _onground:Bool
  106. Field _onwall:Bool
  107. Field _vel:Vec3f
  108. Method OnUpdate( elapsed:Float ) Override
  109. If Keyboard.KeyDown( Key.Left )
  110. Entity.RotateY( 2.5 )
  111. Else If Keyboard.KeyDown( Key.Right )
  112. Entity.RotateY( -2.5 )
  113. Endif
  114. Local src:=Entity.Position
  115. Local moving:=False
  116. If Keyboard.KeyDown( Key.A )
  117. Entity.MoveZ( .15 )
  118. moving=True
  119. Else If Keyboard.KeyDown( Key.Z )
  120. Entity.MoveZ( -.15 )
  121. moving=True
  122. Endif
  123. If _onground _vel.y=-Entity.Collider.Margin
  124. _vel+=Entity.Scene.World.Gravity/60.0/60.0
  125. If Keyboard.KeyHit( Key.Space )
  126. _jumping=True
  127. _vel.y=.125
  128. Endif
  129. Entity.Move( _vel )
  130. Local dst:=Entity.Position
  131. Local qres:=QCollide( Cast<ConvexCollider>( Entity.Collider ),src,dst,True )'moving Or Not _onground )
  132. dst=qres.position
  133. #rem
  134. If Not _jumping And Not qres.hitground And _onground
  135. src=dst
  136. dst.y-=_stepDown
  137. qres=QCollide( Cast<ConvexCollider>( Entity.Collider ),src,dst,False )
  138. dst=qres.position
  139. Endif
  140. #end
  141. _onground=qres.hitground
  142. _onwall=qres.hitwall
  143. If _onground
  144. _jumping=False
  145. _vel.y=0
  146. Else
  147. _vel.y=dst.y-src.y
  148. Endif
  149. Entity.Position=dst
  150. End
  151. End
  152. Class MyWindow Extends Window
  153. Field _scene:Scene
  154. Field _player:Model
  155. Field _camera:Camera
  156. Field _light:Light
  157. Field _godrays:GodraysEffect
  158. Method CreateTerrain:Model()
  159. Local box:=New Boxf( -256,-32,-256,256,0,256 )
  160. Local hmap:=Pixmap.Load( "asset::terrain_256.png",PixelFormat.I8 )
  161. Local material:=PbrMaterial.Load( "asset::mossy.pbr" )
  162. material.ScaleTextureMatrix( 64,64 )
  163. 'model+mesh
  164. Local model:=Model.CreateTerrain( hmap,box,material )
  165. model.CastsShadow=False
  166. Local collider:=model.AddComponent<TerrainCollider>()
  167. collider.Heightmap=hmap
  168. collider.Bounds=box
  169. Local body:=model.AddComponent<RigidBody>()
  170. body.Mass=0
  171. Return model
  172. End
  173. Method CreateCastle:Model()
  174. Local box:=New Boxf( -10000,0,-10000,10000,30,10000 )
  175. Local model:=Model.Load( "asset::castle/CASTLE1.X" )
  176. model.Mesh.FitVertices( box,True )
  177. Local collider:=model.AddComponent<MeshCollider>()
  178. collider.Mesh=model.Mesh
  179. Local body:=model.AddComponent<RigidBody>()
  180. body.Mass=0
  181. Return model
  182. End
  183. Method CreatePlayer:Model()
  184. Local radius:=.25,length:=1.25,segs:=12
  185. Local material:=New PbrMaterial( Color.Green )
  186. Local model:=Model.CreateCapsule( radius,length,Axis.Y,segs,material )
  187. model.Move( 0,10,0 )
  188. Local collider:=model.AddComponent<CapsuleCollider>()
  189. collider.Radius=radius
  190. collider.Length=length
  191. collider.Axis=Axis.Y
  192. ' Local body:=_player.AddComponent<RigidBody>()
  193. ' body.Kinematic=True
  194. Local controller:=model.AddComponent<CharacterController>()
  195. Return model
  196. End
  197. Method CreateScene:Scene()
  198. _scene=New Scene
  199. _scene.AmbientLight=Color.Black
  200. _scene.ClearColor=Color.Sky
  201. ' _scene.EnvColor=Color.Black
  202. _scene.ShadowAlpha=1
  203. Local terrain:=CreateTerrain()
  204. Local castle:=CreateCastle()
  205. _player=CreatePlayer()
  206. _camera=New Camera( _player )
  207. _camera.View=Self
  208. _camera.LocalPosition=New Vec3f( 0,.5,0 )
  209. _camera.RotateX( 45 )
  210. _camera.Move( 0,0,-2 )
  211. ' Return _scene
  212. _light=New Light
  213. _light.Rotate( 60,30,0 ) 'aim directional light 'down' - Pi/2=90 degrees.
  214. _light.CastsShadow=True
  215. _godrays=New GodraysEffect
  216. _godrays.Light=_light
  217. _scene.AddPostEffect( _godrays )
  218. Return _scene
  219. End
  220. Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
  221. Super.New( title,width,height,flags )
  222. CreateScene()
  223. End
  224. Method OnRender( canvas:Canvas ) Override
  225. RequestRender()
  226. _scene.Update()
  227. If Keyboard.KeyDown( Key.Up )
  228. _camera.RotateX( 1,True )
  229. Else If Keyboard.KeyDown( Key.Down )
  230. _camera.RotateX( -1,True )
  231. Endif
  232. _scene.Render( canvas )
  233. Local controller:=_player.GetComponent<CharacterController>()
  234. ' canvas.DrawText( "y="+_player.Position.y+" onground="+controller.OnGround+" FPS="+App.FPS,0,0 )
  235. canvas.DrawText( " onground="+controller.OnGround+" onwall="+controller.OnWall+" FPS="+App.FPS,0,0 )
  236. End
  237. End
  238. Function Main()
  239. Print RealPath( "desktop::hello.png" )
  240. New AppInstance
  241. New MyWindow
  242. App.Run()
  243. End