Guest User

Untitled

a guest
Oct 10th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var version = '0.0.1';
  2.  
  3. var scripts = [
  4.     'scripts/heart.js',
  5.     'scripts/player.js',
  6.     'scripts/bonus.js',
  7.     'scripts/fire.js',
  8.     'scripts/monster.js',
  9.     'scripts/ground.js',
  10.     'scripts/controlbutton.js',
  11.     'scripts/info.js'
  12. ];
  13.  
  14. var assets = {
  15.     'image': 'images/background.png',
  16.     'image': 'images/sprites.png',
  17.     'audio': 'sounds/explosion.ogg',
  18.     'audio': 'sounds/fire.ogg',
  19.     'audio': 'sounds/bonus.ogg',
  20.     'audio': 'sounds/music.ogg'
  21. };
  22.  
  23. var controlButtonsIds = {
  24.     'sound': 0,
  25.     'music': 1
  26. };
  27.  
  28. var chars = {
  29.     'a': {'x': 0, 'y': 200, 'width': 15, 'height': 23},
  30.     'b': {'x': 16, 'y': 200, 'width': 14, 'height': 23},
  31.     'c': {'x': 31, 'y': 200, 'width': 14, 'height': 23},
  32.     'd': {'x': 46, 'y': 200, 'width': 14, 'height': 23},
  33.     'e': {'x': 61, 'y': 200, 'width': 13, 'height': 23},
  34.     'f': {'x': 74, 'y': 200, 'width': 13, 'height': 23},
  35.     'g': {'x': 87, 'y': 200, 'width': 15, 'height': 23},
  36.     'h': {'x': 102, 'y': 200, 'width': 15, 'height': 23},
  37.     'i': {'x': 118, 'y': 200, 'width': 10, 'height': 23},
  38.     'j': {'x': 129, 'y': 200, 'width': 10, 'height': 23},
  39.     'k': {'x': 139, 'y': 200, 'width': 16, 'height': 23},
  40.     'l': {'x': 155, 'y': 200, 'width': 12, 'height': 23},
  41.     'm': {'x': 167, 'y': 200, 'width': 18, 'height': 23},
  42.     'n': {'x': 186, 'y': 200, 'width': 15, 'height': 23},
  43.     'o': {'x': 202, 'y': 200, 'width': 14, 'height': 23},
  44.     'p': {'x': 217, 'y': 200, 'width': 14, 'height': 23},
  45.     'q': {'x': 232, 'y': 200, 'width': 14, 'height': 23},
  46.     'r': {'x': 247, 'y': 200, 'width': 15, 'height': 23},
  47.     's': {'x': 262, 'y': 200, 'width': 15, 'height': 23},
  48.     't': {'x': 277, 'y': 200, 'width': 14, 'height': 23},
  49.     ' ': {'x': 291, 'y': 200, 'width': 13, 'height': 15},
  50.     'u': {'x': 1, 'y': 224, 'width': 14, 'height': 23},
  51.     'v': {'x': 15, 'y': 224, 'width': 16, 'height': 23},
  52.     'w': {'x': 31, 'y': 224, 'width': 20, 'height': 23},
  53.     'x': {'x': 51, 'y': 224, 'width': 15, 'height': 23},
  54.     'y': {'x': 66, 'y': 224, 'width': 13, 'height': 23},
  55.     'z': {'x': 79, 'y': 224, 'width': 13, 'height': 23},
  56.     '0': {'x': 92, 'y': 224, 'width': 15, 'height': 23},
  57.     '1': {'x': 107, 'y': 224, 'width': 12, 'height': 23},
  58.     '2': {'x': 120, 'y': 224, 'width': 14, 'height': 23},
  59.     '3': {'x': 135, 'y': 224, 'width': 14, 'height': 23},
  60.     '4': {'x': 149, 'y': 224, 'width': 15, 'height': 23},
  61.     '5': {'x': 164, 'y': 224, 'width': 15, 'height': 23},
  62.     '6': {'x': 180, 'y': 224, 'width': 14, 'height': 23},
  63.     '7': {'x': 194, 'y': 224, 'width': 13, 'height': 23},
  64.     '8': {'x': 207, 'y': 224, 'width': 14, 'height': 23},
  65.     '9': {'x': 221, 'y': 224, 'width': 15, 'height': 23},
  66.     '.': {'x': 236, 'y': 224, 'width': 8, 'height': 23},
  67.     ':': {'x': 245, 'y': 224, 'width': 8, 'height': 23},
  68.     '-': {'x': 254, 'y': 224, 'width': 11, 'height': 23},
  69.     '/': {'x': 264, 'y': 224, 'width': 13, 'height': 23},
  70.     '?': {'x': 277, 'y': 224, 'width': 15, 'height': 22}
  71. };
  72.  
  73. var isPaused = true, isStarted = false, requestFrame, backgroundSound;
  74. var player, ground, monsters = new Array(), infos = new Array(), controlButtons = new Array();
  75. var infoText, removedInfoText = false;
  76. var userPanel, gameOverText, removedGameOverText = true;
  77.  
  78. var sprites = new Image();
  79.  
  80. var bgCanvas = $('#bg_canvas');
  81. var bgCtx = bgCanvas[0].getContext('2d');
  82.  
  83. var mainCanvas = $('#canvas');
  84. var mainCtx = mainCanvas[0].getContext('2d');
  85.  
  86. function drawChars(text, offsetX, offsetY) {
  87.     var infoIndex = infos.length;
  88.     var infosData = new Array();
  89.    
  90.     text = text.toLowerCase();
  91.    
  92.     for(var i = 0; i < text.length; i++) {
  93.         var character = ' ';
  94.         if(text[i] in chars) {
  95.             character = text[i];
  96.         }
  97.        
  98.         var data = chars[character];
  99.        
  100.         if(i > 0) {
  101.             offsetX += chars[text[i - 1]]['width'];
  102.         }
  103.        
  104.         infos[infoIndex] = new Info(mainCtx, sprites, {
  105.             'srcX': data['x'],
  106.             'srcY': data['y'],
  107.             'width': data['width'],
  108.             'height': data['height'],
  109.             'offsetX': offsetX,
  110.             'offsetY': offsetY
  111.         });
  112.        
  113.         infosData[infosData.length] = infos[infoIndex];
  114.         infoIndex = infos.length;
  115.     }
  116.    
  117.     return infosData;
  118. }
  119.  
  120. function removeChars(data) {
  121.     for(var i = 0; i < data.length; i++) {
  122.         data[i].show = false;
  123.     }
  124. }
  125.  
  126. $(document).ready(function() {
  127.     preload();
  128. });
  129.  
  130. Object.size = function(obj) {
  131.     var size = 0, key;
  132.     for (key in obj) {
  133.         if (obj.hasOwnProperty(key)) size++;
  134.     }
  135.    
  136.     return size;
  137. };
  138.  
  139. function preload() {
  140.     requestFrame = (function() {
  141.         return  window.requestAnimationFrame ||
  142.                 window.webkitRequestAnimationFrame ||
  143.                 window.mozRequestAnimationFrame ||
  144.                 window.oRequestAnimationFrame ||
  145.                 window.msRequestAnimationFrame ||
  146.                 function(callback) {
  147.                     window.setTimeout(callback, 1000 / 60);
  148.                 };
  149.     })();
  150.    
  151.     loadAssets();
  152. }
  153.  
  154. var progress = 0, isLoaded = false;
  155. function handleProgress() {
  156.     var percent = Math.floor(progress * 100);
  157.    
  158.     setPreloadPercent(percent);
  159.     if(percent >= 100) {
  160.         handleLoadComplete();
  161.     }
  162. }
  163.  
  164. function handleLoadComplete() {
  165.     initialize();
  166. }
  167.  
  168. function setPreloadPercent(per) {
  169.     if(per >= 100) {
  170.         isLoaded = true;
  171.     }
  172.    
  173.     if(!isLoaded) {
  174.         mainCtx.fillRect(20, 20, per, 10);
  175.         percent = per;
  176.     } else {
  177.         percent = 0;
  178.     }
  179. }
  180.  
  181. function loadAssets() {
  182.     var size = Object.size(assets) + Object.size(scripts);
  183.     var current = 0;
  184.     var index = 0;
  185.    
  186.     $.each(assets, function(key, value) {
  187.         if(key === 'image') {
  188.             var img = new Image();
  189.             img.src = value;
  190.         } else if(key === 'audio') {
  191.             new Audio(value);
  192.         }
  193.        
  194.         index++;
  195.         current = index;
  196.         progress = current / size;
  197.         handleProgress();
  198.        
  199.         if(index >= Object.size(assets)) {
  200.             $.each(scripts, function(i, file) {
  201.                 $.getScript(scripts[i], function() {
  202.                     index++;
  203.                     current = index;
  204.                     progress = current / size;
  205.                     handleProgress();
  206.                    
  207.                     //initialize();
  208.                 });
  209.             });
  210.         }
  211.     });
  212. }
  213.  
  214. function initialize() {
  215.     var background = new Image();
  216.     background.src = 'images/background.png';
  217.     background.onload = function() {
  218.         bgCtx.drawImage(background, 0, 0);
  219.     };
  220.    
  221.     document.addEventListener('keydown', keyDown, false);
  222.     document.addEventListener('keyup', keyUp, false);
  223.     document.addEventListener('mousedown', mouseDown, false);
  224.    
  225.     start(true);
  226. }
  227.  
  228. function loop() {
  229.     mainCtx.clearRect(0, 0, mainCtx.canvas.width, mainCtx.canvas.height);
  230.     player.monsters = monsters;
  231.     player.draw();
  232.     ground.draw();
  233.  
  234.     for(var c = 0; c < infos.length; c++) {
  235.         infos[c].draw();
  236.     }
  237.  
  238.     for(var d = 0; d < controlButtons.length; d++) {
  239.         controlButtons[d].draw();
  240.     }
  241.        
  242.     if(isStarted && !removedInfoText) {
  243.         removeChars(infoText);
  244.         removedInfoText = true;
  245.     }
  246.    
  247.     if(isStarted) {
  248.         if(backgroundSound.paused) {
  249.             backgroundSound.addEventListener('ended', function() {
  250.                 this.currentTime = 0;
  251.                 if(controlButtons[controlButtonsIds['music']].isChecked) {
  252.                     this.play();
  253.                 }
  254.             }, false);
  255.            
  256.             if(controlButtons[controlButtonsIds['music']].isChecked) {
  257.                 backgroundSound.play();
  258.             }
  259.         }
  260.  
  261.         for(var x = 0; x < player.fires.length; x++) {
  262.             player.fires[x].draw();
  263.         }
  264.        
  265.         var monstersCurrent = monsters.length;
  266.         for(var i = 0; i < monsters.length; i++) {
  267.             monsters[i].draw();
  268.             if(monsters[i].isDead) {
  269.                 monstersCurrent--;
  270.             }
  271.            
  272.             for(var x = 0; x < monsters[i].fires.length; x++) {
  273.                 monsters[i].fires[x].draw();
  274.             }
  275.            
  276.             for(var x = 0; x < monsters[i].bonus.length; x++) {
  277.                 monsters[i].bonus[x].draw();
  278.             }
  279.            
  280.             for(var d = 0; d < monsters[i].hearts.length; d++) {
  281.                 monsters[i].hearts[d].draw();
  282.             }
  283.         }
  284.        
  285.         if(monstersCurrent <= 0) {
  286.             player.setLevel(player.getLevel() + 1);
  287.             monsters = new Array();
  288.             var monstersCount = 2 + player.getLevel();
  289.            
  290.             if(monstersCount > 8) {
  291.                 monstersCount = 8;
  292.             }
  293.            
  294.             spawnMonster(monstersCount);
  295.         }
  296.     } else {
  297.         backgroundSound.currentTime = 0;
  298.         backgroundSound.pause();
  299.     }
  300.    
  301.     removeChars(userPanel);
  302.     userPanel = drawChars('Level: ' + player.getLevel() +
  303.                      ' / Staerke: ' + player.getFireDamage() +
  304.                      ' / Leben: ' + player.getHealth() + '/' + player.getMaxHealth(), 20, 20);
  305.    
  306.     if(!isPaused) {
  307.         requestFrame(loop);
  308.     }
  309. }
  310.  
  311. function stop() {
  312.     isPaused = true;
  313. }
  314.  
  315. function playerDie() {
  316.     return function() {
  317.         var sound = new Audio('sounds/explosion.ogg');
  318.         sound.volume = 0.8;
  319.         sound.play();
  320.            
  321.         gameOverText = drawChars('Game Over. Leertaste zum Neustarten', mainCtx.canvas.width / 4, mainCtx.canvas.height - 70);
  322.     };
  323. }
  324. function restart() {
  325.     player = new Player(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, 'Player');
  326.     player.die = playerDie();
  327.    
  328.     removeChars(gameOverText);
  329.     isStarted = true;
  330.     isPaused = false;
  331.     spawnMonster(3);
  332. }
  333.  
  334. function start(firstStart) {
  335.     backgroundSound = new Audio('sounds/music.ogg');
  336.     backgroundSound.volume = 0.2;
  337.    
  338.     sprites.src = 'images/sprites.png';
  339.     sprites.onload = function() {
  340.         player = new Player(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, 'Player');
  341.         player.die = playerDie();
  342.        
  343.         ground = new Ground(mainCtx, sprites);
  344.         userPanel = drawChars('', 20, 20);
  345.        
  346.         infoText = drawChars('Linke oder rechte Pfeiltaste druecken', mainCtx.canvas.width / 4, mainCtx.canvas.height - 70);
  347.        
  348.         controlButtons[controlButtonsIds['sound']] = new ControlButton(mainCtx, sprites, {
  349.             'srcX': 0,
  350.             'srcY': 249,
  351.             'isChecked': true,
  352.             'show': true,
  353.             'width': 29,
  354.             'height': 29,
  355.             'offsetX': 700,
  356.             'offsetY': 13
  357.         }, {
  358.             'srcX': 29,
  359.             'srcY': 249,
  360.             'isChecked': true,
  361.             'show': true,
  362.             'width': 29,
  363.             'height': 29,
  364.             'offsetX': 700,
  365.             'offsetY': 13
  366.         });
  367.        
  368.         controlButtons[controlButtonsIds['music']] = new ControlButton(mainCtx, sprites, {
  369.             'srcX': 58,
  370.             'srcY': 249,
  371.             'isChecked': true,
  372.             'show': true,
  373.             'width': 29,
  374.             'height': 29,
  375.             'offsetX': 740,
  376.             'offsetY': 13
  377.         }, {
  378.             'srcX': 87,
  379.             'srcY': 249,
  380.             'isChecked': true,
  381.             'show': true,
  382.             'width': 29,
  383.             'height': 29,
  384.             'offsetX': 740,
  385.             'offsetY': 13
  386.         });
  387.        
  388.         isPaused = false;
  389.         loop();
  390.     };
  391. }
  392.  
  393. function spawnMonster(amount) {
  394.     for(var i = 0; i < amount; i++) {
  395.         monsters[monsters.length] = new Monster(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, player, 'Monster');
  396.     }
  397. }
  398.  
  399. function firstStart() {
  400.     if(!isStarted) {
  401.         spawnMonster(3);
  402.         isStarted = true;
  403.     }
  404. }
  405.  
  406. function keyDown(event) {
  407.     var keyId = event.keyCode || event.which;
  408.     if(keyId == 40) { // down
  409.         firstStart();
  410.         player.keys['down'] = true;
  411.         event.preventDefault();
  412.     }
  413.    
  414.     if(keyId == 38) { // up
  415.         firstStart();
  416.         player.keys['up'] = true;
  417.         event.preventDefault();
  418.     }
  419.    
  420.     if(keyId == 37) { // left
  421.         firstStart();
  422.         player.keys['left'] = true;
  423.         event.preventDefault();
  424.     }
  425.    
  426.     if(keyId == 39) { // right
  427.         firstStart();
  428.         player.keys['right'] = true;
  429.         event.preventDefault();
  430.     }
  431.    
  432.     if(keyId == 32) { // space
  433.         restart();
  434.         event.preventDefault();
  435.     }
  436. }
  437.  
  438. function keyUp(event) {
  439.     var keyId = event.keyCode || event.which;
  440.     if(keyId == 40) { // down
  441.         player.keys['down'] = false;
  442.         event.preventDefault();
  443.     }
  444.    
  445.     if(keyId == 38) { // up
  446.         player.keys['up'] = false;
  447.         event.preventDefault();
  448.     }
  449.    
  450.     if(keyId == 37) { // left
  451.         player.keys['left'] = false;
  452.         event.preventDefault();
  453.     }
  454.    
  455.     if(keyId == 39) { // right
  456.         player.keys['right'] = false;
  457.         event.preventDefault();
  458.     }
  459. }
  460.  
  461. function mouseDown(event) {
  462.     var posX = event.clientX - mainCtx.canvas.getBoundingClientRect().left;
  463.     var posY = event.clientY - mainCtx.canvas.getBoundingClientRect().top;
  464.    
  465.     if(posX <= mainCtx.canvas.width && posY <= mainCtx.canvas.height) {
  466.         if(event.which == 1 || event.button == 0) { // left click
  467.             for(var d = 0; d < controlButtons.length; d++) {
  468.                 var offsetX = controlButtons[d].offsetX;
  469.                 var offsetY = controlButtons[d].offsetY;
  470.                 var width = controlButtons[d].width;
  471.                 var height = controlButtons[d].height;
  472.                
  473.                 if(offsetX <= posX && offsetX + width >= posX && offsetY <= posY && offsetY + height >= posY) {
  474.                     if(controlButtons[d].isChecked) {
  475.                         controlButtons[d].isChecked = false;
  476.                     } else {
  477.                         controlButtons[d].isChecked = true;
  478.                     }
  479.                    
  480.                     if(d == controlButtonsIds['music']) {
  481.                         if(controlButtons[d].isChecked) {
  482.                             backgroundSound.currentTime = 0;
  483.                             backgroundSound.play();
  484.                         } else {
  485.                             backgroundSound.currentTime = 0;
  486.                             backgroundSound.pause();
  487.                         }
  488.                     }
  489.                 }
  490.             }
  491.            
  492.             event.preventDefault();
  493.         }
  494.     }
  495. }
Add Comment
Please, Sign In to add comment