PuzzleSDK
Ball.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "Ball.h"
26#include "Paddle.h"
27#include "../VisibleRect.h"
28
30
32{
33}
34
36{
37}
38
40{
41 return getTexture()->getContentSize().width / 2;
42}
43
44Ball* Ball::ballWithTexture(Texture2D* aTexture)
45{
46 Ball* pBall = new (std::nothrow) Ball();
47 pBall->initWithTexture(aTexture);
48 pBall->autorelease();
49
50 return pBall;
51}
52
53void Ball::move(float delta)
54{
55 this->setPosition(getPosition() + _velocity * delta);
56
57 if (getPosition().x > VisibleRect::right().x - radius())
58 {
59 setPosition(VisibleRect::right().x - radius(), getPosition().y);
60 _velocity.x *= -1;
61 }
62 else if (getPosition().x < VisibleRect::left().x + radius())
63 {
64 setPosition(VisibleRect::left().x + radius(), getPosition().y);
65 _velocity.x *= -1;
66 }
67}
68
70{
71 auto paddleRect = paddle->getRect();
72 paddleRect.origin.x += paddle->getPosition().x;
73 paddleRect.origin.y += paddle->getPosition().y;
74
75 float lowY = paddleRect.getMinY();
76 float midY = paddleRect.getMidY();
77 float highY = paddleRect.getMaxY();
78
79 float leftX = paddleRect.getMinX();
80 float rightX = paddleRect.getMaxX();
81
82 if (getPosition().x > leftX && getPosition().x < rightX) {
83
84 bool hit = false;
85 float angleOffset = 0.0f;
86
87 if (getPosition().y > midY && getPosition().y <= highY + radius())
88 {
89 setPosition(getPosition().x, highY + radius());
90 hit = true;
91 angleOffset = (float)M_PI / 2;
92 }
93 else if (getPosition().y < midY && getPosition().y >= lowY - radius())
94 {
95 setPosition(getPosition().x, lowY - radius());
96 hit = true;
97 angleOffset = -(float)M_PI / 2;
98 }
99
100 if (hit)
101 {
102 float hitAngle = (paddle->getPosition() - getPosition()).getAngle() + angleOffset;
103
104 float scalarVelocity = _velocity.getLength() * 1.05f;
105 float velocityAngle = -_velocity.getAngle() + 0.5f * hitAngle;
106
107 _velocity = Vec2::forAngle(velocityAngle) * scalarVelocity;
108 }
109 }
110}
USING_NS_CC
Definition: Ball.cpp:29
Definition: Ball.h:33
void move(float delta)
Definition: Ball.cpp:53
virtual ~Ball()
Definition: Ball.cpp:35
Ball()
Definition: Ball.cpp:31
void collideWithPaddle(Paddle *paddle)
Definition: Ball.cpp:69
float radius()
Definition: Ball.cpp:39
cocos2d::Vec2 _velocity
Definition: Ball.h:34
static Ball * ballWithTexture(cocos2d::Texture2D *aTexture)
Definition: Ball.cpp:44
Definition: Paddle.h:38
cocos2d::Rect getRect()
Definition: Paddle.cpp:37
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45
static cocos2d::Vec2 right()
Definition: VisibleRect.cpp:51