action_nitro: animate the parachute opening
[nit.git] / contrib / action_nitro / src / action_nitro.nit
index e5a68f9..4fdf39e 100644 (file)
@@ -41,9 +41,12 @@ redef class App
        # Textures of the biplane, jet, helicopter, parachute and powerups
        var planes_sheet = new PlanesImages
 
+       # Animation when opening the parachute
+       var parachute_animation = new Animation(planes_sheet.parachute, 16.0)
+
        # Animation for the player movement
-       private var player_textures: Array[Texture] =
-               [for f in [1..12] do new Texture("textures/player/frame_{f.pad(2)}.png")]
+       private var running_texture = new Texture("textures/player.png")
+       private var running_animation: Animation = running_texture.to_animation(10.0, 12, 0)
 
        # Boss 3D model
        private var iss_model = new Model("models/iss.obj")
@@ -140,6 +143,7 @@ redef class App
 
                # Load 3d models
                iss_model.load
+               if iss_model.errors.not_empty then print_error iss_model.errors.join("\n")
 
                # Setup cameras
                world_camera.reset_height 60.0
@@ -275,7 +279,6 @@ redef class App
                        player.moving = 0.0
                        if pressed_keys.has("left") then player.moving -= 1.0
                        if pressed_keys.has("right") then player.moving += 1.0
-                       player.sprite.as(PlayerSprite).update
                end
 
                # Try to fire as long as a key is pressed
@@ -403,16 +406,12 @@ redef class App
                                        if event.name == "left" then
                                                var mod = if event.is_down then -1.0 else 1.0
                                                player.moving += mod
-                                       end
-
-                                       if event.name == "right" then
+                                               player.animate_move
+                                       else if event.name == "right" then
                                                var mod = if event.is_down then 1.0 else -1.0
                                                player.moving += mod
+                                               player.animate_move
                                        end
-
-                                       if player.moving == 0.0 then
-                                       player.sprite.as(PlayerSprite).stop_running
-                                       else player.sprite.as(PlayerSprite).start_running
                                end
                        end
                end
@@ -507,7 +506,7 @@ end
 redef class Boss
        redef var actor is lazy do
                var actor = new Actor(app.iss_model, center)
-               actor.rotation = pi/2.0
+               actor.yaw = pi/2.0
                return actor
        end
 
@@ -524,19 +523,31 @@ redef class Boss
 end
 
 redef class Enemy
-       redef var sprite = new Sprite(app.player_textures.rand, center) is lazy
+       redef var sprite = new Sprite(app.running_animation.frames.rand, center) is lazy
        init do sprite.scale = width/sprite.texture.width * 2.0
 end
 
 redef class Parachute
-       redef var sprite = new Sprite(app.planes_sheet.parachute, center) is lazy
-       init do sprite.scale = width / sprite.texture.width
+       redef var sprite = new Sprite(app.planes_sheet.parachute_open, center) is lazy
+       init
+       do
+               sprite.scale = width / sprite.texture.width
+               sprite.animate app.parachute_animation
+       end
 end
 
 redef class Player
-       redef var sprite = new PlayerSprite(app.player_textures[1], center, app.player_textures, 0.08) is lazy
+       redef var sprite = new Sprite(app.running_animation.frames.last, center) is lazy
        init do sprite.scale = width/sprite.texture.width * 2.0
 
+       # Update current animation
+       fun animate_move
+       do
+               if moving == 0.0 then
+                       sprite.animate_stop
+               else sprite.animate(app.running_animation, -1.0)
+       end
+
        redef fun update(dt, world)
        do
                super
@@ -556,12 +567,12 @@ redef class Player
                        var splatter = new Actor(app.splatter_model,
                                new Point3d[Float](center.x, 0.05 & 0.04, center.y))
                        splatter.scale = 32.0
-                       splatter.rotation = 2.0 * pi.rand
+                       splatter.yaw = 2.0*pi.rand
                        app.actors.add splatter
                end
 
                # Display respawn instructions
-               app.ui_sprites.add new Sprite(app.texts_sheet.respawn, app.ui_camera.center)
+               app.ui_sprites.add new Sprite(app.texts_sheet.respawn, app.ui_camera.center.offset(0.0, 0.0, 0.0))
        end
 end
 
@@ -631,44 +642,6 @@ redef class Int
        end
 end
 
-# Special `Sprite` for the player character which is animated
-class PlayerSprite
-       super Sprite
-
-       # Animation of the running character
-       var running_animation: Array[Texture]
-
-       # Seconds per frame of the animations
-       var time_per_frame: Float
-
-       # Currently playing animation
-       private var current_animation: nullable Array[Texture] = null
-
-       # Second at witch `current_animation` started
-       private var anim_ot = 0.0
-
-       # Start the running animation
-       fun start_running
-       do
-               anim_ot = app.world.t
-               current_animation = running_animation
-       end
-
-       # Stop the running animation
-       fun stop_running do current_animation = null
-
-       # Update `texture` from `current_animation`
-       fun update
-       do
-               var anim = current_animation
-               if anim != null then
-                       var dt = app.world.t - anim_ot
-                       var i = (dt / time_per_frame).to_i+2
-                       texture = anim.modulo(i)
-               end
-       end
-end
-
 # Manager to display numbers in sprite
 class CounterSprites