diff --git a/activities/ChatPrototype.activity/js/loader.js b/activities/ChatPrototype.activity/js/loader.js index 6ca5783bf..a1d7779bd 100644 --- a/activities/ChatPrototype.activity/js/loader.js +++ b/activities/ChatPrototype.activity/js/loader.js @@ -1,38 +1,33 @@ var l10n_s; requirejs.config({ - baseUrl: "lib", - paths: { - activity: "../js" - } + baseUrl: "lib", + paths: { + activity: "../js" + } }); requirejs(["activity/activity"]); -requirejs(["webL10n","sugar-web/env"], function(l10n, env) { - l10n_s = l10n; //global declaration of translate interface +requirejs(["webL10n","sugar-web/env","sugar-web/datastore"], function(l10n, env ,datastore) { + l10n_s = l10n; //global declaration of translate interface - getSettings(function(settings) { //globally setting language from sugar settings + datastore.localStorage.load(function() { + getSettings(function(settings) { //globally setting language from sugar settings l10n_s.language.code = settings.language; + }); }); function getSettings(callback) { var defaultSettings = { name: "", - language: navigator.language + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language }; if (!env.isSugarizer()) { callback(defaultSettings); return; } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } + loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); } }); diff --git a/activities/Clock.activity/js/activity.js b/activities/Clock.activity/js/activity.js index e483c453c..f8d39a6b3 100644 --- a/activities/Clock.activity/js/activity.js +++ b/activities/Clock.activity/js/activity.js @@ -1,4 +1,4 @@ -define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","mustache","moment-with-locales.min"], function (activity,radioButtonsGroup,mustache,moment) { +define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","mustache","moment-with-locales.min","webL10n"], function (activity,radioButtonsGroup,mustache,moment) { // Manipulate the DOM only when it is ready. require(['domReady!'], function (doc) { diff --git a/activities/Clock.activity/js/loader.js b/activities/Clock.activity/js/loader.js index b7f4534c8..cfe0c7a42 100644 --- a/activities/Clock.activity/js/loader.js +++ b/activities/Clock.activity/js/loader.js @@ -1,39 +1,34 @@ var l10n_s; requirejs.config({ - baseUrl: "lib", - paths: { - activity: "../js" - } + baseUrl: "lib", + paths: { + activity: "../js" + } }); requirejs(["activity/activity"]); -requirejs(["webL10n","sugar-web/env","moment-with-locales.min"], function(l10n, env, moment) { - l10n_s = l10n; //global declaration of translate interface +requirejs(["webL10n","sugar-web/env","moment-with-locales.min","sugar-web/datastore"], function(l10n, env, moment,datastore) { + l10n_s = l10n; //global declaration of translate interface - getSettings(function(settings) { //globally setting language from sugar settings + datastore.localStorage.load(function() { + getSettings(function(settings) { //globally setting language from sugar settings l10n_s.language.code = settings.language; moment.locale(settings.language); + }); }); - function getSettings(callback) { + function getSettings(callback) { var defaultSettings = { name: "", - language: navigator.language + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language }; if (!env.isSugarizer()) { callback(defaultSettings); return; } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } + loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); } }); diff --git a/activities/Clock.activity/lib/webL10n.js b/activities/Clock.activity/lib/webL10n.js index e9101be98..f76d66a5e 100644 --- a/activities/Clock.activity/lib/webL10n.js +++ b/activities/Clock.activity/lib/webL10n.js @@ -43,7 +43,7 @@ define(function (require) { * content until the `localized' event happens. */ - var gAsyncResourceLoading = false; // read-only + var gAsyncResourceLoading = true; // read-only /** @@ -807,7 +807,6 @@ define(function (require) { function getL10nData(key, args) { var data = gL10nData[key]; if (!data) { - l10nStartup(); consoleWarn('#' + key + ' missing for [' + gLanguage + ']'); } diff --git a/activities/GetThingsDone.activity/js/loader.js b/activities/GetThingsDone.activity/js/loader.js index b7f4534c8..085bb0b0c 100644 --- a/activities/GetThingsDone.activity/js/loader.js +++ b/activities/GetThingsDone.activity/js/loader.js @@ -1,39 +1,34 @@ var l10n_s; requirejs.config({ - baseUrl: "lib", - paths: { - activity: "../js" - } + baseUrl: "lib", + paths: { + activity: "../js" + } }); requirejs(["activity/activity"]); -requirejs(["webL10n","sugar-web/env","moment-with-locales.min"], function(l10n, env, moment) { - l10n_s = l10n; //global declaration of translate interface +requirejs(["webL10n","sugar-web/env","moment-with-locales.min","sugar-web/datastore"], function(l10n, env, moment,datastore) { + l10n_s = l10n; //global declaration of translate interface - getSettings(function(settings) { //globally setting language from sugar settings + datastore.localStorage.load(function() { + getSettings(function(settings) { //globally setting language from sugar settings l10n_s.language.code = settings.language; moment.locale(settings.language); + }); }); function getSettings(callback) { var defaultSettings = { name: "", - language: navigator.language + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language }; if (!env.isSugarizer()) { callback(defaultSettings); return; } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } + loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); } }); diff --git a/activities/Markdown.activity/js/loader.js b/activities/Markdown.activity/js/loader.js index 9c9d69332..4f9c6e809 100644 --- a/activities/Markdown.activity/js/loader.js +++ b/activities/Markdown.activity/js/loader.js @@ -12,31 +12,26 @@ requirejs.config({ requirejs(["activity/activity"]); -requirejs(["webL10n","sugar-web/env","moment-with-locales.min"], function(l10n, env, moment) { - l10n_s = l10n; //global declaration of translate interface +requirejs(["webL10n","sugar-web/env","moment-with-locales.min","sugar-web/datastore"], function(l10n, env, moment,datastore) { + l10n_s = l10n; //global declaration of translate interface - getSettings(function(settings) { //globally setting language from sugar settings + datastore.localStorage.load(function() { + getSettings(function(settings) { //globally setting language from sugar settings l10n_s.language.code = settings.language; moment.locale(settings.language); + }); }); - function getSettings(callback) { + function getSettings(callback) { var defaultSettings = { name: "", - language: navigator.language + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language }; if (!env.isSugarizer()) { callback(defaultSettings); return; } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } + loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); } }); diff --git a/activities/Moon.activity/js/moon-activity.js b/activities/Moon.activity/js/moon-activity.js index 73342b1e3..86079934c 100644 --- a/activities/Moon.activity/js/moon-activity.js +++ b/activities/Moon.activity/js/moon-activity.js @@ -15,13 +15,15 @@ define(['activity/data-model', 'activity/draw', 'webL10n', 'sugar-web/env', 'sug l10n.ready(function() { if (first) { first = false; - getSugarSettings(function(settings) { - l10n.language.code = settings.language; - moment.locale(settings.language); - var refreshTime = setTimeout(function() { - clearTimeout(refreshTime); - updateView(); - }, 50); + datastore.localStorage.load(function() { + getSugarSettings(function(settings) { + l10n.language.code = settings.language; + moment.locale(settings.language); + var refreshTime = setTimeout(function() { + clearTimeout(refreshTime); + updateView(); + }, 50); + }); }); } }); @@ -75,24 +77,17 @@ define(['activity/data-model', 'activity/draw', 'webL10n', 'sugar-web/env', 'sug function getSugarSettings(callback) { - var defaultSettings = { - name: "", - language: navigator.language - }; - if (!env.isSugarizer()) { - callback(defaultSettings); - return; - } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } - } + var defaultSettings = { + name: "", + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language + }; + if (!env.isSugarizer()) { + callback(defaultSettings); + return; + } + var loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); + } function updateSizes() { diff --git a/activities/Speak.activity/js/SpeakActivity.js b/activities/Speak.activity/js/SpeakActivity.js index 65d8e7fed..ea11c45e4 100644 --- a/activities/Speak.activity/js/SpeakActivity.js +++ b/activities/Speak.activity/js/SpeakActivity.js @@ -1,4 +1,4 @@ -define(["sugar-web/graphics/palette","sugar-web/env","webL10n"], function (palette,env,l10n) { +define(["sugar-web/graphics/palette","sugar-web/env","webL10n","sugar-web/datastore"], function (palette,env,l10n,datastore) { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext('2d'); @@ -42,32 +42,34 @@ define(["sugar-web/graphics/palette","sugar-web/env","webL10n"], function (palet function init(){ speech = Speech(); - getSettings(function(settings) { - sugarSettings = settings; - speech.init(sugarSettings); - // If not IE, setup mouse for capture - if (!IE){ - document.captureEvents(Event.MOUSEMOVE) - } - var FPS = 30; - setInterval(function() { - updateCanvas(); - }, 1000/FPS); - window.addEventListener('localized', function() { - if (first) { - l10n.language.code = sugarSettings.language; - first = false; - return; - } else { - localize(); - var timer = window.setTimeout(function() { - window.clearTimeout(timer); - var language = document.getElementById('speaklang').innerHTML; - var text = l10n.get("TypeSomething", {name:sugarSettings.name}); - speech.playVoice(language, text); - moveMouth(text); - }, 100); + datastore.localStorage.load(function() { + getSettings(function(settings) { + sugarSettings = settings; + speech.init(sugarSettings); + // If not IE, setup mouse for capture + if (!IE){ + document.captureEvents(Event.MOUSEMOVE) } + var FPS = 30; + setInterval(function() { + updateCanvas(); + }, 1000/FPS); + window.addEventListener('localized', function() { + if (first) { + l10n.language.code = sugarSettings.language; + first = false; + return; + } else { + localize(); + var timer = window.setTimeout(function() { + window.clearTimeout(timer); + var language = document.getElementById('speaklang').innerHTML; + var text = l10n.get("TypeSomething", {name:sugarSettings.name}); + speech.playVoice(language, text); + moveMouth(text); + }, 100); + } + }); }); }); } @@ -75,21 +77,14 @@ define(["sugar-web/graphics/palette","sugar-web/env","webL10n"], function (palet function getSettings(callback) { var defaultSettings = { name: "", - language: navigator.language + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language }; if (!env.isSugarizer()) { callback(defaultSettings); return; } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } + var loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); } function hidePalettes(){ diff --git a/activities/TankOp.activity/app.js b/activities/TankOp.activity/app.js index 151b71b6c..0fa77aefd 100644 --- a/activities/TankOp.activity/app.js +++ b/activities/TankOp.activity/app.js @@ -1,134 +1,136 @@ -requirejs(["sugar-web/activity/activity","../lib/webL10n"], function (activity, l10n) { - - l10n.language.code = language_code; //defined in settings.js - - // Main app class - enyo.kind({ - name: "TankOp.App", - kind: enyo.Control, - published: {activity: null}, - classes: "home", - components: [ - // Image - {kind: "Image", classes: "home-image no-select-content", src: "images/home.png"}, - - // Popup - {classes: "start-button", ontap: "play", components: [ - {kind: "Image", classes: "start-button-image no-select-content", src: "images/button.png"}, - {content: l10n.get("Start"), classes: "start-button-text no-select-content"} + +// Main app class +enyo.kind({ + name: "TankOp.App", + kind: enyo.Control, + published: {activity: null}, + classes: "home", + components: [ + // Image + {kind: "Image", classes: "home-image no-select-content", src: "images/home.png"}, + + // Popup + {classes: "start-button", ontap: "play", components: [ + {kind: "Image", classes: "start-button-image no-select-content", src: "images/button.png"}, + {name: "start", classes: "start-button-text no-select-content"} + ]}, + + // Credit + {kind: "Image", classes: "credit-button no-select-content", src: "images/credit.png", ontap: "showCredit"}, + {name: "creditsPopup", kind: "TankOp.CreditsPopup"}, + + // Next mission + {classes: "mission-description no-select-content", components: [ + {components: [ + {name: "nextmission", classes: "mission-header mission-line"}, + {content: ":", classes: "mission-dot mission-line"} ]}, - - // Credit - {kind: "Image", classes: "credit-button no-select-content", src: "images/credit.png", ontap: "showCredit"}, - {name: "creditsPopup", kind: "TankOp.CreditsPopup"}, - - // Next mission - {classes: "mission-description no-select-content", components: [ - {components: [ - {content: l10n.get("NextMission"), classes: "mission-header mission-line"}, - {content: ":", classes: "mission-dot mission-line"} - ]}, - {classes: "go-arrow go-left mission-line", ontap: "previousMission"}, - {name: "mission", content: " ", classes: "mission-text mission-line"}, - {classes: "go-arrow go-right mission-line", ontap: "nextMission"} + {classes: "go-arrow go-left mission-line", ontap: "previousMission"}, + {name: "mission", content: " ", classes: "mission-text mission-line"}, + {classes: "go-arrow go-right mission-line", ontap: "nextMission"} + ]}, + + {classes: "mission-status no-select-content", components: [ + {components: [ + {name: "completed", classes: "mission-header mission-line"}, + {content: ":", classes: "mission-dot mission-line"} ]}, - - {classes: "mission-status no-select-content", components: [ - {components: [ - {content: l10n.get("Completed"), classes: "mission-header mission-line"}, - {content: ":", classes: "mission-dot mission-line"} - ]}, - {name: "stars", components: [ - {classes: "mission mission-completed mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"}, - {classes: "mission mission-tocomplete mission-line"} - ]} + {name: "stars", components: [ + {classes: "mission mission-completed mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"}, + {classes: "mission mission-tocomplete mission-line"} ]} - ], - - // Constructor - create: function() { - this.inherited(arguments); - this.currentlevel = 0; - this.$.mission.setContent(settings.levels[this.currentlevel].name); - - this.init(); - }, - - // Draw screen - init: function() { - // Play theme - sound.play("audio/soundtrack", true); - - // Draw completed mission - var items = []; - enyo.forEach(this.$.stars.getControls(), function(item) { items.push(item); }); - for (var i = 0 ; i < items.length ; i++) { items[i].destroy(); }; - for (var i = 0 ; i < settings.levels.length ; i++) { - this.$.stars.createComponent({ - classes: (settings.levels[i].completed ? "mission mission-completed mission-line" : "mission mission-tocomplete mission-line") - }, - {owner: this}).render(); - } - }, - - // Select mission - previousMission: function() { - this.currentlevel--; - if (this.currentlevel < 0) - this.currentlevel = settings.levels.length-1; - this.$.mission.setContent(settings.levels[this.currentlevel].name); - }, - - nextMission: function() { - this.currentlevel++; - if (this.currentlevel == settings.levels.length) - this.currentlevel = 0; - this.$.mission.setContent(settings.levels[this.currentlevel].name); - }, - - // Show credit - showCredit: function() { - this.$.creditsPopup.show(); - }, - - // Play game - play: function() { - // Stop sound - sound.pause(); - - // Start game - new TankOp.Play({level: this.currentlevel}).renderInto(document.getElementById("board")); - }, - - // Load game from datastore - load: function() { - var datastoreObject = this.activity.getDatastoreObject(); - var currentthis = this; - datastoreObject.loadAsText(function (error, metadata, data) { - var data = JSON.parse(data); - settings.setState(data); - currentthis.init(); - }); - }, - - // Save game in datastore - save: function(count) { - var datastoreObject = this.activity.getDatastoreObject(); - var jsonData = JSON.stringify(settings.getState()); - datastoreObject.setDataAsText(jsonData); - datastoreObject.save(function() {}); + ]} + ], + + // Constructor + create: function() { + this.inherited(arguments); + this.currentlevel = 0; + this.$.mission.setContent(preferences.levels[this.currentlevel].name); + + this.init(); + }, + + // Draw screen + init: function() { + // Localize + this.$.start.setContent(l10n.get("Start")); + this.$.nextmission.setContent(l10n.get("NextMission")); + this.$.completed.setContent(l10n.get("Completed")); + + // Play theme + sound.play("audio/soundtrack", true); + + // Draw completed mission + var items = []; + this.$.mission.setContent(preferences.levels[this.currentlevel].name = l10n.get(preferences.levels[this.currentlevel].id)); + enyo.forEach(this.$.stars.getControls(), function(item) { items.push(item); }); + for (var i = 0 ; i < items.length ; i++) { items[i].destroy(); }; + for (var i = 0 ; i < preferences.levels.length ; i++) { + preferences.levels[i].name = l10n.get(preferences.levels[i].id); + this.$.stars.createComponent({ + classes: (preferences.levels[i].completed ? "mission mission-completed mission-line" : "mission mission-tocomplete mission-line") + }, + {owner: this}).render(); } + }, + + // Select mission + previousMission: function() { + this.currentlevel--; + if (this.currentlevel < 0) + this.currentlevel = preferences.levels.length-1; + this.$.mission.setContent(preferences.levels[this.currentlevel].name); + }, + + nextMission: function() { + this.currentlevel++; + if (this.currentlevel == preferences.levels.length) + this.currentlevel = 0; + this.$.mission.setContent(preferences.levels[this.currentlevel].name); + }, + + // Show credit + showCredit: function() { + this.$.creditsPopup.show(); + }, + + // Play game + play: function() { + // Stop sound + sound.pause(); + + // Start game + new TankOp.Play({level: this.currentlevel}).renderInto(document.getElementById("board")); + }, + + // Load game from datastore + load: function() { + var datastoreObject = this.activity.getDatastoreObject(); + var currentthis = this; + datastoreObject.loadAsText(function (error, metadata, data) { + var data = JSON.parse(data); + preferences.setState(data); + currentthis.init(); + }); + }, + + // Save game in datastore + save: function(count) { + var datastoreObject = this.activity.getDatastoreObject(); + var jsonData = JSON.stringify(preferences.getState()); + datastoreObject.setDataAsText(jsonData); + datastoreObject.save(function() {}); + } - - }); -}); \ No newline at end of file +}); diff --git a/activities/TankOp.activity/credits.js b/activities/TankOp.activity/credits.js index 7d61b285c..5145e6d3f 100644 --- a/activities/TankOp.activity/credits.js +++ b/activities/TankOp.activity/credits.js @@ -1,46 +1,54 @@ -requirejs(["../lib/webL10n"], function (l10n) { - - l10n.language.code = language_code; //defined in settings.js + // Credits popup +enyo.kind({ + name: "TankOp.CreditsPopup", + kind: "Popup", + classes: "credits-popup", + centered: true, + modal: true, + floating: true, + components: [ + { kind: "Scroller", classes: "credit-content no-select-content", components: [ + { kind: "Image", src: "images/hq_blue.png", classes: "credit-image-hq" }, + { kind: "Image", src: "images/target.png", classes: "credit-image-target" }, + { kind: "Image", src: "images/tank_red_0.png", classes: "credit-image-tankred" }, + { name: "cac", classes: "credit-title" }, + { content: "Lionel Laské", classes: "credit-name" }, + { kind: "Image", src: "images/tank_blue_2.png", classes: "credit-image-tank" }, + { name: "arts", classes: "credit-title" }, + { name: "vicki", classes: "credit-name" }, + { name: "tux", classes: "credit-name" }, + { name: "mister", classes: "credit-name" }, + { name: "none", classes: "credit-name" }, + { kind: "Image", src: "images/helo_blue_2.png", classes: "credit-image-helo" }, + { name: "music", classes: "credit-title" }, + { name: "valkyries", classes: "credit-name" }, + { kind: "Image", src: "images/soldier_blue_2.png", classes: "credit-image-soldier" }, + { name: "sounds", classes: "credit-title" }, + { name: "fridobeck", classes: "credit-name" }, + { name: "joshfeed", classes: "credit-name" }, + { name: "danipenet", classes: "credit-name" }, + { name: "juskiddink", classes: "credit-name" } + ]} + ], - // Credits popup - enyo.kind({ - name: "TankOp.CreditsPopup", - kind: "Popup", - classes: "credits-popup", - centered: true, - modal: true, - floating: true, - components: [ - { kind: "Scroller", classes: "credit-content no-select-content", components: [ - { kind: "Image", src: "images/hq_blue.png", classes: "credit-image-hq" }, - { kind: "Image", src: "images/target.png", classes: "credit-image-target" }, - { kind: "Image", src: "images/tank_red_0.png", classes: "credit-image-tankred" }, - { content: l10n.get("CaC"), classes: "credit-title" }, - { content: "Lionel Laské", classes: "credit-name" }, - { kind: "Image", src: "images/tank_blue_2.png", classes: "credit-image-tank" }, - { content: l10n.get("Arts"), classes: "credit-title" }, - { content: l10n.get("Vicki"), classes: "credit-name" }, - { content: l10n.get("Tux"), classes: "credit-name" }, - { content: l10n.get("Mister"), classes: "credit-name" }, - { content: l10n.get("None"), classes: "credit-name" }, - { kind: "Image", src: "images/helo_blue_2.png", classes: "credit-image-helo" }, - { content: l10n.get("Music"), classes: "credit-title" }, - { content: l10n.get("Valkyries"), classes: "credit-name" }, - { kind: "Image", src: "images/soldier_blue_2.png", classes: "credit-image-soldier" }, - { content: l10n.get("Sounds"), classes: "credit-title" }, - { content: l10n.get("Fridobeck"), classes: "credit-name" }, - { content: l10n.get("Joshfeed"), classes: "credit-name" }, - { content: l10n.get("Danipenet"), classes: "credit-name" }, - { content: l10n.get("Juskiddink"), classes: "credit-name" } - ]} - ], + // Constructor + create: function() { + this.inherited(arguments); + }, - // Constructor - create: function() { - this.inherited(arguments); - }, - - rendered: function() { - } - }); -}); \ No newline at end of file + rendered: function() { + this.$.cac.setContent(l10n.get("CaC")); + this.$.arts.setContent(l10n.get("Arts")); + this.$.vicki.setContent(l10n.get("Vicki")); + this.$.tux.setContent(l10n.get("Tux")); + this.$.mister.setContent(l10n.get("Mister")); + this.$.none.setContent(l10n.get("None")); + this.$.music.setContent(l10n.get("Music")); + this.$.valkyries.setContent(l10n.get("Valkyries")); + this.$.sounds.setContent(l10n.get("Sounds")); + this.$.fridobeck.setContent(l10n.get("Fridobeck")); + this.$.joshfeed.setContent(l10n.get("Joshfeed")); + this.$.danipenet.setContent(l10n.get("Danipenet")); + this.$.juskiddink.setContent(l10n.get("Juskiddink")); + } +}); diff --git a/activities/TankOp.activity/index.html b/activities/TankOp.activity/index.html index 00953dc9e..9b98247b5 100644 --- a/activities/TankOp.activity/index.html +++ b/activities/TankOp.activity/index.html @@ -9,12 +9,11 @@ + href="lib/sugar-web/graphics/css/sugar-200dpi.css"> - @@ -33,8 +32,8 @@ - - + +
@@ -42,11 +41,11 @@

TANK OPERATION

- +
- +
-
+ diff --git a/activities/TankOp.activity/js/activity.js b/activities/TankOp.activity/js/activity.js index 5a215cff1..99d12713b 100644 --- a/activities/TankOp.activity/js/activity.js +++ b/activities/TankOp.activity/js/activity.js @@ -1,4 +1,6 @@ -var app; +var app = null; +var l10n; +var preferences; var play; var sound; var mouse = {}; @@ -6,26 +8,37 @@ var mouse = {}; define(["sugar-web/activity/activity"], function (activity) { // Manipulate the DOM only when it is ready. - require(['domReady!'], function (doc) { + require(['domReady!',"settings"], function (doc, settings) { // Initialize the activity. activity.setup(); // Save mouse position document.onmousemove = function(e) { mouse.position = {x: e.pageX, y: e.pageY}; } - - // Create sound component - sound = new TankOp.Audio(); - sound.renderInto(document.getElementById("audio")); - - // Launch main screen - app = new TankOp.App({activity: activity}); - app.load(); - app.renderInto(document.getElementById("board")); - - // Stop sound at end of game to sanitize media environment, specifically on Android - document.getElementById("stop-button").addEventListener('click', function (event) { - sound.pause(); + preferences = settings; + preferences.load(function() { + l10n = preferences.l10n; + window.addEventListener('localized', function() { + // Force language + if (preferences.l10n.language.code != preferences.language) { + preferences.l10n.language.code = preferences.language; + return; + } + + // Create sound component + sound = new TankOp.Audio(); + sound.renderInto(document.getElementById("audio")); + + // Launch main screen + app = new TankOp.App({activity: activity}); + app.load(); + app.renderInto(document.getElementById("board")); + + // Stop sound at end of game to sanitize media environment, specifically on Android + document.getElementById("stop-button").addEventListener('click', function (event) { + sound.pause(); + }); + }); }); }); diff --git a/activities/TankOp.activity/settings.js b/activities/TankOp.activity/lib/settings.js similarity index 82% rename from activities/TankOp.activity/settings.js rename to activities/TankOp.activity/lib/settings.js index ae59d3189..f229a92de 100644 --- a/activities/TankOp.activity/settings.js +++ b/activities/TankOp.activity/lib/settings.js @@ -1,13 +1,38 @@ -var language_code = "en"; -requirejs(["webL10n","sugar-web/env"], function (l10n,env) { +define(["webL10n","sugar-web/env","sugar-web/datastore"], function (webL10n,env,datastore) { - getSettings(function(settings) { - language_code = settings.language; - l10n.language.code = language_code; - }); + var settings = {}; - settings = {}; + settings.l10n = webL10n; + + settings.load = function(callback) { + // Load datastore settings + datastore.localStorage.load(function() { + getSettings(function(conf) { + settings.language = conf.language; + settings.l10n.language.code = conf.language; + callback(); + }); + }); + + // Stop sound at end of game to sanitize media environment, specifically on Android + document.getElementById("stop-button").addEventListener('click', function (event) { + sound.pause(); + }); + } + + function getSettings(callback) { + var defaultSettings = { + name: "", + language: (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language + }; + if (!env.isSugarizer()) { + callback(defaultSettings); + return; + } + var loadedSettings = datastore.localStorage.getValue('sugar_settings'); + callback(loadedSettings); + } // Get/Set state settings.getState = function() { @@ -27,7 +52,6 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { } - // Game map settings.gameMap = function(map) { var width = constant.boardWidth, height = constant.boardHeight; @@ -121,7 +145,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { // Play levels settings.levels = [ { - name: l10n.get("Type"), // Name of level + id: "Type", // Id of level + name: "Type", // Name of level map: "mountain", // Name of map, see in util.js defense: [4, 0, 4, 0, 0], // Defense composition: #HQ, #Soldier, #Tank, #Canon, #Helo attack: 22, // #Attacking units @@ -130,7 +155,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false // Use at runtime to see if completed }, { - name: l10n.get("ADD3"), + id: "ADD3", + name: "ADD3", map: "trees", defense: [4, 2, 2, 0, 0], attack: 20, @@ -139,7 +165,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("ADD5"), + id: "ADD5", + name: "ADD5", map: "grass", defense: [4, 3, 1, 0, 0], attack: 30, @@ -148,7 +175,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("SUM10"), + id: "SUM10", + name: "SUM10", map: "mountain", defense: [4, 0, 4, 0, 0], attack: 30, @@ -157,7 +185,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("SUM15"), + id: "SUM15", + name: "SUM15", map: "trees", defense: [4, 2, 2, 0, 0], attack: 30, @@ -166,7 +195,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("SUM20"), + id: "SUM20", + name: "SUM20", map: "grass", defense: [4, 3, 1, 0, 0], attack: 40, @@ -175,7 +205,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("TDN"), + id: "TDN", + name: "TDN", map: "mountain", defense: [4, 4, 4, 0, 0], attack: 40, @@ -184,7 +215,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("Missing"), + id: "Missing", + name: "Missing", map: "trees", defense: [4, 2, 2, 0, 0], attack: 40, @@ -193,7 +225,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("S010"), + id: "S010", + name: "S010", map: "grass", defense: [4, 0, 4, 0, 0], attack: 40, @@ -202,7 +235,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("S020"), + id: "S020", + name: "S020", map: "trees", defense: [4, 2, 2, 0, 0], attack: 20, @@ -211,7 +245,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("STW"), + id: "STW", + name: "STW", map: "mountain", defense: [4, 4, 4, 0, 0], attack: 30, @@ -220,7 +255,8 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { completed: false }, { - name: l10n.get("AAS"), + id: "AAS", + name: "AAS", map: "trees", defense: [4, 2, 2, 0, 0], attack: 40, @@ -230,29 +266,5 @@ requirejs(["webL10n","sugar-web/env"], function (l10n,env) { } ]; - function getSettings(callback) { - var defaultSettings = { - name: "", - language: navigator.language - }; - if (!env.isSugarizer()) { - callback(defaultSettings); - return; - } - if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { - var loadedSettings = JSON.parse(values.sugar_settings); - chrome.storage.local.get('sugar_settings', function(values) { - callback(loadedSettings); - }); - } else { - var loadedSettings = JSON.parse(localStorage.sugar_settings); - callback(loadedSettings); - } - } - - // Stop sound at end of game to sanitize media environment, specifically on Android - document.getElementById("stop-button").addEventListener('click', function (event) { - sound.pause(); - }); - + return settings; }); diff --git a/activities/TankOp.activity/lib/webL10n.js b/activities/TankOp.activity/lib/webL10n.js index e9101be98..1ecdaf189 100644 --- a/activities/TankOp.activity/lib/webL10n.js +++ b/activities/TankOp.activity/lib/webL10n.js @@ -43,7 +43,7 @@ define(function (require) { * content until the `localized' event happens. */ - var gAsyncResourceLoading = false; // read-only + var gAsyncResourceLoading = true; // read-only /** diff --git a/activities/TankOp.activity/play.js b/activities/TankOp.activity/play.js index cba979c13..9c61fda03 100644 --- a/activities/TankOp.activity/play.js +++ b/activities/TankOp.activity/play.js @@ -1,497 +1,494 @@ -requirejs(["../lib/webL10n"], function (l10n) { - - l10n.language.code = language_code; //defined in settings.js - - // Play class - enyo.kind({ - name: "TankOp.Play", - kind: enyo.Control, - classes: "board", - published: { level: 0 }, - components: [ - // Playing zone - {name: "gamebox", classes: "game-box", ontap: "gameClick", components: [ +// Play class +enyo.kind({ + name: "TankOp.Play", + kind: enyo.Control, + classes: "board", + published: { level: 0 }, + components: [ + // Playing zone + {name: "gamebox", classes: "game-box", ontap: "gameClick", components: [ + ]}, + + // Status and score + {classes: "status-line", components: [ + {name: "wavetext", classes: "wave-text no-select-content"}, + {name: "wave", content: "1", classes: "wave-value no-select-content"}, + {name: "scoretext", classes: "score-text no-select-content"}, + {name: "score", content: "0000", classes: "score-value no-select-content"} + ]}, + + // Home button + {kind: "Image", classes: "home-button no-select-content", src: "images/gohome.png", ontap: "goHome"}, + + // LCD counter and Keyboard + {name: "keyboard", classes: "keyboard-set no-select-content", components: [ + {classes: "display-line", components: [ + {name: "lcd", kind: "LcdDisplay", classes: "lcd-value", size: 3, value: ""} ]}, - - // Status and score - {classes: "status-line", components: [ - {content: l10n.get("Wave"), classes: "wave-text no-select-content"}, - {name: "wave", content: "1", classes: "wave-value no-select-content"}, - {content: l10n.get("Score"), classes: "score-text no-select-content"}, - {name: "score", content: "0000", classes: "score-value no-select-content"} - ]}, - - // Home button - {kind: "Image", classes: "home-button no-select-content", src: "images/gohome.png", ontap: "goHome"}, - - // LCD counter and Keyboard - {name: "keyboard", classes: "keyboard-set no-select-content", components: [ - {classes: "display-line", components: [ - {name: "lcd", kind: "LcdDisplay", classes: "lcd-value", size: 3, value: ""} - ]}, - {classes: "keyboard-line", components: [ - {kind: "Image", src: "images/key_1.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_2.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_3.svg", classes: "keyboard", ontap: "virtkeyPressed"} - ]}, - {classes: "keyboard_line", components: [ - {kind: "Image", src: "images/key_4.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_5.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_6.svg", classes: "keyboard", ontap: "virtkeyPressed"} - ]}, - {classes: "keyboard_line", components: [ - {kind: "Image", src: "images/key_7.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_8.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_9.svg", classes: "keyboard", ontap: "virtkeyPressed"} - ]}, - {classes: "keyboard_line", components: [ - {kind: "Image", src: "images/key_0.svg", classes: "keyboard", ontap: "virtkeyPressed"}, - {kind: "Image", src: "images/key_fire.svg", classes: "keyboard", ontap: "virtkeyPressed"} - ]} + {classes: "keyboard-line", components: [ + {kind: "Image", src: "images/key_1.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_2.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_3.svg", classes: "keyboard", ontap: "virtkeyPressed"} ]}, - - // Key handling - {kind: "Signals", onkeypress: "keyPressed"}, - - // Image cache - {kind: "ImageCache", showing: false, onCacheLoaded: "cacheLoaded"} - ], - - // Constructor - create: function() { - this.inherited(arguments); - play = this; - this.imagesToLoad++; - this.endOfGame = false; - this.pausedGame = true; - this.initializedGame = false; - this.waitForClick = false; - - // Init canvas - var wsize = document.body.clientWidth; - if (wsize <= 480) { - this.zoom = 0.4; - } else if (wsize <= 640) { - this.zoom = 0.55; - } else if (wsize <= 768) { - this.zoom = 0.62; - } else if (wsize <= 854) { - this.zoom = 0.65; - } else if (wsize <= 960) { - this.zoom = 0.75; - } else if (wsize <= 1024) { - this.zoom = 0.88; - } else { - this.zoom = 1; - } - - this.$.gamebox.setStyle("max-height: "+(this.zoom*constant.areaHeight)+"px;"); - this.canvas = this.$.gamebox.createComponent({kind: "Canvas", id: "acanvas", name: "canvas", attributes: {width: constant.areaWidth, height: constant.areaHeight}}); - - // Start game loop - this.loopTimer = window.setInterval(enyo.bind(this, "gameLoopTick"), constant.loopInterval); - }, - - // Init game - initGame: function() { - // Game init - var level = this.currentlevel = settings.levels[this.level]; - var settings_map = level.map; - var settings_hq = level.defense[0]; - var settings_soldier = level.defense[1]; - var settings_tank = level.defense[2]; - var settings_canon = level.defense[3]; - var settings_helo = level.defense[4]; - - // Init board - this.initializedGame = true; - this.game = util.createMap(settings.gameMap(settings_map)); - this.targetpos = {x: 7, y: 4}; - - // Init units - var width = constant.boardWidth, height = constant.boardHeight; - var goodEngine = enyo.bind(this, "goodEngine"); - this.units = [] - - // Set HQ - var step = constant.boardHeight/(settings_hq+1); - var hqs = []; - for (var i = 0 ; i < settings_hq ; i++) { - var hq = util.createUnit({type: "hq", color: "blue", x: 0, y: Math.floor((i+1)*step), engine: null}); - this.units.push(hq); - hqs.push(hq); - } - - // Create defending units - var defense = []; - for(var i = 0 ; i < settings_helo ; i++) - defense.push({type: "helo", color: "blue", engine: goodEngine}); - for(var i = 0 ; i < settings_canon ; i++) - defense.push({type: "canon", color: "blue", engine: goodEngine}); - for(var i = 0 ; i < settings_tank ; i++) - defense.push({type: "tank", color: "blue", engine: goodEngine}); - for(var i = 0 ; i < settings_soldier ; i++) - defense.push({type: "soldier", color: "blue", engine: goodEngine}); - - // Set defense around hq - if (defense.length > 0) { - var hqindex = 0; - var defenselength = Math.min(defense.length, 1+hqs.length*2); - for (var i = 0 ; i < defenselength ; i++) { - var position = {}; - do { - var arounds = [{dx: 1, dy: 0}, {dx: 0, dy: -1}, {dx: 0, dy: 1}]; - for (j = 0 ; j < arounds.length ; j++) { - position = {x: hqs[hqindex].x+arounds[j].dx, y: hqs[hqindex].y+arounds[j].dy}; - if (util.lookForUnit(position) == null) - break; - else - position = {x: -1, y: -1}; - } - hqindex = (hqindex + 1) % hqs.length; - } while (position.x == -1); - defense[i].x = position.x; - defense[i].y = position.y; - this.units.push(util.createUnit(defense[i])); - } - } - - // Prepare bad units arrival - this.score = 0; - this.wave = 1; - this.enemyCount = level.attack; - this.enemyWaveSize = constant.waveInitSize; - this.enemyNextWaveCount = constant.waveInitSize; - this.enemyWaveCount = 0; - this.enemyArrivalTurn = constant.startArrival; - - // Let's Go ! - this.pausedGame = false; - }, - - // Render - rendered: function() { - // Init game - if (!this.initializedGame) { - // Init context - this.initGame(); - - // Set zoom - this.canvas.hasNode().style.MozTransform = "scale("+this.zoom+")"; - this.canvas.hasNode().style.MozTransformOrigin = "0 0"; - this.canvas.hasNode().style.zoom = this.zoom; - } - }, - - cacheLoaded: function() { - }, - - // Draw - draw: function() { - // Clear all - var ctx = this.canvas.hasNode().getContext('2d'); - ctx.clearRect(0, 0, this.canvas.attributes.width, this.canvas.attributes.height); - - // Draw board - var grass = document.getElementById("grass"); - var trees = document.getElementById("trees"); - var mountain = document.getElementById("mountain"); - var water = document.getElementById("water"); - for (var i = 0 ; i < constant.boardHeight ; i++ ) { - for (var j = 0 ; j < constant.boardWidth ; j++ ) { - ctx.save(); - ctx.translate(j*constant.tileSize, i*constant.tileSize); - ctx.drawImage(grass, 0, 0); - var tileType = this.game[i][j]; - if (tileType == constant.tileTrees) - ctx.drawImage(trees, 0, 0); - else if (tileType == constant.tileMountain) - ctx.drawImage(mountain, 0, 0); - else if (tileType == constant.tileWater) - ctx.drawImage(water, 0, 0); - ctx.restore(); - } - } - - // Draw tanks - if (!this.endOfGame) { - for (var i = 0 ; i < this.units.length ; i++) - this.units[i].draw(ctx); - - // Draw target - var target = document.getElementById("target"); - ctx.save(); - ctx.translate(this.targetpos.x*constant.tileSize, this.targetpos.y*constant.tileSize); - ctx.drawImage(target, 0, 0); - ctx.restore(); + {classes: "keyboard_line", components: [ + {kind: "Image", src: "images/key_4.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_5.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_6.svg", classes: "keyboard", ontap: "virtkeyPressed"} + ]}, + {classes: "keyboard_line", components: [ + {kind: "Image", src: "images/key_7.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_8.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_9.svg", classes: "keyboard", ontap: "virtkeyPressed"} + ]}, + {classes: "keyboard_line", components: [ + {kind: "Image", src: "images/key_0.svg", classes: "keyboard", ontap: "virtkeyPressed"}, + {kind: "Image", src: "images/key_fire.svg", classes: "keyboard", ontap: "virtkeyPressed"} + ]} + ]}, + + // Key handling + {kind: "Signals", onkeypress: "keyPressed"}, + + // Image cache + {kind: "ImageCache", showing: false, onCacheLoaded: "cacheLoaded"} + ], + + // Constructor + create: function() { + this.inherited(arguments); + play = this; + this.imagesToLoad++; + this.endOfGame = false; + this.pausedGame = true; + this.initializedGame = false; + this.waitForClick = false; + + // Init canvas + var wsize = document.body.clientWidth; + if (wsize <= 480) { + this.zoom = 0.4; + } else if (wsize <= 640) { + this.zoom = 0.55; + } else if (wsize <= 768) { + this.zoom = 0.62; + } else if (wsize <= 854) { + this.zoom = 0.65; + } else if (wsize <= 960) { + this.zoom = 0.75; + } else if (wsize <= 1024) { + this.zoom = 0.88; + } else { + this.zoom = 1; + } + + this.$.gamebox.setStyle("max-height: "+(this.zoom*constant.areaHeight)+"px;"); + this.canvas = this.$.gamebox.createComponent({kind: "Canvas", id: "acanvas", name: "canvas", attributes: {width: constant.areaWidth, height: constant.areaHeight}}); + + // Start game loop + this.loopTimer = window.setInterval(enyo.bind(this, "gameLoopTick"), constant.loopInterval); + }, + + // Init game + initGame: function() { + // Game init + var level = this.currentlevel = preferences.levels[this.level]; + var settings_map = level.map; + var settings_hq = level.defense[0]; + var settings_soldier = level.defense[1]; + var settings_tank = level.defense[2]; + var settings_canon = level.defense[3]; + var settings_helo = level.defense[4]; + + // Init board + this.initializedGame = true; + this.game = util.createMap(preferences.gameMap(settings_map)); + this.targetpos = {x: 7, y: 4}; + this.$.wavetext.setContent(l10n.get("Wave")); + this.$.scoretext.setContent(l10n.get("Score")); + + // Init units + var width = constant.boardWidth, height = constant.boardHeight; + var goodEngine = enyo.bind(this, "goodEngine"); + this.units = [] + + // Set HQ + var step = constant.boardHeight/(settings_hq+1); + var hqs = []; + for (var i = 0 ; i < settings_hq ; i++) { + var hq = util.createUnit({type: "hq", color: "blue", x: 0, y: Math.floor((i+1)*step), engine: null}); + this.units.push(hq); + hqs.push(hq); + } + + // Create defending units + var defense = []; + for(var i = 0 ; i < settings_helo ; i++) + defense.push({type: "helo", color: "blue", engine: goodEngine}); + for(var i = 0 ; i < settings_canon ; i++) + defense.push({type: "canon", color: "blue", engine: goodEngine}); + for(var i = 0 ; i < settings_tank ; i++) + defense.push({type: "tank", color: "blue", engine: goodEngine}); + for(var i = 0 ; i < settings_soldier ; i++) + defense.push({type: "soldier", color: "blue", engine: goodEngine}); + + // Set defense around hq + if (defense.length > 0) { + var hqindex = 0; + var defenselength = Math.min(defense.length, 1+hqs.length*2); + for (var i = 0 ; i < defenselength ; i++) { + var position = {}; + do { + var arounds = [{dx: 1, dy: 0}, {dx: 0, dy: -1}, {dx: 0, dy: 1}]; + for (j = 0 ; j < arounds.length ; j++) { + position = {x: hqs[hqindex].x+arounds[j].dx, y: hqs[hqindex].y+arounds[j].dy}; + if (util.lookForUnit(position) == null) + break; + else + position = {x: -1, y: -1}; + } + hqindex = (hqindex + 1) % hqs.length; + } while (position.x == -1); + defense[i].x = position.x; + defense[i].y = position.y; + this.units.push(util.createUnit(defense[i])); } - - // End of game - else { - // Draw end of game screen - var endscreen = this.win ? document.getElementById("endgame_victory") : document.getElementById("endgame_defeat"); + } + + // Prepare bad units arrival + this.score = 0; + this.wave = 1; + this.enemyCount = level.attack; + this.enemyWaveSize = constant.waveInitSize; + this.enemyNextWaveCount = constant.waveInitSize; + this.enemyWaveCount = 0; + this.enemyArrivalTurn = constant.startArrival; + + // Let's Go ! + this.pausedGame = false; + }, + + // Render + rendered: function() { + // Init game + if (!this.initializedGame) { + // Init context + this.initGame(); + + // Set zoom + this.canvas.hasNode().style.MozTransform = "scale("+this.zoom+")"; + this.canvas.hasNode().style.MozTransformOrigin = "0 0"; + this.canvas.hasNode().style.zoom = this.zoom; + } + }, + + cacheLoaded: function() { + }, + + // Draw + draw: function() { + // Clear all + var ctx = this.canvas.hasNode().getContext('2d'); + ctx.clearRect(0, 0, this.canvas.attributes.width, this.canvas.attributes.height); + + // Draw board + var grass = document.getElementById("grass"); + var trees = document.getElementById("trees"); + var mountain = document.getElementById("mountain"); + var water = document.getElementById("water"); + for (var i = 0 ; i < constant.boardHeight ; i++ ) { + for (var j = 0 ; j < constant.boardWidth ; j++ ) { ctx.save(); - ctx.translate((constant.areaWidth-constant.endGameWidth)/2, (constant.areaHeight-constant.endGameHeight)/2); - ctx.drawImage(endscreen, 0, 0); + ctx.translate(j*constant.tileSize, i*constant.tileSize); + ctx.drawImage(grass, 0, 0); + var tileType = this.game[i][j]; + if (tileType == constant.tileTrees) + ctx.drawImage(trees, 0, 0); + else if (tileType == constant.tileMountain) + ctx.drawImage(mountain, 0, 0); + else if (tileType == constant.tileWater) + ctx.drawImage(water, 0, 0); ctx.restore(); - - // Play end of game sound - if (!this.waitForClick) { - sound.play(this.win ? "audio/mission_completed" : "audio/mission_failed", true); - this.waitForClick = true; - } - } - - }, - - // A key was pressed - keyPressed: function(s, e) { - var key = e.charCode; - if (this.endOfGame) - return; - - // Digit key - if (key >= 48 && key <= 57) { - // Add digit to string - var value = this.$.lcd.getValue(); - if (value.length == this.$.lcd.getSize()) - value = value.substr(1); - value += String.fromCharCode(key); - this.$.lcd.setValue(value); } + } - // Dash key - else if (key == 45) { - this.$.lcd.setValue("-"); + // Draw tanks + if (!this.endOfGame) { + for (var i = 0 ; i < this.units.length ; i++) + this.units[i].draw(ctx); + + // Draw target + var target = document.getElementById("target"); + ctx.save(); + ctx.translate(this.targetpos.x*constant.tileSize, this.targetpos.y*constant.tileSize); + ctx.drawImage(target, 0, 0); + ctx.restore(); + } + + // End of game + else { + // Draw end of game screen + var endscreen = this.win ? document.getElementById("endgame_victory") : document.getElementById("endgame_defeat"); + ctx.save(); + ctx.translate((constant.areaWidth-constant.endGameWidth)/2, (constant.areaHeight-constant.endGameHeight)/2); + ctx.drawImage(endscreen, 0, 0); + ctx.restore(); + + // Play end of game sound + if (!this.waitForClick) { + sound.play(this.win ? "audio/mission_completed" : "audio/mission_failed", true); + this.waitForClick = true; } - - // Fire key - else if (key == 32) { - // Look for unit with the value - var units = util.lookForValue(this.$.lcd.getValue().replace(/ /g,'')); - if (units.length != 0) { - for (var i = 0 ; i < units.length ; i++) { - util.processFight(null, units[i], constant.userPower); - this.targetpos.x = units[i].x; - this.targetpos.y = units[i].y; - } + } + + }, + + // A key was pressed + keyPressed: function(s, e) { + var key = e.charCode; + if (this.endOfGame) + return; + + // Digit key + if (key >= 48 && key <= 57) { + // Add digit to string + var value = this.$.lcd.getValue(); + if (value.length == this.$.lcd.getSize()) + value = value.substr(1); + value += String.fromCharCode(key); + this.$.lcd.setValue(value); + } + + // Dash key + else if (key == 45) { + this.$.lcd.setValue("-"); + } + + // Fire key + else if (key == 32) { + // Look for unit with the value + var units = util.lookForValue(this.$.lcd.getValue().replace(/ /g,'')); + if (units.length != 0) { + for (var i = 0 ; i < units.length ; i++) { + util.processFight(null, units[i], constant.userPower); + this.targetpos.x = units[i].x; + this.targetpos.y = units[i].y; } - else - sound.play("audio/missed"); - this.$.lcd.setValue(""); } - }, - - // A virtual key is pressed - virtkeyPressed: function(s) { - var classes = s.getSrc().replace(".svg", ""); - var value = classes.substr("images/key_".length,classes.indexOf("key_")); - if (value == "fire") - this.keyPressed(null, {charCode: 32}); else - this.keyPressed(null, {charCode: 48+parseInt(value)}); - }, - - goHome: function() { - // Click at the end of game - if (this.waitForClick) { - this.gameClick(); - return; - } - + sound.play("audio/missed"); + this.$.lcd.setValue(""); + } + }, + + // A virtual key is pressed + virtkeyPressed: function(s) { + var classes = s.getSrc().replace(".svg", ""); + var value = classes.substr("images/key_".length,classes.indexOf("key_")); + if (value == "fire") + this.keyPressed(null, {charCode: 32}); + else + this.keyPressed(null, {charCode: 48+parseInt(value)}); + }, + + goHome: function() { + // Click at the end of game + if (this.waitForClick) { + this.gameClick(); + return; + } + + // Stop game loop + window.clearInterval(this.loopTimer); + + // Back to app + app.init(); + app.renderInto(document.getElementById("board")); + }, + + // A tap occur on the game + gameClick: function() { + // At end of game, quit + if (this.endOfGame) { // Stop game loop window.clearInterval(this.loopTimer); + // Set mission result + if (this.win) { + preferences.levels[this.level].completed = true; + app.nextMission(); + app.save(); + } + app.init(); + // Back to app - app.init(); app.renderInto(document.getElementById("board")); - }, - - // A tap occur on the game - gameClick: function() { - // At end of game, quit - if (this.endOfGame) { - // Stop game loop - window.clearInterval(this.loopTimer); - - // Set mission result - if (this.win) { - settings.levels[this.level].completed = true; - app.nextMission(); - app.save(); - } - app.init(); - - // Back to app - app.renderInto(document.getElementById("board")); - } - - // Compute direction - var screen_width = document.documentElement.clientWidth; - var screen_height = document.documentElement.clientHeight; - var center_x = Math.floor(screen_width/2.0); - var center_y = Math.floor((screen_height+constant.pubHeight)/2.0); - var diffx = mouse.position.x-center_x, diffy = mouse.position.y-center_y; - var absdiffx = Math.abs(diffx); - var absdiffy = Math.abs(diffy); - if (absdiffx >= 0 && absdiffx < constant.fireZoneWidth && absdiffy >= 0 && absdiffy < constant.fireZoneHeight) { - var targetunit = util.lookForUnit(this.targetpos); - if (targetunit != null) - util.processFight(null, targetunit); - return; - } else if (absdiffx > absdiffy) { - dx = diffx > 0 ? 1 : -1; - dy = 0; + } + + // Compute direction + var screen_width = document.documentElement.clientWidth; + var screen_height = document.documentElement.clientHeight; + var center_x = Math.floor(screen_width/2.0); + var center_y = Math.floor((screen_height+constant.pubHeight)/2.0); + var diffx = mouse.position.x-center_x, diffy = mouse.position.y-center_y; + var absdiffx = Math.abs(diffx); + var absdiffy = Math.abs(diffy); + if (absdiffx >= 0 && absdiffx < constant.fireZoneWidth && absdiffy >= 0 && absdiffy < constant.fireZoneHeight) { + var targetunit = util.lookForUnit(this.targetpos); + if (targetunit != null) + util.processFight(null, targetunit); + return; + } else if (absdiffx > absdiffy) { + dx = diffx > 0 ? 1 : -1; + dy = 0; + } else { + dx = 0; + dy = diffy > 0 ? 1 : -1; + } + + // Move target + var newX = this.targetpos.x + dx; + var newY = this.targetpos.y + dy; + if (newX < 0 || newX == constant.boardWidth || newY < 0 || newY == constant.boardHeight) + return; + this.targetpos.x = newX; + this.targetpos.y = newY; + }, + + // Tick for game loop + gameLoopTick: function() { + if (this.pausedGame) + return; + + // Sanitize: clean dead units and compute victory/defeat conditions + var alives = []; + var hqs = []; + var livingHq = 0; + var livingEnemy = 0; + for (var i = 0 ; i < this.units.length ; i++) { + var unit = this.units[i]; + var isRed = unit.getCurrentImage().indexOf("red") != -1; + if (unit.power > 0) { + alives.push(unit); } else { - dx = 0; - dy = diffy > 0 ? 1 : -1; - } - - // Move target - var newX = this.targetpos.x + dx; - var newY = this.targetpos.y + dy; - if (newX < 0 || newX == constant.boardWidth || newY < 0 || newY == constant.boardHeight) - return; - this.targetpos.x = newX; - this.targetpos.y = newY; - }, - - // Tick for game loop - gameLoopTick: function() { - if (this.pausedGame) - return; - - // Sanitize: clean dead units and compute victory/defeat conditions - var alives = []; - var hqs = []; - var livingHq = 0; - var livingEnemy = 0; - for (var i = 0 ; i < this.units.length ; i++) { - var unit = this.units[i]; - var isRed = unit.getCurrentImage().indexOf("red") != -1; - if (unit.power > 0) { - alives.push(unit); - } else { - if (isRed) { - this.enemyNextWaveCount--; - this.score += util.unitPowers[util.getUnitType(unit)]; - } - continue; + if (isRed) { + this.enemyNextWaveCount--; + this.score += util.unitPowers[util.getUnitType(unit)]; } - if (util.getUnitType(unit) == 0) { - hqs[livingHq++] = unit; - } - if (isRed) - livingEnemy++; + continue; } - this.units = alives; - this.endOfGame = (livingHq == 0 || (livingEnemy == 0 && this.enemyCount == 0)); - this.win = (livingHq > 0); - - // Game play - if (!this.endOfGame) { - // Next wave - if (this.enemyNextWaveCount == 0) { - this.wave++; - this.enemyWaveSize += 2; - this.enemyWaveCount = 0; - this.enemyNextWaveCount = this.enemyWaveSize; - this.enemyArrivalTurn = constant.startArrival; - } - - // Enemy arrival - else if (this.enemyWaveCount != this.enemyWaveSize) { - if (this.enemyArrivalTurn == 0 && this.enemyCount > 0) { - var badEngine = enyo.bind(this, "badEngine"); - var unit = util.createUnit({ - type: util.randomUnit(this.currentlevel.stats), - color: "red", - heading: 0, - engine: badEngine, - x: constant.boardWidth-1, - y: util.random(constant.boardHeight) - }); - unit.value = this.currentlevel.generator(); - this.units.push(unit); - this.enemyCount = this.enemyCount-1; - this.enemyWaveCount++; - this.enemyArrivalTurn = constant.startArrival; - } else { - this.enemyArrivalTurn = this.enemyArrivalTurn - 1; - } - } - - // Launch engine for each unit - for (var i = 0 ; i < this.units.length ; i++) { - var engine = this.units[i].engine; - if (engine != null) - engine(this.units[i], hqs); - } - } - - // Draw - this.draw(); - - // HACK: On Android, force redraw of canvas - if (enyo.platform.android && document.location.protocol.substr(0,4) != "http") { - document.getElementById('acanvas').style.display='none'; - document.getElementById('acanvas').offsetHeight; - document.getElementById('acanvas').style.display='block'; - } - - // Draw score - this.$.wave.setContent(String("0000"+this.wave).slice(-4)) - this.$.score.setContent(String("0000"+this.score).slice(-4)) - }, - - // Engine for good tank moves - goodEngine: function(that, hqs) { - // Look for enemy unit - var opponent = util.lookForOpponent(that); - if (opponent != null) { - // Change heading toward opponent - that.heading = opponent.heading; - - // Fight - util.processFight(that, opponent.unit); - return; + if (util.getUnitType(unit) == 0) { + hqs[livingHq++] = unit; } - }, - - // Engine for bad tank moves - badEngine: function(that, hqs) { - // Look for enemy unit around - var opponent = util.lookForOpponent(that); - if (opponent != null) { - // Change heading toward opponent - that.heading = opponent.heading; - - // Fight - util.processFight(that, opponent.unit); - return; + if (isRed) + livingEnemy++; + } + this.units = alives; + this.endOfGame = (livingHq == 0 || (livingEnemy == 0 && this.enemyCount == 0)); + this.win = (livingHq > 0); + + // Game play + if (!this.endOfGame) { + // Next wave + if (this.enemyNextWaveCount == 0) { + this.wave++; + this.enemyWaveSize += 2; + this.enemyWaveCount = 0; + this.enemyNextWaveCount = this.enemyWaveSize; + this.enemyArrivalTurn = constant.startArrival; } - - // Change heading to go toward the nearest HQ - var nearestHQ = util.nearestUnit(that, hqs); - if (nearestHQ != null) { - var dx = that.x - nearestHQ.x; - var dy = that.y - nearestHQ.y; - if (Math.abs(dx) > Math.abs(dy)) - that.heading = dx > 0 ? 0 : 2; - else - that.heading = dy > 0 ? 1 : 3; + + // Enemy arrival + else if (this.enemyWaveCount != this.enemyWaveSize) { + if (this.enemyArrivalTurn == 0 && this.enemyCount > 0) { + var badEngine = enyo.bind(this, "badEngine"); + var unit = util.createUnit({ + type: util.randomUnit(this.currentlevel.stats), + color: "red", + heading: 0, + engine: badEngine, + x: constant.boardWidth-1, + y: util.random(constant.boardHeight) + }); + unit.value = this.currentlevel.generator(); + this.units.push(unit); + this.enemyCount = this.enemyCount-1; + this.enemyWaveCount++; + this.enemyArrivalTurn = constant.startArrival; + } else { + this.enemyArrivalTurn = this.enemyArrivalTurn - 1; + } } - // Is it a valid position ? - var next = util.nextPositionOnHeading(that); - while (!util.isValidPosition(next, that)) { - // No, try a random heading - that.heading = util.random(4); - next = util.nextPositionOnHeading(that); + // Launch engine for each unit + for (var i = 0 ; i < this.units.length ; i++) { + var engine = this.units[i].engine; + if (engine != null) + engine(this.units[i], hqs); } + } + + // Draw + this.draw(); + + // HACK: On Android, force redraw of canvas + if (enyo.platform.android && document.location.protocol.substr(0,4) != "http") { + document.getElementById('acanvas').style.display='none'; + document.getElementById('acanvas').offsetHeight; + document.getElementById('acanvas').style.display='block'; + } + + // Draw score + this.$.wave.setContent(String("0000"+this.wave).slice(-4)) + this.$.score.setContent(String("0000"+this.score).slice(-4)) + }, + + // Engine for good tank moves + goodEngine: function(that, hqs) { + // Look for enemy unit + var opponent = util.lookForOpponent(that); + if (opponent != null) { + // Change heading toward opponent + that.heading = opponent.heading; + + // Fight + util.processFight(that, opponent.unit); + return; + } + }, + + // Engine for bad tank moves + badEngine: function(that, hqs) { + // Look for enemy unit around + var opponent = util.lookForOpponent(that); + if (opponent != null) { + // Change heading toward opponent + that.heading = opponent.heading; + + // Fight + util.processFight(that, opponent.unit); + return; + } + + // Change heading to go toward the nearest HQ + var nearestHQ = util.nearestUnit(that, hqs); + if (nearestHQ != null) { + var dx = that.x - nearestHQ.x; + var dy = that.y - nearestHQ.y; + if (Math.abs(dx) > Math.abs(dy)) + that.heading = dx > 0 ? 0 : 2; + else + that.heading = dy > 0 ? 1 : 3; + } + + // Is it a valid position ? + var next = util.nextPositionOnHeading(that); + while (!util.isValidPosition(next, that)) { + // No, try a random heading + that.heading = util.random(4); next = util.nextPositionOnHeading(that); - that.x = next.x; - that.y = next.y; } + next = util.nextPositionOnHeading(that); + that.x = next.x; + that.y = next.y; + } }); -}); \ No newline at end of file