not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

  1. define(["sugar-web/activity/activity"], function (activity) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. // Initialize the activity.
  5. activity.setup();
  6. var myHighestScore = 0;
  7. //Loads highest score from Journal
  8. loadHighScore();
  9. var $game = $('#canvas').blockrain({
  10. speed: 20,
  11. theme: 'candy',
  12. autoplay: false,
  13. autoplayRestart: true,
  14. autoBlockWidth: true,
  15. autoBlockSize: 24,
  16. touchControls: true,
  17. highestScore: myHighestScore,
  18. onStart: function(){},
  19. onRestart: function(){
  20. loadHighScore(); // loads highscore when game is restarted.
  21. },
  22. onGameOver: function(score){
  23. saveHighestScore();
  24. },
  25. onLine: function(lines, scoreIncrement, score){
  26. if (score > myHighestScore){
  27. myHighestScore = score;
  28. }
  29. },
  30. });
  31. // Save high score in Journal on Stop
  32. document.getElementById("stop-button").addEventListener('click', function (event) {
  33. saveHighestScore();
  34. });
  35. //Function to save high score
  36. function saveHighestScore() {
  37. console.log("writing...");
  38. var jsonData = JSON.stringify(myHighestScore);
  39. //console.log(jsonData);
  40. activity.getDatastoreObject().setDataAsText(jsonData);
  41. activity.getDatastoreObject().save(function (error) {
  42. if (error === null) {
  43. console.log("write done.");
  44. } else {
  45. console.log("write failed.");
  46. }
  47. });
  48. }
  49. //Function to load high score
  50. function loadHighScore() {
  51. activity.getDatastoreObject().loadAsText(function(error, metadata, data) {
  52. if (error == null && data != null) {
  53. myHighestScore = JSON.parse(data);
  54. console.log(myHighestScore);
  55. }
  56. });
  57. }
  58. function switchTheme(next) {
  59. var themes = Object.keys(BlockrainThemes);
  60. var currentTheme = $game.blockrain('theme');
  61. var currentIx = themes.indexOf(currentTheme);
  62. if( next ) { currentIx++; }
  63. else { currentIx--; }
  64. if( currentIx >= themes.length ){ currentIx = 0; }
  65. if( currentIx < 0 ){ currentIx = themes.length-1; }
  66. $game.blockrain('theme', themes[currentIx]);
  67. }
  68. document.getElementById("btn-next").onclick = function() {
  69. switchTheme(true);
  70. };
  71. });
  72. });