lib: move poll_in from curses to file
[nit.git] / examples / leapfrog / game.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # 2D management of game elements
16 #
17 # FIXME: game is a bad name
18 # TODO: collision framework (with quad tree?)
19 module game
20
21 # The root class of the living objects (sprites, group of sprites, etc.)
22 abstract class LiveObject
23 # Compute the position, state and appearance.
24 fun update do end
25
26 # Controls whether `update' and `draw' are automatically called by `LiveGroup'
27 var exists writable = true
28
29 # Redefine this method to asks how to draw on a view
30 fun draw(view: View) is abstract
31 end
32
33
34 # The basic atomic living and moving object.
35 #
36 # A sprite has a position and a velocity
37 class Sprite
38 super LiveObject
39
40 # x coordinate of the top-left point
41 var x: Int writable = 0
42
43 # y coordinate of the top-left point
44 var y: Int writable = 0
45
46 # width of the sprite
47 var width: Int writable = 100
48
49 # height of the sprite
50 var height: Int writable = 100
51
52 # x velocity (applied by `update')
53 var vx: Int writable = 0
54
55 # y velocity (applied by `update')
56 var vy: Int writable = 0
57
58 redef fun update
59 do
60 self.x += self.vx
61 self.y += self.vy
62 end
63
64 redef fun draw(view) do view.draw_sprite(self)
65
66 # Is self overlaps (or contains) an other sprite
67 # `x', `y', `width', and `height' of both sprites are considered
68 fun overlaps(other: Sprite): Bool
69 do
70 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
71 end
72 end
73
74 # Organizational class to manage groups of sprites and other live objects.
75 class LiveGroup[E: LiveObject]
76 super LiveObject
77 super List[E]
78
79 init
80 do
81 end
82
83 # Recursively update each live objects that `exists'
84 redef fun update
85 do
86 for x in self do if x.exists then x.update
87 end
88
89 # Recursively draw each live objects that `exists'
90 redef fun draw(view)
91 do
92 for x in self do if x.exists then x.draw(view)
93 end
94 end
95
96 # Abstract view do draw sprites
97 #
98 # Concrete views are specific for each back-end.
99 # View can also be used to implements camera and other fun things.
100 interface View
101 # Draw a specific sprite on the view
102 #
103 # This method must be implemented for each specific view.
104 # A traditional way of implementation is to use a double-dispatch mechanism
105 #
106 # Exemple:
107 # class MyView
108 # redef fun draw_sprite(s) do s.draw_on_myview(self)
109 # end
110 # redef class Sprite
111 # # How to draw a sprite on my specific view
112 # fun draw_on_myview(myview: MyView) is abstract
113 # end
114 fun draw_sprite(s: Sprite) is abstract
115 end