ballz: rename to `ballz_android` so tests can check it
[nit.git] / examples / mnit_ballz / src / game_logic.nit
1 #this file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
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 module game_logic
18
19 import mnit_android
20
21 class Ball
22 var x: Float
23 var y: Float
24 var dim: Int
25 var walls_activated: Bool
26 var offset_x = 0.0
27 var offset_y = 0.0
28 var going_left: Bool
29 var going_down: Bool
30
31 var game: Game
32
33 init(game: Game, x,y: Float, walls: Bool)
34 do
35 self.x = x
36 self.y = y
37 self.dim = 20
38 self.game = game
39 self.walls_activated = walls
40 end
41
42 # not very useful at this time
43 fun do_turn
44 do
45 end
46
47 fun intercepts(event: InputEvent): Bool
48 do
49 if event isa ASensorAccelerometer then
50 do_move(event)
51 else if event isa ASensorMagneticField then
52 #deal with Magnetic field sensor
53 print "ASensorMagneticField : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
54 else if event isa ASensorGyroscope then
55 #deal with Gyroscope sensor
56 print "ASensorGyroscope : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
57 else if event isa ASensorLight then
58 #deal with light sensor
59 print "ASensorLight : light = " + event.light.to_s
60 else if event isa ASensorProximity then
61 #deal with proximity sensor
62 print "ASensorProximity : distance = " + event.distance.to_s
63 else if event isa MotionEvent then
64 activate_walls(event)
65 end
66 return true
67 end
68
69 fun do_move (event: ASensorAccelerometer)
70 do
71 # acceleration value
72 var vx = event.x
73 var vy = event.y
74
75 var gw = game.width
76 var gh = game.height
77
78 # acceleration
79 var max_value = 9.80
80 var acceleration_x = vx/max_value
81 var acceleration_y = vy/max_value
82 offset_x -= (acceleration_x/10.0)*(vx.abs) + offset_x/125.0
83 offset_y += (acceleration_y/10.0)*(vy.abs) - offset_y/125.0
84 var nx = self.x + offset_x
85 var ny = self.y + offset_y
86 going_left = offset_x > 0.0
87 going_down = offset_y > 0.0
88
89 # x value
90 if nx >= 0.0 and nx <= gw then
91 self.x = nx
92 else if nx < 0.0 then
93 if not walls_activated then self.x = gw else do_bounce(1)
94 else if nx > gw then
95 if not walls_activated then self.x = 0.0 else do_bounce(1)
96 end
97
98 # y value
99 if ny >= 0.0 and ny <= gh then
100 self.y = ny
101 else if ny < 0.0 then
102 if not walls_activated then self.y = gh else do_bounce(2)
103 else if ny > gh then
104 if not walls_activated then self.y = 0.0 else do_bounce(2)
105 end
106 end
107
108 # bounce in function of the position of the wall relative to the ball: 1=left or right, 2=top or down
109 fun do_bounce(wall_position: Int)
110 do
111 if wall_position == 1 then
112 offset_x = -offset_x*0.85
113 else if wall_position == 2 then
114 offset_y = -offset_y*0.85
115 end
116 if offset_x.abs > 1.0 and offset_y.abs > 1.0 then
117 self.x += offset_x
118 self.y += offset_y
119 end
120 end
121
122 fun activate_walls(event: MotionEvent)
123 do
124 if event.just_went_down then
125 walls_activated = not walls_activated
126 end
127 end
128 end
129
130 class Screen
131 var ball_img: Image
132 var game: Game
133
134 init(app: App, display: Display)
135 do
136 game = new Game(display)
137 ball_img = app.load_asset("images/ball.png").as(Image)
138 var scale = game.img_dim.to_f / game.img_ori_dim.to_f
139 ball_img.scale = scale
140 ball_img.scale = 3.0
141 end
142
143 fun do_frame(display: Display)
144 do
145 display.clear(0.0, 0.0, 0.0)
146 display.blit_rotated(ball_img, game.ball.x, game.ball.y, 0.0)
147 end
148
149 fun input(ie: InputEvent): Bool
150 do
151 if ie isa ASensorProximity then
152 if ie.distance == 0.0 then ball_img.scale = 6.0 else ball_img.scale = 3.0
153 else
154 game.ball.intercepts(ie)
155 end
156 return true
157 end
158 end
159
160 class Game
161 var ball: Ball
162 var width: Float
163 var height: Float
164
165 var img_ori_dim: Int = 256
166 fun img_dim: Int do return 210
167
168 init(display: Display)
169 do
170 width = display.width.to_f
171 height = display.height.to_f
172 ball = new Ball(self, width/2.0, height/2.0, false)
173 end
174
175 fun do_turn
176 do
177 ball.do_turn
178 end
179 end