-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractplank.cpp
95 lines (83 loc) · 2 KB
/
abstractplank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "abstractplank.h"
#include"bordersquare.h"
#include<QGraphicsScene>
#include<QTimer>
AbstractPlank::AbstractPlank(QGraphicsItem* parent,qreal movingSpeed):
QGraphicsObject(parent),
timer(new QTimer(this)),
movingState(notMove),
collidingEffect{0.0,0.0}
{
timer->setInterval(movingSpeed);
this->connect(timer,SIGNAL(timeout()),this,SLOT(movingForward()));
timer->start();
}
AbstractPlank::~AbstractPlank()
{
}
QPainterPath AbstractPlank::shape() const
{
QPainterPath path;
path.addRect(this->boundingRect());
return path;
}
bool AbstractPlank::isMovingState(AbstractPlank::MovingState state)
{
return state==this->movingState;
}
void AbstractPlank::setMovingState(AbstractPlank::MovingState state)
{
this->movingState=state;
}
void AbstractPlank::collidingWithBall(QGraphicsItem* sender)
{
emit setOffset(this->collidingEffect.x(),this->collidingEffect.y(),sender);
}
void AbstractPlank::startToMove()
{
if(!this->timer->isActive())
{
this->timer->start();
}
}
void AbstractPlank::stopMoving()
{
if(this->timer->isActive())
{
this->timer->stop();
}
}
void AbstractPlank::movingForward()
{
static const qreal movingOffset=2.0;
static const qreal effect=1.0;
if(this->movingState==notMove)
{
this->collidingEffect/=2;
if(collidingEffect.x()<0.3)
{
collidingEffect.rx()=0;
}
return;
}
const qreal xOff=this->boundingRect().width()/2;
auto oldPos=this->pos();
switch (movingState) {
case moveToLeft:
if(!dynamic_cast<BorderSquare*>(this->scene()->itemAt(oldPos+QPointF{-xOff-movingOffset,0},this->transform())))
{
this->collidingEffect={-effect,0};
this->setPos(oldPos+QPointF(-movingOffset,0));
}
break;
case moveToRight:
if(!dynamic_cast<BorderSquare*>(this->scene()->itemAt(oldPos+QPointF{xOff+movingOffset,0},this->transform())))
{
this->collidingEffect={effect,0};
this->setPos(oldPos+QPointF(movingOffset,0));
}
break;
default:
break;
}
}