Skip to content
Snippets Groups Projects
Commit b82a4401 authored by Tim Repke's avatar Tim Repke
Browse files

moved all event handling to eventhandler

parent d3948419
No related branches found
No related tags found
No related merge requests found
......@@ -128,8 +128,7 @@ Char.prototype.physics = function() {
else
this.moveTarget.shift();
var event = this.pathFinder.getEventOn(nextPosition[0], nextPosition[1]);
if (event) Game.eventHandler.handleEvent(event);
Game.eventHandler.triggerEventOn('walkon', nextPosition[0], nextPosition[1]);
this.updatePosition();
};
......@@ -140,10 +139,7 @@ Char.prototype.updatePosition = 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;
var y = newY-matrix.f;
Char.prototype.setMoveTarget = function(x,y) {
if (Game.config.usePathFinding)
this.moveTarget = this.pathFinder.smoothPath(this.pathFinder.findPath(this.translation[0], this.translation[1], x, y));
......
function EventHandler() {
function EventHandler(svg) {
this.eventNodes = {
hover: [],
click: [],
walkon: []
};
var self = this;
svg.selectAll('g').each(function(d, i) {
var label = this.getAttribute('inkscape:label');
if (!self.rawNodes && label == "EVENT")
self.rawNodes = this;
});
var eventTranslation = getTranslation(svg[0][0], this.rawNodes);
d3.select(self.rawNodes).selectAll('path').each(function() {
var trigger = this.getAttribute('trigger');
if (trigger && self.eventNodes[trigger]) {
self.eventNodes[trigger].push({
path: new Path(this.getAttribute("d"), eventTranslation),
id: this.getAttribute('id'),
type: this.getAttribute('type'),
trigger: trigger,
stopsWalk: this.getAttribute('stopsWalk') === 'true'
});
}
});
}
EventHandler.prototype.hasEventOn = function (trigger, x, y) {
return this.getEventOn(trigger, x, y) !== undefined;
};
EventHandler.prototype.getEventOn = function(trigger, x, y) {
for (var i = 0; i < this.eventNodes[trigger].length; ++i) {
if (this.eventNodes[trigger][i].path.isInside(x, y)) {
return this.eventNodes[trigger][i];
}
}
return undefined;
};
EventHandler.prototype.triggerEventOn = function (trigger, x, y) {
var event = this.getEventOn(trigger, x, y);
if (event) this.handleEvent(event);
};
/**
* Receives an event object and handles the necessary actions
*
......
......@@ -5,7 +5,7 @@ function Game(config) {
Game.instance = this;
Game.achievements = new Achievements();
Game.eventHandler = new EventHandler();
Game.eventHandler = null;
Game.char = null;
Game.cam = null;
}
......@@ -30,6 +30,7 @@ Game.prototype.run = function() {
);
}).style('display', displayEvents);
Game.eventHandler = new EventHandler(svg);
// -------------------------------------
// init view stuff
......@@ -57,9 +58,12 @@ Game.prototype.run = function() {
}, 10);
svg.on("click", function(d) {
Game.char.setMoveTarget(d3.event.pageX, d3.event.pageY);
var matrix = svg[0][0].getScreenCTM();
var x = d3.event.pageX-matrix.e;
var y = d3.event.pageY-matrix.f;
Game.char.setMoveTarget(x, y);
Game.eventHandler.triggerEventOn('click', x, y);
});
});
};
......@@ -3,7 +3,6 @@ function PathFinder(svg) {
this.walkNodes = [];
this.noWalkNodes = [];
this.walkEventNodes = [];
this.raster = null;
......@@ -19,8 +18,6 @@ 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() {
......@@ -30,17 +27,6 @@ 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() {
......@@ -58,8 +44,7 @@ PathFinder.prototype.generateRaster = function() {
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
from: null
};
}
}
......@@ -190,13 +175,4 @@ 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;
};
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment