Gio,
I wasn't happy with the way mouse following was working in my miner 49er game. After some fiddling, I came up with a solution I liked. I have always thought the wade.setCameraTarget() was a handy function in that you could set inertia and offset to follow a scene object around. The problem with it was that you could scroll off the game area. Before, you had shared some code that bounds the camera to the game area. I thought, why not combine those two pieces of code to create a "bounded" camera target. It seems to work as advertised, here it is:
this.setBoundedCameraTarget = function(target, inertia, offset) {
if (!target)
{
wade.setMainLoop(0, '_wade_cameraTarget');
return;
}
inertia = inertia || 0;
offset = offset || {x:0, y:0};
wade.setMainLoop(function()
{
if (target.isInScene())
{
var targetPos = target.getPosition();
var cameraPos = wade.getCameraPosition();
targetPos.x += offset.x;
targetPos.y += offset.y;
targetPos.z = cameraPos.z;
if (inertia)
{
var actualPos = {x: targetPos.x * (1 - inertia) + cameraPos.x * inertia,
y: targetPos.y * (1 - inertia) + cameraPos.y * inertia,
z: targetPos.z * (1 - inertia) + cameraPos.z * inertia};
var dx = actualPos.x - targetPos.x;
var dy = actualPos.y - targetPos.y;
var dz = actualPos.z - targetPos.z;
if (dx*dx + dy*dy + dz*dz > inertia * inertia)
{
targetPos = actualPos;
}
}
wade.setCameraPosition(targetPos);
var newCameraPosition = wade.getCameraPosition();
var screenBox = {minX: -wade.getScreenWidth()/2, maxX: wade.getScreenWidth() / 2,
minY: -wade.getScreenHeight() / 2, maxY: wade.getScreenHeight() / 2};
var screenInWorldSpace = wade.screenBoxToWorld(wade.app.GAME_LAYER, screenBox);
if (screenInWorldSpace.minX < mapBB.minX) {
newCameraPosition.x += mapBB.minX - screenInWorldSpace.minX;
}
if (screenInWorldSpace.minY < mapBB.minY) {
newCameraPosition.y += mapBB.minY - screenInWorldSpace.minY;
}
if (screenInWorldSpace.maxX > mapBB.maxX) {
newCameraPosition.x += mapBB.maxX - screenInWorldSpace.maxX;
}
if (screenInWorldSpace.maxY > mapBB.maxY) {
newCameraPosition.y += mapBB.maxY - screenInWorldSpace.maxY;
}
wade.setCameraPosition(newCameraPosition);
}
}, '_wade_cameraTarget');
}; // end setBoundedCameraTarget
You would have to generalize some of the code (bounding box and layer stuff), but that shouldn't be too hard. I'm not sure if there is already something like this in wade that I am unaware of ? Anyway, just a suggestion.
cheers - shri