Not anymore
Notice lack of moving flag? Was causing 1 loop with no movement - hence a slight "staggered" look as you crossed tile bounds.
Hows this work for ya?
#include "DarkGDK.h"
#define SPEED 2;
bool check_collision( int Sprite, int Target );
void DarkGDK ( void )
{
dbSetDir("D:/files/code/VCX/TestApp"); // this is for on my machine - force correct loc of PNG :)
enum status {LEFT,RIGHT,UP,DOWN}; status picture=LEFT;
int sprite_image=1;
//--------------------------------------------------------------------------
int PICleft=1; // I didn't use the defines here cuz these could change :)
int PICright=2; // You knew that ...it looks like - You might have a ton
int PICup=3; // of diffeent images - walking animation - explosions -
int PICdown=4; // idle breathing....etc. Constants Would Work Here maybe to.
// Like:
// #define SPRITE_PLAYER_UP 3
//--------------------------------------------------------------------------
bool collision=false;
int x=40,y=40,oldx=x, oldy=y;
bool bXMod=false;
bool bYMod=false;
dbSyncOn ( );
dbSyncRate ( 60 );
dbLoadImage("trupright.png",PICright,1);
dbLoadImage("trupleft.png",PICleft,1);
dbLoadImage("trupup.png",PICup,1);
dbLoadImage("trupdown.png",PICdown,1);
dbLoadImage("enemy.png",34,1);
while ( LoopGDK ( ) ){
dbSprite(2,120,120,34);// THE "ENEMY" SPRITE OR THE WALL
/////////////////////////////////////////////////////////////////////////////////////////
///////////////// THIS IS WHERE THE INPUT IS ROCCESED //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
oldx=x;oldy=y;// Preserve where we ARE NOW
bXMod=(x%40==0);
bYMod=(y%40==0);
if(bXMod && bYMod){
if ( dbLeftKey() ){ //left
picture=LEFT; sprite_image=PICleft;x-=SPEED;
}else{
if ( dbRightKey() ){ //right
picture=RIGHT; sprite_image=PICright;x+=SPEED;
}else{
if( dbUpKey() ){ //UP
picture=UP;sprite_image=PICup;y-=SPEED;
}else{
if( dbDownKey() ){//down
picture=DOWN;sprite_image=PICdown;y+=SPEED;
};
};
};
};
};
if(!(bXMod && bYMod)){
switch(picture){
case LEFT:if(x%40!=0){x-=SPEED};break;
case RIGHT:if(x%40!=0){x+=SPEED};break;
case UP:if(y%40!=0){y-=SPEED};break;
case DOWN:if(y%40!=0){y+=SPEED};break;
};
};
/////////////////////////////////////////////////////////////////////////////////////
// OK We just determined the "player" WANTS to move. So We FACED that direction
// VIA the various Sprite refercnes above. Note How We Set the Moving Flag?
// that is the ONLY "FLAG" you need - none of that bool left , right etc
//.. In short - you're jiggle is your response stuff trying to move the Sprite
// back. Much Easier to save WHERE the Sprite is - Move it - check for collide
// and if no collide do nothing - otherwise just force the sprite back where is was
// originally - this way - no trying to calculate walking backwards - just "reset"
// to last position :)
/////////////////////////////////////////////////////////////////////////////////////
dbSprite(1,x,y,sprite_image);//THE SPRITE WE CONTROLL
if(check_collision(1, 2 )){
x=oldx;y=oldy;
dbSprite(1,x,y,sprite_image);//THE SPRITE WE CONTROLL
};
dbSync();
};
};
//////////////////////////////////////////////////////
//THE COLLISION FUNCTION
///////////////////////////////////////////////////
bool check_collision( int Sprite, int Target ){
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
leftA = dbSpriteX ( Sprite );
rightA = leftA + 40; //// THE WIDTH AND HEIGHT OF EVERY SPRITE IS 40
topA = dbSpriteY ( Sprite );
bottomA = topA + 40;
leftB = dbSpriteX ( Target );
rightB = leftB + 40;
topB = dbSpriteY ( Target );
bottomB = topB + 40;
if( bottomA <= topB )
{
return false;
}
if( topA >= bottomB )
{
return false;
}
if( rightA <= leftB )
{
return false;
}
if( leftA >= rightB )
{
return false;
}
return true;
};