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