ee41e9cc877e5221e47f3def902ab549c3906cd4
[nit.git] / examples / mnit_ballz / src / ballz_android.nit
1 # this file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 # Android part of mnit_ballz
17 module ballz_android is
18 app_version(0, 2, git_revision)
19 app_name("Ballz")
20 app_namespace "org.nitlanguage.ballz"
21 android_api_target 19
22 end
23
24 import android::portrait
25 import android::sensors
26 import display
27 import mnit::android
28
29 redef class App
30
31 # The game
32 var game: nullable Game is noautoinit
33
34 redef fun run do
35 accelerometer.enabled = true
36 accelerometer.event_rate = 10000
37 magnetic_field.enabled = true
38 gyroscope.enabled = true
39 light.enabled = true
40 proximity.enabled = true
41 maximum_fps = 50.0
42 sensors_support_enabled = true
43 super
44 end
45
46 redef fun on_create
47 do
48 super
49 var display = self.display.as(not null)
50 game = new Game(display.width.to_f, display.height.to_f)
51 end
52
53 redef fun frame_core(display)
54 do
55 var game = game
56 if game != null then
57 game.do_turn
58 game.draw(display, assets)
59 end
60 end
61
62 redef fun input(ie)
63 do
64 if paused then return false
65 if ie isa QuitEvent then
66 quit = true
67 return true
68 end
69 var game = game
70 if game != null then
71 return game.input(ie)
72 end
73 return false
74 end
75 end
76
77 redef class Ball
78
79 redef fun intercepts(event)
80 do
81 if event isa ASensorAccelerometer then
82 acceleration(event.x, event.y)
83 else if event isa ASensorMagneticField then
84 #deal with Magnetic field sensor
85 #print "ASensorMagneticField : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
86 else if event isa ASensorGyroscope then
87 #deal with Gyroscope sensor
88 #print "ASensorGyroscope : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
89 else if event isa ASensorLight then
90 #deal with light sensor
91 #print "ASensorLight : light = " + event.light.to_s
92 else if event isa ASensorProximity then
93 #deal with proximity sensor
94 #print "ASensorProximity : distance = " + event.distance.to_s
95 else if event isa MotionEvent then
96 end
97 return true
98 end
99 end
100
101
102 redef class Game
103
104 redef fun input(ie)
105 do
106 if ie isa ASensorAccelerometer or ie isa MotionEvent then
107 ball.intercepts(ie)
108 return true
109 end
110 return false
111 end
112 end