/// <summary>
/// Is called every time an object is colliding.
/// Help to keep in check the surrounding environment of the block.
/// </summary>
/// <param name="collision"></param>
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player" ) { // Check if the colliding object is the player
player = collision.gameObject.GetComponent("PlayerWalk") as PlayerWalk ; // Get the player Object
direction = player.getLastInput(); // Allow us to know in which direction the player wanted to go
// For each direction we check:
// -if the player was going in this direction
// -if the block has an "solid" object next to him in this direction
// if the two conditions are true then it call fonctBlocDestruction() because the block is blocked
// else if neitheir is true it send the block in the direction the player wanted to
if (direction == "right" && right ){
fonctBlocDestruction();
}
else if (direction == "left" && left){
fonctBlocDestruction();
}
else if (direction == "up" && up){
fonctBlocDestruction();
}
else if (direction == "down" && down){
fonctBlocDestruction();
}
else{
moving = true;
}
}
else if (collision.gameObject.tag == "Enemy" ){ // Check if the colliding object is an ennemy
if (moving == true){ // If the block is moving it destroys the ennemy else it doesn't do anything
fonctCollisionDestruction(collision);
}
}
else { // If it isn't the player or an ennemy ist check if our block is moving, if it's moving it check if it's colliding with a "solid" object
// If it is, the block stop itself Else it continue sliding
fonctCollisionEnter(collision);
if (moving == true)
{
if ((collisionposition.x > position.x) && (collisionposition.y == position.y) && (direction == "right"))
{
direction = null;
moving = false;
target.x = Mathf.Round(transform.position.x);
}
if ((collisionposition.x < position.x) && (collisionposition.y == position.y) && (direction == "left"))
{
direction = null;
moving = false;
target.x = Mathf.Round(transform.position.x);
}
if ((collisionposition.y > position.y) && (collisionposition.x == position.x) && (direction == "up"))
{
direction = null;
moving = false;
target.y = Mathf.Round(transform.position.y);
}
if ((collisionposition.y < position.y) && (collisionposition.x == position.x) && (direction == "down"))
{
direction = null;
moving = false;
target.y = Mathf.Round(transform.position.y);
}
}
}
}