/** * Copyright 2015 Christopher Scott. All rights reserved. */ isMiniGame = true; function triggerEvent(eventType, object) { if (miniGame == undefined) { return; } if (!isMiniGame) { return; } miniGame.events[eventType](object); } function defineMiniGame() { miniGame = { 'id': 'tower_defense', 'events': { 'death': function(object) { if (object.player != player) { addGold(10); } else { if (object.objectType == 'building') { if (object.buildingType == 'Castle') { triggerEvent('end_game'); } } } }, 'birth': function(object) { var cost = miniGame['unit_attributes'][object.key].cost; addGold(cost * -1); }, 'restart': function() { // game.sound.stopAll(); stopFireSounds(); miniGame.variables(); }, 'end_game': function() { Clock.pause(); window.clearInterval(miniGameTimer); var curScore = $('#current_time').text(); var prevScore = localStorage.getItem("fw_tower_defense_time"); if (prevScore != null && prevScore != undefined) { var prevScoreAsNum = prevScore.replace(/\D/g, ''); } else { prevScoreAsNum = 0; } var curScoreAsNum = curScore.replace(/\D/g, ''); var bodyHTML; if (curScoreAsNum > prevScoreAsNum) { localStorage.setItem("fw_tower_defense_time", curScore); if (prevScore == null || prevScore == undefined) { bodyHTML = '

New best time: ' + curScore + '.

'; } else { bodyHTML = '

You beat your previous best time of ' + prevScore + '!

New best time: ' + curScore + '.

'; } } else { bodyHTML = '

Current time: ' + curScore + '.

You did not beat your best time of ' + prevScore + '.

'; } var mailChimpForm = '

Thanks for playing Tower Defense - a short mini game meant to demonstrate the power of the Feudal Wars game engine in the browser. To learn more about the kickstarter (coming soon) and full game which will feature online mutiplayer and full-fledged classic RTS game mechanics, subscribe to the mailing list below!

'; var stats = '

Reset Game

'; bodyHTML = bodyHTML + stats + mailChimpForm; var id = 'game_over'; var className = id; if ($('.' + className).length > 0) { $('.' + className).show(); } else { var options = { width: 500, dialogClass: className }; var title = 'Game Over: Your castle was destroyed!'; createUIDialog(title, id, bodyHTML, options); } gameOver = true; game.paused = true; $('#reset_game_button').on('click', function(event) { event.preventDefault(); $('.ui-dialog').hide(); ClearAllIntervals(); resetGame(); }); } }, 'variables': function() { alwaysShowHealthbars = false; currentWaveNumber = undefined; startingGold = 900; $('#gold_count').text(startingGold); secondsBetweenWaves = 40; }, 'unit_attributes': { '138-1': { // archer 'cost': 30, 'attack': 13, 'speed': 70, 'health': 60 }, '181-1': { // crossbow 'cost': 50, 'attack': 17, 'speed': 70, 'health': 60 }, '153-Earldom-1': { // tower 'cost': 250, 'attack': 20 } }, 'start_functions': function() { game.camera.x = 900; game.camera.y = 0; createUnitMenu(); // addInfo(); var title = 'Welcome to Tower Defense!'; var id = 'welcome'; var mailChimpForm = '
'; var bodyHTML = '
Objectives

Controls: in windowed mode, use the arrow keys to scroll around the map. In full screen mode (accessed by clicking the icon in the bottom right), use your mouse to scroll by pointing it at the edge of the screen. Select your units by dragging and dropping over them. They will automatically attack nearby enemy units but you can order them to attack by right clicking on an enemy unit (while your units are still selected).

Subscribe to the mailing list to get early access and development updates:

' + mailChimpForm + '

Start Game

'; var options = { 'width': '500px' } createUIDialog(title, id, bodyHTML, options); if (!gameReset) { playSoundNext('cc1_looped', null, .3, true); $('#start_game_button,.ui-dialog-titlebar-close').on('click', function(event) { event.preventDefault(); startMiniGame(); $('.ui-dialog').hide(); $("#" + id).dialog("close"); }); } } } miniGame.variables(); } function addKeyHandlers() { // game.input.keyboard.addKeyCapture(Phaser.Keyboard.ONE); key1 = game.input.keyboard.addKey(Phaser.Keyboard.ONE); key2 = game.input.keyboard.addKey(Phaser.Keyboard.TWO); key3 = game.input.keyboard.addKey(Phaser.Keyboard.THREE); key1.onDown.add(function() { $('#unit_menu img:eq(0)').trigger('click'); }, this); key2.onDown.add(function() { $('#unit_menu img:eq(1)').trigger('click'); }, this); key3.onDown.add(function() { $('#unit_menu img:eq(2)').trigger('click'); }, this); } function startMiniGame() { startWaveEvents(); startClock(); startTimer(secondsBetweenWaves); // toggleFullScreen(); testMap(); addKeyHandlers(); } function addInfo() { $(window).ready(function() { $("#block-block-5").dialog({ width: 730, close: function(event, ui) { startMiniGame(); } }); $('#block-block-5').show(); $('.feature_title').on('click', function(event) { event.preventDefault(); if ($(this).find('span').hasClass('ui-icon-triangle-1-s')) { $(this).find('span').attr('class', 'ui-icon ui-icon-triangle-1-e'); $(this).parent().next().hide(400); } else { $(this).find('span').attr('class', 'ui-icon ui-icon-triangle-1-s'); $(this).parent().next().show(400); } }); }); } function createUnitMenu() { if ($('#unit_menu').length > 0) { return; } var keys = ['138-1', '181-1', '153-Earldom-1']; var clock = 'Current Time:
00:00:00

'; var nextWave = '
'; var prevScore = localStorage.getItem("fw_tower_defense_time"); var prevScoreText = ''; if (prevScore != undefined) { prevScoreText = 'Best Time: ' + prevScore; } var cont = '
' + clock + prevScoreText + '
Enemy Wave #0
' + nextWave + '
' + startingGold + '
'; $('body').append(cont); for (var i = 0; i < keys.length; i++) { var nodeObject = gameAssetsKeyed[keys[i]]; if (nodeObject.object_type == 'unit') { var thumbURL = nodeObject.thumb; } else if (nodeObject.object_type == 'building') { var thumbURL = nodeObject.variants_building; } else if (nodeObject.object_type == 'wall') { var thumbURL = nodeObject.ne_sw; } var cost; var attack; var health; var speed; cost = miniGame.unit_attributes[keys[i]].cost; var key = i + 1; var img = '

' + cost + '

hotkey: ' + key + '

'; $('#unit_menu').append(img); } $('.unit_button').on('click', function() { var key = ($(this).data('keys')); var cost = parseInt($(this).data('cost')); var gold = parseInt($('#gold_count').text()); if ((gold - cost) < 0) { playSoundNext('error1', null); return; } var objectType = gameAssetsKeyed[key].object_type; var unitClass = gameAssetsKeyed[key].class; var onlyOnce; if (objectType == 'unit') { onlyOnce = true; } else { onlyOnce = true; } if (objectType != 'unit') { var uArray = createArrayFromString(gameAssetsKeyed[key].unwalkables); } placeObject(objectType, key, uArray, undefined, onlyOnce); }); } function addGold(addAmt) { playSoundNext('gold_coin', 300, .15, undefined); var gold = parseInt($('#gold_count').text()); gold = gold + addAmt; $('#gold_count').text(gold); } function spawnUnit(key, player, spawnX, spawnY, cost) { var staticXY = findNearestWalkable(roundDown(spawnX / matrixNodeSize), roundDown(spawnY / matrixNodeSize), staticGrid); spawnX = staticXY[0] * matrixNodeSize; spawnY = staticXY[1] * matrixNodeSize; var unit = game.add.sprite(spawnX, spawnY, key); gameObjects.addChild(unit); if (player == 1) { playSoundNext('ready1', 0, .3, undefined, unit); } setUnitProperties(unit, key, null, player); setUnitPositionsGrid(unit); return unit; } function startWaveEvents() { var howOftenSpawnWaves = 0; // time first wave spawns if (currentWaveNumber == undefined) { currentWaveNumber = 0; } else { var howOftenSpawnWaves = Phaser.Timer.SECOND * secondsBetweenWaves; } game.time.events.add(howOftenSpawnWaves, function() { currentWaveNumber++; $('#enemy_wave').text('Enemy Wave: # ' + currentWaveNumber); var numUnitsInWave = 20 + currentWaveNumber; var i = 0; var wave = setInterval(function() { if (game.paused) { return; } i++; var locations = [ [1430, 130], ]; var spawnAt = rData.pick(locations); var unit = spawnUnit('128-2', 2, spawnAt[0], spawnAt[1]); unit.findEnemyRange = 99999999; if (i == numUnitsInWave) { clearInterval(wave); } }, 300); startWaveEvents(); }, this); } function startClock() { Clock = { seconds: 0, minutes: 0, hours: 0, displayNumber: function(n, type) { this[type] = n; var formattedNumber = (n).toString().length == 1 ? "0" + n : n; document.getElementById(type).innerHTML = formattedNumber; }, increase: function() { if (game.paused) { return; } this.seconds = parseInt(this.seconds) + 1; this.displayNumber((parseInt((parseInt(+this.seconds / 60) + +this.minutes) / 60) + +this.hours) % 24, "hours"); this.displayNumber((parseInt(+this.seconds / 60) + +this.minutes) % 60, "minutes"); this.displayNumber(this.seconds % 60, "seconds"); }, start: function() { var that = this; this.interval = setInterval(function() { that.increase(); }, 1000) }, pause: function() { clearInterval(this.interval); delete this.interval; }, resume: function() { if (!this.interval) this.start(); }, reset: function() { this.seconds = 0; this.minutes = 0; this.hours = 0; } } Clock.start(); /* document.getElementById("pause").addEventListener("click", Clock.pause.bind(Clock)); document.getElementById("reset").addEventListener("click", Clock.reset.bind(Clock)); document.getElementById("resume").addEventListener("click", Clock.resume.bind(Clock)); */ } function startTimer(duration) { display = document.querySelector('#time_till_next_wave'); var timer = duration, minutes, seconds; miniGameTimer = setInterval(function() { if (game.paused) { return; } timer--; minutes = parseInt(timer / 60, 10) seconds = parseInt(timer % 60, 10); minutes = minutes < 10 ? "" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; if (timer < 1) { window.clearInterval(miniGameTimer); startTimer(secondsBetweenWaves); } display.textContent = 'Next Wave: ' + minutes + ":" + seconds; }, 1000); }