asteronits: convert sound effect from a broken mp3 to ogg
[nit.git] / contrib / asteronits / src / asteronits.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 # Portable game to destroy asteroids
16 module asteronits is
17 app_name "Asteronits"
18 app_namespace "org.nitlanguage.asteronits"
19 app_version(1, 0, git_revision)
20
21 android_manifest_activity """android:screenOrientation="sensorLandscape""""
22 android_api_target 10
23 end
24
25 import gamnit::flat
26
27 import game_logic
28 import spritesheet
29 import app::audio
30
31 redef class Spritesheet
32 # Largest meteors, organized by color
33 var meteors_big: Array[Array[Texture]] = [
34 [meteor_brown_big1, meteor_brown_big2, meteor_brown_big3, meteor_brown_big4],
35 [meteor_grey_big1, meteor_grey_big2, meteor_grey_big3, meteor_grey_big4]]
36
37 # Medium size meteors, organized by color
38 var meteors_med: Array[Array[Texture]] = [
39 [meteor_brown_med1, meteor_brown_med3],
40 [meteor_grey_med1, meteor_grey_med2]]
41
42 # Small meteors, organized by color
43 var meteors_small: Array[Array[Texture]] = [
44 [meteor_brown_small1, meteor_brown_small2],
45 [meteor_grey_small1, meteor_grey_small2]]
46
47 # Tiny meteors, organized by color
48 #
49 # TODO use these in particles
50 var meteors_tiny: Array[Array[Texture]] = [
51 [meteor_brown_tiny1, meteor_brown_tiny2],
52 [meteor_grey_tiny1, meteor_grey_tiny2]]
53
54 # Available ships
55 var ships: Array[Texture] = [enemy_green1]
56 end
57
58 redef class App
59
60 # Current world in play
61 var world = new World(12, 2, display.aspect_ratio) is lazy
62
63 # Sound effects
64 private var fx_fire = new Sound("sounds/fire.ogg")
65 private var fx_explosion_ship = new Sound("sounds/explosion_ship.wav")
66 private var fx_explosion_asteroids = new Sound("sounds/explosion_asteroids.wav")
67
68 redef fun on_create
69 do
70 super
71
72 # Move the camera to show all the world world in the screen range
73 world_camera.reset_height(world.half_height * 2.0)
74 end
75
76 # Main spritesheet with ships, asteroids and beams
77 var spritesheet = new Spritesheet
78
79 redef fun update(dt)
80 do
81 # Update game logic
82 world.do_turn dt
83
84 # Setup new world if all asteroids are destroyed
85 if world.asteroids.is_empty then
86 sprites.clear
87 world = new World(world.n_asteroids*2, world.n_asteroid_parts+1, display.aspect_ratio)
88 end
89 end
90
91 redef fun accept_event(event)
92 do
93 if event isa QuitEvent then
94 exit 0
95 else if event isa KeyEvent then
96 var thrust = event.thrust
97 if thrust != 0.0 then
98 app.world.ship.applied_thrust = if event.is_down then thrust else 0.0
99 return true
100 end
101
102 var rot = event.rotation
103 if rot != 0.0 then
104 app.world.ship.applied_rotation = if event.is_down then rot else 0.0
105 return true
106 end
107
108 if event.name == "space" and event.is_down then
109 app.world.ship.fire
110 return true
111 else if event.name == "escape" then
112 exit 0
113 else if event.name == "." and event.is_down then
114 dynamic_resolution_ratio *= 2.0
115 print dynamic_resolution_ratio
116 else if event.name == "," and event.is_down then
117 dynamic_resolution_ratio /= 2.0
118 print dynamic_resolution_ratio
119 end
120 end
121
122 return false
123 end
124 end
125
126 redef class SpacialObject
127 # Main `Sprite` to draw for this object
128 var sprite: Sprite is noinit
129
130 # All `Sprites` composing this object
131 var sprites: Collection[Sprite] = new Ref[Sprite](sprite) is lazy
132
133 init do app.sprites.add_all sprites
134
135 redef fun do_turn(dt)
136 do
137 super
138 sprite.rotation = rotation - pi/2.0
139 end
140
141 redef fun destroy
142 do
143 super
144 for s in sprites do app.sprites.remove s
145 end
146 end
147
148 redef class Asteroid
149
150 init
151 do
152 # Select texture from `size` and `color`
153 var tex = if size == 3 then
154 app.spritesheet.meteors_big[color].rand
155 else if size == 2 then
156 app.spritesheet.meteors_med[color].rand
157 else app.spritesheet.meteors_small[color].rand
158
159 sprite = new Sprite(tex, center)
160 super
161 end
162
163 redef fun destroy
164 do
165 super
166 app.fx_explosion_asteroids.play
167 end
168 end
169
170 redef class Bullet
171 init
172 do
173 sprite = new Sprite(app.spritesheet.laser_blue01, center)
174 super
175 end
176 end
177
178 redef class Ship
179 init
180 do
181 sprite = new Sprite(app.spritesheet.ships.rand, center)
182 sprites = [sprite, thrust_sprite]
183
184 super
185 end
186
187 private var thrust_sprite = new Sprite(app.spritesheet.fire09, new Point3d[Float](0.0, 0.0, 0.0))
188
189 private var sprites_with_fire: Array[Sprite] = [thrust_sprite, sprite] is lazy
190
191 redef fun do_turn(dt)
192 do
193 super
194
195 # Update position of the thrust sprite
196 var dist_to_engine = 45.0
197 thrust_sprite.center.x = center.x - dist_to_engine*rotation.cos
198 thrust_sprite.center.y = center.y - dist_to_engine*rotation.sin
199 thrust_sprite.center.z = center.z
200 thrust_sprite.rotation = rotation - pi/2.0
201
202 # Show or hide the thrust sprite
203 if applied_thrust > 0.0 then
204 thrust_sprite.alpha = 1.0
205
206 else if thrust_sprite.alpha > 0.0 then
207 thrust_sprite.alpha -= dt*4.0
208 if thrust_sprite.alpha < 0.0 then thrust_sprite.alpha = 0.0
209 end
210
211 # HACK, the "enemy" ship used for the player points downwards
212 sprite.rotation += pi
213 end
214
215 redef fun fire
216 do
217 super
218 app.fx_fire.play
219 end
220
221 redef fun hit
222 do
223 super
224 app.fx_explosion_ship.play
225 end
226 end
227
228 redef class KeyEvent
229
230 # How does this event affect the ship thrust?
231 fun thrust: Float
232 do
233 if name == "up" or name == "w" then return 1.0
234 return 0.0
235 end
236
237 # How does this event affect the ship thrust?
238 fun rotation: Float
239 do
240 if name == "right" or name == "d" then return -1.0
241 if name == "left" or name == "a" then return 1.0
242 return 0.0
243 end
244 end