Skip to content
Snippets Groups Projects
Commit 178e73b0 authored by Tim Repke's avatar Tim Repke
Browse files
parents 4fa7c83f c6f6c7eb
No related branches found
No related tags found
No related merge requests found
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -6,6 +6,7 @@ function Char(svg, options) {
this.translation = options.spawn ? options.spawn : this.findSpawn();
this.moveTarget = [];
this.maxSpeed = 2;
this.loaded = false;
var self = this;
d3.xml('chars/bernd.svg', 'image/svg+xml', function(xml) {
......@@ -16,7 +17,10 @@ function Char(svg, options) {
self.image[0][0].appendChild(this);
});
self.initializeAnimations();
self.updatePosition();
self.loaded = true;
});
}
Char.prototype.findSpawn = function() {
......@@ -25,7 +29,82 @@ Char.prototype.findSpawn = function() {
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;
this.animateStep = 0;
this.lastPosition = this.translation.slice();
this.lastDirection = 2;
this.currentFrame = 0;
this.frames = null;
this.animations = {};
this.image.selectAll('g').filter(function() {
return this.getAttribute('inkscape:groupmode') == 'layer';
}).each(function() {
var label = this.getAttribute('inkscape:label');
var element = d3.select(this)
.style('display', 'block');
var frames = [];
element.selectAll('image')
.style('display', 'none')
.each(function() {
frames.push(this);
});
self.animations[label] = frames;
});
}
Char.directionToName = ["UP", "RIGHT", "DOWN", "LEFT"];
Char.prototype.animate = function() {
this.animateStep += 1;
if (this.animateStep <= 8) return;
while (this.animateStep > 8)
this.animateStep -= 8;
var xSpeed = this.translation[0] - this.lastPosition[0];
var ySpeed = - this.translation[1] + this.lastPosition[1];
this.lastPosition = this.translation.slice();
var speed = Math.max(Math.abs(xSpeed), Math.abs(ySpeed)); // estimate
var direction = this.lastDirection;
if (speed > g_smallValue) {
if (Math.abs(xSpeed) >= Math.abs(ySpeed))
direction = (xSpeed >= 0) ? 1 : 3;
else
direction = (ySpeed >= 0) ? 0 : 2;
}
// hide last visible frame
if (this.frames && this.frames[this.currentFrame]) {
var lastFrame = this.frames[this.currentFrame];
lastFrame.style.display = 'none';
}
// change animation
if (direction != this.lastDirection) {
this.lastDirection = direction;
this.currentFrame = 0;
this.frames = this.animations[Char.directionToName[direction]];
}
// if no current frames available show fallback downwards frame
if (!this.frames || this.frames.length == 0)
this.frames = this.animations['DOWN'];
// if everything fails..
if (!this.frames || this.frames.length == 0)
return;
// frames depending on speed
if (speed < g_smallValue)
this.currentFrame = 0; // stand still
else
this.currentFrame += 1; // walk
this.currentFrame %= this.frames.length;
// show current frame
this.frames[this.currentFrame].style.display = 'block';
}
Char.prototype.physics = function() {
if (this.moveTarget && this.moveTarget.length == 0) return;
......@@ -47,7 +126,7 @@ Char.prototype.physics = function() {
if (this.pathFinder.canWalkOn(nextPosition[0], nextPosition[1]))
Vec.assign(this.translation, nextPosition);
else
this.moveTarget.shift();
this.moveTarget.shift();
this.updatePosition();
}
......
......@@ -4,6 +4,7 @@ function Game(config) {
Game.config = config;
Game.instance = this;
}
Game.eventLayers = ['CLICKABLE', 'WALK', 'NOWALK', 'EVENTS'];
Game.prototype.run = function() {
d3.xml('maps/'+Game.config.startMap, 'image/svg+xml', function(xml) {
var gameCanvas = document.getElementById("gameCanvas");
......@@ -11,10 +12,20 @@ Game.prototype.run = function() {
gameCanvas.style.width = Game.config.size[0]+'px';
gameCanvas.style.height = Game.config.size[1]+'px';
gameRoot.appendChild(xml.documentElement);
var svg = d3.select("svg");
var displayEvents = Game.config.showEventLayers ? 'block' : 'none';
svg.selectAll('g').filter(function() {
return (
this.getAttribute('inkscape:groupmode') == 'layer'
&& Game.eventLayers.indexOf(this.getAttribute('inkscape:label')) >= 0
);
}).style('display', displayEvents);
var char = new Char(svg);
var cam = new Camera(svg, char.translation);
// test animation
var ship = svg.select("#shipGroup");
......@@ -26,11 +37,13 @@ Game.prototype.run = function() {
// animate
setInterval(function() {
// move player
char.physics();
char.animate();
// cam movement
cam.movement();
if (char.loaded) {
// move player
char.physics();
char.animate();
// cam movement
cam.movement();
}
}, 10);
svg.on("click", function(d) {
......
......@@ -23,6 +23,7 @@ g_smallValue = 0.000001; // fun with floats
var game = new Game({
startMap: 'map_landing.svg',
showEventLayers: false,
pathFindingGridSize: 5,
usePathFinding: true,
size: [800, 600]
......
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