2012-04-19 14:35:52 +08:00
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
2012-10-24 16:28:54 +08:00
|
|
|
#include "../VisibleRect.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
Ball::Ball(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Ball::~Ball(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-27 11:53:35 +08:00
|
|
|
float Ball::radius()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return getTexture()->getContentSize().width / 2;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Ball* Ball::ballWithTexture(Texture2D* aTexture)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Ball* pBall = new (std::nothrow) Ball();
|
2012-04-19 14:35:52 +08:00
|
|
|
pBall->initWithTexture(aTexture);
|
|
|
|
pBall->autorelease();
|
2010-08-27 11:53:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pBall;
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void Ball::move(float delta)
|
2010-08-27 11:53:35 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
this->setPosition(getPosition() + _velocity * delta);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-23 17:48:50 +08:00
|
|
|
if (getPosition().x > VisibleRect::right().x - radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
setPosition(VisibleRect::right().x - radius(), getPosition().y);
|
2013-06-15 14:03:30 +08:00
|
|
|
_velocity.x *= -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-10-23 17:48:50 +08:00
|
|
|
else if (getPosition().x < VisibleRect::left().x + radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
setPosition(VisibleRect::left().x + radius(), getPosition().y);
|
2013-06-15 14:03:30 +08:00
|
|
|
_velocity.x *= -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ball::collideWithPaddle(Paddle* paddle)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto paddleRect = paddle->getRect();
|
2012-04-19 14:35:52 +08:00
|
|
|
paddleRect.origin.x += paddle->getPosition().x;
|
|
|
|
paddleRect.origin.y += paddle->getPosition().y;
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float lowY = paddleRect.getMinY();
|
|
|
|
float midY = paddleRect.getMidY();
|
|
|
|
float highY = paddleRect.getMaxY();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float leftX = paddleRect.getMinX();
|
|
|
|
float rightX = paddleRect.getMaxX();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (getPosition().x > leftX && getPosition().x < rightX) {
|
|
|
|
|
|
|
|
bool hit = false;
|
|
|
|
float angleOffset = 0.0f;
|
|
|
|
|
|
|
|
if (getPosition().y > midY && getPosition().y <= highY + radius())
|
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
setPosition(getPosition().x, highY + radius());
|
2012-04-19 14:35:52 +08:00
|
|
|
hit = true;
|
|
|
|
angleOffset = (float)M_PI / 2;
|
|
|
|
}
|
|
|
|
else if (getPosition().y < midY && getPosition().y >= lowY - radius())
|
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
setPosition(getPosition().x, lowY - radius());
|
2012-04-19 14:35:52 +08:00
|
|
|
hit = true;
|
|
|
|
angleOffset = -(float)M_PI / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hit)
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
float hitAngle = (paddle->getPosition() - getPosition()).getAngle() + angleOffset;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-11 16:38:58 +08:00
|
|
|
float scalarVelocity = _velocity.getLength() * 1.05f;
|
|
|
|
float velocityAngle = -_velocity.getAngle() + 0.5f * hitAngle;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_velocity = Vec2::forAngle(velocityAngle) * scalarVelocity;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|