src: transform all old writable in annotations
[nit.git] / examples / shoot / src / shoot_logic.nit
index 646c1f9..838c78b 100644 (file)
@@ -36,17 +36,17 @@ class Player
        end
 
        # Current forture of the player
-       var money: Int writable = 0
+       var money: Int = 0 is writable
 
        # Number of basic bullets fired together
-       var nbshoots: Int writable = 1
+       var nbshoots: Int = 1 is writable
 
        # Time bebore the player shoot again a basic bullet (cooldown)
        # Shoot if 0
        var shoot_ttl = 0
 
        # Number of missiles
-       var nbmissiles: Int writable = 0
+       var nbmissiles: Int = 0 is writable
 
        # Time bebore the player shoot again a missile (cooldown)
        # Shoot if 0
@@ -73,15 +73,15 @@ class Player
                if self.y < 0 then
                        self.y = 0
                        self.vy = 0
-               else if self.y > 60000 then
-                       self.y = 60000
+               else if self.y > scene.height then
+                       self.y = scene.height
                        self.vy = 0
                end
                if self.x < 0 then
                        self.x = 0
                        self.vx = 0
-               else if self.x > 80000 then
-                       self.x = 80000
+               else if self.x > scene.width then
+                       self.x = scene.width
                        self.vx = 0
                end
 
@@ -94,7 +94,7 @@ class Player
                else
                        shoot_ttl = 30
                        for i in [0..nbshoots[ do
-                               var shoot = new Shoot
+                               var shoot = new Shoot(scene)
                                shoot.x = x
                                shoot.y = top
                                shoot.vy = -500
@@ -108,7 +108,7 @@ class Player
                        missile_ttl -= 1
                else if nbmissiles > 0 then
                        missile_ttl = 500 / nbmissiles
-                       var shoot = new Missile
+                       var shoot = new Missile(scene)
                        shoot.x = x
                        shoot.y = top
                        shoot.vy = -300
@@ -128,8 +128,8 @@ class Player
                self.exists = false
 
                # Reset the position for respawn
-               self.x = 400 * 100
-               self.y = 500 * 100
+               self.x = scene.width / 2
+               self.y = scene.height - 10000
                self.vx = 0
                self.vy = 0
                self.respawn_ttl = 50
@@ -152,7 +152,7 @@ class GoingTarget
        super Hitable
 
        # true in on move, false if player is at rest
-       var active writable = false
+       var active = false is writable
 
        init do
                self.width = 500
@@ -176,7 +176,13 @@ class Shoot
        # Since there is no frendly fire, it is important to distinguish ownership
        var enemy: Bool = false
 
-       init do
+       # The scene of the sprite
+       # Is used with bound limits
+       var scene: PlayScene
+
+       init(scene: PlayScene)
+       do
+               self.scene = scene
                self.width = 800
                self.height = 800
        end
@@ -186,7 +192,7 @@ class Shoot
                super
 
                # Out of screen ?
-               if self.y < -100 * 100 or self.y > 700 * 100 or self.x < -100 * 100 or self.x > 900 * 100 then
+               if self.y < -100 * 100 or self.y > scene.height + 10000 or self.x < -100 * 100 or self.x > scene.width + 10000 then
                        self.exists = false
                end
        end
@@ -203,7 +209,7 @@ class Missile
        super Shoot
 
        # The target aquired by the missile
-       var target: nullable Sprite
+       var target: nullable Sprite = null
 
        # When ttl is 0 then the angle stay fixed
        # The angle is updated toward the target if ttl>0
@@ -255,7 +261,7 @@ abstract class Enemy
                super
 
                # Out of screen ?
-               if self.y > 700 * 100 or self.x < -100 * 100 or self.x > 900 * 100 then
+               if self.y > scene.height + 10000 or self.x < -100 * 100 or self.x > scene.width + 10000 then
                        # Note: no control on the top to let ennemies appear
                        self.exists = false
                end
@@ -282,7 +288,7 @@ abstract class Enemy
                self.exists = false
                scene.explosion(self.x, self.y, 5)
                if 100.rand < 3 then
-                       var upmissile = new UpMissile
+                       var upmissile = new UpMissile(scene)
                        upmissile.x = self.x
                        upmissile.y = self.y
                        upmissile.vx = 0
@@ -291,7 +297,7 @@ abstract class Enemy
                        scene.hitables.add(new LootArea(upmissile, 2000))
                else
                        for i in [0..self.loot[ do
-                               var money = new Money
+                               var money = new Money(scene)
                                money.x = self.x
                                money.y = self.y
                                money.set_velocity(100.rand.to_f*pi/50.0, (500+self.loot).rand)
@@ -313,12 +319,24 @@ class Enemy0
        super Enemy
 
        redef fun loot do return 3
+
+       redef init(scene)
+       do
+               self.width = 3600
+               self.height = 3600
+       end
 end
 
-# Simple shooter of paris of basic bullets
+# Simple shooter of pairs of basic bullets
 class Enemy1
        super Enemy
 
+       redef init(scene)
+       do
+               self.width = 4400
+               self.height = 4400
+       end
+
        redef fun shoot
        do
                # Next shoot
@@ -326,7 +344,7 @@ class Enemy1
 
                # two bullets shoot each time
                for dx in [-11, 11] do
-                       var shoot = new Shoot
+                       var shoot = new Shoot(scene)
                        shoot.enemy = true
                        shoot.x = self.x + dx * 100
                        shoot.y = self.bottom
@@ -342,13 +360,19 @@ end
 class Enemy2
        super Enemy
 
+       redef init(scene)
+       do
+               self.width = 6000
+               self.height = 6000
+       end
+
        redef fun shoot
        do
                # Next shoot
                shoot_ttl = 200
 
                # The missile targets the player
-               var shoot = new Missile
+               var shoot = new Missile(scene)
                shoot.enemy = true
                shoot.x = self.x
                shoot.y = self.bottom
@@ -360,17 +384,23 @@ class Enemy2
        redef fun loot do return 10
 end
 
-# Enem that shoot rings of basic bullets
+# Enemy that shoot rings of basic bullets
 class Enemy3
        super Enemy
 
+       redef init(scene)
+       do
+               self.width = 5800
+               self.height = 5800
+       end
+
        redef fun shoot
        do
                # Next shoot
                shoot_ttl = 50
 
                for i in [0..10[ do
-                       var shoot = new Shoot
+                       var shoot = new Shoot(scene)
                        shoot.enemy = true
                        shoot.x = self.x
                        shoot.y = self.bottom
@@ -389,6 +419,12 @@ class Enemy4
        # The angle of the turret
        var angle: Float = 0.0
 
+       redef init(scene)
+       do
+               self.width = 4200
+               self.height = 4200
+       end
+
        redef fun update
        do
                super
@@ -416,7 +452,7 @@ class Enemy4
                end
 
                # Shoot with the turret angle
-               var shoot = new Shoot
+               var shoot = new Shoot(scene)
                shoot.enemy = true
                shoot.x = self.x
                shoot.y = self.y
@@ -431,6 +467,12 @@ end
 class EnemyKamikaze
        super Enemy
 
+       redef init(scene)
+       do
+               self.width = 3200
+               self.height = 3200
+       end
+
        redef fun update
        do
                super
@@ -459,12 +501,12 @@ class Boss
        init(scene)
        do
                super
-               self.width = 128 * 100
-               self.height = 100 * 100
-               self.x = 400 * 100
+               self.width = 140 * 100
+               self.height = 96 * 100
+               self.x = scene.width / 2
                self.y = -100 * 100
-               self.left_part = new BossPart(self, -48*100)
-               self.right_part = new BossPart(self, 48*100)
+               self.left_part = new BossPart(self, -66*100)
+               self.right_part = new BossPart(self, 66*100)
        end
 
        var flick_ttl: Int = 0
@@ -480,9 +522,9 @@ class Boss
                else if self.vx == 0 then
                        self.vx = 100
                        self.vy = 0
-               else if self.x > 700 * 100 and self.vx > 0 then
+               else if self.x > scene.width - 10000 and self.vx > 0 then
                        self.vx = -self.vx
-               else if self.x < 100 * 100 and self.vx < 0 then
+               else if self.x < 10000 and self.vx < 0 then
                        self.vx = -self.vx
                end
 
@@ -506,7 +548,7 @@ class Boss
                end
 
                # Shoot the player with a basic bullet
-               var shoot = new Shoot
+               var shoot = new Shoot(scene)
                shoot.enemy = true
                shoot.x = self.x
                shoot.y = self.bottom
@@ -554,8 +596,8 @@ class BossPart
                self.boss = boss
                self.relx = relx
                super(boss.scene)
-               self.width = 32 * 100
-               self.height = 60 * 100
+               self.width = 38 * 100
+               self.height = 48 * 100
 
                # Alternate the shoots of the arms
                if relx > 0 then
@@ -584,7 +626,7 @@ class BossPart
                shoot_ttl = 600
 
                # Shoot a missile that targets the player
-               var shoot = new Missile
+               var shoot = new Missile(scene)
                shoot.enemy = true
                shoot.x = self.x
                shoot.y = self.bottom
@@ -612,8 +654,11 @@ end
 abstract class Loot
        super Hitable
 
-       init
+       var scene: PlayScene
+
+       init(scene: PlayScene)
        do
+               self.scene = scene
                self.width = 400
                self.height = 400
        end
@@ -627,7 +672,7 @@ abstract class Loot
                super
 
                # Out of screen ?
-               if self.y > 700 * 100 then
+               if self.y > scene.height + 10000 then
                        self.exists = false
                end
 
@@ -759,11 +804,16 @@ end
 class Star
        super Sprite
 
-       init
+       # The scene of the sprite
+       # Is used with bound limits
+       var scene: ShotScene
+
+       init(scene: ShotScene)
        do
+               self.scene = scene
                # Randomely places stars on the plane
-               self.x = 800.rand * 100
-               self.y = 600.rand * 100
+               self.x = scene.width.rand
+               self.y = scene.height.rand
                self.vy = 40.rand + 11
        end
 
@@ -772,9 +822,9 @@ class Star
                super
 
                # Replace the star on the top
-               if self.y > 600 * 100 then
+               if self.y > scene.height then
                        self.y = 200.rand * -100
-                       self.x = 800.rand * 100
+                       self.x = scene.width.rand
                        self.vy = 40.rand + 11
                end
        end
@@ -784,13 +834,13 @@ class ShotScene
        super Scene
 
        # When a scene need to be replaced, just assign the next_scene to a non null value
-       var next_scene: nullable ShotScene writable = null
+       var next_scene: nullable ShotScene = null is writable
 
        # The width of the whole scene
-       var width: Int writable
+       var width: Int is writable
 
        # The height of the whole scene
-       var height: Int writable
+       var height: Int is writable
 
        init(w,h: Int)
        do
@@ -834,8 +884,8 @@ class PlayScene
        do
                super
                self.player = new Player(self)
-               player.x = 400 * 100
-               player.y = 500 * 100
+               player.x = self.width / 2
+               player.y = self.height - 10000
                self.sprites.add(background)
                self.sprites.add(pasive_stuff)
                self.sprites.add(loots)
@@ -846,7 +896,7 @@ class PlayScene
                self.sprites.add(hitables)
 
                for i in [0..100[ do
-                       background.add(new Star)
+                       background.add(new Star(self))
                end
 
                hitables.add(player.going_target)
@@ -905,7 +955,7 @@ class PlayScene
                        else
                                enemy = new Enemy4(self)
                        end
-                       enemy.x = 600.rand * 100 + 10000
+                       enemy.x = (self.width - 20000).rand + 10000
                        enemy.vy = 200.rand + 100
                        if 10.rand < 3 then
                                enemy.vx = 200.rand - 100
@@ -980,11 +1030,11 @@ class MenuScene
        do
                super
                for i in [0..100[ do
-                       sprites.add(new Star)
+                       sprites.add(new Star(self))
                end
        end
 
-       var play: Bool writable = false
+       var play: Bool = false is writable
        var ttl: Int = 50
 
        redef fun update