examples: update users of `mnit::mnit_fps`
[nit.git] / examples / mnit_dino / src / dino.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 # App for the Dino game, manages App lifecyle and inputs
18 module dino is
19 app_version(0, 2, git_revision)
20 app_name("mnit Dino")
21 end
22
23 import mnit
24
25 import graphism
26 import fancy_dino
27 import splash
28
29 redef class App
30 var cavemen_at_first_level = 6
31 var cavemen_incr = 4
32
33 var game : nullable Game = null
34 var score = new Ref[Int](0)
35 var imgs : nullable ImageSet = null
36 var splash : nullable SplashScreen = null
37
38 redef fun on_create
39 do
40 super
41
42 maximum_fps = 80.0
43
44 var display = display
45 assert display != null
46
47 # load only splash images
48 splash = new SplashScreen( self )
49 splash.draw( display, false )
50
51 # load other images
52 imgs = new ImageSet( self )
53
54 splash.draw( display, true )
55 end
56
57 redef fun frame_core( display )
58 do
59 var game = game
60 if game != null then
61 var turn = game.do_turn
62 game.draw( display, imgs.as(not null), turn )
63 else
64 splash.draw( display, true )
65 end
66 end
67
68 redef fun input( input_event )
69 do
70 if input_event isa QuitEvent then # close window button
71 quit = true # orders system to quit
72 return true # this event has been handled
73
74 else if input_event isa PointerEvent then
75 if game == null then
76 # start from splash
77 game = new Game( cavemen_at_first_level, score )
78 else if game.over and game.ready_to_start_over then
79 # play next game
80 var next_nbr_caveman = game.nbr_wanted_cavemen
81 if game.won then
82 next_nbr_caveman += cavemen_incr
83 else
84 score = new Ref[Int](0)
85 next_nbr_caveman = cavemen_at_first_level
86 end
87 game = new Game( next_nbr_caveman, score )
88 else
89 # normal play
90 game.dino.going_to = (new ScreenPos( input_event.x, input_event.y )).to_game( display.as(not null) )
91 end
92 return true
93
94 else if input_event isa KeyEvent then
95 return false
96 end
97
98 return false # unknown event, can be handled by something else
99 end
100 end