2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*global $: false, tizen: false, navigator: false, app: true*/
21 R: 2000.0, // // gravity constant * m * M
41 resistance: 0.98, // air
42 friction: 0.90, // bounce
50 animationInterval: 40,
53 * Draw earth background position
54 * @param {int} x current x earth position
55 * @param {int} y current y earth position
57 earthUpdateBackgroundPosition: function earthUpdateBackgroundPosition(x, y) {
68 cX = (this.gameWidth - this.backgroundWidth) / 2;
69 cY = (this.gameHeight - this.backgroundHeight) / 2;
74 bdX = tX - this.backgroundLeft;
75 bdY = tY - this.backgroundTop;
79 this.backgroundLeft += bdX * br;
80 this.backgroundTop += bdY * br;
82 $('.background').css('background-position', (this.backgroundLeft - 330) + 'px ' + (this.backgroundTop - 330) + 'px');
87 * @param {int} x current x earth position
88 * @param {int} y current y earth position
90 earthUpdateSunPosition: function earthUpdateSunPosition(x, y) {
101 cX = (this.gameWidth - this.sunWidth) / 2;
102 cY = (this.gameHeight - this.sunHeight) / 2;
107 bdX = tX - this.sunX;
108 bdY = tY - this.sunY;
112 this.sunX += bdX * br;
113 this.sunY += bdY * br;
115 $('#sun').css('left', this.sunX + 'px');
116 $('#sun').css('top', this.sunY + 'px');
121 * Deceleration - used when the earth leaves the Sun's gravitation
123 deceleration: function deceleration() {
130 * Draw the next animation frame for the 'earth' tab
132 earthEvents: function earthEvents() {
135 var event, borderTolerance,
144 borderTolerance = 30; // when Earth reach a border, then Earth is "moving"
146 x = -event.accelerationIncludingGravity.x;
147 y = -event.accelerationIncludingGravity.y;
149 // calculate X and Y distances between the Sun and Earth
150 dXl = (this.sunX + this.sunWidth / 2 - (this.ballX + (this.ballWidth / 2))); // x distance
151 dYl = (this.sunY + this.sunHeight / 2 - (this.ballY + (this.ballHeight / 2))); // y distance
153 if (Math.abs(dXl) < 1) {
154 dXl = dXl < 0 ? -1 : 1; // round to 1 * sign
156 if (Math.abs(dYl) < 1) {
157 dYl = dYl < 0 ? -1 : 1; // round to 1 * sign
161 d2 = Math.pow(dXl, 2) + Math.pow(dYl, 2);
165 // acceleration is proportional to 1/d2 [a=GM/r^2]
166 // X component is also proportional to dXl / d
167 ddx = (this.R * dXl) / (d2 * d);
168 ddy = (this.R * dYl) / (d2 * d);
170 // apply acceleration to speed
174 ratio = Math.sqrt(Math.pow(this.dX, 2) + Math.pow(this.dY, 2)) / 25; // max speed
175 if (ratio > 1) { // speed limit achieved
180 // apply speed to Earth position
181 this.ballX += this.dX;
182 this.ballY += this.dY;
184 // What do it when the earth leaves gravitation of the Sun?;
185 if (this.ballX > (this.gameWidth + borderTolerance)) {
186 this.ballX = -borderTolerance; this.deceleration();
188 if (this.ballY > (this.gameHeight + borderTolerance)) {
189 this.ballY = -borderTolerance; this.deceleration();
191 if (this.ballX < -borderTolerance) {
192 this.ballX = this.gameWidth + borderTolerance; this.deceleration();
194 if (this.ballY < -borderTolerance) {
195 this.ballY = this.gameHeight + borderTolerance; this.deceleration();
198 // update Earth position
199 $('.ball').css('left', this.ballX + 'px');
200 $('.ball').css('top', this.ballY + 'px');
202 // relative depth Sun / Earth
204 $('.ball').css('z-index', 100);
206 $('.ball').css('z-index', 20);
209 this.earthUpdateBackgroundPosition(x, y);
210 this.earthUpdateSunPosition(x, y);
214 * Checks if the ball already was on the edge in the previous step
216 * If so, this is not a 'real' bounce - the ball is just laying on the edge
217 * Uses globals: ballX, ballY, ballWidth, ballHeight, gameWidth, gameHeight
221 shouldVibrateIfHitsEdge: function shouldVibrateIfHitsEdge() {
228 if (this.ballX <= 0) {
230 } else if ((this.ballX + this.ballWidth) >= this.gameWidth) {
233 if (this.ballY <= 0) {
235 } else if ((this.ballY + this.ballHeight) >= this.gameHeight) {
243 * Draw the next animation frame for the 'ball' tab
245 ballEvents: function ballEvents() {
258 shouldVibrate = null,
259 isHittingEdge = null;
263 x = -event.accelerationIncludingGravity.x;
264 y = -event.accelerationIncludingGravity.y;
277 this.dX *= this.resistance;
278 this.dY *= this.resistance;
280 shouldVibrate = this.shouldVibrateIfHitsEdge();
282 this.ballX += this.dX;
283 this.ballY += this.dY;
285 if (this.ballX < 0) {
287 this.dX = Math.abs(this.dX) * this.friction - this.frictionC;
288 this.dY *= this.sideFriction;
290 } else if ((this.ballX + this.ballWidth) > this.gameWidth) {
291 this.ballX = this.gameWidth - this.ballWidth;
292 this.dX = -Math.abs(this.dX) * this.friction + this.frictionC;
293 this.dY *= this.sideFriction;
295 if (this.ballX < 0) {
300 if (this.ballY < 0) {
302 this.dY = Math.abs(this.dY) * this.friction - this.frictionC;
303 this.dX *= this.sideFriction;
305 } else if ((this.ballY + this.ballHeight) > this.gameHeight) {
306 this.ballY = this.gameHeight - this.ballHeight;
307 this.dY = -Math.abs(this.dY) * this.friction + this.frictionC;
308 this.dX *= this.sideFriction;
310 if (this.ballY < 0) {
316 x: (stickLeft || stickRight) && Math.abs(this.dX) > 1,
317 y: (stickTop || stickBottom) && Math.abs(this.dY) > 1
320 // if on the edge and the hitting speed is high enough
321 if ((shouldVibrate.x && isHittingEdge.x) || (shouldVibrate.y && isHittingEdge.y)) {
322 if (typeof navigator.webkitVibrate === 'function') {
323 navigator.webkitVibrate(100);
325 navigator.vibrate(100);
329 $('.ball').css('left', this.ballX + 'px');
330 $('.ball').css('top', this.ballY + 'px');
332 rX = this.ballX - rX;
333 rY = this.ballY - rY;
338 * Draw the next animation frame
340 fun: function fun() {
343 switch (this.current) {
354 console.warn("Incorrect current mode");
359 // animation - go to next step;
360 setTimeout(this.fun.bind(this), this.animationInterval);
364 * Switch to the 'ball' tab
366 startBall: function startBall() {
369 $('.ui-content').removeClass('background1 background2 background3').addClass('background1');
370 $(':jqmData(role="controlbar")').find('.ui-btn').removeClass('ui-btn-hover-s ui-btn-down-s');
371 this.gameHeight = $('.background').outerHeight();
374 this.resistance = 0.98;
375 this.friction = 0.90;
376 this.sideFriction = 0.95;
377 this.frictionC = 0.002;
379 this.current = 'ball';
382 $('.ball').attr('src', './images/ball1.png');
383 $('.ball').css('width', '86px');
384 $('.ball').css('height', '86px');
386 $('.background').css('background-position', '0px -90px');
388 this.ballWidth = parseInt($('.ball').css('width'), 10);
389 this.ballHeight = parseInt($('.ball').css('height'), 10);
393 * Switch to the 'sky' tab
395 startSky: function startSky() {
397 $('.ui-content').removeClass('background1 background2 background3').addClass('background2');
398 $(':jqmData(role="controlbar")').find('.ui-btn').removeClass('ui-btn-hover-s ui-btn-down-s');
399 this.gameHeight = $('.background').outerHeight();
402 this.resistance = 0.90;
403 this.friction = 0.98;
404 this.sideFriction = 0.95;
405 this.frictionC = 0.002;
407 this.current = 'baloon';
410 $('.ball').attr('src', './images/balloon.png');
411 $('.ball').css('width', '100px');
412 $('.ball').css('height', '100px');
414 $('.background').css('background-position', '0px -90px');
416 this.ballWidth = parseInt($('.ball').css('width'), 10);
417 this.ballHeight = parseInt($('.ball').css('height'), 10);
421 * Switch to the 'space' tab
423 startSpace: function startSpace() {
425 var backgroundPosition, arrayPos;
427 $('.ui-content').removeClass('background1 background2 background3').addClass('background3');
428 $(':jqmData(role="controlbar")').find('.ui-btn').removeClass('ui-btn-hover-s ui-btn-down-s');
430 this.gameHeight = $('.background').outerHeight();
432 this.friction = 0.60; // bounce
433 this.sideFriction = 0.95;
434 this.frictionC = 0.0;
436 this.current = 'earth';
438 $('.ball').attr('src', './images/earth.png');
439 $('#main').append('<img id="sun" class="sun" src="./images/sun.png" style="display: none;"></img>');
441 this.sunX = (this.gameWidth - parseInt($('#sun').css('width'), 10)) / 2;
442 this.sunY = (this.gameHeight - parseInt($('#sun').css('height'), 10)) / 2;
443 $('.ball').css('width', '50px');
444 $('.ball').css('height', '50px');
446 $('.background').css('background-position', '0px 0px');
448 this.ballWidth = parseInt($('.ball').css('width'), 10);
449 this.ballHeight = parseInt($('.ball').css('height'), 10);
450 this.sunWidth = parseInt($('#sun').css('width'), 10);
451 this.sunHeight = parseInt($('#sun').css('height'), 10);
453 backgroundPosition = $('.background').css('background-position');
455 arrayPos = backgroundPosition.split(' ');
456 this.backgroundTop = parseInt(arrayPos[0], 10);
457 this.backgroundLeft = parseInt(arrayPos[1], 10);
458 this.backgroundWidth = parseInt($('.background').css('width'), 10);
459 this.backgroundHeight = parseInt($('.background').css('height'), 10);
462 saveSensorData: function saveSensorData(event) {
468 $(document).ready(function () {
471 contentHeight = screen.availHeight - $('div[data-role="header"]').outerHeight() - $('div[data-role="footer"]').outerHeight();
473 $('div[data-role="content"]').css('height', contentHeight - 33);
474 app.gameWidth = screen.availWidth;
475 app.ballWidth = parseInt($('.ball').css('width'), 10);
476 app.ballHeight = parseInt($('.ball').css('height'), 10);
478 window.addEventListener('devicemotion', app.saveSensorData.bind(app), false);
482 $('.ui-btn-back').bind('touchstart', function (event) {
483 tizen.application.getCurrentApplication().exit();
486 $('#btnBall').bind('tap', function (event) {
491 $('#btnSky').bind('tap', function (event) {
496 $('#btnSpace').bind('tap', function (event) {
501 $('#mainPage').on('pageshow', function () {
505 // Preload backgrounds;
506 img = $('<img>').hide();
507 img.attr('src', 'images/background1.png');
510 $(window).resize(function () {
512 app.gameWidth = screen.availWidth;
513 app.gameHeight = $('.background').outerHeight();