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""""
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]]
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]]
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]]
47 # Tiny meteors, organized by color
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]]
55 var ships: Array[Texture] = [enemy_green1]
60 # Current world in play
61 var world = new World(12, 2, display.aspect_ratio) is lazy
64 private var fx_fire = new Sound("sounds
/fire
.mp3
")
65 private var fx_explosion_ship = new Sound("sounds
/explosion_ship
.wav
")
66 private var fx_explosion_asteroids = new Sound("sounds
/explosion_asteroids
.wav
")
72 # Move the camera to show all the world world in the screen range
73 world_camera.reset_height(world.half_height * 2.0)
76 # Main spritesheet with ships, asteroids and beams
77 var spritesheet = new Spritesheet
84 # Setup new world if all asteroids are destroyed
85 if world.asteroids.is_empty then
87 world = new World(world.n_asteroids*2, world.n_asteroid_parts+1, display.aspect_ratio)
91 redef fun accept_event(event)
93 if event isa QuitEvent then
95 else if event isa KeyEvent then
96 var thrust = event.thrust
98 app.world.ship.applied_thrust = if event.is_down then thrust else 0.0
102 var rot = event.rotation
104 app.world.ship.applied_rotation = if event.is_down then rot else 0.0
108 if event.name == "space
" and event.is_down then
111 else if event.name == "escape
" then
120 redef class SpacialObject
121 # Main `Sprite` to draw for this object
122 var sprite: Sprite is noinit
124 # All `Sprites` composing this object
125 var sprites: Collection[Sprite] = new Ref[Sprite](sprite) is lazy
127 init do app.sprites.add_all sprites
129 redef fun do_turn(dt)
132 sprite.rotation = rotation - pi/2.0
138 for s in sprites do app.sprites.remove s
146 # Select texture from `size` and `color`
147 var tex = if size == 3 then
148 app.spritesheet.meteors_big[color].rand
149 else if size == 2 then
150 app.spritesheet.meteors_med[color].rand
151 else app.spritesheet.meteors_small[color].rand
153 sprite = new Sprite(tex, center)
160 app.fx_explosion_asteroids.play
167 sprite = new Sprite(app.spritesheet.laser_blue01, center)
175 sprite = new Sprite(app.spritesheet.ships.rand, center)
176 sprites = [sprite, thrust_sprite]
181 private var thrust_sprite = new Sprite(app.spritesheet.fire09, new Point3d[Float](0.0, 0.0, 0.0))
183 private var sprites_with_fire: Array[Sprite] = [thrust_sprite, sprite] is lazy
185 redef fun do_turn(dt)
189 # Update position of the thrust sprite
190 var dist_to_engine = 45.0
191 thrust_sprite.center.x = center.x - dist_to_engine*rotation.cos
192 thrust_sprite.center.y = center.y - dist_to_engine*rotation.sin
193 thrust_sprite.center.z = center.z
194 thrust_sprite.rotation = rotation - pi/2.0
196 # Show or hide the thrust sprite
197 if applied_thrust > 0.0 then
198 thrust_sprite.alpha = 1.0
200 else if thrust_sprite.alpha > 0.0 then
201 thrust_sprite.alpha -= dt*4.0
202 if thrust_sprite.alpha < 0.0 then thrust_sprite.alpha = 0.0
205 # HACK, the "enemy
" ship used for the player points downwards
206 sprite.rotation += pi
218 app.fx_explosion_ship.play
224 # How does this event affect the ship thrust?
227 if is_arrow_up or name == "w
" then return 1.0
231 # How does this event affect the ship thrust?
234 if is_arrow_right or name == "d
" then return -1.0
235 if is_arrow_left or name == "a
" then return 1.0