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