spacechimps.monkey2 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. Namespace spacechimps
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "assets/bang.wav"
  5. #Import "assets/spaceship_32.png"
  6. Using std..
  7. Using mojo..
  8. Const VirtualWidth:=320
  9. Const VirtualHeight:=240
  10. Class MyWindow Extends Window
  11. Field timer:Timer
  12. Field laser:Sound
  13. Field image:Image
  14. Field pos:=New Vec2f
  15. Field vel:=New Vec2f
  16. Field rot:Float
  17. Field filter:Bool=True
  18. Method New( title:String,width:Int,height:Int )
  19. 'Call super class constructor - this just passes the arguments 'up' to the Window class constructor.
  20. '
  21. Super.New( title,width,height )
  22. ' Style.Border=New Recti( -16,-16,16,16 )
  23. ' Style.BorderColor=Color.Yellow
  24. ' Layout="fill"
  25. Layout="letterbox"
  26. 'Letterbox color
  27. '
  28. ClearColor=New Color( .03,.03,.03 )
  29. 'Black 'coz we're in space!
  30. '
  31. Style.BackgroundColor=Color.Black
  32. 'Load laser sound effecy
  33. '
  34. laser=Sound.Load( "asset::bang.wav" )
  35. If Not laser Print "Couldn't load laser"
  36. 'Load and setup our image...
  37. '
  38. 'Note: Scaling image here is faster than scaling in DrawImage.
  39. '
  40. image=Image.Load( "asset::spaceship_32.png" )
  41. image.Handle=New Vec2f( .5 )
  42. ' image.TextureFilter=TextureFilter.Mipmap
  43. 'Set initial image pos
  44. '
  45. pos=New Vec2f( VirtualWidth/2,VirtualHeight/2 )
  46. 'Start update timer
  47. '
  48. ' timer=New Timer( 60,OnUpdate )
  49. 'Vwait always recommended...
  50. '
  51. SwapInterval=1
  52. End
  53. Method OnKeyEvent( event:KeyEvent ) Override
  54. Select event.Type
  55. Case EventType.KeyDown
  56. Select event.Key
  57. Case Key.S
  58. SwapInterval=1-SwapInterval
  59. '#If __TARGET__<>"emscripten"
  60. Case Key.T
  61. If timer
  62. timer.Cancel()
  63. timer=Null
  64. App.RequestRender()
  65. Else
  66. timer=New Timer( 60,OnUpdate )
  67. Endif
  68. '#Endif
  69. Case Key.F
  70. filter=Not filter
  71. End
  72. End
  73. End
  74. Method OnWindowEvent( event:WindowEvent ) Override
  75. Select event.Type
  76. Case EventType.WindowMoved
  77. Case EventType.WindowResized
  78. App.RequestRender()
  79. Case EventType.WindowGainedFocus
  80. If timer timer.Suspended=False
  81. Case EventType.WindowLostFocus
  82. If timer timer.Suspended=True
  83. Default
  84. Super.OnWindowEvent( event )
  85. End
  86. End
  87. Method OnUpdate()
  88. App.RequestRender()
  89. 'rotate
  90. '
  91. If Keyboard.KeyDown( Key.Left )
  92. rot+=.1
  93. Else If Keyboard.KeyDown( Key.Right )
  94. rot-=.1
  95. Endif
  96. 'wrap rot to [-Pi,Pi)
  97. '
  98. rot=(rot+Pi*3) Mod TwoPi-Pi
  99. 'calc forward vector..
  100. '
  101. Local dir:=New Vec2f( Cos( rot ),-Sin( rot ) )
  102. 'thrust
  103. '
  104. If Keyboard.KeyDown( Key.Up )
  105. vel+=(dir * 5 - vel) *.025 'arcadey thruster
  106. ' vel+=dir * .03 'realistic...
  107. Else
  108. vel*=.999
  109. End
  110. 'add velocity to position
  111. '
  112. pos+=vel
  113. 'wrap pos to [0,size)
  114. '
  115. pos.x=(pos.x+Width) Mod Width
  116. pos.y=(pos.y+Height) Mod Height
  117. If Keyboard.KeyPressed( Key.Space )
  118. laser.Play()
  119. ' Print "Fire!"
  120. End
  121. If Keyboard.KeyReleased( Key.Space )
  122. ' Print "UnFire!"
  123. End
  124. End
  125. Field ms:=0
  126. Method OnRender( canvas:Canvas ) Override
  127. Local e:=App.Millisecs-ms 'ideally, e should be 16,17,17,16,17,17 ie: 16.6666...
  128. ' If e<>16 And e<>17 Print "elapsed="+e 'show glitches
  129. ms+=e
  130. If Not timer OnUpdate()
  131. 'Turn off texture filtering for a 'pixel art' look
  132. '
  133. canvas.TextureFilter=filter ? TextureFilter.Mipmap Else TextureFilter.Nearest
  134. canvas.DrawText( "FPS="+App.FPS,Width/2,0,.5,0 )
  135. canvas.DrawText( "Arrow keys to fly",Width/2,16,.5,0 )
  136. canvas.DrawText( "Swap interval="+SwapInterval +" ('S' to toggle)",Width/2,32,.5,0 )
  137. canvas.DrawText( "Timer sync="+(timer ? "true" Else "false")+" ('T' to toggle)",Width/2,48,.5,0 )
  138. canvas.DrawText( "Filtering="+(filter ? "true" Else "false")+" ('F' to toggle)",Width/2,64,.5,0 )
  139. 'Draw image
  140. '
  141. Local x:=pos.x,y:=pos.y,r:=rot-Pi/2
  142. ' If Not filter x=Round( x ) ; y=Round( y )
  143. canvas.DrawImage( image,x,y,r )
  144. 'Draw wrap around(s)
  145. '
  146. If x-image.Radius<0 canvas.DrawImage( image,x+Width,y,r )
  147. If x+image.Radius>Width canvas.DrawImage( image,x-Width,y,r )
  148. If y-image.Radius<0 canvas.DrawImage( image,x,y+Height,r )
  149. If y+image.Radius>Height canvas.DrawImage( image,x,y-Height,r )
  150. If Mouse.ButtonDown( MouseButton.Left )
  151. canvas.Color=Color.Green
  152. canvas.DrawCircle( Mouse.X,Mouse.Y,16 )
  153. Endif
  154. End
  155. Method OnMeasure:Vec2i() Override
  156. Return New Vec2i( VirtualWidth,VirtualHeight )
  157. End
  158. End
  159. Function Main()
  160. New AppInstance
  161. New MyWindow( "Chimps in Space!",640,480 )
  162. App.Run()
  163. End