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 # Portable game to destroy asteroids
18 app_namespace
"org.nitlanguage.asteronits"
19 app_version
(1, 0, git_revision
)
21 android_manifest_activity
"""android:screenOrientation="sensorLandscape""""
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]]
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]]
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]]
46 # Tiny meteors, organized by color
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]]
54 var ships: Array[Texture] = [enemy_green1]
59 # Current world in play
60 var world = new World(12, 2, display.aspect_ratio) is lazy
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
")
71 # Move the camera to show all the world world in the screen range
72 world_camera.reset_height(world.half_height * 2.0)
75 # Main spritesheet with ships, asteroids and beams
76 var spritesheet = new Spritesheet
83 # Setup new world if all asteroids are destroyed
84 if world.asteroids.is_empty then
86 world = new World(world.n_asteroids*2, world.n_asteroid_parts+1, display.aspect_ratio)
90 redef fun accept_event(event)
92 if super then return true
94 if event isa QuitEvent then
96 else if event isa KeyEvent then
97 var thrust = event.thrust
99 app.world.ship.applied_thrust = if event.is_down then thrust else 0.0
103 var rot = event.rotation
105 app.world.ship.applied_rotation = if event.is_down then rot else 0.0
109 if event.name == "space
" and event.is_down then
112 else if event.name == "escape
" then
114 else if event.name == "." and event.is_down then
115 dynamic_resolution_ratio *= 2.0
116 print dynamic_resolution_ratio
117 else if event.name == "," and event.is_down then
118 dynamic_resolution_ratio /= 2.0
119 print dynamic_resolution_ratio
127 redef class SpacialObject
128 # Main `Sprite` to draw for this object
129 var sprite: Sprite is noinit
131 # All `Sprites` composing this object
132 var sprites: Collection[Sprite] = new Ref[Sprite](sprite) is lazy
134 init do app.sprites.add_all sprites
136 redef fun do_turn(dt)
139 sprite.rotation = rotation - pi/2.0
145 for s in sprites do app.sprites.remove s
153 # Select texture from `size` and `color`
154 var tex = if size == 3 then
155 app.spritesheet.meteors_big[color].rand
156 else if size == 2 then
157 app.spritesheet.meteors_med[color].rand
158 else app.spritesheet.meteors_small[color].rand
160 sprite = new Sprite(tex, center)
167 app.fx_explosion_asteroids.play
174 sprite = new Sprite(app.spritesheet.laser_blue01, center)
182 sprite = new Sprite(app.spritesheet.ships.rand, center)
183 sprites = [sprite, thrust_sprite]
188 private var thrust_sprite = new Sprite(app.spritesheet.fire09, new Point3d[Float](0.0, 0.0, 0.0))
190 private var sprites_with_fire: Array[Sprite] = [thrust_sprite, sprite] is lazy
192 redef fun do_turn(dt)
196 # Update position of the thrust sprite
197 var dist_to_engine = 45.0
198 thrust_sprite.center.x = center.x - dist_to_engine*rotation.cos
199 thrust_sprite.center.y = center.y - dist_to_engine*rotation.sin
200 thrust_sprite.center.z = center.z
201 thrust_sprite.rotation = rotation - pi/2.0
203 # Show or hide the thrust sprite
204 if applied_thrust > 0.0 then
205 thrust_sprite.alpha = 1.0
207 else if thrust_sprite.alpha > 0.0 then
208 thrust_sprite.alpha -= dt*4.0
209 if thrust_sprite.alpha < 0.0 then thrust_sprite.alpha = 0.0
212 # HACK, the "enemy
" ship used for the player points downwards
213 sprite.rotation += pi
225 app.fx_explosion_ship.play
231 # How does this event affect the ship thrust?
234 if name == "up
" or name == "w
" then return 1.0
238 # How does this event affect the ship thrust?
241 if name == "right
" or name == "d
" then return -1.0
242 if name == "left
" or name == "a
" then return 1.0