Merge: curl: basic Unix domain socket support
[nit.git] / contrib / asteronits / src / asteronits.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 # Portable game to destroy asteroids
16 module asteronits is
17 app_name "Asteronits"
18 app_namespace "org.nitlanguage.asteronits"
19 app_version(1, 0, git_revision)
20 end
21
22 import gamnit::flat
23
24 import gamnit::landscape
25
26 import game_logic
27 import spritesheet
28
29 redef class Spritesheet
30 # Largest meteors, organized by color
31 var meteors_big: Array[Array[Texture]] = [
32 [meteor_brown_big1, meteor_brown_big2, meteor_brown_big3, meteor_brown_big4],
33 [meteor_grey_big1, meteor_grey_big2, meteor_grey_big3, meteor_grey_big4]]
34
35 # Medium size meteors, organized by color
36 var meteors_med: Array[Array[Texture]] = [
37 [meteor_brown_med1, meteor_brown_med3],
38 [meteor_grey_med1, meteor_grey_med2]]
39
40 # Small meteors, organized by color
41 var meteors_small: Array[Array[Texture]] = [
42 [meteor_brown_small1, meteor_brown_small2],
43 [meteor_grey_small1, meteor_grey_small2]]
44
45 # Tiny meteors, organized by color
46 #
47 # TODO use these in particles
48 var meteors_tiny: Array[Array[Texture]] = [
49 [meteor_brown_tiny1, meteor_brown_tiny2],
50 [meteor_grey_tiny1, meteor_grey_tiny2]]
51
52 # Available ships
53 var ships: Array[Texture] = [enemy_green1]
54 end
55
56 redef class App
57
58 # Current world in play
59 var world = new World(12, 2, display.aspect_ratio) is lazy
60
61 # Sound effects
62 private var fx_fire = new Sound("sounds/fire.ogg")
63 private var fx_explosion_ship = new Sound("sounds/explosion_ship.wav")
64 private var fx_explosion_asteroids = new Sound("sounds/explosion_asteroids.wav")
65
66 redef fun create_scene
67 do
68 super
69
70 # Move the camera to show all the world world in the screen range
71 world_camera.reset_height(world.half_height * 2.0)
72
73 ui_camera.reset_height 720.0
74 end
75
76 # Main spritesheet with ships, asteroids and beams
77 var spritesheet = new Spritesheet
78
79 redef fun update(dt)
80 do
81 # Update game logic
82 world.do_turn dt
83
84 # Setup new world if all asteroids are destroyed
85 if world.asteroids.is_empty then
86 sprites.clear
87 world = new World(world.n_asteroids*2, world.n_asteroid_parts+1, display.aspect_ratio)
88 end
89 end
90
91 redef fun accept_event(event)
92 do
93 if super then return true
94
95 if event isa QuitEvent then
96 exit 0
97 else if event isa KeyEvent then
98 var thrust = event.thrust
99 if thrust != 0.0 then
100 app.world.ship.applied_thrust = if event.is_down then thrust else 0.0
101 return true
102 end
103
104 var rot = event.rotation
105 if rot != 0.0 then
106 app.world.ship.applied_rotation = if event.is_down then rot else 0.0
107 return true
108 end
109
110 if event.name == "space" and event.is_down then
111 app.world.ship.fire
112 return true
113 else if event.name == "escape" then
114 exit 0
115 else if event.name == "." and event.is_down then
116 dynamic_resolution_ratio *= 2.0
117 print dynamic_resolution_ratio
118 else if event.name == "," and event.is_down then
119 dynamic_resolution_ratio /= 2.0
120 print dynamic_resolution_ratio
121 end
122 end
123
124 return false
125 end
126 end
127
128 redef class SpacialObject
129 # Main `Sprite` to draw for this object
130 var sprite: Sprite is noinit
131
132 # All `Sprites` composing this object
133 var sprites: Collection[Sprite] = new Ref[Sprite](sprite) is lazy
134
135 init do app.sprites.add_all sprites
136
137 redef fun do_turn(dt)
138 do
139 super
140 sprite.rotation = rotation - pi/2.0
141 end
142
143 redef fun destroy
144 do
145 super
146 for s in sprites do app.sprites.remove s
147 end
148 end
149
150 redef class Asteroid
151
152 init
153 do
154 # Select texture from `size` and `color`
155 var tex = if size == 3 then
156 app.spritesheet.meteors_big[color].rand
157 else if size == 2 then
158 app.spritesheet.meteors_med[color].rand
159 else app.spritesheet.meteors_small[color].rand
160
161 sprite = new Sprite(tex, center)
162 super
163 end
164
165 redef fun destroy
166 do
167 super
168 app.fx_explosion_asteroids.play
169 end
170 end
171
172 redef class Bullet
173 init
174 do
175 sprite = new Sprite(app.spritesheet.laser_blue01, center)
176 super
177 end
178 end
179
180 redef class Ship
181 init
182 do
183 sprite = new Sprite(app.spritesheet.ships.rand, center)
184 sprites = [sprite, thrust_sprite]
185
186 super
187 end
188
189 private var thrust_sprite = new Sprite(app.spritesheet.fire09, new Point3d[Float](0.0, 0.0, 0.0))
190
191 private var sprites_with_fire: Array[Sprite] = [thrust_sprite, sprite] is lazy
192
193 redef fun do_turn(dt)
194 do
195 super
196
197 # Update position of the thrust sprite
198 var dist_to_engine = 45.0
199 thrust_sprite.center.x = center.x - dist_to_engine*rotation.cos
200 thrust_sprite.center.y = center.y - dist_to_engine*rotation.sin
201 thrust_sprite.center.z = center.z
202 thrust_sprite.rotation = rotation - pi/2.0
203
204 # Show or hide the thrust sprite
205 if applied_thrust > 0.0 then
206 thrust_sprite.alpha = 1.0
207
208 else if thrust_sprite.alpha > 0.0 then
209 thrust_sprite.alpha -= dt*4.0
210 if thrust_sprite.alpha < 0.0 then thrust_sprite.alpha = 0.0
211 end
212
213 # HACK, the "enemy" ship used for the player points downwards
214 sprite.rotation += pi
215 end
216
217 redef fun fire
218 do
219 super
220 app.fx_fire.play
221 end
222
223 redef fun hit
224 do
225 super
226 app.fx_explosion_ship.play
227 end
228 end
229
230 redef class KeyEvent
231
232 # How does this event affect the ship thrust?
233 fun thrust: Float
234 do
235 if name == "up" or name == "w" then return 1.0
236 return 0.0
237 end
238
239 # How does this event affect the ship thrust?
240 fun rotation: Float
241 do
242 if name == "right" or name == "d" then return -1.0
243 if name == "left" or name == "a" then return 1.0
244 return 0.0
245 end
246 end