Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var version = '0.0.1';
- var scripts = [
- 'scripts/heart.js',
- 'scripts/player.js',
- 'scripts/bonus.js',
- 'scripts/fire.js',
- 'scripts/monster.js',
- 'scripts/ground.js',
- 'scripts/controlbutton.js',
- 'scripts/info.js'
- ];
- var assets = {
- 'image': 'images/background.png',
- 'image': 'images/sprites.png',
- 'audio': 'sounds/explosion.ogg',
- 'audio': 'sounds/fire.ogg',
- 'audio': 'sounds/bonus.ogg',
- 'audio': 'sounds/music.ogg'
- };
- var controlButtonsIds = {
- 'sound': 0,
- 'music': 1
- };
- var chars = {
- 'a': {'x': 0, 'y': 200, 'width': 15, 'height': 23},
- 'b': {'x': 16, 'y': 200, 'width': 14, 'height': 23},
- 'c': {'x': 31, 'y': 200, 'width': 14, 'height': 23},
- 'd': {'x': 46, 'y': 200, 'width': 14, 'height': 23},
- 'e': {'x': 61, 'y': 200, 'width': 13, 'height': 23},
- 'f': {'x': 74, 'y': 200, 'width': 13, 'height': 23},
- 'g': {'x': 87, 'y': 200, 'width': 15, 'height': 23},
- 'h': {'x': 102, 'y': 200, 'width': 15, 'height': 23},
- 'i': {'x': 118, 'y': 200, 'width': 10, 'height': 23},
- 'j': {'x': 129, 'y': 200, 'width': 10, 'height': 23},
- 'k': {'x': 139, 'y': 200, 'width': 16, 'height': 23},
- 'l': {'x': 155, 'y': 200, 'width': 12, 'height': 23},
- 'm': {'x': 167, 'y': 200, 'width': 18, 'height': 23},
- 'n': {'x': 186, 'y': 200, 'width': 15, 'height': 23},
- 'o': {'x': 202, 'y': 200, 'width': 14, 'height': 23},
- 'p': {'x': 217, 'y': 200, 'width': 14, 'height': 23},
- 'q': {'x': 232, 'y': 200, 'width': 14, 'height': 23},
- 'r': {'x': 247, 'y': 200, 'width': 15, 'height': 23},
- 's': {'x': 262, 'y': 200, 'width': 15, 'height': 23},
- 't': {'x': 277, 'y': 200, 'width': 14, 'height': 23},
- ' ': {'x': 291, 'y': 200, 'width': 13, 'height': 15},
- 'u': {'x': 1, 'y': 224, 'width': 14, 'height': 23},
- 'v': {'x': 15, 'y': 224, 'width': 16, 'height': 23},
- 'w': {'x': 31, 'y': 224, 'width': 20, 'height': 23},
- 'x': {'x': 51, 'y': 224, 'width': 15, 'height': 23},
- 'y': {'x': 66, 'y': 224, 'width': 13, 'height': 23},
- 'z': {'x': 79, 'y': 224, 'width': 13, 'height': 23},
- '0': {'x': 92, 'y': 224, 'width': 15, 'height': 23},
- '1': {'x': 107, 'y': 224, 'width': 12, 'height': 23},
- '2': {'x': 120, 'y': 224, 'width': 14, 'height': 23},
- '3': {'x': 135, 'y': 224, 'width': 14, 'height': 23},
- '4': {'x': 149, 'y': 224, 'width': 15, 'height': 23},
- '5': {'x': 164, 'y': 224, 'width': 15, 'height': 23},
- '6': {'x': 180, 'y': 224, 'width': 14, 'height': 23},
- '7': {'x': 194, 'y': 224, 'width': 13, 'height': 23},
- '8': {'x': 207, 'y': 224, 'width': 14, 'height': 23},
- '9': {'x': 221, 'y': 224, 'width': 15, 'height': 23},
- '.': {'x': 236, 'y': 224, 'width': 8, 'height': 23},
- ':': {'x': 245, 'y': 224, 'width': 8, 'height': 23},
- '-': {'x': 254, 'y': 224, 'width': 11, 'height': 23},
- '/': {'x': 264, 'y': 224, 'width': 13, 'height': 23},
- '?': {'x': 277, 'y': 224, 'width': 15, 'height': 22}
- };
- var isPaused = true, isStarted = false, requestFrame, backgroundSound;
- var player, ground, monsters = new Array(), infos = new Array(), controlButtons = new Array();
- var infoText, removedInfoText = false;
- var userPanel, gameOverText, removedGameOverText = true;
- var sprites = new Image();
- var bgCanvas = $('#bg_canvas');
- var bgCtx = bgCanvas[0].getContext('2d');
- var mainCanvas = $('#canvas');
- var mainCtx = mainCanvas[0].getContext('2d');
- function drawChars(text, offsetX, offsetY) {
- var infoIndex = infos.length;
- var infosData = new Array();
- text = text.toLowerCase();
- for(var i = 0; i < text.length; i++) {
- var character = ' ';
- if(text[i] in chars) {
- character = text[i];
- }
- var data = chars[character];
- if(i > 0) {
- offsetX += chars[text[i - 1]]['width'];
- }
- infos[infoIndex] = new Info(mainCtx, sprites, {
- 'srcX': data['x'],
- 'srcY': data['y'],
- 'width': data['width'],
- 'height': data['height'],
- 'offsetX': offsetX,
- 'offsetY': offsetY
- });
- infosData[infosData.length] = infos[infoIndex];
- infoIndex = infos.length;
- }
- return infosData;
- }
- function removeChars(data) {
- for(var i = 0; i < data.length; i++) {
- data[i].show = false;
- }
- }
- $(document).ready(function() {
- preload();
- });
- Object.size = function(obj) {
- var size = 0, key;
- for (key in obj) {
- if (obj.hasOwnProperty(key)) size++;
- }
- return size;
- };
- function preload() {
- requestFrame = (function() {
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(callback) {
- window.setTimeout(callback, 1000 / 60);
- };
- })();
- loadAssets();
- }
- var progress = 0, isLoaded = false;
- function handleProgress() {
- var percent = Math.floor(progress * 100);
- setPreloadPercent(percent);
- if(percent >= 100) {
- handleLoadComplete();
- }
- }
- function handleLoadComplete() {
- initialize();
- }
- function setPreloadPercent(per) {
- if(per >= 100) {
- isLoaded = true;
- }
- if(!isLoaded) {
- mainCtx.fillRect(20, 20, per, 10);
- percent = per;
- } else {
- percent = 0;
- }
- }
- function loadAssets() {
- var size = Object.size(assets) + Object.size(scripts);
- var current = 0;
- var index = 0;
- $.each(assets, function(key, value) {
- if(key === 'image') {
- var img = new Image();
- img.src = value;
- } else if(key === 'audio') {
- new Audio(value);
- }
- index++;
- current = index;
- progress = current / size;
- handleProgress();
- if(index >= Object.size(assets)) {
- $.each(scripts, function(i, file) {
- $.getScript(scripts[i], function() {
- index++;
- current = index;
- progress = current / size;
- handleProgress();
- //initialize();
- });
- });
- }
- });
- }
- function initialize() {
- var background = new Image();
- background.src = 'images/background.png';
- background.onload = function() {
- bgCtx.drawImage(background, 0, 0);
- };
- document.addEventListener('keydown', keyDown, false);
- document.addEventListener('keyup', keyUp, false);
- document.addEventListener('mousedown', mouseDown, false);
- start(true);
- }
- function loop() {
- mainCtx.clearRect(0, 0, mainCtx.canvas.width, mainCtx.canvas.height);
- player.monsters = monsters;
- player.draw();
- ground.draw();
- for(var c = 0; c < infos.length; c++) {
- infos[c].draw();
- }
- for(var d = 0; d < controlButtons.length; d++) {
- controlButtons[d].draw();
- }
- if(isStarted && !removedInfoText) {
- removeChars(infoText);
- removedInfoText = true;
- }
- if(isStarted) {
- if(backgroundSound.paused) {
- backgroundSound.addEventListener('ended', function() {
- this.currentTime = 0;
- if(controlButtons[controlButtonsIds['music']].isChecked) {
- this.play();
- }
- }, false);
- if(controlButtons[controlButtonsIds['music']].isChecked) {
- backgroundSound.play();
- }
- }
- for(var x = 0; x < player.fires.length; x++) {
- player.fires[x].draw();
- }
- var monstersCurrent = monsters.length;
- for(var i = 0; i < monsters.length; i++) {
- monsters[i].draw();
- if(monsters[i].isDead) {
- monstersCurrent--;
- }
- for(var x = 0; x < monsters[i].fires.length; x++) {
- monsters[i].fires[x].draw();
- }
- for(var x = 0; x < monsters[i].bonus.length; x++) {
- monsters[i].bonus[x].draw();
- }
- for(var d = 0; d < monsters[i].hearts.length; d++) {
- monsters[i].hearts[d].draw();
- }
- }
- if(monstersCurrent <= 0) {
- player.setLevel(player.getLevel() + 1);
- monsters = new Array();
- var monstersCount = 2 + player.getLevel();
- if(monstersCount > 8) {
- monstersCount = 8;
- }
- spawnMonster(monstersCount);
- }
- } else {
- backgroundSound.currentTime = 0;
- backgroundSound.pause();
- }
- removeChars(userPanel);
- userPanel = drawChars('Level: ' + player.getLevel() +
- ' / Staerke: ' + player.getFireDamage() +
- ' / Leben: ' + player.getHealth() + '/' + player.getMaxHealth(), 20, 20);
- if(!isPaused) {
- requestFrame(loop);
- }
- }
- function stop() {
- isPaused = true;
- }
- function playerDie() {
- return function() {
- var sound = new Audio('sounds/explosion.ogg');
- sound.volume = 0.8;
- sound.play();
- gameOverText = drawChars('Game Over. Leertaste zum Neustarten', mainCtx.canvas.width / 4, mainCtx.canvas.height - 70);
- };
- }
- function restart() {
- player = new Player(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, 'Player');
- player.die = playerDie();
- removeChars(gameOverText);
- isStarted = true;
- isPaused = false;
- spawnMonster(3);
- }
- function start(firstStart) {
- backgroundSound = new Audio('sounds/music.ogg');
- backgroundSound.volume = 0.2;
- sprites.src = 'images/sprites.png';
- sprites.onload = function() {
- player = new Player(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, 'Player');
- player.die = playerDie();
- ground = new Ground(mainCtx, sprites);
- userPanel = drawChars('', 20, 20);
- infoText = drawChars('Linke oder rechte Pfeiltaste druecken', mainCtx.canvas.width / 4, mainCtx.canvas.height - 70);
- controlButtons[controlButtonsIds['sound']] = new ControlButton(mainCtx, sprites, {
- 'srcX': 0,
- 'srcY': 249,
- 'isChecked': true,
- 'show': true,
- 'width': 29,
- 'height': 29,
- 'offsetX': 700,
- 'offsetY': 13
- }, {
- 'srcX': 29,
- 'srcY': 249,
- 'isChecked': true,
- 'show': true,
- 'width': 29,
- 'height': 29,
- 'offsetX': 700,
- 'offsetY': 13
- });
- controlButtons[controlButtonsIds['music']] = new ControlButton(mainCtx, sprites, {
- 'srcX': 58,
- 'srcY': 249,
- 'isChecked': true,
- 'show': true,
- 'width': 29,
- 'height': 29,
- 'offsetX': 740,
- 'offsetY': 13
- }, {
- 'srcX': 87,
- 'srcY': 249,
- 'isChecked': true,
- 'show': true,
- 'width': 29,
- 'height': 29,
- 'offsetX': 740,
- 'offsetY': 13
- });
- isPaused = false;
- loop();
- };
- }
- function spawnMonster(amount) {
- for(var i = 0; i < amount; i++) {
- monsters[monsters.length] = new Monster(mainCtx, sprites, controlButtons, controlButtonsIds, monsters, player, 'Monster');
- }
- }
- function firstStart() {
- if(!isStarted) {
- spawnMonster(3);
- isStarted = true;
- }
- }
- function keyDown(event) {
- var keyId = event.keyCode || event.which;
- if(keyId == 40) { // down
- firstStart();
- player.keys['down'] = true;
- event.preventDefault();
- }
- if(keyId == 38) { // up
- firstStart();
- player.keys['up'] = true;
- event.preventDefault();
- }
- if(keyId == 37) { // left
- firstStart();
- player.keys['left'] = true;
- event.preventDefault();
- }
- if(keyId == 39) { // right
- firstStart();
- player.keys['right'] = true;
- event.preventDefault();
- }
- if(keyId == 32) { // space
- restart();
- event.preventDefault();
- }
- }
- function keyUp(event) {
- var keyId = event.keyCode || event.which;
- if(keyId == 40) { // down
- player.keys['down'] = false;
- event.preventDefault();
- }
- if(keyId == 38) { // up
- player.keys['up'] = false;
- event.preventDefault();
- }
- if(keyId == 37) { // left
- player.keys['left'] = false;
- event.preventDefault();
- }
- if(keyId == 39) { // right
- player.keys['right'] = false;
- event.preventDefault();
- }
- }
- function mouseDown(event) {
- var posX = event.clientX - mainCtx.canvas.getBoundingClientRect().left;
- var posY = event.clientY - mainCtx.canvas.getBoundingClientRect().top;
- if(posX <= mainCtx.canvas.width && posY <= mainCtx.canvas.height) {
- if(event.which == 1 || event.button == 0) { // left click
- for(var d = 0; d < controlButtons.length; d++) {
- var offsetX = controlButtons[d].offsetX;
- var offsetY = controlButtons[d].offsetY;
- var width = controlButtons[d].width;
- var height = controlButtons[d].height;
- if(offsetX <= posX && offsetX + width >= posX && offsetY <= posY && offsetY + height >= posY) {
- if(controlButtons[d].isChecked) {
- controlButtons[d].isChecked = false;
- } else {
- controlButtons[d].isChecked = true;
- }
- if(d == controlButtonsIds['music']) {
- if(controlButtons[d].isChecked) {
- backgroundSound.currentTime = 0;
- backgroundSound.play();
- } else {
- backgroundSound.currentTime = 0;
- backgroundSound.pause();
- }
- }
- }
- }
- event.preventDefault();
- }
- }
- }
Add Comment
Please, Sign In to add comment