mnit_ballz: Android module
[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
42 sensors_support_enabled = true
43 super
44 end
45
46 redef fun on_create
47 do
48 super
49 game = new Game(display.width.to_f, display.height.to_f)
50 end
51
52 redef fun frame_core(display)
53 do
54 var game = game
55 if game != null then
56 game.do_turn
57 game.draw(display, assets)
58 end
59 end
60
61 redef fun input(ie)
62 do
63 if paused then return false
64 if ie isa QuitEvent then
65 quit = true
66 return true
67 end
68 var game = game
69 if game != null then
70 return game.input(ie)
71 end
72 return false
73 end
74 end
75
76 redef class Ball
77
78 redef fun intercepts(event)
79 do
80 if event isa ASensorAccelerometer then
81 acceleration(event.x, event.y)
82 else if event isa ASensorMagneticField then
83 #deal with Magnetic field sensor
84 #print "ASensorMagneticField : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
85 else if event isa ASensorGyroscope then
86 #deal with Gyroscope sensor
87 #print "ASensorGyroscope : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
88 else if event isa ASensorLight then
89 #deal with light sensor
90 #print "ASensorLight : light = " + event.light.to_s
91 else if event isa ASensorProximity then
92 #deal with proximity sensor
93 #print "ASensorProximity : distance = " + event.distance.to_s
94 else if event isa MotionEvent then
95 end
96 return true
97 end
98 end
99
100
101 redef class Game
102
103 redef fun input(ie)
104 do
105 if ie isa ASensorAccelerometer or ie isa MotionEvent then
106 ball.intercepts(ie)
107 return true
108 end
109 return false
110 end
111 end