update NOTICE and LICENSE
[nit.git] / lib / game.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # This module contains some classes that help to develop games
14 package game
15
16 import sdl
17
18 # This class helps to count events by unit of time.
19 # For example, an FPS counter:
20 # var fps = new TickCounter
21 # while(true) do
22 # screen_draw
23 # if fps.update then
24 # print(fps)
25 # end
26 # end
27 class TickCounter
28 # Register a new tick
29 # Return true every time interval
30 fun update: Bool
31 do
32 return update_with(sdl_get_ticks)
33 end
34
35 # Like `update' but with an explicit time instead of `sdl_get_ticks`
36 fun update_with(time: Int): Bool
37 do
38 var f = _ticks + 1
39 var dt = time - _time
40 var ti = _time_interval
41 if dt < ti then
42 _ticks = f
43 return false
44 else if _time == 0 then
45 _ticks = 0
46 _time = time
47 return false
48 else
49 _count = ti * f / dt
50 _ticks = 0
51 _time = time
52 return true
53 end
54 end
55
56 # The number of ticks in the last time interval
57 readable var _count: Int
58
59 redef fun to_s: String
60 do
61 return _count.to_s
62 end
63
64 # A ticks counter
65 var _ticks: Int
66
67 # Last update time
68 var _time: Int
69
70 # The time interval
71 readable writable var _time_interval: Int
72
73 # Create a new counter (time interval is 1000)
74 init
75 do
76 with_time(1000)
77 end
78
79 # Create a new counter
80 init with_time(time_interval: Int)
81 do
82 _time_interval = time_interval
83 end
84 end
85
86 class Rectangle
87 fun left: Int is abstract
88 fun right: Int is abstract
89 fun top: Int is abstract
90 fun bottom: Int is abstract
91 fun width: Int is abstract
92 fun height: Int is abstract
93
94 fun contain_pixel(x: Int, y: Int): Bool
95 # Is `self' contains the point (`x',`y') ?
96 do
97 return x >= left and
98 x < right and
99 y >= top and
100 y < bottom
101 end
102
103 fun collide(o: Rectangle): Bool
104 do
105 var s_l = left
106 var s_r = right
107 var s_t = top
108 var s_b = bottom
109 var o_l = o.left
110 var o_r = o.right
111 var o_t = o.top
112 var o_b = o.bottom
113 return ((s_l <= o_l and o_l < s_r) or
114 (s_l < o_r and o_r <= s_r) or
115 (o_l <= s_l and s_l < o_r) or
116 (o_l < s_r and s_r <= o_r)) and
117 ((s_t <= o_t and o_t < s_b) or
118 (s_t < o_b and o_b <= s_b) or
119 (o_t <= s_t and s_t < o_b) or
120 (o_t < s_b and s_b <= o_b))
121 end
122 end
123
124 # A sprite is a drawable element.
125 # It is represented by a main pixel (x,y) and an image (image)
126 class Sprite
127 super Rectangle
128
129 # Absolute X coordinate of the main pixel in the screen
130 readable writable var _x: Int
131
132 # Absolute Y coordinate of the main pixel in the screen
133 readable writable var _y: Int
134
135 # Set two coordinates in one instruction
136 fun set_xy(x: Int, y: Int)
137 do
138 self.x = x
139 self.y = y
140 end
141
142 # The current image of the object
143 readable var _image: SDL_Surface
144
145 # Relative X cordinate of the main pixel in the image
146 readable var _x_image: Int
147
148 # Relative Y cordinate of the main pixel in the image
149 readable var _y_image: Int
150
151 # Set image and relative coordinates in one instruction
152 fun set_image(i: SDL_Surface, x: Int, y: Int)
153 do
154 _image = i
155 _x_image = x
156 _y_image = y
157 end
158
159 # Set image centered on the main pixel (adjust x_image and y_image)
160 fun set_centered_image(i: SDL_Surface)
161 do
162 _image = i
163 _x_image = i.width / 2
164 _y_image = i.height / 2
165 end
166
167 redef fun left: Int
168 do
169 return _x - _x_image
170 end
171
172 redef fun top: Int
173 do
174 return _y - _y_image
175 end
176
177 redef fun right: Int
178 do
179 return _x - _x_image + _image.width
180 end
181
182 redef fun bottom: Int
183 do
184 return _y - _y_image + _image.height
185 end
186
187 redef fun width: Int
188 do
189 return _image.width
190 end
191
192 redef fun height: Int
193 do
194 return _image.height
195 end
196
197 # Draw the image on the surface
198 fun blit_on(s: SDL_Surface)
199 do
200 _image.blit_on_xy(s, _x - _x_image, _y - _y_image)
201 end
202 end
203