• Create Account

#ActualKhatharr

Posted 14 February 2013 - 04:54 PM

Here's a simplified implementation I threw together in SciTe. It's a bit rough and it's not tested, but I hope it can demonstrate one way of implementing the concept.

//Assume directions match numbers on the numeric keypad: 2 is down, 8 is up, etc.

class Actor {
int update() { //do frame logic
if(pendingMotionFrames == 0) { //no motion in progress
int inputDir = Input.dir4(); //get a direction from the input state
if(inputDir != 5) { //a direction was input
facing = inputDir;
int targetTileX = tileX;
int targetTileY = tileY;
switch(facing) {
case 2: ++targetTileY; break;
case 4: --targetTileX; break;
case 6: ++targetTileX; break;
case 8: --targetTileY; break;
}
if(tileIsPassable(targetTileX, targetTileY)) {
tileX = targetTileX;
tileY = targetTileY;
pendingMotionFrames = FRAMES_FOR_SINGLE_TILE_MOTION;
}
}
}
else { //motion already in progress
--pendingMotionFrames;
updateWalkAnimationFrame(); //select the correct spritesheet frame
int positionOffset = pendingMotionFrames * STEPPING_FRAME_DISTANCE;
sprite.y = tileYPosToScreenYPos(tileY);
sprite.x = tileXPosToScreenXPos(tileX);
switch(facing) {
//in the simulation we already occupy the destination tile, so we position the sprite there
//and then move it backwards based on the position offset. As the frames advance the offset
//shrinks to zero and the sprite moves smoothly to the new tile.
case 2: sprite.y -= positionOffset; break;
case 4: sprite.x += positionOffset; break;
case 6: sprite.x -= positionOffset; break;
case 8: sprite.y += positionOffset; break;
}
}
}
int tileX;
int tileY;
int pendingMotionFrames;
int facing;
Sprite sprite;
}



#1Khatharr

Posted 14 February 2013 - 04:50 PM

Here's a simplified implementation I threw together in SciTe. It's a bit rough and it's not tested, but I hope it can demonstrate one way of implementing the concept.

//Assume directions match numbers on the numeric keypad: 2 is down, 8 is up, etc.

class Actor {
int update() { //do frame logic
if(pendingMotionFrames == 0) { //no motion in progress
int inputDir = Input.dir4(); //get a direction from the input state
if(inputDir != 5) { //a direction was input
facing = inputDir;
int targetTileX = tileX;
int targetTileY = tileY;
switch(facing) {
case 2: ++targetTileY; break;
case 4: --targetTileX; break;
case 6: ++targetTileX; break;
case 8: --targetTileY; break;
}
if(tileIsPassable(targetTileX, targetTileY)) {
tileX = targetTileX;
tileY = targetTileY;
pendingMotionFrames = FRAMES_FOR_SINGLE_TILE_MOTION;
}
}
}
else { //motion already in progress
--pendingMotionFrames;
updateWalkAnimationFrame(); //select the correct spritesheet frame
int positionOffset = pendingMotionFrames * STEPPING_FRAME_DISTANCE;
switch(facing) {
//in the simulation we already occupy the destination tile, so we position the sprite there
//and then move it backwards based on the position offset. As the frames advance the offset
//shrinks to zero and the sprite moves smoothly to the new tile.
case 2: sprite.y = tileYPosToScreenYPos(tileY) - positionOffset; break;
case 4: sprite.x = tileXPosToScreenXPos(tileX) + positionOffset; break;
case 6: sprite.x = tileXPosToScreenXPos(tileX) - positionOffset; break;
case 8: sprite.y = tileYPosToScreenYPos(tileY) + positionOffset; break;
}
}
}
int tileX;
int tileY;
int pendingMotionFrames;
int facing;
}


PARTNERS