action_nitro: animate the parachute opening
[nit.git] / contrib / action_nitro / src / action_nitro.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 module action_nitro is
16 app_name "Action Nitro"
17 app_namespace "net.xymus.action_nitro"
18 app_version(1, 0, git_revision)
19
20 android_manifest_activity """android:screenOrientation="sensorLandscape""""
21 android_api_target 10
22 end
23
24 import gamnit::depth
25 import gamnit::keys
26 import gamnit::limit_fps
27
28 import game
29
30 import gen::texts
31 import gen::planes
32
33 redef class App
34
35 # Game world
36 var world: World = new World is lazy
37
38 # ---
39 # Game world assets
40
41 # Textures of the biplane, jet, helicopter, parachute and powerups
42 var planes_sheet = new PlanesImages
43
44 # Animation when opening the parachute
45 var parachute_animation = new Animation(planes_sheet.parachute, 16.0)
46
47 # Animation for the player movement
48 private var running_texture = new Texture("textures/player.png")
49 private var running_animation: Animation = running_texture.to_animation(10.0, 12, 0)
50
51 # Boss 3D model
52 private var iss_model = new Model("models/iss.obj")
53
54 # ---
55 # Ground textures
56
57 private var ground_texture = new Texture("textures/fastgras01.jpg")
58 private var tree_texture = new Texture("textures/Tree03.png")
59
60 # ---
61 # Blood splatter
62
63 private var splatter_texture = new Texture("textures/blood_splatter.png")
64 private var splatter_material: TexturedMaterial do
65 var mat = new TexturedMaterial([1.0]*4, [0.0]*4, [0.0]*4)
66 mat.ambient_texture = splatter_texture
67 return mat
68 end
69 private var splatter_model = new LeafModel(new Plane, splatter_material)
70
71 # ---
72 # Background
73
74 private var city_texture = new TextureAsset("textures/city_background_clean.png")
75
76 private var stars_texture = new Texture("textures/stars.jpg")
77 private var stars = new Sprite(stars_texture, new Point3d[Float](0.0, 1100.0, -600.0)) is lazy
78
79 # ---
80 # Particle effects
81
82 # Explosion particles
83 var explosions = new ParticleSystem(20, explosion_program,
84 new Texture("particles/explosion00.png"))
85
86 # Blood explosion particles
87 var blood = new ParticleSystem(20, explosion_program,
88 new Texture("particles/blood07.png"))
89
90 # Smoke for the background
91 var smoke = new ParticleSystem(500, smoke_program,
92 new Texture("particles/blackSmoke12.png"))
93
94 # Static clouds particles
95 var clouds = new ParticleSystem(1600, static_program,
96 new Texture("particles/whitePuff12.png"))
97
98 # ---
99 # Sound effects
100
101 # TODO
102 #private var fx_fire = new Sound("sounds/fire.mp3")
103
104 # ---
105 # UI
106 private var texts_sheet = new TextsImages
107
108 private var tutorial_wasd = new Sprite(app.texts_sheet.tutorial_wasd,
109 app.ui_camera.center.offset(0.0, -250.0, 0.0)) is lazy
110
111 private var tutorial_arrows = new Sprite(app.texts_sheet.tutorial_arrows,
112 app.ui_camera.center.offset(0.0, -350.0, 0.0)) is lazy
113
114 private var tutorial_chute = new Sprite(app.texts_sheet.tutorial_chute,
115 app.ui_camera.center.offset(0.0, -450.0, 0.0)) is lazy
116
117 private var tutorial_goal = new Sprite(app.texts_sheet.goal,
118 app.ui_camera.center.offset(0.0, 0.0, 0.0)) is lazy
119
120 private var outro_directed = new Sprite(app.texts_sheet.directed,
121 app.ui_camera.center.offset(0.0, 400.0, 0.0)) is lazy
122
123 private var outro_created = new Sprite(app.texts_sheet.created,
124 app.ui_camera.center.offset(0.0, -200.0, 0.0)) is lazy
125
126 # ---
127 # Counters for the UI
128
129 private var score_counter = new CounterSprites(texts_sheet.n,
130 new Point3d[Float](32.0, -64.0, 0.0))
131
132 private var altitude_counter = new CounterSprites(texts_sheet.n,
133 new Point3d[Float](1400.0, -64.0, 0.0))
134
135 # Did the player asked to skip the intro animation?
136 private var skip_intro = false
137
138 redef fun on_create
139 do
140 super
141
142 show_splash_screen new Texture("textures/splash.jpg")
143
144 # Load 3d models
145 iss_model.load
146 if iss_model.errors.not_empty then print_error iss_model.errors.join("\n")
147
148 # Setup cameras
149 world_camera.reset_height 60.0
150 ui_camera.reset_height 1080.0
151
152 # Register particle systems
153 particle_systems.add explosions
154 particle_systems.add blood
155 particle_systems.add smoke
156 particle_systems.add clouds
157
158 # Stars background
159 sprites.add stars
160 stars.scale = 2.1
161
162 # City background
163 city_texture.pixelated = true
164 var city_sprite = new Sprite(city_texture, new Point3d[Float](0.0, 370.0, -600.0))
165 city_sprite.scale = 0.8
166 sprites.add city_sprite
167
168 # Setup ground
169 var ground_mesh = new Plane
170 ground_mesh.repeat_x = 100.0
171 ground_mesh.repeat_y = 100.0
172
173 var ground_material = new TexturedMaterial(
174 [0.0, 0.1, 0.0, 1.0], [0.4, 0.4, 0.4, 1.0], [0.0]*4)
175 ground_material.diffuse_texture = ground_texture
176
177 var ground_model = new LeafModel(ground_mesh, ground_material)
178 var ground = new Actor(ground_model, new Point3d[Float](0.0, 0.0, 0.0))
179 ground.scale = 5000.0
180 actors.add ground
181
182 # Trees
183 for i in 2000.times do
184 var s = 0.1 + 0.1.rand
185 var h = tree_texture.height * s
186 var sprite = new Sprite(tree_texture,
187 new Point3d[Float](0.0 & 1500.0, h/2.0 - 10.0*s, 10.0 - 609.0.rand))
188 sprite.static = true
189 sprite.scale = s
190 sprites.add sprite
191
192 var c = 1.0.rand
193 c *= 0.7
194 sprite.tint = [c, 1.0, c, 1.0]
195 end
196
197 # Clouds
198 var no_clouds_layer = 200.0
199 for i in [0 .. 32[ do
200 var zp = 1.0.rand
201 var x = 0.0 & 1000.0 * zp
202 var y = no_clouds_layer + (world.boss_altitude - no_clouds_layer*2.0).rand
203 var z = -500.0*zp - 10.0
204
205 var r = 50.0 & 1.0
206 for j in [0..32[ do
207 var a = 2.0*pi.rand
208 var rj = r.rand
209 clouds.add(new Point3d[Float](x+2.0*a.cos*rj, y+a.sin*rj, z & 1.0),
210 48000.0 & 16000.0, inf)
211 end
212 end
213
214 # Move the sun to best light the ISS
215 light.position.x = 2000.0
216 light.position.z = 4000.0
217
218 # Prepare for intro animation
219 ui_sprites.add tutorial_goal
220 world_camera.far = 1024.0
221 end
222
223 redef fun update(dt)
224 do
225 # Game logic
226 world.update dt
227
228 # Update background color
229 var player = world.player
230 var player_pos = if player != null then player.center else new Point3d[Float](0.0, 200.0, 0.0)
231 var altitude = player_pos.y
232 var p = altitude / world.boss_altitude
233 var ip = 1.0 - p
234 glClearColor(0.3*ip, 0.3*ip, ip, 1.0)
235 stars.alpha = (1.4*p-0.4).clamp(0.0, 1.0)
236
237 # Randomly add smoke
238 var poss = [
239 new Point3d[Float](291.0, 338.0, -601.0),
240 new Point3d[Float](-356.0, 422.0, -601.0)]
241
242 var r = 8.0
243 if 2.rand == 0 then
244 var pos = poss.rand
245 smoke.add(
246 new Point3d[Float](pos.x & r, pos.y & r, pos.z & r),
247 96000.0 & 16000.0, 10.0)
248 end
249
250 # Move camera
251 world_camera.position.x = player_pos.x
252 world_camera.position.y = player_pos.y + 5.0
253
254 # Cinematic?
255 var t = world.t
256 var intro_duration = 8.0
257 if t < intro_duration and not skip_intro then
258 var pitch = t/intro_duration
259 pitch = (pitch*pi).sin
260 world_camera.pitch = pitch
261 return
262 end
263
264 if world.player == null then
265 world_camera.pitch = 0.0
266 world_camera.far = 700.0
267
268 begin_play true
269 end
270
271 # Update counters
272 score_counter.value = world.score
273 var alt = 0
274 if world.player != null then alt = world.player.altitude.to_i
275 altitude_counter.value = alt
276
277 # General movement on the X axis
278 if player != null then
279 player.moving = 0.0
280 if pressed_keys.has("left") then player.moving -= 1.0
281 if pressed_keys.has("right") then player.moving += 1.0
282 end
283
284 # Try to fire as long as a key is pressed
285 if pressed_keys.not_empty then
286 var a = inf
287 if pressed_keys.has("a") then
288 if pressed_keys.has("w") then
289 a = 0.75 * pi
290 else if pressed_keys.has("s") then
291 a = 1.25 * pi
292 else
293 a = pi
294 end
295 else if pressed_keys.has("d") then
296 if pressed_keys.has("w") then
297 a = 0.25 * pi
298 else if pressed_keys.has("s") then
299 a = 1.75 * pi
300 else
301 a = 0.0
302 end
303 else if pressed_keys.has("w") then
304 a = 0.50 * pi
305 else if pressed_keys.has("s") then
306 a = 1.50 * pi
307 end
308
309 if a != inf and player != null then
310 player.shoot(a, world)
311 hide_tutorial_wasd
312 end
313 end
314
315 # Low-gravity controls
316 if player != null and player.is_alive and player.altitude >= world.boss_altitude then
317 var d = 50.0*dt
318 for key in pressed_keys do
319 if key == "up" then
320 player.inertia.y += d
321 else if key == "down" then
322 player.inertia.y -= d
323 else if key == "left" then
324 player.inertia.x -= d
325 else if key == "right" then
326 player.inertia.x += d
327 end
328 end
329 end
330
331 # Detect if game won
332 var won_at = won_at
333 if won_at == null then
334 var boss = world.boss
335 if boss != null and not boss.is_alive then
336 self.won_at = world.t
337 end
338 else
339 # Show outro
340 var t_since_won = world.t - won_at
341 if t_since_won > 1.0 and not ui_sprites.has(outro_directed) then ui_sprites.add outro_directed
342 if t_since_won > 2.0 and not ui_sprites.has(outro_created) then ui_sprites.add outro_created
343 end
344 end
345
346 # Begin playing, after intro if `initial`, otherwise after death
347 fun begin_play(initial: Bool)
348 do
349 ui_sprites.clear
350
351 world.spawn_player
352 world.planes.add new Airplane(new Point3d[Float](0.0, world.player.center.y - 10.0, 0.0), 16.0, 4.0)
353
354 if initial then
355 # Setup tutorial
356 ui_sprites.add_all([tutorial_wasd, tutorial_arrows, tutorial_chute])
357 end
358 end
359
360 # Seconds at which the game was won, using `world.t` as reference
361 private var won_at: nullable Float = null
362
363 # Remove the tutorial sprite about WASD from `ui_sprites`
364 private fun hide_tutorial_wasd do if ui_sprites.has(tutorial_wasd) then ui_sprites.remove(tutorial_wasd)
365
366 # Remove the tutorial sprite about arrows from `ui_sprites`
367 private fun hide_tutorial_arrows do if ui_sprites.has(tutorial_arrows) then ui_sprites.remove(tutorial_arrows)
368
369 # Remove the tutorial sprite about the parachute from `ui_sprites`
370 private fun hide_tutorial_chute do if ui_sprites.has(tutorial_chute) then ui_sprites.remove(tutorial_chute)
371
372 redef fun accept_event(event)
373 do
374 if super then return true
375
376 if event isa QuitEvent then
377 print perfs
378 exit 0
379 else if event isa KeyEvent then
380 if event.name == "escape" and event.is_down then
381 print perfs
382 exit 0
383 end
384
385 var player = world.player
386 if player != null and player.is_alive then
387
388 # Hide tutorial about arrows once they are used
389 var arrows = once ["left", "right"]
390 if arrows.has(event.name) then hide_tutorial_arrows
391
392 if player.altitude < world.boss_altitude then
393 if event.name == "space" and event.is_down and not player.parachute_deployed and player.plane == null then
394 player.parachute
395 if player.parachute_deployed then
396 var pc = player.center
397 world.parachute = new Parachute(new Point3d[Float](pc.x, pc.y + 5.0, pc.z-0.1), 8.0, 5.0)
398 end
399 hide_tutorial_chute
400 end
401
402 if (event.name == "space" or event.name == "up") and event.is_down then
403 player.jump
404 end
405
406 if event.name == "left" then
407 var mod = if event.is_down then -1.0 else 1.0
408 player.moving += mod
409 player.animate_move
410 else if event.name == "right" then
411 var mod = if event.is_down then 1.0 else -1.0
412 player.moving += mod
413 player.animate_move
414 end
415 end
416 end
417 end
418
419 # When player is dead, respawn on spacebar or pointer depressed
420 if (event isa KeyEvent and event.name == "space") or
421 (event isa PointerEvent and not event.is_move and event.depressed) then
422 var player = world.player
423 if player == null then
424 skip_intro = true
425 else if not player.is_alive then
426 begin_play false
427 end
428 end
429
430 return false
431 end
432 end
433
434 redef class Body
435 # Sprite representing this entity if there is no `actor`
436 fun sprite: Sprite is abstract
437
438 # 3D actor
439 fun actor: nullable Actor do return null
440
441 init
442 do
443 var actor = actor
444 if actor != null then
445 app.actors.add actor
446 else app.sprites.add sprite
447 end
448
449 redef fun destroy(world)
450 do
451 super
452
453 var actor = actor
454 if actor != null then
455 app.actors.remove actor
456 else app.sprites.remove sprite
457 end
458 end
459
460 redef class Human
461 redef fun die(world)
462 do
463 super
464
465 death_animation
466 end
467
468 # Show death animation (explosion)
469 fun death_animation
470 do
471 var force = 4.0
472 health = 0.0
473 for i in 32.times do
474 app.blood.add(
475 new Point3d[Float](center.x & force, center.y & force, center.z & force),
476 (2048.0 & 4096.0) * force, 0.3 & 0.1)
477 end
478 end
479 end
480
481 redef class Platform
482 init do sprite.scale = width/sprite.texture.width
483
484 redef fun update(dt, world)
485 do
486 super
487
488 if inertia.x < 0.0 then
489 sprite.invert_x = false
490 else if inertia.x > 0.0 then
491 sprite.invert_x = true
492 end
493 end
494 end
495
496 redef class Airplane
497 private fun texture: Texture do return if center.y < 600.0 then app.planes_sheet.biplane else app.planes_sheet.jet
498
499 redef var sprite = new Sprite(texture, center) is lazy
500 end
501
502 redef class Helicopter
503 redef var sprite = new Sprite(app.planes_sheet.helicopter, center) is lazy
504 end
505
506 redef class Boss
507 redef var actor is lazy do
508 var actor = new Actor(app.iss_model, center)
509 actor.yaw = pi/2.0
510 return actor
511 end
512
513 redef fun death_animation
514 do
515 var force = 64.0
516 app.explosions.add(center, 4096.0 * force, 0.3)
517 for i in (8.0*force).to_i.times do
518 app.explosions.add(
519 new Point3d[Float](center.x & force, center.y & force/8.0, center.z & force),
520 (2048.0 & 1024.0) * force, 0.3 + 5.0.rand, 5.0.rand)
521 end
522 end
523 end
524
525 redef class Enemy
526 redef var sprite = new Sprite(app.running_animation.frames.rand, center) is lazy
527 init do sprite.scale = width/sprite.texture.width * 2.0
528 end
529
530 redef class Parachute
531 redef var sprite = new Sprite(app.planes_sheet.parachute_open, center) is lazy
532 init
533 do
534 sprite.scale = width / sprite.texture.width
535 sprite.animate app.parachute_animation
536 end
537 end
538
539 redef class Player
540 redef var sprite = new Sprite(app.running_animation.frames.last, center) is lazy
541 init do sprite.scale = width/sprite.texture.width * 2.0
542
543 # Update current animation
544 fun animate_move
545 do
546 if moving == 0.0 then
547 sprite.animate_stop
548 else sprite.animate(app.running_animation, -1.0)
549 end
550
551 redef fun update(dt, world)
552 do
553 super
554 if moving > 0.0 then
555 sprite.invert_x = false
556 else if moving < 0.0 then
557 sprite.invert_x = true
558 end
559 end
560
561 redef fun die(world)
562 do
563 super
564
565 if center.y < 10.0 then
566 # Blood splatter on the ground
567 var splatter = new Actor(app.splatter_model,
568 new Point3d[Float](center.x, 0.05 & 0.04, center.y))
569 splatter.scale = 32.0
570 splatter.yaw = 2.0*pi.rand
571 app.actors.add splatter
572 end
573
574 # Display respawn instructions
575 app.ui_sprites.add new Sprite(app.texts_sheet.respawn, app.ui_camera.center.offset(0.0, 0.0, 0.0))
576 end
577 end
578
579 redef class Bullet
580 redef var sprite = new Sprite(weapon.bullet_texture, center) is lazy
581 init
582 do
583 sprite.scale = 0.03
584 sprite.rotation = angle
585 end
586 end
587
588 redef class Weapon
589 fun bullet_texture: Texture do return app.planes_sheet.bullet_ak
590 end
591
592 redef class Pistol
593 redef fun bullet_texture do return app.planes_sheet.bullet_pistol
594 end
595
596 redef class RocketLauncher
597 redef fun bullet_texture do return app.planes_sheet.bullet_rocket
598 end
599
600 redef class Powerup
601 # Scale so it looks like 5 world units wide, not matter the size of the texture
602 init do sprite.scale = 5.0/sprite.texture.width
603 end
604
605 redef class Ak47PU
606 redef var sprite = new Sprite(app.planes_sheet.ak, center) is lazy
607 end
608
609 redef class RocketLauncherPU
610 redef var sprite = new Sprite(app.planes_sheet.rocket, center) is lazy
611 end
612
613 redef class Life
614 redef var sprite = new Sprite(app.planes_sheet.health, center) is lazy
615 init do sprite.scale = 3.0/sprite.texture.height
616 end
617
618 redef class World
619
620 redef fun explode(center, force)
621 do
622 super
623
624 # Particles
625 app.explosions.add(center, 8192.0 * force, 0.3)
626 for i in (4.0*force).to_i.times do
627 app.explosions.add(
628 new Point3d[Float](center.x & force, center.y & force/2.0, center.z & force),
629 (4096.0 & 2048.0) * force, 0.3 & 0.3, 0.5.rand)
630 end
631 end
632 end
633
634 redef class Int
635 # Pad a number with `0`s on the left side to reach `size` digits
636 private fun pad(size: Int): String
637 do
638 var s = to_s
639 var d = size - s.length
640 if d > 0 then s = "0"*d + s
641 return s
642 end
643 end
644
645 # Manager to display numbers in sprite
646 class CounterSprites
647
648 # TODO clean up and move up to lib
649
650 # Number textures, from 0 to 9
651 #
652 # Require: `textures.length == 10`
653 var textures: Array[Texture]
654
655 # Center of the first digit in UI coordinates
656 var anchor: Point3d[Float]
657
658 # Last set of sprites generated to display the `value=`
659 private var sprites = new Array[Sprite]
660
661 # Update the value displayed by the counter by inserting new sprites into `app.ui_sprites`
662 fun value=(value: Int)
663 do
664 # Clean up last used sprites
665 for s in sprites do if app.ui_sprites.has(s) then app.ui_sprites.remove s
666 sprites.clear
667
668 # Build new sprites
669 var s = value.to_s # TODO manipulate ints directly
670 var x = 0.0
671 for c in s do
672 var i = c.to_i
673 var tex = textures[i]
674
675 x += tex.width/2.0
676 sprites.add new Sprite(tex, new Point3d[Float](anchor.x + x, anchor.y, anchor.z))
677 x += tex.width/2.0
678 end
679
680 # Register sprites to be drawn by `app.ui_camera`
681 app.ui_sprites.add_all sprites
682 end
683 end
684
685 redef class SmokeProgram
686
687 # Redef source to get particles that move up faster
688 redef fun vertex_shader_core do return """
689 vec4 c = center;
690 c.y += dt * 20.0;
691 c.x += dt * dt * 2.0;
692
693 gl_Position = c * mvp;
694 gl_PointSize = scale / gl_Position.z * (pt+0.1);
695
696 if (pt < 0.1)
697 v_color.a = pt / 0.1;
698 else
699 v_color.a = 1.0 - pt*0.9;
700 """
701 end