//sounds var sound_thwip; var sound_upg; var sound_amb; //constants var WINDOW_WIDTH = 800; var WINDOW_HEIGHT = 600; var PLAYER_POSX = WINDOW_WIDTH / 2; var PLAYER_POSY = WINDOW_HEIGHT / 2; var MOVE_SPEED = 8; var REFRESH_RATE = 15; var FIRE_COOLDOWN = 15; //number of frames var DRAW_COOLDOWN = 5; var CARD_EXPIRATION_DATE = 100; //age at which thrown cards self-remove var DEFAULT_CARD_SPEED = 12; var FOUR_TIME = 3; //delay between shots var NINE_TIME = 6; var HEALTH_DRAIN = 0; //was 0.12 var KING_LIMIT = 1; var QUEEN_LIMIT = 1; var ROOK_LIMIT = 2; var BISHOP_LIMIT = 2; var KNIGHT_LIMIT = 2; var PAWN_LIMIT = 8; var KING_COST = 9; var QUEEN_COST = 9; var ROOK_COST = 5; var BISHOP_COST = 3; var KNIGHT_COST = 3; var PAWN_COST = 1; //variables var credits = 0; var muted = 0; var move_left = 0; var move_right = 0; var move_up = 0; var move_down = 0; var offsetx = 0; var offsety = 0; var deltax = 0; var deltay = 0; var mousex = 0; var mousey = 0; var facing = 0; var health = 100; var heldcard = 15; //the first card is always the 2 of clubs var number = 2; var suit = 1; var cooldown = 0; var uid = 0; var generate_num = 20; //encounter your first shrine sooner //upgrade array var deckupgrades = new Array(52); for (var i = 0; i < 52; i++) { if (i % 13 >= 1 && i % 13 <= 4) { deckupgrades[i] = 1; } else { deckupgrades[i] = 0; } } var totalupgrades = 0; //objects and arrays (mostly for web workers) var newenemies = []; //array that contains pertinant stuff about all new enemies var deadenemies = []; //array of dead enemies returned from the web worker to be deleted var movedenemies = []; //array of moved enemies returned from the web worker var hitenemies = []; //array of enemies hit by a card var message = { "newenemies": newenemies, "hitenemies": hitenemies}; var delayed_shots = []; function startenemyWorker() { if (typeof (Worker) !== "undefined") { if (typeof (w) == "undefined") { w = new Worker("enemyWorker.js"); //defining file that will run on new thread } w.onmessage = function (event) { var message = JSON.parse(event.data); deadenemies.push.apply(deadenemies, message[0]); movedenemies.push.apply(movedenemies, message[1]); for (var j = 0; j < message[2].length; j++) { //response is array of numbers so three pawns attack would look like [10,10,10] health -= message[2][j]; //subtracts health from player done by units in range } }; } else { $("#playground").text = "Sorry, your browser does not support Web Workers... Please use the lastest version of Chrome, Internet Explorer, or Firefox."; } } function stopenemyWorker() { w.terminate(); } var load_sounds = function () { sound_thwip = soundManager.createSound({ url: "assets/aud/thwip.mp3", autoLoad: true }); sound_upg = soundManager.createSound({ url: "assets/aud/upgrade.mp3" }); sound_amb = soundManager.createSound({ url: "assets/aud/ambient.mp3" }); loopSound(sound_amb); }; function loopSound(sound) { sound.play({ onfinish: function() { loopSound(sound); } }); } //initialize soundManager soundManager.setup({ // where to find flash audio SWFs, as needed url: '/assets/swf-files/', // optional: prefer HTML5 over Flash for MP3/MP4 preferFlash: false, debugMode: false, onready: function () { load_sounds(); } }); $.when( $.getScript("functions.js"), $.getScript("init.js"), $.Deferred(function (deferred) { $(deferred.resolve); }) ).done(function () { $(function () { //main loop! $.playground().registerCallback(function () { message.delta = { "x": deltax, "y": deltay }; w.postMessage(JSON.stringify(message)); //sends new enemies and hit enemies to the enemy worker newenemies.length = 0; hitenemies.length = 0; $("#healthbar").width( 8 * health ); health -= HEALTH_DRAIN; if (health <= 0) { $.playground().pauseGame(); stopenemyWorker(); $("#healthbar").hide(); $("#playground").prepend("
"); $("#welcomeScreen").css('background-image', 'url(assets/img/youdiedcredits.png)'); $("#welcomeScreen").css('opacity', '100'); } //get input, but block movement into solids if ($.gQ.keyTracker[65] && $("#leftbound").collision("#terrain,.solid").length === 0) { move_left = 1; } else { move_left = 0; } if ($.gQ.keyTracker[68] && $("#rightbound").collision("#terrain,.solid").length === 0) { move_right = 1; } else { move_right = 0; } if ($.gQ.keyTracker[87] && $("#upbound").collision("#terrain,.solid").length === 0) { move_up = 1; } else { move_up = 0; } if ($.gQ.keyTracker[83] && $("#downbound").collision("#terrain,.solid").length === 0) { move_down = 1; } else { move_down = 0; } var delta_vector = normal({x: (move_left - move_right), y: (move_up - move_down)}); deltax = MOVE_SPEED * delta_vector.x; deltay = MOVE_SPEED * delta_vector.y; mousex = $.gQ.mouseTracker.x; mousey = $.gQ.mouseTracker.y; //get mouse rotation facing = (180 / Math.PI) * angle(mousex - PLAYER_POSX, mousey - PLAYER_POSY); //turn to face mouse $("#feet").rotate(facing + 90, false); $("#playerspr").rotate(facing, false); //pause/resume feet animation if (delta_vector.x !== 0 || delta_vector.y !== 0) { $("#feet").resumeAnimation(); } else { $("#feet").pauseAnimation(); } //spin the wheel $("#sealui").rotate(1, true); //tick cooldown if (cooldown > 0) cooldown--; //obtain upgrades! if ($("#playerspr").collision("#terrain,.upgrade").length > 0) { var counter; do { counter++; if (counter > 1000) break; heldcard = Math.floor(51 * Math.random()); number = heldcard % 13; suit = Math.floor(heldcard / 13); } while (deckupgrades[heldcard] == 1 || number < 2 || number > 10); deckupgrades[heldcard] = 1; totalupgrades++; //DING! sound_upg.play(); update_hud(); //destroy pickup $("#playerspr").collision("#terrain,.upgrade").each(function () { $(this).remove(); }); //win the game! if (totalupgrades >= 40) { $.playground().pauseGame(); stopenemyWorker(); $("#healthbar").hide(); $("#playground").prepend("
"); $("#welcomeScreen").css('background-image', 'url(assets/img/youwincredits.png)'); $("#welcomeScreen").css('opacity', '100'); } } //scroll terrain offsetx += deltax; offsety += deltay; if (offsetx >= 64) { offsetx -= 64; generate("left"); } if (offsetx <= -64) { offsetx += 64; generate("right"); } if (offsety >= 64) { offsety -= 64; generate("up"); } if (offsety <= -64) { offsety += 64; generate("down"); } $("#bg").xy(offsetx - 64, offsety - 64, false); //resolve delay effects for (var i = 0; i < delayed_shots.length; i++) { if (delayed_shots[i].delay < 1) { spawn_card(PLAYER_POSX - 16, PLAYER_POSY - 16, delayed_shots[i].suit, facing + delayed_shots[i].dirmod, DEFAULT_CARD_SPEED); delayed_shots.splice(i, 1); } else { delayed_shots[i].delay -= 1; } } //process thrown cards $(".thrown_card").each(function () { //age and expire $(this)[0].thrown_card.age += 1; if ($(this)[0].thrown_card.age > CARD_EXPIRATION_DATE) { $(this).remove(); return; } //handle movement UPDATE WHEN VECTORS ARE ADDED $(this).xy( $(this)[0].thrown_card.speed * Math.cos($(this)[0].thrown_card.direction * (Math.PI / 180)) + deltax, $(this)[0].thrown_card.speed * Math.sin($(this)[0].thrown_card.direction * (Math.PI / 180)) + deltay, true); //star turn! if ($(this)[0].thrown_card.option == 5 && $(this)[0].thrown_card.age == 10) { $(this)[0].thrown_card.direction += 162; } //boomerang curve! if ($(this)[0].thrown_card.option == 7) { $(this)[0].thrown_card.direction -= 4; } //diffusion split! if ($(this)[0].thrown_card.option == 10 && $(this)[0].thrown_card.age % 5 === 0) { spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction + 90, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction - 90, DEFAULT_CARD_SPEED); } //detect collisions var hitsomething = 0; $(this).collision("#enemies,.enemy").each(function () { $(this)[0].enemy.health -= 1; if ($(this)[0].enemy.health <= 1) { hitenemies.push({ "id": $(this)[0].enemy.name }); console.log($(this)[0].enemy.name + " was killed."); switch ($(this)[0].enemy.type) { case "pawn": health += 5; break; case "knight": health += 10; break; case "bishop": health += 10; break; case "rook": health += 10; break; case "queen": health += 25; break; case "king": health += 50; break; } if (health > 100) { health = 100; } } hitsomething = 1; }); if ((hitsomething || $(this).collision("#terrain,.solid").length > 0) && ($(this)[0].thrown_card.option != 7 && $(this)[0].thrown_card.option != 10)) { spawn_fx($(this)[0].thrown_card.suit, $(this).x(), $(this).y()); //bomb boom! if ($(this)[0].thrown_card.option == 6) { spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction - 150, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction - 90, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction - 30, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction + 30, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction + 90, DEFAULT_CARD_SPEED); spawn_card($(this).x(), $(this).y(), $(this)[0].thrown_card.suit, $(this)[0].thrown_card.direction + 150, DEFAULT_CARD_SPEED); } $(this).remove(); return; } }); //process obstacles $(".doodad").each(function () { //handle movement $(this).xy(deltax, deltay, true); //handle outofbounds - past farthest spawn distance if ($(this).x() < -2 * $(this).h() || $(this).x() > WINDOW_WIDTH + $(this).h() || $(this).y() < -2 * $(this).h() || $(this).y() > WINDOW_HEIGHT + $(this).h()) { $(this).remove(); } }); //----process enemies---- if (movedenemies.length > 0) { for (var j = 0; j <= movedenemies.length - 1; j++) { $("#" + movedenemies[j].id).xy(movedenemies[j].x, movedenemies[j].y, true); } movedenemies.length = 0; } if (deadenemies.length > 0) { for (var k = 0; k <= deadenemies.length - 1; k++) { console.log(deadenemies[k] + " died"); $("#" + deadenemies[k]).remove(); } deadenemies.length = 0; } //----process enemy projectiles---- }, REFRESH_RATE); }); });