gamnit: add the check rule to the template makefile and remove all
[nit.git] / lib / gamnit / examples / template_flat / src / template_flat.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the Do What The Fuck You Want To
5 # Public License, Version 2, as published by Sam Hocevar. See
6 # http://sam.zoy.org/projects/COPYING.WTFPL for more details.
7
8 # Template for a 2D gamnit game
9 module template_flat is
10 app_name "gamnit 2D Template"
11 app_namespace "net.xymus.template_flat"
12 app_version(1, 0, git_revision)
13 android_api_target 10
14 end
15
16 import gamnit::flat # For `Texture, Sprite`, etc.
17 import gamnit::keys # For `pressed_keys`
18 import app::audio # For `Sound`
19
20 redef class App
21
22 # Texture, loaded automatically at `on_create`
23 var texture = new Texture("fighter.png")
24
25 # Sound effect, lazy loaded at first use
26 var sound = new Sound("laser.mp3")
27
28 # Sprite, must be loaded in or after `on_create`
29 var sprite = new Sprite(texture, new Point3d[Float](0.0, 0.0, 0.0)) is lazy
30
31 redef fun on_create
32 do
33 super
34
35 # Report errors on all loaded textures.
36 # Root textures are associated to pixel data,
37 # whereas other texture may be subtextures of root textures.
38 for tex in all_root_textures do
39 var error = tex.error
40 if error != null then print_error "Texture '{tex}' failed to load: {error}"
41 end
42
43 # Draw the texture as pixelated, it looks better for such
44 # a small texture.
45 texture.as(TextureAsset).pixelated = true
46
47 # Create the sprite and make it visible.
48 sprites.add sprite
49
50 # Make the sprite smaller, by default each pixel corresponds to 1 world unit.
51 # However, it is often preferable to make 1 world unit correspond to
52 # something meaningful in the game world, such as 1 meter.
53 #
54 # Scale the ship so it is approximately 5 world units wide.
55 sprite.scale = 5.0 / texture.width
56
57 # Move the camera to show 10 world units on the Y axis at Z = 0.
58 # The `sprite` should take approximately 1/4 of the height of the screen.
59 world_camera.reset_height 20.0
60
61 # Move the near clip wall closer to the camera because our world unit
62 # range is small. Moving the clip wall too close to the camera can
63 # cause glitches on mobiles devices with small depth buffer.
64 world_camera.near = 1.0
65
66 # Make the background blue and opaque.
67 glClearColor(0.0, 0.0, 1.0, 1.0)
68
69 # If the first command line argument is an integer, add extra sprites.
70 if args.not_empty and args.first.is_int then
71 # It's a performance test, unlock the framerate.
72 maximum_fps = -1.0
73
74 # Add `args.first` sprites.
75 for i in args.first.to_i.times do
76 var s = new Sprite(texture, new Point3d[Float](30.0.rand - 15.0, 20.0.rand - 10.0, 0.0 - 30.0.rand))
77 s.scale = 0.1
78 sprites.add s
79 end
80 end
81 end
82
83 redef fun update(dt)
84 do
85 # Update game logic here.
86 sprite.rotation += 0.1*pi*dt
87
88 # Move `sprite` with the keyboard arrows.
89 # Set the speed according to the elapsed time since the last frame `dt`
90 # for a smooth animation.
91 var unit_per_second = 2.0
92 for key in pressed_keys do
93 if key == "left" then
94 sprite.center.x -= unit_per_second*dt
95 else if key == "right" then
96 sprite.center.x += unit_per_second*dt
97 else if key == "up" then
98 sprite.center.y += unit_per_second*dt
99 else if key == "down" then
100 sprite.center.y -= unit_per_second*dt
101 end
102 end
103 end
104
105 redef fun accept_event(event)
106 do
107 if super then return true
108
109 if event isa QuitEvent or
110 (event isa KeyEvent and event.name == "escape" and event.is_up) then
111 # When window close button, escape or back key is pressed
112 # show the average FPS over the last few seconds.
113 print "{current_fps} fps"
114 print sys.perfs
115
116 # Quit abruptly
117 exit 0
118 else if event isa KeyEvent and event.is_down then
119 if event.name == "space" then
120 # Play a sound when space bar is pressed.
121 sound.play
122 return true
123 else if event.name == "s" then
124 # Remove a random sprite.
125 if sprites.not_empty then sprites.remove sprites.rand
126 else if event.name == "w" then
127 # Add a random sprite.
128 var s = new Sprite(texture, new Point3d[Float](30.0.rand - 15.0, 20.0.rand - 10.0, 0.0 - 30.0.rand))
129 s.scale = 0.1
130 s.tint[1] = 0.0
131 s.tint[2] = 0.0
132 sprites.add s
133 end
134 end
135
136 return false
137 end
138 end