2012-04-19 14:35:52 +08:00
|
|
|
#include "Ball.h"
|
|
|
|
#include "Paddle.h"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Ball* Ball::ballWithTexture(CCTexture2D* aTexture)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
Ball* pBall = new Ball();
|
|
|
|
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
|
|
|
{
|
2012-06-11 14:44:32 +08:00
|
|
|
CCSize size = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setPosition( ccpAdd(getPosition(), ccpMult(m_velocity, delta)) );
|
|
|
|
|
2012-06-11 14:44:32 +08:00
|
|
|
if (getPosition().x > size.width - radius())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-11 14:44:32 +08:00
|
|
|
setPosition( ccp( size.width - radius(), getPosition().y) );
|
2012-04-19 14:35:52 +08:00
|
|
|
m_velocity.x *= -1;
|
|
|
|
}
|
|
|
|
else if (getPosition().x < radius())
|
|
|
|
{
|
|
|
|
setPosition( ccp(radius(), getPosition().y) );
|
|
|
|
m_velocity.x *= -1;
|
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ball::collideWithPaddle(Paddle* paddle)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRect paddleRect = paddle->rect();
|
|
|
|
paddleRect.origin.x += paddle->getPosition().x;
|
|
|
|
paddleRect.origin.y += paddle->getPosition().y;
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
float lowY = CCRect::CCRectGetMinY(paddleRect);
|
2012-04-19 14:35:52 +08:00
|
|
|
float midY = CCRect::CCRectGetMidY(paddleRect);
|
|
|
|
float highY = CCRect::CCRectGetMaxY(paddleRect);
|
|
|
|
|
|
|
|
float leftX = CCRect::CCRectGetMinX(paddleRect);
|
|
|
|
float rightX = CCRect::CCRectGetMaxX(paddleRect);
|
|
|
|
|
|
|
|
if (getPosition().x > leftX && getPosition().x < rightX) {
|
|
|
|
|
|
|
|
bool hit = false;
|
|
|
|
float angleOffset = 0.0f;
|
|
|
|
|
|
|
|
if (getPosition().y > midY && getPosition().y <= highY + radius())
|
|
|
|
{
|
|
|
|
setPosition( CCPointMake(getPosition().x, highY + radius()) );
|
|
|
|
hit = true;
|
|
|
|
angleOffset = (float)M_PI / 2;
|
|
|
|
}
|
|
|
|
else if (getPosition().y < midY && getPosition().y >= lowY - radius())
|
|
|
|
{
|
|
|
|
setPosition( CCPointMake(getPosition().x, lowY - radius()) );
|
|
|
|
hit = true;
|
|
|
|
angleOffset = -(float)M_PI / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hit)
|
|
|
|
{
|
|
|
|
float hitAngle = ccpToAngle(ccpSub(paddle->getPosition(), getPosition())) + angleOffset;
|
|
|
|
|
|
|
|
float scalarVelocity = ccpLength(m_velocity) * 1.05f;
|
|
|
|
float velocityAngle = -ccpToAngle(m_velocity) + 0.5f * hitAngle;
|
|
|
|
|
|
|
|
m_velocity = ccpMult(ccpForAngle(velocityAngle), scalarVelocity);
|
|
|
|
}
|
|
|
|
}
|
2010-08-27 11:53:35 +08:00
|
|
|
}
|