function AdventureGame() { var container = null; var canvas = null; var contextType = '2d'; var contextAttributes = {}; var context = null; var game = null; var heroes = []; var destinations = []; var minHeroes = 6; var minShops = 10; var minMonsters = 10; var minCoins = 10; var leaderboardElement = null; this.nameGenerator = new NameGenerator(); this.create = function(elementName, leaderboard) { game = this; container = document.getElementById(elementName); canvas = container.getElementsByTagName('canvas')[0]; window.addEventListener('resize', game.handleResize); canvas.addEventListener('mousemove', game.handleMouseMove); canvas.addEventListener('mousedown', game.handleMouseClick); canvas.addEventListener('contextmenu', game.handleMouseClick); canvas.addEventListener('touchstart', game.handleMouseClick); window.requestAnimationFrame(game.handleRequestAnimationFrame); this.handleResize(null); game.init(canvas); leaderboardElement = document.getElementById(leaderboard); }; this.handleResize = function(event) { canvas.width = container.offsetWidth; canvas.height = container.offsetHeight; context = canvas.getContext(contextType, contextAttributes); context.translate(0.5, 0.5); }; this.handleRequestAnimationFrame = function(event) { game.update(canvas); context.fillStyle = "#D3D3D3"; context.fillRect(0 ,0, canvas.width, canvas.height); game.draw(context); window.requestAnimationFrame(game.handleRequestAnimationFrame); } this.handleMouseMove = function(event) { game.mouseMove(canvas, event.offsetX, event.offsetY); } this.handleMouseClick = function(event) { game.mouseClick(canvas, event.button, event.offsetX, event.offsetY); event.preventDefault(); } this.init = function(canvas) { destinations = []; heroes = []; for(var i = 0; i < minHeroes; i++) { var newbie = new Hero(); destinations.push(newbie); heroes.push(newbie); } for(var i = 0; i < 25; i++) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; destinations.push(new CoinDestination(x, y, 0)); } /*for(var i = 0; i < 25; i++) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; destinations.push(new HeartDestination(x,y)); }*/ for(var i = 0; i < minMonsters; i++) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; destinations.push(new MonsterDestination(x,y)); } for(var i = 0; i < minShops; i++) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; destinations.push(new ShopDestination(x,y)); } for(var i = 0; i < destinations.length; i++) { destinations[i].init(canvas, this, i); } }; this.update = function(canvas) { for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) { destination.update(canvas); } for(var i = destinations.length - 1; i >= 0; i--) { var destination = destinations[i]; if(destination.isConsumed) { destinations.splice(i, 1); } } var heroCount = 0; var coinCount = 0; var monsterCount = 0; //heroes = []; for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) { switch(destination.getType()) { case "hero": heroCount++; //heroes.push(destination); break; case "coin": coinCount++; break; case "monster": monsterCount++; break; } } if(heroCount < minHeroes) { var newbie = new Hero(); destinations.push(newbie); newbie.init(canvas, this, destinations.length); heroes.push(newbie); } if(monsterCount < minMonsters) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; var mob = new MonsterDestination(x, y); destinations.push(mob); mob.init(canvas, this, destinations.length); } if(coinCount < minCoins) { var x = canvas.width * Math.random() + 1; var y = canvas.height * Math.random() + 1; var coin = new CoinDestination(x, y, 0); destinations.push(coin); coin.init(canvas, this, destinations.length); } leaderboardElement.innerHTML = ""; leaderboard = []; for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) { //hero.addToLeaderboard(leaderboardElement); leaderboard.push(hero.getInfo()); } leaderboard = leaderboard.sort(compare); for(var i = 0; i < leaderboard.length; i++) { displayBoard(leaderboard[i]); } }; var displayBoard = function(row) { var topInfo = "level " + row.level + " " + row.name + " " + row.inventory.hearts + "/" + row.inventory.maxHearts; var info = "coins:" + row.inventory.coins + " xp:" + row.inventory.xp + " dmg:" + row.inventory.weaponStrength; var topNode = document.createTextNode(topInfo); var infoNode = document.createTextNode(info); var topLine = document.createElement('p'); topLine.style.color = row.heroColor; var infoLine = document.createElement('p'); infoLine.style.color = row.heroColor; topLine.appendChild(topNode); infoLine.appendChild(infoNode); leaderboardElement.appendChild(topLine); leaderboardElement.appendChild(infoLine); }; var compare = function(a,b) { if (a.inventory.xp < b.inventory.xp) { return 1; } if (a.inventory.xp > b.inventory.xp) { return -1; } return 0; } this.draw = function(context) { for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) { destination.draw(context); } }; this.mouseMove = function(canvas, x, y) { for(var i = 0, destination = destinations[0]; hero = destinations[i]; i++) { destination.mouseMove(canvas, x, y); } }; this.mouseClick = function(canvas, button, x, y) { var newDest = null; if(button == 0) { game.spawnCoins({x: x, y: y}, Math.floor(25 * Math.random() + 1)); } else if (button == 1) { newDest = new MonsterDestination(x, y); destinations.push(newDest); newDest.init(canvas, this); } else { newDest = new HeartDestination(x, y); destinations.push(newDest); newDest.init(canvas, this); } }; this.getNewDestination = function() { var destinationLength = destinations.length; if(destinationLength == 0) { return; } var index = Math.floor(destinations.length * Math.random()); var destination = destinations[index]; return destination; } this.getVisibleDestinations = function(x, y, viewRange) { var visibleDestinations = []; for(var i = 0, destination = destinations[0]; destination = destinations[i]; i++) { var destPos = destination.getPosition(); if(destPos.x >= x - viewRange && destPos.x <= x + viewRange && destPos.y >= y - viewRange && destPos.y <= y + viewRange) { visibleDestinations.push(destination); } } return shuffle(visibleDestinations); } var shuffle = function(collection) { for (var i = collection.length - 1; i > 0; i--) { var temp = Math.floor(Math.random() * (i + 1)); [collection[i], collection[temp]] = [collection[temp], collection[i]]; } return collection; } this.getAllDestinations = function() { return shuffle(destinations); } this.heroDies = function(deadHero) { for(var i = destinations.length - 1; i >= 0; i--) { var hero = destinations[i]; if(hero == deadHero) { destinations.splice(i, 1); } } for(var i = heroes.length - 1; i >= 0; i--) { var hero = heroes[i]; if(hero == deadHero) { heroes.splice(i, 1); } } } this.spawnCoins = function(position, amount) { var coinsRemaining = amount; var magnitude = 1; var bounds = 50; while(coinsRemaining > 0) { var spawn = coinsRemaining % Math.pow(10, magnitude) / Math.pow(10, magnitude - 1); for(var i = 0; i < spawn; i++) { var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds); var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds); var coin = new CoinDestination(coinX, coinY, magnitude - 1); destinations.push(coin); coin.init(canvas, this); } coinsRemaining -= spawn * Math.pow(10, magnitude - 1); magnitude++; } } /*this.spawnCoins = function(position, amount) { var level2Coins = Math.floor(amount / 100); var level0Coins = amount - (level2Coins * 100); var bounds = 50; for(var i = 0; i < level2Coins; i++) { var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds); var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds); var coin = new CoinDestination(coinX, coinY, 2); destinations.push(coin); coin.init(canvas, this); } for(var i = 0; i < level0Coins; i++) { var coinX = Math.floor(2 * bounds * Math.random() + position.x - bounds); var coinY = Math.floor(2 * bounds * Math.random() + position.y - bounds); var coin = new CoinDestination(coinX, coinY, 0); destinations.push(coin); coin.init(canvas, this); } }*/ this.spawnHeart = function(x, y) { var bounds = 50; var heartX = Math.floor(2 * bounds * Math.random() + x - bounds); var heartY = Math.floor(2 * bounds * Math.random() + y - bounds); var heart = new HeartDestination(heartX, heartY); destinations.push(heart); heart.init(canvas, this); } this.getAveragePlayerLevel = function() { var average = 0; for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) { average += hero.getLevel(); } return Math.round(average / heroes.length); } this.getMaxPlayerLevel = function() { var max = 0; for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) { if(hero.getLevel() > max) { max = hero.getLevel(); } } return max; } this.getMinPlayerLevel = function() { var min = 99; for(var i = 0, hero = heroes[0]; hero = heroes[i]; i++) { if(hero.getLevel() < min) { min = hero.getLevel(); } } return min; } }