X-Git-Url: http://nitlanguage.org diff --git a/examples/mnit_dino/src/game_logic.nit b/examples/mnit_dino/src/game_logic.nit index 08f790a..63b6d53 100644 --- a/examples/mnit_dino/src/game_logic.nit +++ b/examples/mnit_dino/src/game_logic.nit @@ -15,7 +15,7 @@ # limitations under the License. # Entire game logic for the Dino game -# Depends only on Nit standard library +# Depends only on Nit `core` library module game_logic interface Turnable @@ -34,7 +34,7 @@ class Game var over_since = 0 - var score: Container[Int] + var score: Ref[Int] var random_radius_min = 200 var random_radius_max = 400 @@ -43,7 +43,7 @@ class Game var entities_sorter = new EntitiesSorter - init( cavemen_nbr : Int, score: Container[Int] ) + init( cavemen_nbr : Int, score: Ref[Int] ) do srand_from(cavemen_nbr) @@ -133,16 +133,9 @@ class GamePos var x : Int var y : Int - init ( x, y : Int ) - do - self.x = x - self.y = y - end - init copy( src : GamePos ) do - x = src.x - y = src.y + init(src.x, src.y) end fun squared_dist_with( other : GamePos ) : Int @@ -159,19 +152,30 @@ end class Entity super Turnable - fun run_over_distance: Int do return 500 + fun run_over_distance_x: Int do return 50 + fun run_over_distance_y: Int do return 16 + var pos = new GamePos( 0, 0 ) fun squared_dist_with_dino( game : Game ) : Int do return pos.squared_dist_with( game.dino.pos ) end + + fun under_dino(game: Game): Bool + do + var dy = pos.y - game.dino.pos.y + if dy.abs > run_over_distance_y then return false + + var dx = pos.x - game.dino.pos.x + return dx.abs <= run_over_distance_x + end end class MovingEntity super Entity - var going_to : nullable GamePos writable = null + var going_to : nullable GamePos = null is writable fun speed : Int is abstract @@ -233,8 +237,7 @@ class Dino end for i in t.game.items_on_ground do - var dwd = i.squared_dist_with_dino(t.game) - if dwd < i.run_over_distance then + if i.under_dino(t.game) then t.game.items_on_ground.remove i t.game.entities.remove i end @@ -267,20 +270,20 @@ class Caveman fun is_afraid( turn : Turn ) : Bool do return turn.nbr < afraid_until fun can_throw( turn : Turn ) : Bool do return cannot_throw_until < turn.nbr + fun die(turn: Turn) do is_alive = false redef fun do_turn( t ) do if is_alive then - var dwd = squared_dist_with_dino( t.game ) - - if dwd < run_over_distance then - if t.game.dino.is_alive then is_alive = false + if under_dino(t.game) then + if t.game.dino.is_alive then die(t) return else if is_afraid( t ) then # going to destination else if t.game.dino.life <= 0 then # dino is dead, chill else + var dwd = squared_dist_with_dino( t.game ) if dwd < fear_distance then afraid_until = t.nbr + fear_duration @@ -376,7 +379,8 @@ class Bush super Entity end # Sort entities on screen in order of Y, entities in the back are drawn first class EntitiesSorter - super AbstractSorter[Entity] + super Comparator + redef type COMPARED: Entity redef fun compare(a, b) do return b.pos.y <=> a.pos.y end