Skip to content

Instantly share code, notes, and snippets.

@Zerophase
Created July 17, 2014 18:37
Show Gist options
  • Save Zerophase/13df3cc54195e114239a to your computer and use it in GitHub Desktop.
Save Zerophase/13df3cc54195e114239a to your computer and use it in GitHub Desktop.
Interface Example for bounce
public class Ball : MonoBehaviour
{
private IBounce bounce;
public void Initialize(IBounce bounce)
{
this.bounce = bounce;
}
void Update ()
{
bounce.BounceOnWall(rigidbody2D);
}
}
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));
}
}
}
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