update NOTICE
[nit.git] / lib / bucketed_game.nit
index 45a439c..ae0d792 100644 (file)
@@ -24,6 +24,8 @@ module bucketed_game
 
 # Something acting on the game
 class Turnable[G: Game]
+
+       # Execute `turn` for this instance.
        fun do_turn(turn: GameTurn[G]) is abstract
 end
 
@@ -39,9 +41,11 @@ class Bucketable[G: Game]
        fun cancel_act do act_at = null
 end
 
-# Optiomized organization of `Bucketable` instances
+# Optimized organization of `Bucketable` instances
 class Buckets[G: Game]
        super Turnable[G]
+
+       # Bucket type used in this implementation.
        type BUCKET: HashSet[Bucketable[G]]
 
        private var buckets: Array[BUCKET] is noinit
@@ -59,6 +63,7 @@ class Buckets[G: Game]
                end
        end
 
+       # Add the Bucketable event `e` at `at_tick`.
        fun add_at(e: Bucketable[G], at_tick: Int)
        do
                var at_key = key_for_tick(at_tick)
@@ -83,6 +88,8 @@ class Buckets[G: Game]
                var current_bucket = buckets[current_bucket_key]
 
                var next_bucket = new HashSet[Bucketable[G]]
+               buckets[current_bucket_key] = next_bucket
+               self.next_bucket = next_bucket
 
                for e in current_bucket do
                        var act_at = e.act_at
@@ -96,14 +103,13 @@ class Buckets[G: Game]
                                end
                        end
                end
-
-               self.next_bucket = next_bucket
-               buckets[current_bucket_key] = next_bucket
        end
 end
 
 # Game related event
 interface GameEvent
+
+       # Apply `self` to `game` logic.
        fun apply( game : ThinGame ) is abstract
 end
 
@@ -114,39 +120,43 @@ end
 
 # Game logic on the client
 class ThinGame
+
+       # Game tick when `self` should act.
+       #
+       # Default is 0.
        var tick: Int = 0 is protected writable
 end
 
 # Game turn on the client
 class ThinGameTurn[G: ThinGame]
-       var tick: Int = 0 is protected writable
 
-       var events: List[GameEvent] = new List[GameEvent] is protected writable
+       # Game tick when `self` should act.
+       var tick: Int is protected writable
 
-       init (t: Int) do tick = t
+       # List of game events occured for `self`.
+       var events = new List[GameEvent] is protected writable
 end
 
 # Game turn on the full logic
 class GameTurn[G: Game]
        super ThinGameTurn[G]
+
+       # Game that `self` belongs to.
        var game: G
 
-       init (g: G)
-       do
-               super(g.tick)
-               game = g
+       # Create a new game turn for `game`.
+       init (game: G) is old_style_init do
+               super(game.tick)
+               self.game = game
        end
 
-       fun act_next(e: Bucketable[G])
-       do
-               game.buckets.add_at(e, tick + 1)
-       end
+       # Insert the Bucketable event `e` to be executed at next tick.
+       fun act_next(e: Bucketable[G]) do game.buckets.add_at(e, tick + 1)
 
-       fun act_in(e: Bucketable[G], t: Int)
-       do
-               game.buckets.add_at(e, tick + t)
-       end
+       # Insert the Bucketable event `e` to be executed at tick `t`.
+       fun act_in(e: Bucketable[G], t: Int) do game.buckets.add_at(e, tick + t)
 
+       # Add and `apply` a game `event`.
        fun add_event( event : GameEvent )
        do
                event.apply( game )
@@ -157,8 +167,11 @@ end
 # Full game logic
 class Game
        super ThinGame
+
+       # Game type used in this implementation.
        type G: Game
 
+       # Bucket list in this game.
        var buckets: Buckets[G] = new Buckets[G]
 
        # Last turn executed in this game
@@ -166,8 +179,10 @@ class Game
        # but cannot be used to add new Events.
        var last_turn: nullable ThinGameTurn[G] = null
 
-       init do end
-
+       # Execute and return a new GameTurn.
+       #
+       # This method calls `do_pre_turn` before executing the GameTurn
+       # and `do_post_turn` after.
        fun do_turn: GameTurn[G]
        do
                var turn = new GameTurn[G](self)
@@ -183,6 +198,15 @@ class Game
                return turn
        end
 
+       # Execute something before executing the current GameTurn.
+       #
+       # Should be redefined by clients to add a pre-turn behavior.
+       # See `Game::do_turn`.
        fun do_pre_turn(turn: GameTurn[G]) do end
+
+       # Execute something after executing the current GameTurn.
+       #
+       # Should be redefined by clients to add a post-turn behavior.
+       # See `Game::do_turn`.
        fun do_post_turn(turn: GameTurn[G]) do end
 end