38cf9e584765ce496c13989f2635d436da1e523f
[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 import android::sensors
21
22 class Ball
23 var x: Float
24 var y: Float
25 var dim: Int
26 var walls_activated: Bool
27 var offset_x = 0.0
28 var offset_y = 0.0
29 var going_left: Bool
30 var going_down: Bool
31
32 var game: Game
33
34 init(game: Game, x,y: Float, walls: Bool)
35 do
36 self.x = x
37 self.y = y
38 self.dim = 20
39 self.game = game
40 self.walls_activated = walls
41 end
42
43 # not very useful at this time
44 fun do_turn
45 do
46 end
47
48 fun intercepts(event: InputEvent): Bool
49 do
50 if event isa ASensorAccelerometer then
51 do_move(event)
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
65 activate_walls(event)
66 end
67 return true
68 end
69
70 fun do_move (event: ASensorAccelerometer)
71 do
72 # acceleration value
73 var vx = event.x
74 var vy = event.y
75
76 var gw = game.width
77 var gh = game.height
78
79 # acceleration
80 var max_value = 9.80
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
89
90 # x value
91 if nx >= 0.0 and nx <= gw then
92 self.x = nx
93 else if nx < 0.0 then
94 if not walls_activated then self.x = gw else do_bounce(1)
95 else if nx > gw then
96 if not walls_activated then self.x = 0.0 else do_bounce(1)
97 end
98
99 # y value
100 if ny >= 0.0 and ny <= gh then
101 self.y = ny
102 else if ny < 0.0 then
103 if not walls_activated then self.y = gh else do_bounce(2)
104 else if ny > gh then
105 if not walls_activated then self.y = 0.0 else do_bounce(2)
106 end
107 end
108
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)
111 do
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
116 end
117 if offset_x.abs > 1.0 and offset_y.abs > 1.0 then
118 self.x += offset_x
119 self.y += offset_y
120 end
121 end
122
123 fun activate_walls(event: MotionEvent)
124 do
125 if event.just_went_down then
126 walls_activated = not walls_activated
127 end
128 end
129 end
130
131 class Screen
132 var ball_img: Image
133 var game: Game
134
135 init(app: App, display: Display)
136 do
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
141 ball_img.scale = 3.0
142 end
143
144 fun do_frame(display: Display)
145 do
146 display.clear(0.0, 0.0, 0.0)
147 display.blit_rotated(ball_img, game.ball.x, game.ball.y, 0.0)
148 end
149
150 fun input(ie: InputEvent): Bool
151 do
152 if ie isa ASensorProximity then
153 if ie.distance == 0.0 then ball_img.scale = 6.0 else ball_img.scale = 3.0
154 else
155 game.ball.intercepts(ie)
156 end
157 return true
158 end
159 end
160
161 class Game
162 var ball: Ball
163 var width: Float
164 var height: Float
165
166 var img_ori_dim: Int = 256
167 fun img_dim: Int do return 210
168
169 init(display: Display)
170 do
171 width = display.width.to_f
172 height = display.height.to_f
173 ball = new Ball(self, width/2.0, height/2.0, false)
174 end
175
176 fun do_turn
177 do
178 ball.do_turn
179 end
180 end