1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2006-2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 super SDL_EventListener
22 redef fun on_mouse_button
(evt
: SDL_MouseButtonEvent)
24 if evt
.is_pressed
then
25 if evt
.button
== 1 then
27 else if evt
.button
== 4 then
28 load_level
(_level
+ 1)
29 else if evt
.button
== 5 then
30 load_level
(_level
- 1)
31 else if evt
.button
== 3 then
32 _quit_requested
= true
37 redef fun on_mouse_motion
(evt
: SDL_MouseMotionEvent)
45 _quit_requested
= true
48 readable var _quit_requested
: Bool
53 var t
= sdl_get_ticks
# Current time (ms)
54 var quantum
= 10 # Quantum of time (ms) for one step
59 _last_t
= _last_t
+ quantum
65 #if _fps.update(t) then
80 _player
.update_one_step
(mx
, my
)
83 while i
< _boxes
.length
do
85 if b
.gettable
and b
.collide
(_player
) then
86 _boxes
[i
] = _boxes
.last
92 if _boxes
.is_empty
then
94 load_level
(_level
+ 1)
96 for j
in [0..boxnumber
[ do
97 var angle
= (2.0*pi
).rand
99 _others
.add
(new Particle(bx
, by
, angle
.cos
* force
, angle
.sin
* force
, 10 + 100.rand
))
108 while i
< _others
.length
do
110 if s
.update_one_step
then
111 _others
[i
] = _others
.last
119 fun update_draw
(frames
: Int)
121 _player
.update_draw
(frames
)
130 _player
.blit_on
(_screen
)
137 private fun load_level
(l
: Int)
142 var levels
= once load_levels
("data/levels.txt")
143 if _level
> levels
.length
then
148 var lev
= levels
[_level
- 1]
149 print
("Level {lev.number}: {lev.name}")
153 private fun init_level
(s
: String)
158 for i
in [0..s
.length
[ do
163 else if c
== ' ' then
172 var dx
= _screen
.width
/ width
173 var dy
= _screen
.height
/ height
177 for i
in [0..s
.length
[ do
182 else if c
== '#' then
183 _boxes
.add
(new Box(dx
/2 + dx
* w
, dy
/2 + dy
* h
))
184 else if c
== '*' then
185 _boxes
.add
(new Box(dx
/2 + dx
* w
, dy
/2 + dy
* h
))
192 var _screen
: SDL_Screen
197 var _fps
: TickCounter
199 var _boxes
: Array[Box]
200 var _others
: Array[Particle]
210 _player
= new Player(320, 240)
212 _boxes
= new Array[Box]
213 _others
= new Array[Particle]
214 _fps
= new TickCounter
216 _last_t
= sdl_get_ticks
224 private fun load_images
(name
: String, number
: Int): Array[SDL_Surface]
226 print
("load images '{name}' ({number})")
227 var res
= new Array[SDL_Surface].with_capacity
(number
)
233 res
.add
(sdl_load_bmp
("data/{name}-0{i}.png"))
235 for i
in [10..number
[ do
236 res
.add
(sdl_load_bmp
("data/{name}-{i}.png"))
244 var _rx
: Float # Real X axis (sub-pixel)
245 var _ry
: Float # Real Y axix (sub-pixel)
246 readable var _vx
: Float # X velocity (speed)
247 readable var _vy
: Float # Y velovity (speed)
264 fun update_one_step
(mx
: Int, my
: Int)
269 # Calculate objective distance
272 var dbm2
= dbmx
* dbmx
+ dbmy
* dbmy
274 # Calculate acceleration and update speed
276 var dbm
= dbm2
.to_f
.sqrt
278 var force
= 0.200 # maximal force
280 # Less force if near the objective
282 force
= force
* dbm
/ 100.0
283 else if not _boost
and dbm
> 60.0 then
287 var ax
= dbmx
.to_f
/ dbm
* force
288 var ay
= dbmy
.to_f
/ dbm
* force
294 # Speed modifier (min/max)
295 var dv2
= vx
*vx
+ vy
*vy
# Absolute speed
300 else if dv2
< 1.0 then
318 fun update_draw
(frames
: Int)
320 var a
= - atan2
(_vx
, _vy
)
321 var x
= ((a
/ 2.0 / pi
+ 0.5) * 16.0 + 0.5).to_i
% 16
327 var imgs
= _images
[x
]
329 var inum
= _image_number
+ frames
330 if inum
>= imgs
.length
* 8 then
331 inum
= inum
% (imgs
.length
* 8)
333 _image
= imgs
[inum
/ 8]
338 var _images
: Array[Array[SDL_Surface]]
339 var _image_number
: Int
347 _images
= once load_ship_images
349 set_centered_image
(_images
.first
.first
)
350 #set_centered_image(once sdl_load_bmp("data/ship-up.png"))
352 fun load_ship_images
: Array[Array[SDL_Surface]]
354 var res
= new Array[Array[SDL_Surface]]
355 for i
in ["up", "22", "45", "68", "90", "112", "135", "158", "180", "202", "225", "248", "270", "292", "315", "338"] do
356 res
.add
(load_images
("ship-{i}", 4))
357 res
.add
(load_images
("ship2-{i}", 4))
365 var _images
: Array[SDL_Surface]
366 var _image_number
: Int
367 var _image_delay
: Int
368 var _back_rotate
: Bool
372 _image_delay
= _image_delay
+ 1
373 if _image_delay
> 0 and _image_delay
% 8 == 0 then
374 var n
= _image_number
+ 1
375 if n
>= _images
.length
then
381 _image
= _images
[_images
.length
- n
- 1]
387 redef fun blit_on
(s
: SDL_Surface)
389 if _image_delay
> 0 then
390 _image
.blit_on_xy
(s
, _x
- _x_image
, _y
- _y_image
)
396 return _image_delay
> 0
404 _images
= once load_images
("box", 15)
405 _image_delay
= - (30 + 20.rand
)
406 _image_number
= _images
.length
.rand
407 _back_rotate
= 2.rand
== 1
408 set_centered_image
(_images
.first
)
415 fun update_one_step
: Bool
427 if _ttl
% 4 == 0 then
428 var n
= (_ttl
/ 4) % _images
.length
436 var _images
: Array[SDL_Surface]
439 init(x
: Int, y
: Int, vx
: Float, vy
: Float, ttl
: Int)
445 _images
= once load_images
("debris4", 8)
446 _image
= _images
.first
451 readable var _number
: Int
452 readable var _name
: String
453 readable var _data
: String
455 redef fun to_s
: String
457 return "level {_number}"
461 init(n
: Int, name
: String, d
: String)
469 fun load_levels
(filename
: String): Array[Level]
471 var levels
= new Array[Level]
472 var f
= new IFStream.open
(filename
)
479 else if l
[0] == '>' then
481 name
= l
.substring_from
(1)
482 else if l
[0] == '<' then
484 else if l
[0] == '!' then
486 levels
.add
(new Level(levels
.length
+ 1, name
, data
))
491 if data
.length
> 0 then
504 sdl_show_cursor
= true
506 #var screen = sdl_set_video_mode(800, 600, 32)
507 var screen
= sdl_set_fullscreen_video_mode
(800, 600, 32)
509 var evt_proc
= new SDL_EventProcessor
510 var evt_list
= new Playing(screen
)
512 evt_proc
.add_listener
(evt_list
)
514 while not evt_list
.quit_requested
do
515 evt_proc
.process_all_events