1 #this file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 import android
::sensors
26 var walls_activated
: Bool
34 init(game
: Game, x
,y
: Float, walls
: Bool)
40 self.walls_activated
= walls
43 # not very useful at this time
48 fun intercepts
(event
: InputEvent): Bool
50 if event
isa ASensorAccelerometer then
52 else if event
isa ASensorMagneticField then
53 #deal with Magnetic field sensor
54 print
"ASensorMagneticField : x = " + event
.x
.to_s
+ " y = " + event
.y
.to_s
+ " z = " + event
.z
.to_s
55 else if event
isa ASensorGyroscope then
56 #deal with Gyroscope sensor
57 print
"ASensorGyroscope : x = " + event
.x
.to_s
+ " y = " + event
.y
.to_s
+ " z = " + event
.z
.to_s
58 else if event
isa ASensorLight then
59 #deal with light sensor
60 print
"ASensorLight : light = " + event
.light
.to_s
61 else if event
isa ASensorProximity then
62 #deal with proximity sensor
63 print
"ASensorProximity : distance = " + event
.distance
.to_s
64 else if event
isa MotionEvent then
70 fun do_move
(event
: ASensorAccelerometer)
81 var acceleration_x
= vx
/max_value
82 var acceleration_y
= vy
/max_value
83 offset_x
-= (acceleration_x
/10.0)*(vx
.abs
) + offset_x
/125.0
84 offset_y
+= (acceleration_y
/10.0)*(vy
.abs
) - offset_y
/125.0
85 var nx
= self.x
+ offset_x
86 var ny
= self.y
+ offset_y
87 going_left
= offset_x
> 0.0
88 going_down
= offset_y
> 0.0
91 if nx
>= 0.0 and nx
<= gw
then
94 if not walls_activated
then self.x
= gw
else do_bounce
(1)
96 if not walls_activated
then self.x
= 0.0 else do_bounce
(1)
100 if ny
>= 0.0 and ny
<= gh
then
102 else if ny
< 0.0 then
103 if not walls_activated
then self.y
= gh
else do_bounce
(2)
105 if not walls_activated
then self.y
= 0.0 else do_bounce
(2)
109 # bounce in function of the position of the wall relative to the ball: 1=left or right, 2=top or down
110 fun do_bounce
(wall_position
: Int)
112 if wall_position
== 1 then
113 offset_x
= -offset_x
*0.85
114 else if wall_position
== 2 then
115 offset_y
= -offset_y
*0.85
117 if offset_x
.abs
> 1.0 and offset_y
.abs
> 1.0 then
123 fun activate_walls
(event
: MotionEvent)
125 if event
.just_went_down
then
126 walls_activated
= not walls_activated
135 init(app
: App, display
: Display)
137 game
= new Game(display
)
138 ball_img
= app
.load_asset
("images/ball.png").as(Image)
139 var scale
= game
.img_dim
.to_f
/ game
.img_ori_dim
.to_f
140 ball_img
.scale
= scale
144 fun do_frame
(display
: Display)
146 display
.clear
(0.0, 0.0, 0.0)
147 display
.blit_rotated
(ball_img
, game
.ball
.x
, game
.ball
.y
, 0.0)
150 fun input
(ie
: InputEvent): Bool
152 if ie
isa ASensorProximity then
153 if ie
.distance
== 0.0 then ball_img
.scale
= 6.0 else ball_img
.scale
= 3.0
155 game
.ball
.intercepts
(ie
)
166 var img_ori_dim
: Int = 256
167 fun img_dim
: Int do return 210
169 init(display
: Display)
171 width
= display
.width
.to_f
172 height
= display
.height
.to_f
173 ball
= new Ball(self, width
/2.0, height
/2.0, false)