My question is there a better way of detaching collisions. I never done a 2D platformer and I already learned a lot of lessons after solving the coordinate comparisons problem with the main character colliding with the block.
I was thinking storing the blocks tiles into a list for a given "game map" class. But then it seems the main character need to go through constantly a list of blocks let say for "24 blocks" which means 24 elements in an array list even though the main character is no where the blocks but is in the game map that contains those blocks.
I love to hear everyone suggestions. I know the below code can be improved. block 1 code is the same code as block 0 code. The difference is the replace block0 name with block1 name and you will see the code is exactly the same.
Code is in Java.
// block 0 code
if(getX() + idleRightAnim.getWidth() > block.getX() - camera.getX()
&& getX() < block.getX() + block.getWidth() - camera.getX()
&& getY() + idleRightAnim.getHeight() == block.getY() - camera.getY()
&& state != ActionState.RUNNING && state != ActionState.ATTACKING)
{
state = ActionState.IDLE;
onSurface = true;
}
if((getX() + idleRightAnim.getWidth() < block.getX() - camera.getX()
|| getX() > block.getX() + block.getWidth() - camera.getX())
&& getY() + idleRightAnim.getHeight() == block.getY() - camera.getY())
{
onSurface = false;
}
// block 1 code
if(getX() + idleRightAnim.getWidth() > block1.getX() - camera.getX()
&& getX() < block1.getX() + block1.getWidth() - camera.getX()
&& getY() + idleRightAnim.getHeight() == block1.getY() - camera.getY()
&& state != ActionState.RUNNING && state != ActionState.ATTACKING)
{
state = ActionState.IDLE;
onSurface = true;
}
if((getX() + idleRightAnim.getWidth() < block1.getX() - camera.getX()
|| getX() > block1.getX() + block1.getWidth() - camera.getX())
&& getY() + idleRightAnim.getHeight() == block1.getY() - camera.getY())
{
onSurface = false;
}