spacechimps.monkey2 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.Enter
  58. If event.Modifiers & Modifier.Alt
  59. If Fullscreen EndFullscreen() Else BeginFullscreen()
  60. Endif
  61. Case Key.S
  62. SwapInterval=1-SwapInterval
  63. '#If __TARGET__<>"emscripten"
  64. Case Key.T
  65. If timer
  66. timer.Cancel()
  67. timer=Null
  68. App.RequestRender()
  69. Else
  70. timer=New Timer( 60,OnUpdate )
  71. Endif
  72. '#Endif
  73. Case Key.F
  74. filter=Not filter
  75. End
  76. End
  77. End
  78. Method OnWindowEvent( event:WindowEvent ) Override
  79. Select event.Type
  80. Case EventType.WindowMoved
  81. Case EventType.WindowResized
  82. App.RequestRender()
  83. Case EventType.WindowGainedFocus
  84. If timer timer.Suspended=False
  85. Case EventType.WindowLostFocus
  86. If timer timer.Suspended=True
  87. Default
  88. Super.OnWindowEvent( event )
  89. End
  90. End
  91. Method OnUpdate()
  92. App.RequestRender()
  93. 'rotate
  94. '
  95. If Keyboard.KeyDown( Key.Left )
  96. rot+=.1
  97. Else If Keyboard.KeyDown( Key.Right )
  98. rot-=.1
  99. Endif
  100. 'wrap rot to [-Pi,Pi)
  101. '
  102. rot=(rot+Pi*3) Mod TwoPi-Pi
  103. 'calc forward vector..
  104. '
  105. Local dir:=New Vec2f( Cos( rot ),-Sin( rot ) )
  106. 'thrust
  107. '
  108. If Keyboard.KeyDown( Key.Up )
  109. vel+=(dir * 5 - vel) *.025 'arcadey thruster
  110. ' vel+=dir * .03 'realistic...
  111. Else
  112. vel*=.999
  113. End
  114. 'add velocity to position
  115. '
  116. pos+=vel
  117. 'wrap pos to [0,size)
  118. '
  119. pos.x=(pos.x+Width) Mod Width
  120. pos.y=(pos.y+Height) Mod Height
  121. If Keyboard.KeyPressed( Key.Space )
  122. laser.Play()
  123. ' Print "Fire!"
  124. End
  125. If Keyboard.KeyReleased( Key.Space )
  126. ' Print "UnFire!"
  127. End
  128. End
  129. Field ms:=0
  130. Method OnRender( canvas:Canvas ) Override
  131. Local e:=App.Millisecs-ms 'ideally, e should be 16,17,17,16,17,17 ie: 16.6666...
  132. ' If e<>16 And e<>17 Print "elapsed="+e 'show glitches
  133. ms+=e
  134. If Not timer OnUpdate()
  135. 'Turn off texture filtering for a 'pixel art' look
  136. '
  137. canvas.TextureFilter=filter ? TextureFilter.Mipmap Else TextureFilter.Nearest
  138. canvas.DrawText( "FPS="+App.FPS,Width/2,0,.5,0 )
  139. canvas.DrawText( "Arrow keys to fly",Width/2,16,.5,0 )
  140. canvas.DrawText( "Swap interval="+SwapInterval +" ('S' to toggle)",Width/2,32,.5,0 )
  141. canvas.DrawText( "Timer sync="+(timer ? "true" Else "false")+" ('T' to toggle)",Width/2,48,.5,0 )
  142. canvas.DrawText( "Filtering="+(filter ? "true" Else "false")+" ('F' to toggle)",Width/2,64,.5,0 )
  143. 'Draw image
  144. '
  145. Local x:=pos.x,y:=pos.y,r:=rot-Pi/2
  146. ' If Not filter x=Round( x ) ; y=Round( y )
  147. canvas.DrawImage( image,x,y,r )
  148. 'Draw wrap around(s)
  149. '
  150. If x-image.Radius<0 canvas.DrawImage( image,x+Width,y,r )
  151. If x+image.Radius>Width canvas.DrawImage( image,x-Width,y,r )
  152. If y-image.Radius<0 canvas.DrawImage( image,x,y+Height,r )
  153. If y+image.Radius>Height canvas.DrawImage( image,x,y-Height,r )
  154. If Mouse.ButtonDown( MouseButton.Left )
  155. canvas.Color=Color.Green
  156. canvas.DrawCircle( Mouse.X,Mouse.Y,16 )
  157. Endif
  158. End
  159. Method OnMeasure:Vec2i() Override
  160. Return New Vec2i( VirtualWidth,VirtualHeight )
  161. End
  162. End
  163. Function Main()
  164. New AppInstance
  165. New MyWindow( "Chimps in Space!",640,480 )
  166. App.Run()
  167. End