additional tests for `do catch` structure
[nit.git] / examples / shoot / src / shoot.nit
index 9612992..fd94447 100644 (file)
 
 # Space shooter.
 # This program is a fun game but also a good example of the scene2d module
-module shoot
+module shoot is
+       app_name("Space Shooter")
+       app_version(0, 1, git_revision)
+end
 
 import mnit
 import shoot_logic
@@ -23,18 +26,18 @@ redef class Sprite
        # mnit specific method to draw a sprite
        # app is used to optain the assets and the display
        # Each sprite should implements this method
-       fun draw_on_display(app: ShootApp) do end
+       fun draw_on_display(app: App) do end
 
        # Helper function to draw an image centered on the current sprite position
-       fun draw_image(app: ShootApp, img: Image)
+       fun draw_image(app: App, img: Image)
        do
-               app.display.blit_centered(img, self.x/100, self.y/100)
+               app.display.blit_centered(img, (self.x.to_f/app.scale).to_i, (self.y.to_f/app.scale).to_i)
        end
 
        # Helper function to draw an image translated and rotated on the current sprite position
-       fun draw_rotated_image(app: ShootApp, img: Image, dx, dy: Int, angle: Float)
+       fun draw_rotated_image(app: App, img: Image, dx, dy: Int, angle: Float)
        do
-               app.display.blit_rotated(img, self.x.to_f/100.0, self.y.to_f/100.0, angle)
+               app.display.blit_rotated(img, self.x.to_f/app.scale, self.y.to_f/app.scale, angle)
        end
 end
 
@@ -166,8 +169,8 @@ redef class Star
 end
 
 redef class Scene
-       fun draw_on_display(app: ShootApp) do end
-       fun input(input_event: InputEvent): Bool do return false
+       fun draw_on_display(app: App) do end
+       fun input(app: App, input_event: InputEvent): Bool do return false
 end
 
 redef class PlayScene
@@ -177,19 +180,19 @@ redef class PlayScene
                self.sprites.draw(app)
                for i in [0..player.money[
                do
-                       app.display.blit(app.img_money, 10, 590-i)
+                       app.display.blit(app.img_money, 10, app.display.height-10-i)
                end
                for i in [1..player.nbshoots]
                do
-                       app.display.blit(app.img_player_shoot, 30, 590 - i*10)
+                       app.display.blit(app.img_player_shoot, 30, app.display.height-10 - i*10)
                end
                for i in [1..player.nbmissiles]
                do
-                       app.display.blit(app.img_player_missile, 40, 590 - i*20)
+                       app.display.blit(app.img_player_missile, 40, app.display.height-10 - i*20)
                end
        end
 
-       redef fun input( input_event )
+       redef fun input(app, input_event)
        do
                var speed = 400
                if input_event isa KeyEvent then
@@ -219,8 +222,10 @@ redef class PlayScene
                                end
                        end
                        return true
-               else if input_event isa PointerEvent then
-                       player.goes_to((input_event.x*100.0).to_i, (input_event.y*100.0).to_i, speed)
+               else if input_event isa PointerEvent and input_event.pressed then
+                       var x = (input_event.x * app.scale).to_i
+                       var y = (input_event.y * app.scale).to_i
+                       player.goes_to(x, y, speed)
                        return true
                end
                return false # unknown event, can be handled by something else
@@ -232,14 +237,23 @@ end
 redef class MenuScene
        redef fun draw_on_display(app)
        do
-               app.display.blit(app.img_splash, 0, 0)
+               var display = app.display
+               assert display != null
+               blit_fs(display, app.img_splash)
                sprites.draw(app)
                if not play or ttl%10 > 5 then
-                       app.display.blit(app.img_splash_play, 0, 0)
+                       blit_fs(display, app.img_splash_play)
                end
        end
 
-       redef fun input(input_event)
+       fun blit_fs(d: Display, img: Image)
+       do
+               var w = d.width.to_f
+               var h = d.height.to_f
+               d.blit_stretched(img, 0.0,0.0, 0.0,h, w,h, w,0.0)
+       end
+
+       redef fun input(app, input_event)
        do
                if input_event isa KeyEvent then
                        play = true
@@ -254,8 +268,7 @@ end
 
 ###
 
-class ShootApp
-       super App
+redef class App
        super View
 
        var debug: Bool = false
@@ -264,17 +277,15 @@ class ShootApp
        do
                s.draw_on_display(self)
                if debug and s.width != 0 and s.height != 0 then
-                       var left = s.left.to_f/100.0
-                       var right = s.right.to_f/100.0
-                       var top = s.top.to_f/100.0
-                       var bot = s.bottom.to_f/100.0
+                       var left = s.left.to_f/scale
+                       var right = s.right.to_f/scale
+                       var top = s.top.to_f/scale
+                       var bot = s.bottom.to_f/scale
                        display.blit_stretched(img_hitbox, right, top, right, bot, left, bot, left, top)
                end
        end
 
-       init do super
-
-       var scene: Scene
+       var scene: ShotScene
 
        var img_hitbox: Image
 
@@ -304,10 +315,14 @@ class ShootApp
        var img_boss_left: Image
        var img_boss_right: Image
 
-       redef fun init_window
+       redef fun on_create
        do
                super
 
+               scale = (800.0 * 600.0 / display.width.to_f / display.height.to_f).sqrt * 100.0
+
+               debug = args.length > 0 and args.first == "--debug"
+
                # TODO load assets here
                # ex: img = load_image( "img.png" )
                #     to get file located at assets/img.png before deployement
@@ -338,7 +353,19 @@ class ShootApp
                self.img_boss_left = load_image("boss_left.png")
                self.img_boss_right = load_image("boss_right.png")
 
-               self.scene = new MenuScene
+               var w = (display.width.to_f * scale).to_i
+               var h = (display.height.to_f * scale).to_i
+               self.scene = new MenuScene(w, h)
+       end
+
+       # Whole scaling to convert display pixels to game pixels
+       var scale: Float = 200.0
+
+       redef fun load_image(filename)
+       do
+               var res = super
+               res.scale = 100.0 / self.scale
+               return res
        end
 
        redef fun frame_core( display )
@@ -353,9 +380,6 @@ class ShootApp
                        if not self.scene.exists then quit = true
                end
                self.scene.draw_on_display(self)
-
-               # Wait the next frame
-               sys.nanosleep(0, 16000000)
        end
 
        var paused: Bool = false
@@ -376,7 +400,7 @@ class ShootApp
                end
 
                # Maybe the event is specific to the scene
-               return self.scene.input(input_event)
+               return self.scene.input(self, input_event)
        end
 end
 
@@ -385,6 +409,5 @@ if args.length > 0 and args.first == "--headless" then
        return
 end
 
-var app = new ShootApp
-app.debug = args.length > 0 and args.first == "--debug"
-app.main_loop
+app.setup
+app.run