fb5418a8080610ade1ec675341b1cc356e587057
[nit.git] / examples / mnit_dino / src / game_logic.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Entire game logic for the Dino game
18 # Depends only on Nit standard library
19 module game_logic
20
21 interface Turnable
22 fun do_turn( turn : Turn ) is abstract
23 end
24
25 class Game
26 var nbr_wanted_cavemen : Int
27
28 var dino = new Dino
29 var cavemen = new Array[Caveman]
30 var javelins_in_air = new Array[Javelin]
31 var javelins_on_ground = new Array[Javelin]
32 var entities = new Array[Entity].with_items(dino)
33 var turn_nbr = 0
34
35 var over_since = 0
36
37 var random_radius_min = 200
38 var random_radius_max = 400
39 protected var random_radius_diff : Int =
40 random_radius_max - random_radius_min
41
42 init( cavemen_nbr : Int )
43 do
44 srand
45
46 nbr_wanted_cavemen = cavemen_nbr
47
48 for n in [0..nbr_wanted_cavemen[ do
49 var man = new Caveman
50 cavemen.add( man )
51 entities.add( man )
52
53 var radius = (random_radius_min + random_radius_diff.rand).to_f
54 var angle = (2.0*pi).rand
55 man.pos.x = ( angle.cos * radius ).to_i
56 man.pos.y = ( angle.sin * radius ).to_i
57 end
58 end
59
60 fun do_turn : Turn
61 do
62 turn_nbr += 1
63 var turn = new Turn( self )
64
65 dino.do_turn( turn )
66 for man in cavemen do
67 man.do_turn( turn )
68 if not man.is_alive then
69 cavemen.remove( man )
70 end
71 end
72
73 for j in javelins_in_air do
74 j.do_turn( turn )
75 if j.hit_dino then
76 javelins_in_air.remove( j )
77 entities.remove( j )
78 else if j.hit_ground then
79 javelins_in_air.remove( j )
80 javelins_on_ground.add( j )
81 end
82 end
83
84 if over and over_since == 0 then
85 over_since = turn.nbr
86 end
87
88 # sort for blitting, firsts and in the back
89 entities.sort !cmp( a, b ) = b.pos.y <=> a.pos.y
90
91 return turn
92 end
93
94 fun add_javelin( j : Javelin )
95 do
96 javelins_in_air.add( j )
97 entities.add( j )
98 end
99
100 fun over : Bool do return dino.life <= 0 or cavemen.is_empty
101 fun won : Bool do return cavemen.is_empty and dino.is_alive
102 fun lost : Bool do return not won
103
104 fun ready_to_start_over : Bool do return over_since + 80 < turn_nbr
105 end
106
107 class Turn
108 var game : Game
109 var nbr : Int
110
111 init ( g : Game )
112 do
113 game = g
114 nbr = game.turn_nbr
115 end
116 end
117
118 class GamePos
119 var x : Int
120 var y : Int
121
122 init ( x, y : Int )
123 do
124 self.x = x
125 self.y = y
126 end
127
128 init copy( src : GamePos )
129 do
130 x = src.x
131 y = src.y
132 end
133
134 fun squared_dist_with( other : GamePos ) : Int
135 do
136 var dx = other.x - x
137 var dy = other.y - y
138
139 return dx*dx + dy*dy
140 end
141
142 redef fun to_s do return "<{x}|{y}>"
143 end
144
145 class Entity
146 super Turnable
147
148 var pos = new GamePos( 0, 0 )
149
150 fun squared_dist_with_dino( game : Game ) : Int
151 do
152 return pos.squared_dist_with( game.dino.pos )
153 end
154 end
155
156 class MovingEntity
157 super Entity
158
159 var going_to : nullable GamePos writable = null
160
161 fun speed : Int is abstract
162
163 var going_left = false
164 var going_right = false
165
166 redef fun do_turn( t )
167 do
168 if going_to != null then
169 var ds = pos.squared_dist_with( going_to.as(not null) )
170 if ds < speed*speed then
171 going_to = null # is there
172 else
173 var dx = going_to.x - pos.x
174 var dy = going_to.y - pos.y
175 var a = atan2( dy.to_f, dx.to_f )
176 var mx = a.cos*speed.to_f
177 var my = a.sin*speed.to_f
178
179 pos.x += mx.to_i
180 pos.y += my.to_i
181
182 going_left = mx < 0.0
183 going_right = mx > 0.0
184 end
185 end
186 end
187 end
188
189 class MortalEntity
190 super Entity
191
192 fun is_alive : Bool is abstract
193 end
194
195 class Dino
196 super MovingEntity
197 super MortalEntity
198
199 #var running_until = 0
200 var total_life = 20
201 var life : Int = total_life
202
203 redef fun speed do return 8
204
205 redef fun is_alive do return life > 0
206
207 fun hit( hitter : Entity, damage : Int )
208 do
209 if is_alive then
210 life -= damage
211 end
212 end
213
214 redef fun do_turn( t )
215 do
216 if is_alive then
217 super
218 end
219 end
220 end
221
222 class Caveman
223 super MovingEntity
224 super MortalEntity
225
226 var afraid_until = 0
227 var cannot_throw_until = 0
228
229 var throw_distance : Int = 400*40+10.rand
230 var fear_distance : Int = 300*20+8.rand
231 var flee_distance : Int = 600*60+16.rand
232 var run_over_distance = 500
233
234 var fear_duration : Int = 80+40.rand
235 var throw_period : Int = 40+8.rand
236
237 redef var is_alive : Bool = true
238
239 redef fun speed do return 4
240
241 fun is_afraid( turn : Turn ) : Bool do return turn.nbr < afraid_until
242 fun can_throw( turn : Turn ) : Bool do return cannot_throw_until < turn.nbr
243
244 redef fun do_turn( t )
245 do
246 if is_alive then
247 var dwd = squared_dist_with_dino( t.game )
248
249 if dwd < run_over_distance then
250 is_alive = false
251 return
252 else if is_afraid( t ) then
253 # going to destination
254 else if t.game.dino.life <= 0 then
255 # dino is dead, chill
256 else
257 if dwd < fear_distance then
258 afraid_until = t.nbr + fear_duration
259
260 var dino_pos = t.game.dino.pos
261 var dx = dino_pos.x - pos.x
262 var dy = dino_pos.y - pos.y
263 var a = atan2( dy.to_f, dx.to_f )
264 a += pi # get opposite
265 var x = a.cos*flee_distance.to_f
266 var y = a.sin*flee_distance.to_f
267 going_to = new GamePos( x.to_i, y.to_i )
268 else if dwd < throw_distance then
269 if can_throw( t ) then
270 cannot_throw_until = t.nbr + throw_period
271 var javelin = new Javelin( pos, t.game.dino.pos )
272 t.game.add_javelin( javelin )
273 going_to = null
274 end
275 else
276 # get closer to dino
277 going_to = t.game.dino.pos
278 end
279 end
280
281 super
282 end
283 end
284 end
285
286 class Javelin
287 super Entity
288
289 var z = 400
290 var angle : Float = pi/2.0
291
292 var thrown_angle_xy : Float
293 #var thrown_angle_z
294 var speed_z = 60
295
296 var hit_ground = false
297 var hit_dino = false
298
299 var speed : Int = 10+2.rand
300 var hit_dino_distance = 128
301 var hit_damage = 1
302 var gravity : Int = -3
303
304 init ( from : GamePos, to : GamePos )
305 do
306 var dx = to.x-from.x
307 var dy = to.y-from.y
308 thrown_angle_xy = atan2( dy.to_f, dx.to_f )
309 pos = new GamePos.copy( from )
310 end
311
312 redef fun do_turn( t )
313 do
314 var dwd = squared_dist_with_dino( t.game )
315 if dwd < hit_dino_distance and t.game.dino.is_alive then
316 t.game.dino.hit( self, hit_damage )
317 hit_dino = true
318 else
319 if z <= 0 then
320 hit_ground = true
321 else
322 # in the air
323 speed_z += gravity
324 z += speed_z
325
326 var mx = (thrown_angle_xy.cos * speed.to_f).to_i
327 pos.x += mx
328 pos.y += (thrown_angle_xy.sin * speed.to_f).to_i
329
330 if mx > 0 then
331 angle = atan2( (speed_z/10).to_f, -1.0*speed.to_f )-pi/2.0
332 else
333 angle = atan2( (speed_z/10).to_f, speed.to_f )-pi/2.0
334 end
335 end
336 end
337 end
338 end