a0e2a837690b6acfa7fe6975f5c5de85d7b6d3aa
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # 2D management of game elements
17 # FIXME: game is a bad name
18 # TODO: collision framework (with quad tree?)
21 # The root class of the living objects (sprites, group of sprites, etc.)
22 abstract class LiveObject
23 # Compute the position, state and appearance.
26 # Controls whether `update' and `draw' are automatically called by `LiveGroup'
27 var exists
writable = true
29 # Redefine this method to asks how to draw on a view
30 fun draw
(view
: View) is abstract
34 # The basic atomic living and moving object.
36 # A sprite has a position and a velocity
40 # x coordinate of the top-left point
41 var x
: Int writable = 0
43 # y coordinate of the top-left point
44 var y
: Int writable = 0
47 var width
: Int writable = 100
49 # height of the sprite
50 var height
: Int writable = 100
52 # x velocity (applied by `update')
53 var vx
: Int writable = 0
55 # y velocity (applied by `update')
56 var vy
: Int writable = 0
64 redef fun draw
(view
) do view
.draw_sprite
(self)
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
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
74 # Organizational class to manage groups of sprites and other live objects.
75 class LiveGroup[E
: LiveObject]
83 # Recursively update each live objects that `exists'
86 for x
in self do if x
.exists
then x
.update
89 # Recursively draw each live objects that `exists'
92 for x
in self do if x
.exists
then x
.draw
(view
)
96 # Abstract view do draw sprites
98 # Concrete views are specific for each back-end.
99 # View can also be used to implements camera and other fun things.
101 # Draw a specific sprite on the view
103 # This method must be implemented for each specific view.
104 # A traditional way of implementation is to use a double-dispatch mechanism
108 # redef fun draw_sprite(s) do s.draw_on_myview(self)
111 # # How to draw a sprite on my specific view
112 # fun draw_on_myview(myview: MyView) is abstract
114 fun draw_sprite
(s
: Sprite) is abstract