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.
25 var walls_activated
: Bool
33 init(game
: Game, x
,y
: Float, walls
: Bool)
39 self.walls_activated
= walls
42 # not very useful at this time
47 fun intercepts
(event
: InputEvent): Bool
49 if event
isa ASensorAccelerometer then
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
69 fun do_move
(event
: ASensorAccelerometer)
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
90 if nx
>= 0.0 and nx
<= gw
then
93 if not walls_activated
then self.x
= gw
else do_bounce
(1)
95 if not walls_activated
then self.x
= 0.0 else do_bounce
(1)
99 if ny
>= 0.0 and ny
<= gh
then
101 else if ny
< 0.0 then
102 if not walls_activated
then self.y
= gh
else do_bounce
(2)
104 if not walls_activated
then self.y
= 0.0 else do_bounce
(2)
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)
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
116 if offset_x
.abs
> 1.0 and offset_y
.abs
> 1.0 then
122 fun activate_walls
(event
: MotionEvent)
124 if event
.just_went_down
then
125 walls_activated
= not walls_activated
134 init(app
: App, display
: Display)
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
143 fun do_frame
(display
: Display)
145 display
.clear
(0.0, 0.0, 0.0)
146 display
.blit_rotated
(ball_img
, game
.ball
.x
, game
.ball
.y
, 0.0)
149 fun input
(ie
: InputEvent): Bool
151 if ie
isa ASensorProximity then
152 if ie
.distance
== 0.0 then ball_img
.scale
= 6.0 else ball_img
.scale
= 3.0
154 game
.ball
.intercepts
(ie
)
165 var img_ori_dim
: Int = 256
166 fun img_dim
: Int do return 210
168 init(display
: Display)
170 width
= display
.width
.to_f
171 height
= display
.height
.to_f
172 ball
= new Ball(self, width
/2.0, height
/2.0, false)