-
-
Save Zerophase/13df3cc54195e114239a to your computer and use it in GitHub Desktop.
Interface Example for bounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Ball : MonoBehaviour | |
{ | |
private IBounce bounce; | |
public void Initialize(IBounce bounce) | |
{ | |
this.bounce = bounce; | |
} | |
void Update () | |
{ | |
bounce.BounceOnWall(rigidbody2D); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BounceLogic : IBounce | |
{ | |
private IBounds bounds; | |
private IBall ball; | |
public BounceLogic(IBall ball) | |
{ | |
this.ball = ball; | |
bounds = new BoundsLogic(); | |
} | |
public void BounceOnWall(Rigidbody2D rigidbody2D) | |
{ | |
if ((rigidbody2D.transform.position.x <= bounds.LeftBounds && | |
rigidbody2D.velocity.x < 0) || (rigidbody2D.transform.position.x >= bounds.RightBounds && | |
rigidbody2D.velocity.x > 0)) | |
{ | |
rigidbody2D.velocity = new Vector2(ball.Bounce(rigidbody2D.velocity.x), | |
rigidbody2D.velocity.y); | |
} | |
else if ((rigidbody2D.transform.position.y <= bounds.DownBounds && | |
rigidbody2D.velocity.y < 0) || (rigidbody2D.transform.position.y >= bounds.UpBounds && | |
rigidbody2D.velocity.y > 0)) | |
{ | |
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, | |
ball.Bounce(rigidbody2D.velocity.y)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IBounce | |
{ | |
void BounceOnWall(Rigidbody2D rigidbody2D); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment