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 module action_nitro
is
16 app_name
"Action Nitro"
17 app_namespace
"net.xymus.action_nitro"
18 app_version
(1, 0, git_revision
)
20 android_manifest_activity
"""android:screenOrientation="sensorLandscape""""
26 import gamnit::limit_fps
36 var world: World = new World is lazy
41 # Textures of the biplane, jet, helicopter, parachute and powerups
42 var planes_sheet = new PlanesImages
44 # Animation for the player movement
45 private var running_texture = new Texture("textures
/player
.png
")
46 private var running_animation: Animation = running_texture.to_animation(10.0, 12, 0)
49 private var iss_model = new Model("models
/iss
.obj
")
54 private var ground_texture = new Texture("textures
/fastgras01
.jpg
")
55 private var tree_texture = new Texture("textures
/Tree03.png
")
60 private var splatter_texture = new Texture("textures
/blood_splatter
.png
")
61 private var splatter_material: TexturedMaterial do
62 var mat = new TexturedMaterial([1.0]*4, [0.0]*4, [0.0]*4)
63 mat.ambient_texture = splatter_texture
66 private var splatter_model = new LeafModel(new Plane, splatter_material)
71 private var city_texture = new TextureAsset("textures
/city_background_clean
.png
")
73 private var stars_texture = new Texture("textures
/stars
.jpg
")
74 private var stars = new Sprite(stars_texture, new Point3d[Float](0.0, 1100.0, -600.0)) is lazy
80 var explosions = new ParticleSystem(20, explosion_program,
81 new Texture("particles
/explosion00
.png
"))
83 # Blood explosion particles
84 var blood = new ParticleSystem(20, explosion_program,
85 new Texture("particles
/blood07
.png
"))
87 # Smoke for the background
88 var smoke = new ParticleSystem(500, smoke_program,
89 new Texture("particles
/blackSmoke12
.png
"))
91 # Static clouds particles
92 var clouds = new ParticleSystem(1600, static_program,
93 new Texture("particles
/whitePuff12
.png
"))
99 #private var fx_fire = new Sound("sounds
/fire
.mp3
")
103 private var texts_sheet = new TextsImages
105 private var tutorial_wasd = new Sprite(app.texts_sheet.tutorial_wasd,
106 app.ui_camera.center.offset(0.0, -250.0, 0.0)) is lazy
108 private var tutorial_arrows = new Sprite(app.texts_sheet.tutorial_arrows,
109 app.ui_camera.center.offset(0.0, -350.0, 0.0)) is lazy
111 private var tutorial_chute = new Sprite(app.texts_sheet.tutorial_chute,
112 app.ui_camera.center.offset(0.0, -450.0, 0.0)) is lazy
114 private var tutorial_goal = new Sprite(app.texts_sheet.goal,
115 app.ui_camera.center.offset(0.0, 0.0, 0.0)) is lazy
117 private var outro_directed = new Sprite(app.texts_sheet.directed,
118 app.ui_camera.center.offset(0.0, 400.0, 0.0)) is lazy
120 private var outro_created = new Sprite(app.texts_sheet.created,
121 app.ui_camera.center.offset(0.0, -200.0, 0.0)) is lazy
124 # Counters for the UI
126 private var score_counter = new CounterSprites(texts_sheet.n,
127 new Point3d[Float](32.0, -64.0, 0.0))
129 private var altitude_counter = new CounterSprites(texts_sheet.n,
130 new Point3d[Float](1400.0, -64.0, 0.0))
132 # Did the player asked to skip the intro animation?
133 private var skip_intro = false
139 show_splash_screen new Texture("textures
/splash
.jpg
")
143 if iss_model.errors.not_empty then print_error iss_model.errors.join("\n
")
146 world_camera.reset_height 60.0
147 ui_camera.reset_height 1080.0
149 # Register particle systems
150 particle_systems.add explosions
151 particle_systems.add blood
152 particle_systems.add smoke
153 particle_systems.add clouds
160 city_texture.pixelated = true
161 var city_sprite = new Sprite(city_texture, new Point3d[Float](0.0, 370.0, -600.0))
162 city_sprite.scale = 0.8
163 sprites.add city_sprite
166 var ground_mesh = new Plane
167 ground_mesh.repeat_x = 100.0
168 ground_mesh.repeat_y = 100.0
170 var ground_material = new TexturedMaterial(
171 [0.0, 0.1, 0.0, 1.0], [0.4, 0.4, 0.4, 1.0], [0.0]*4)
172 ground_material.diffuse_texture = ground_texture
174 var ground_model = new LeafModel(ground_mesh, ground_material)
175 var ground = new Actor(ground_model, new Point3d[Float](0.0, 0.0, 0.0))
176 ground.scale = 5000.0
180 for i in 2000.times do
181 var s = 0.1 + 0.1.rand
182 var h = tree_texture.height * s
183 var sprite = new Sprite(tree_texture,
184 new Point3d[Float](0.0 & 1500.0, h/2.0 - 10.0*s, 10.0 - 609.0.rand))
191 sprite.tint = [c, 1.0, c, 1.0]
195 var no_clouds_layer = 200.0
196 for i in [0 .. 32[ do
198 var x = 0.0 & 1000.0 * zp
199 var y = no_clouds_layer + (world.boss_altitude - no_clouds_layer*2.0).rand
200 var z = -500.0*zp - 10.0
206 clouds.add(new Point3d[Float](x+2.0*a.cos*rj, y+a.sin*rj, z & 1.0),
207 48000.0 & 16000.0, inf)
211 # Move the sun to best light the ISS
212 light.position.x = 2000.0
213 light.position.z = 4000.0
215 # Prepare for intro animation
216 ui_sprites.add tutorial_goal
217 world_camera.far = 1024.0
225 # Update background color
226 var player = world.player
227 var player_pos = if player != null then player.center else new Point3d[Float](0.0, 200.0, 0.0)
228 var altitude = player_pos.y
229 var p = altitude / world.boss_altitude
231 glClearColor(0.3*ip, 0.3*ip, ip, 1.0)
232 stars.alpha = (1.4*p-0.4).clamp(0.0, 1.0)
236 new Point3d[Float](291.0, 338.0, -601.0),
237 new Point3d[Float](-356.0, 422.0, -601.0)]
243 new Point3d[Float](pos.x & r, pos.y & r, pos.z & r),
244 96000.0 & 16000.0, 10.0)
248 world_camera.position.x = player_pos.x
249 world_camera.position.y = player_pos.y + 5.0
253 var intro_duration = 8.0
254 if t < intro_duration and not skip_intro then
255 var pitch = t/intro_duration
256 pitch = (pitch*pi).sin
257 world_camera.pitch = pitch
261 if world.player == null then
262 world_camera.pitch = 0.0
263 world_camera.far = 700.0
269 score_counter.value = world.score
271 if world.player != null then alt = world.player.altitude.to_i
272 altitude_counter.value = alt
274 # General movement on the X axis
275 if player != null then
277 if pressed_keys.has("left
") then player.moving -= 1.0
278 if pressed_keys.has("right
") then player.moving += 1.0
281 # Try to fire as long as a key is pressed
282 if pressed_keys.not_empty then
284 if pressed_keys.has("a
") then
285 if pressed_keys.has("w
") then
287 else if pressed_keys.has("s
") then
292 else if pressed_keys.has("d
") then
293 if pressed_keys.has("w
") then
295 else if pressed_keys.has("s
") then
300 else if pressed_keys.has("w
") then
302 else if pressed_keys.has("s
") then
306 if a != inf and player != null then
307 player.shoot(a, world)
312 # Low-gravity controls
313 if player != null and player.is_alive and player.altitude >= world.boss_altitude then
315 for key in pressed_keys do
317 player.inertia.y += d
318 else if key == "down
" then
319 player.inertia.y -= d
320 else if key == "left
" then
321 player.inertia.x -= d
322 else if key == "right
" then
323 player.inertia.x += d
330 if won_at == null then
331 var boss = world.boss
332 if boss != null and not boss.is_alive then
333 self.won_at = world.t
337 var t_since_won = world.t - won_at
338 if t_since_won > 1.0 and not ui_sprites.has(outro_directed) then ui_sprites.add outro_directed
339 if t_since_won > 2.0 and not ui_sprites.has(outro_created) then ui_sprites.add outro_created
343 # Begin playing, after intro if `initial`, otherwise after death
344 fun begin_play(initial: Bool)
349 world.planes.add new Airplane(new Point3d[Float](0.0, world.player.center.y - 10.0, 0.0), 16.0, 4.0)
353 ui_sprites.add_all([tutorial_wasd, tutorial_arrows, tutorial_chute])
357 # Seconds at which the game was won, using `world.t` as reference
358 private var won_at: nullable Float = null
360 # Remove the tutorial sprite about WASD from `ui_sprites`
361 private fun hide_tutorial_wasd do if ui_sprites.has(tutorial_wasd) then ui_sprites.remove(tutorial_wasd)
363 # Remove the tutorial sprite about arrows from `ui_sprites`
364 private fun hide_tutorial_arrows do if ui_sprites.has(tutorial_arrows) then ui_sprites.remove(tutorial_arrows)
366 # Remove the tutorial sprite about the parachute from `ui_sprites`
367 private fun hide_tutorial_chute do if ui_sprites.has(tutorial_chute) then ui_sprites.remove(tutorial_chute)
369 redef fun accept_event(event)
371 if super then return true
373 if event isa QuitEvent then
376 else if event isa KeyEvent then
377 if event.name == "escape
" and event.is_down then
382 var player = world.player
383 if player != null and player.is_alive then
385 # Hide tutorial about arrows once they are used
386 var arrows = once ["left
", "right
"]
387 if arrows.has(event.name) then hide_tutorial_arrows
389 if player.altitude < world.boss_altitude then
390 if event.name == "space
" and event.is_down and not player.parachute_deployed and player.plane == null then
392 if player.parachute_deployed then
393 var pc = player.center
394 world.parachute = new Parachute(new Point3d[Float](pc.x, pc.y + 5.0, pc.z-0.1), 8.0, 5.0)
399 if (event.name == "space
" or event.name == "up
") and event.is_down then
403 if event.name == "left
" then
404 var mod = if event.is_down then -1.0 else 1.0
407 else if event.name == "right
" then
408 var mod = if event.is_down then 1.0 else -1.0
416 # When player is dead, respawn on spacebar or pointer depressed
417 if (event isa KeyEvent and event.name == "space
") or
418 (event isa PointerEvent and not event.is_move and event.depressed) then
419 var player = world.player
420 if player == null then
422 else if not player.is_alive then
432 # Sprite representing this entity if there is no `actor`
433 fun sprite: Sprite is abstract
436 fun actor: nullable Actor do return null
441 if actor != null then
443 else app.sprites.add sprite
446 redef fun destroy(world)
451 if actor != null then
452 app.actors.remove actor
453 else app.sprites.remove sprite
465 # Show death animation (explosion)
472 new Point3d[Float](center.x & force, center.y & force, center.z & force),
473 (2048.0 & 4096.0) * force, 0.3 & 0.1)
479 init do sprite.scale = width/sprite.texture.width
481 redef fun update(dt, world)
485 if inertia.x < 0.0 then
486 sprite.invert_x = false
487 else if inertia.x > 0.0 then
488 sprite.invert_x = true
494 private fun texture: Texture do return if center.y < 600.0 then app.planes_sheet.biplane else app.planes_sheet.jet
496 redef var sprite = new Sprite(texture, center) is lazy
499 redef class Helicopter
500 redef var sprite = new Sprite(app.planes_sheet.helicopter, center) is lazy
504 redef var actor is lazy do
505 var actor = new Actor(app.iss_model, center)
510 redef fun death_animation
513 app.explosions.add(center, 4096.0 * force, 0.3)
514 for i in (8.0*force).to_i.times do
516 new Point3d[Float](center.x & force, center.y & force/8.0, center.z & force),
517 (2048.0 & 1024.0) * force, 0.3 + 5.0.rand, 5.0.rand)
523 redef var sprite = new Sprite(app.running_animation.frames.rand, center) is lazy
524 init do sprite.scale = width/sprite.texture.width * 2.0
527 redef class Parachute
528 redef var sprite = new Sprite(app.planes_sheet.parachute, center) is lazy
529 init do sprite.scale = width / sprite.texture.width
533 redef var sprite = new Sprite(app.running_animation.frames.last, center) is lazy
534 init do sprite.scale = width/sprite.texture.width * 2.0
536 # Update current animation
539 if moving == 0.0 then
541 else sprite.animate(app.running_animation, -1.0)
544 redef fun update(dt, world)
548 sprite.invert_x = false
549 else if moving < 0.0 then
550 sprite.invert_x = true
558 if center.y < 10.0 then
559 # Blood splatter on the ground
560 var splatter = new Actor(app.splatter_model,
561 new Point3d[Float](center.x, 0.05 & 0.04, center.y))
562 splatter.scale = 32.0
563 splatter.yaw = 2.0*pi.rand
564 app.actors.add splatter
567 # Display respawn instructions
568 app.ui_sprites.add new Sprite(app.texts_sheet.respawn, app.ui_camera.center.offset(0.0, 0.0, 0.0))
573 redef var sprite = new Sprite(weapon.bullet_texture, center) is lazy
577 sprite.rotation = angle
582 fun bullet_texture: Texture do return app.planes_sheet.bullet_ak
586 redef fun bullet_texture do return app.planes_sheet.bullet_pistol
589 redef class RocketLauncher
590 redef fun bullet_texture do return app.planes_sheet.bullet_rocket
594 # Scale so it looks like 5 world units wide, not matter the size of the texture
595 init do sprite.scale = 5.0/sprite.texture.width
599 redef var sprite = new Sprite(app.planes_sheet.ak, center) is lazy
602 redef class RocketLauncherPU
603 redef var sprite = new Sprite(app.planes_sheet.rocket, center) is lazy
607 redef var sprite = new Sprite(app.planes_sheet.health, center) is lazy
608 init do sprite.scale = 3.0/sprite.texture.height
613 redef fun explode(center, force)
618 app.explosions.add(center, 8192.0 * force, 0.3)
619 for i in (4.0*force).to_i.times do
621 new Point3d[Float](center.x & force, center.y & force/2.0, center.z & force),
622 (4096.0 & 2048.0) * force, 0.3 & 0.3, 0.5.rand)
628 # Pad a number with `0`s on the left side to reach `size` digits
629 private fun pad(size: Int): String
632 var d = size - s.length
633 if d > 0 then s = "0"*d + s
638 # Manager to display numbers in sprite
641 # TODO clean up and move up to lib
643 # Number textures, from 0 to 9
645 # Require: `textures.length == 10`
646 var textures: Array[Texture]
648 # Center of the first digit in UI coordinates
649 var anchor: Point3d[Float]
651 # Last set of sprites generated to display the `value=`
652 private var sprites = new Array[Sprite]
654 # Update the value displayed by the counter by inserting new sprites into `app.ui_sprites`
655 fun value=(value: Int)
657 # Clean up last used sprites
658 for s in sprites do if app.ui_sprites.has(s) then app.ui_sprites.remove s
662 var s = value.to_s # TODO manipulate ints directly
666 var tex = textures[i]
669 sprites.add new Sprite(tex, new Point3d[Float](anchor.x + x, anchor.y, anchor.z))
673 # Register sprites to be drawn by `app.ui_camera`
674 app.ui_sprites.add_all sprites
678 redef class SmokeProgram
680 # Redef source to get particles that move up faster
681 redef fun vertex_shader_core do return """
684 c.x += dt * dt * 2.0;
686 gl_Position = c * mvp;
687 gl_PointSize = scale / gl_Position.z * (pt+0.1);
690 v_color.a = pt / 0.1;
692 v_color.a = 1.0 - pt*0.9;