a28514cc7fbe47162ef70623346dd19707b3aa36
[nit.git] / examples / mnit_moles / src / moles.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 # Classic moles game
18 module moles
19
20 import mnit
21
22 class Hole
23 var game : Game
24 var x : Int
25 var y : Int
26 var dim : Int # Should it be removed?
27
28 # state
29 var up : Bool = false
30 var hitted : Bool = false
31
32 init ( g : Game, x, y : Int )
33 do
34 game = g
35 self.x = x
36 self.y = y
37 dim = game.img_dim
38 end
39
40 fun do_turn
41 do
42 if up then
43 if hitted then
44 if (20.0*game.speed_modifier).to_i.rand == 0 then
45 # dead / hide
46 hitted = false
47 up = false
48 end
49 else if (80.0*game.speed_modifier).to_i.rand == 0 then
50 # hide
51 up = false
52 end
53 else if (100.0*game.speed_modifier).to_i.rand == 0 then
54 # show up
55 up = true
56 end
57 end
58
59 fun intercepts( event : PointerEvent ) : Bool
60 do
61 var ex = event.x.to_i
62 var ey = event.y.to_i
63 return ex > x and ex < x + dim and
64 ey > y and ey < y + dim
65 end
66
67 fun hit
68 do
69 if hitted then return
70
71 if up then
72 hitted = true
73 game.points += 1
74 else
75 abort # should not happen
76 end
77 end
78 end
79
80 class Game
81 var holes : Sequence[Hole] = new Array[Hole].with_capacity(4)
82
83 # rule / const
84 var modifier_half_life : Float = 40.0
85 var rows : Int = 5
86 var columns : Int = 3
87
88 # state
89 var points : Int = 0
90 var speed_modifier : Float = 1.0
91
92 # configs
93 var img_ori_dim : Int = 256
94 fun img_dim : Int do return 210
95 fun global_speed_modifier : Float do return 2.0
96
97 init
98 do
99 var d = img_dim
100 for x in [ 0 .. rows [ do
101 for y in [ 0 .. columns [ do
102 holes.add( new Hole( self, x*d, y*d ) )
103 end
104 end
105 end
106
107 fun do_turn do
108 for hole in holes do hole.do_turn
109
110 speed_modifier = modifier_half_life / (modifier_half_life+points.to_f) * global_speed_modifier
111
112 print "p: {points} sm: {speed_modifier}"
113 end
114 end
115
116 # Where all the UI stuff is done
117 class Screen
118 var empty_img : Image
119 var up_img : Image
120 var hit_img : Image
121
122 var game : Game = new Game
123
124 init ( app : App )
125 do
126 empty_img = app.load_asset( "images/empty.png" ).as(Image)
127 up_img = app.load_asset( "images/up.png" ).as(Image)
128 hit_img = app.load_asset( "images/hit.png" ).as(Image)
129
130 var scale = game.img_dim.to_f / game.img_ori_dim.to_f
131 empty_img.scale = scale
132 up_img.scale = scale
133 hit_img.scale = scale
134 end
135
136 fun do_frame( display : Display )
137 do
138 display.clear( 0.0, 0.7, 0.0 )
139
140 for hole in game.holes do
141 var img
142
143 if hole.hitted then
144 img = hit_img
145 else if hole.up then
146 img = up_img
147 else
148 img = empty_img
149 end
150
151 display.blit( img, hole.x, hole.y-64 )
152 end
153 end
154
155 fun input( event : InputEvent ) : Bool
156 do
157 if event isa PointerEvent then
158 for hole in game.holes do
159 if hole.intercepts( event ) then
160 if hole.up then
161 hole.hit
162 end
163 return true
164 end
165 end
166 end
167
168 return false
169 end
170 end
171
172 class MyApp
173 super App
174
175 var screen : nullable Screen = null
176
177 init do super
178
179 redef fun init_window
180 do
181 super
182
183 screen = new Screen( self )
184 end
185
186 redef fun frame_core( display )
187 do
188 var screen = self.screen
189 if screen != null then
190 screen.game.do_turn
191 screen.do_frame( display )
192 end
193 end
194
195 redef fun input( ie )
196 do
197 var screen = screen
198 if ie isa QuitEvent or
199 ( ie isa KeyEvent and ie.to_c == 'q' ) then
200 quit = true
201 return true
202 else if screen != null then
203 return screen.input( ie )
204 else
205 print "unknown input: {ie}"
206 return false
207 end
208 end
209 end
210
211 var app = new MyApp
212 app.main_loop
213