# this file is part of NIT ( http://www.nitlanguage.org ).
#
-# Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
-#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# See the License for the specific language governing permissions and
# limitations under the License.
-module ballz_android
-
-import realtime
-import mnit_android
-import game_logic
-class MyApp
- super App
+# Android part of mnit_ballz
+module ballz_android is
+ app_version(0, 2, git_revision)
+ app_name("Ballz")
+ app_namespace "org.nitlanguage.ballz"
+ android_api_target 19
+end
- var screen: nullable Screen
+import android::portrait
+import android::sensors
+import display
+import mnit::android
- var target_dt = 20000000
+redef class App
- init do super
+ # The game
+ var game: nullable Game is noautoinit
+ redef fun run do
+ accelerometer.enabled = true
+ accelerometer.event_rate = 10000
+ magnetic_field.enabled = true
+ gyroscope.enabled = true
+ light.enabled = true
+ proximity.enabled = true
+ maximum_fps = 50.0
+ sensors_support_enabled = true
+ super
+ end
- redef fun init_window
+ redef fun on_create
do
super
- screen = new Screen(self, display.as(Display))
-
+ var display = self.display.as(not null)
+ game = new Game(display.width.to_f, display.height.to_f)
end
redef fun frame_core(display)
do
- var screen = self.screen
- if screen != null then
- var clock = new Clock
-
- screen.game.do_turn
- screen.do_frame(display)
-
- var dt = clock.lapse
- if dt.sec == 0 and dt.nanosec < target_dt then
- var sleep_t = target_dt - dt.nanosec
- sys.nanosleep(0, sleep_t)
- end
+ var game = game
+ if game != null then
+ game.do_turn
+ game.draw(display, assets)
end
end
redef fun input(ie)
- do
- if ie isa QuitEvent then
+ do
+ if paused then return false
+ if ie isa QuitEvent then
quit = true
return true
end
- if screen != null then
- return screen.input(ie)
+ var game = game
+ if game != null then
+ return game.input(ie)
end
return false
end
end
-var app = new MyApp
-app.sensors_support_enabled = true
-app.accelerometer.enabled = true
-app.accelerometer.event_rate = 10000
-app.magnetic_field.enabled = true
-app.gyroscope.enabled = true
-app.light.enabled = true
-app.proximity.enabled = true
-app.main_loop
+redef class Ball
+
+ redef fun intercepts(event)
+ do
+ if event isa ASensorAccelerometer then
+ acceleration(event.x, event.y)
+ else if event isa ASensorMagneticField then
+ #deal with Magnetic field sensor
+ #print "ASensorMagneticField : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
+ else if event isa ASensorGyroscope then
+ #deal with Gyroscope sensor
+ #print "ASensorGyroscope : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
+ else if event isa ASensorLight then
+ #deal with light sensor
+ #print "ASensorLight : light = " + event.light.to_s
+ else if event isa ASensorProximity then
+ #deal with proximity sensor
+ #print "ASensorProximity : distance = " + event.distance.to_s
+ else if event isa MotionEvent then
+ end
+ return true
+ end
+end
+
+
+redef class Game
+
+ redef fun input(ie)
+ do
+ if ie isa ASensorAccelerometer or ie isa MotionEvent then
+ ball.intercepts(ie)
+ return true
+ end
+ return false
+ end
+end