diff --git a/registration-system/view/signups/game1/js/achievements.js b/registration-system/view/signups/game1/js/achievements.js index 652c34902b4308c06154acc62f190187bf97149a..4db9a05a8fb07f71ee053941c82dc49f410d0111 100644 --- a/registration-system/view/signups/game1/js/achievements.js +++ b/registration-system/view/signups/game1/js/achievements.js @@ -56,14 +56,13 @@ Achievements.prototype.triggerAchievement = function (achievementId) { if (!this.achievements[achievementId]){ console.error("No such achievement: " + achievementId); } - else if (!this.achievedAchievements.indexOf(achievementId) >= 0) { + else if (! (this.achievedAchievements.indexOf(achievementId) >= 0)) { this.achievedAchievements.push(achievementId); this.updateStatusBar(); this.updateStatusText(); this.logMessage(this.achievements[achievementId]); } - else - console.warn("Achievement already achieved: " + achievementId); + // else console.warn("Achievement already achieved: " + achievementId); if (this.numCompletedAchievements() === 42 ) this.triggerAchievement('achievement42') }; \ No newline at end of file diff --git a/registration-system/view/signups/game1/js/character.js b/registration-system/view/signups/game1/js/character.js index 39eea831b7f779f6c84851987fd964114fb87595..14aeab3dcdbc7c4ca958720470740203f427a45d 100644 --- a/registration-system/view/signups/game1/js/character.js +++ b/registration-system/view/signups/game1/js/character.js @@ -28,7 +28,7 @@ Char.prototype.findSpawn = function() { var spawn = this.svg.select("#player_spawn"); var bbox = spawn[0][0].getBBox(); return Vec.add(getTranslation(this.svg[0][0], spawn[0][0]), [bbox.x, bbox.y]); -} +}; Char.prototype.initializeAnimations = function() { var self = this; @@ -57,7 +57,7 @@ Char.prototype.initializeAnimations = function() { self.animations[label] = frames; }); -} +}; Char.directionToName = ["UP", "RIGHT", "DOWN", "LEFT"]; Char.prototype.animate = function() { this.animateStep += 1; @@ -105,7 +105,7 @@ Char.prototype.animate = function() { // show current frame this.frames[this.currentFrame].style.display = 'block'; -} +}; Char.prototype.physics = function() { if (this.moveTarget && this.moveTarget.length == 0) return; if (Vec.equals(this.translation, this.moveTarget[0])) { @@ -126,17 +126,20 @@ Char.prototype.physics = function() { if (this.pathFinder.canWalkOn(nextPosition[0], nextPosition[1])) Vec.assign(this.translation, nextPosition); else - this.moveTarget.shift(); + this.moveTarget.shift(); + + var event = this.pathFinder.getEventOn(nextPosition[0], nextPosition[1]); + if (event) Game.eventHandler.handleEvent(event); this.updatePosition(); -} +}; Char.prototype.updatePosition = function() { if (!this.image) return; var self = this; this.image.attr("transform", function() { return translate.apply(null, self.translation); }); -} +}; Char.prototype.setMoveTarget = function(newX, newY) { var matrix = this.svg[0][0].getScreenCTM(); var x = newX-matrix.e; @@ -146,5 +149,8 @@ Char.prototype.setMoveTarget = function(newX, newY) { this.moveTarget = this.pathFinder.smoothPath(this.pathFinder.findPath(this.translation[0], this.translation[1], x, y)); else this.moveTarget = [[x, y]]; -} +}; +Char.prototype.stopMovement = function() { + this.moveTarget = null; +}; \ No newline at end of file diff --git a/registration-system/view/signups/game1/js/events.js b/registration-system/view/signups/game1/js/events.js new file mode 100644 index 0000000000000000000000000000000000000000..9f2d1685b89635a9630e717680f7f7353adb37b0 --- /dev/null +++ b/registration-system/view/signups/game1/js/events.js @@ -0,0 +1,26 @@ +function EventHandler() { + +} + +/** + * Receives an event object and handles the necessary actions + * + * Object has: + * id: svg elem id, + * type: (achievement,...) + * trigger: (walkon, hover, click) + * stopsWalk: (true, false) + * + * @param event + */ +EventHandler.prototype.handleEvent = function (event) { + switch (event.type) { + case 'achievement': + Game.achievements.triggerAchievement(event.id); + break; + } + + if (event.stopsWalk) { + Game.char.stopMovement(); + } +}; \ No newline at end of file diff --git a/registration-system/view/signups/game1/js/game.js b/registration-system/view/signups/game1/js/game.js index 1378bce55a15a4a1271413523691b80357504a4d..e7c5818cadc1351b2f5421a7de72712dc9c7b6e5 100644 --- a/registration-system/view/signups/game1/js/game.js +++ b/registration-system/view/signups/game1/js/game.js @@ -4,7 +4,10 @@ function Game(config) { Game.config = config; Game.instance = this; - this.achievements = new Achievements(); + Game.achievements = new Achievements(); + Game.eventHandler = new EventHandler(); + Game.char = null; + Game.cam = null; } Game.eventLayers = ['CLICKABLE', 'WALK', 'NOWALK', 'EVENT']; Game.prototype.run = function() { @@ -17,6 +20,8 @@ Game.prototype.run = function() { var svg = d3.select("svg"); + // ------------------------------------- + // init event related stuff var displayEvents = Game.config.showEventLayers ? 'block' : 'none'; svg.selectAll('g').filter(function() { return ( @@ -25,8 +30,11 @@ Game.prototype.run = function() { ); }).style('display', displayEvents); - var char = new Char(svg); - var cam = new Camera(svg, char.translation); + + // ------------------------------------- + // init view stuff + Game.char = new Char(svg); + Game.cam = new Camera(svg, Game.char.translation); // test animation @@ -39,17 +47,17 @@ Game.prototype.run = function() { // animate setInterval(function() { - if (char.loaded) { + if (Game.char.loaded) { // move player - char.physics(); - char.animate(); + Game.char.physics(); + Game.char.animate(); // cam movement - cam.movement(); + Game.cam.movement(); } }, 10); svg.on("click", function(d) { - char.setMoveTarget(d3.event.pageX, d3.event.pageY); + Game.char.setMoveTarget(d3.event.pageX, d3.event.pageY); }); }); diff --git a/registration-system/view/signups/game1/js/pathFinder.js b/registration-system/view/signups/game1/js/pathFinder.js index 476b31b0579d4bb0a7262b669eb3de01e074dd16..765d746254cf62ea46e9e9c32137c6ece200d708 100644 --- a/registration-system/view/signups/game1/js/pathFinder.js +++ b/registration-system/view/signups/game1/js/pathFinder.js @@ -3,12 +3,14 @@ function PathFinder(svg) { this.walkNodes = []; this.noWalkNodes = []; + this.walkEventNodes = []; this.raster = null; this.scanWalkables(); this.generateRaster(); } + PathFinder.prototype.scanWalkables = function() { var self = this; this.svg.selectAll('g').each(function(d, i) { @@ -17,6 +19,8 @@ PathFinder.prototype.scanWalkables = function() { self.walkNode = this; if (!self.noWalkNode && label == "NOWALK") self.noWalkNode = this; + if (!self.eventNode && label == "EVENT") + self.eventNode = this; }); var walkTranslation = getTranslation(this.svg[0][0], this.walkNode); d3.select(self.walkNode).selectAll('path').each(function() { @@ -26,25 +30,42 @@ PathFinder.prototype.scanWalkables = function() { d3.select(self.noWalkNode).selectAll('path').each(function() { self.noWalkNodes.push(new Path(this.getAttribute("d"), noWalkTranslation)); }); -} + var eventTranslation = getTranslation(this.svg[0][0], this.eventNode); + d3.select(self.eventNode).selectAll('path').each(function() { + if (this.getAttribute('trigger') == 'walkon' ) + self.walkEventNodes.push({ + path: new Path(this.getAttribute("d"), eventTranslation), + id: this.getAttribute('id'), + type: this.getAttribute('type'), + trigger: 'walkon', + stopsWalk: this.getAttribute('stopsWalk') === 'true' + }); + }); +}; + PathFinder.prototype.generateRaster = function() { if (!Game.config.usePathFinding) return; this.raster = []; var subraster = null; - var bbox = this.svg[0][0].getBBox() + var bbox = this.svg[0][0].getBBox(); var ymax = Math.floor(bbox.height/Game.config.pathFindingGridSize); var xmax = Math.floor(bbox.width/Game.config.pathFindingGridSize); for (var y = 0; y < ymax; ++y) { - var subraster = this.raster[y] = []; + subraster = this.raster[y] = []; for (var x = 0; x < xmax; ++x) { - subraster[x] = {walkable: this.canWalkOn(x*Game.config.pathFindingGridSize, y*Game.config.pathFindingGridSize), score: -1, from: null}; + subraster[x] = { + walkable: this.canWalkOn(x*Game.config.pathFindingGridSize, y*Game.config.pathFindingGridSize), + score: -1, + from: null, + event: this.getEventOn(x*Game.config.pathFindingGridSize, y*Game.config.pathFindingGridSize) !== null + }; } } -} +}; PathFinder.prototype.clearPathScore = function() { - var bbox = this.svg[0][0].getBBox() + var bbox = this.svg[0][0].getBBox(); var ymax = Math.floor(bbox.height/Game.config.pathFindingGridSize); var xmax = Math.floor(bbox.width/Game.config.pathFindingGridSize); for (var y = 0; y < ymax; ++y) { @@ -55,7 +76,7 @@ PathFinder.prototype.clearPathScore = function() { e.from = null; } } -} +}; PathFinder.prototype.findPath = function(fromX, fromY, toX, toY) { if (!this.canWalkOn(toX, toY)) return []; @@ -116,7 +137,7 @@ PathFinder.prototype.findPath = function(fromX, fromY, toX, toY) { resultPath.unshift([fromX, fromY]); return resultPath; -} +}; PathFinder.prototype.smoothPath = function(path) { if (!path || path.length == 0) return []; @@ -133,7 +154,7 @@ PathFinder.prototype.smoothPath = function(path) { resultPath.push(path[path.length-1]); return resultPath; -} +}; PathFinder.prototype.isDirectlyReachable = function(fromX, fromY, x, y) { var canWalk = true; for (var i = 0; i < this.walkNodes.length; ++i) { @@ -151,7 +172,7 @@ PathFinder.prototype.isDirectlyReachable = function(fromX, fromY, x, y) { } } return canWalk; -} +}; PathFinder.prototype.canWalkOn = function(x, y) { var canWalk = false; for (var i = 0; i < this.walkNodes.length; ++i) { @@ -169,5 +190,13 @@ PathFinder.prototype.canWalkOn = function(x, y) { } } return canWalk; -} +}; +PathFinder.prototype.getEventOn = function(x, y) { + for (var i = 0; i < this.walkEventNodes.length; ++i) { + if (this.walkEventNodes[i].path.isInside(x, y)) { + return this.walkEventNodes[i]; + } + } + return null; +}; diff --git a/registration-system/view/signups/game1/maps/map_landing.svg b/registration-system/view/signups/game1/maps/map_landing.svg index 1a8aab377a107bb32dc8ef82fa31f91fb6a3f920..6c404dc27e6ae00bed65a18d449783ca0fc8adf1 100644 Binary files a/registration-system/view/signups/game1/maps/map_landing.svg and b/registration-system/view/signups/game1/maps/map_landing.svg differ diff --git a/registration-system/view/signups/game1/test.html b/registration-system/view/signups/game1/test.html index 0065a1d93579b2a9e7100ffe369de55ce8016990..12a1a81db5b40fdbefc12515d264ff2bc8c470c3 100644 --- a/registration-system/view/signups/game1/test.html +++ b/registration-system/view/signups/game1/test.html @@ -7,6 +7,7 @@ <script src="jslib/priority-queue.min.js" type="text/javascript"></script> <script src="jslib/checkLineIntersection.js" type="text/javascript"></script> <script src="../../js/api.js" type="text/javascript"></script> +<script src="js/events.js" type="text/javascript"></script> <script src="js/achievements.js" type="text/javascript"></script> <script src="js/svgUtils.js" type="text/javascript"></script> <script src="js/pathFinder.js" type="text/javascript"></script> @@ -54,7 +55,7 @@ FAPI.methodBasepath = ''; var game = new Game({ startMap: 'map_landing.svg', - showEventLayers: false, + showEventLayers: UrlComponents.isSet('showEL'), pathFindingGridSize: 5, usePathFinding: true, size: [800, 600]