From 6f19823645c317d61e4525128d030cd73285c7b0 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 25 Oct 2012 10:57:50 -0400 Subject: [PATCH] lib: move game.nit as lib/scene2d.nit Update the game leapfrog to use it. Signed-off-by: Jean Privat --- examples/leapfrog/leapfrog.nit | 6 +-- examples/leapfrog/leapfrog_curses.nit | 4 +- examples/leapfrog/game.nit => lib/scene2d.nit | 58 ++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 10 deletions(-) rename examples/leapfrog/game.nit => lib/scene2d.nit (68%) create mode 100644 tests/sav/scene2d.res diff --git a/examples/leapfrog/leapfrog.nit b/examples/leapfrog/leapfrog.nit index f666b89..ac8cff0 100644 --- a/examples/leapfrog/leapfrog.nit +++ b/examples/leapfrog/leapfrog.nit @@ -17,7 +17,7 @@ # This module is an example of a simple game using a curses backend module leapfrog -import game +import scene2d # A falling apple # If the sheep grab it, it scores one point. @@ -116,8 +116,8 @@ class Duck end end -class Scene - super LiveObject +class PlayScene + super Scene var apples = new LiveGroup[Apple] var duck = new Duck diff --git a/examples/leapfrog/leapfrog_curses.nit b/examples/leapfrog/leapfrog_curses.nit index e894f8c..0420277 100644 --- a/examples/leapfrog/leapfrog_curses.nit +++ b/examples/leapfrog/leapfrog_curses.nit @@ -105,7 +105,7 @@ redef class Duck end end -redef class Scene +redef class PlayScene fun draw_on_curses(view: CursesView) do var window = view.window @@ -133,7 +133,7 @@ redef class Scene end end -var game = new Scene +var game = new PlayScene var win = new Window var main_view = new CursesView(win) diff --git a/examples/leapfrog/game.nit b/lib/scene2d.nit similarity index 68% rename from examples/leapfrog/game.nit rename to lib/scene2d.nit index a0e2a83..18a5d13 100644 --- a/examples/leapfrog/game.nit +++ b/lib/scene2d.nit @@ -14,9 +14,8 @@ # 2D management of game elements # -# FIXME: game is a bad name # TODO: collision framework (with quad tree?) -module game +module scene2d # The root class of the living objects (sprites, group of sprites, etc.) abstract class LiveObject @@ -37,10 +36,10 @@ end class Sprite super LiveObject - # x coordinate of the top-left point + # x coordinate of the center point var x: Int writable = 0 - # y coordinate of the top-left point + # y coordinate of the center point var y: Int writable = 0 # width of the sprite @@ -49,6 +48,11 @@ class Sprite # height of the sprite var height: Int writable = 100 + fun left: Int do return x - width/2 + fun right: Int do return x + width/2 + fun top: Int do return y - height/2 + fun bottom: Int do return y + height/2 + # x velocity (applied by `update') var vx: Int writable = 0 @@ -67,8 +71,30 @@ class Sprite # `x', `y', `width', and `height' of both sprites are considered fun overlaps(other: Sprite): Bool do - return self.x+self.width > other.x and self.x < other.x+other.width and self.y+self.height > other.y and self.y < other.y+other.width + return self.right > other.left and self.left < other.right and self.bottom > other.top and self.top < other.bottom end + + # Return the current angle of velocity + # Often used to rotate the displayed image with the correct angle + fun velocity_angle: Float + do + return atan2(self.vx.to_f, -self.vy.to_f) + end + + # Return the angle to target an other sprite + fun angle_to(target: Sprite): Float + do + return atan2((target.x-self.x).to_f, (self.y-target.y).to_f) + end + + # Update of vx and vy toward a given angle and magnitude + fun set_velocity(angle: Float, maginude: Int) + do + var magf = maginude.to_f + self.vx = (angle.sin * magf).to_i + self.vy = (angle.cos * -magf).to_i + end + end # Organizational class to manage groups of sprites and other live objects. @@ -86,6 +112,22 @@ class LiveGroup[E: LiveObject] for x in self do if x.exists then x.update end + # Remove all live Objects that do not exists + # Call this to cleanup the live group + fun gc + do + var i = self.iterator + while i.is_ok do + var e = i.item + if not e.exists then + i.delete + else if e isa LiveGroup[LiveObject] then + e.gc + end + i.next + end + end + # Recursively draw each live objects that `exists' redef fun draw(view) do @@ -93,6 +135,12 @@ class LiveGroup[E: LiveObject] end end +# A state in the game logic +# A scene manage a bunch of live objects +class Scene + super LiveObject +end + # Abstract view do draw sprites # # Concrete views are specific for each back-end. diff --git a/tests/sav/scene2d.res b/tests/sav/scene2d.res new file mode 100644 index 0000000..e69de29 -- 1.7.9.5