summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/errorstate/tankitem.cpp
blob: 21d4a25da145d03396976e4a92e86861aafa48e3 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "tankitem.h"

#include <QPainter>
#include <QGraphicsScene>

#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

class Action
{
public:
    Action(TankItem *item) : m_item(item)
    {
    }

    TankItem *item() const { return m_item; }
    void setItem(TankItem *item) { m_item = item; }

    virtual bool apply(qreal timeDelta) = 0;

private:
    TankItem *m_item;
};

class MoveAction: public Action
{
public:
    MoveAction(TankItem *item, qreal distance)
        : Action(item), m_distance(distance)
    {
    }

    bool apply(qreal timeDelta) 
    {
        qreal dist = timeDelta * item()->speed();
        m_distance -= dist;
        if (qFuzzyCompare(m_distance, 0.0))
            return false;        

        qreal a = item()->direction() * M_PI / 180.0;

        qreal yd = dist * sin(a);
        qreal xd = dist * sin(M_PI / 2.0 - a);

        item()->setPos(item()->pos() + QPointF(xd, yd));
        return true;
    }

private:
    qreal m_distance;
};

class TurnAction: public Action
{
public:
    TurnAction(TankItem *item, qreal distance)
        : Action(item), m_distance(distance)
    {
    }

    bool apply(qreal timeDelta) 
    {
        qreal dist = timeDelta * item()->angularSpeed();
        m_distance -= dist;
        if (qFuzzyCompare(m_distance, 0.0))
            return false;        

        item()->setDirection(item()->direction() + dist);
        return true;
    }

private:
    qreal m_distance;
};

class FireCannonAction: public Action
{
public:
    FireCannonAction(TankItem *item, qreal distance)
        : Action(item), m_distance(distance)
    {
    }

    bool apply(qreal )
    {
        return false;
    }

private:
    qreal m_distance;
};

TankItem::TankItem(QObject *parent) : Tank(parent), m_currentAction(0), m_currentDirection(0.0)
{
}

void TankItem::idle(qreal elapsed)
{
    if (m_currentAction != 0) {
        if (!m_currentAction->apply(elapsed)) {
            setAction(0);
            emit actionCompleted();
        }
    }
}

void TankItem::setAction(Action *newAction)
{
    if (m_currentAction != 0) 
        delete m_currentAction;

    m_currentAction = newAction;
}

void TankItem::moveForwards(qreal length)
{
    setAction(new MoveAction(this, length));
}

void TankItem::moveBackwards(qreal length)
{
    setAction(new MoveAction(this, -length));
}

void TankItem::turn(qreal newDirection)
{
    setAction(new TurnAction(this, m_currentDirection - newDirection));
}

void TankItem::stop()
{
    setAction(0);
}

void TankItem::fireCannon(qreal distance)
{
    setAction(new FireCannonAction(this, distance));
}

QPointF TankItem::tryMove(const QPointF &requestedPosition) const
{
    QLineF movementPath(pos(), requestedPosition);    

    qreal cannonLength = 0.0;
    {
        QPointF p1 = boundingRect().center();
        QPointF p2 = QPointF(boundingRect().right() + 10.0, p1.y());

        cannonLength = QLineF(mapToScene(p1), mapToScene(p2)).length();
    }

    movementPath.setLength(movementPath.length() + cannonLength);

    QRectF boundingRectPath(QPointF(qMin(movementPath.x1(), movementPath.x2()), qMin(movementPath.y1(), movementPath.y2())),
                            QPointF(qMax(movementPath.x1(), movementPath.x2()), qMax(movementPath.y1(), movementPath.y2())));

    m_brp = mapFromScene(boundingRectPath);

    QList<QGraphicsItem *> itemsInRect = scene()->items(boundingRectPath, Qt::IntersectsItemBoundingRect);

    QPointF nextPoint = requestedPosition;
    QRectF sceneRect = scene()->sceneRect();

    QLineF collidedLine;
    foreach (QGraphicsItem *item, itemsInRect) {
        if (item == static_cast<const QGraphicsItem *>(this))
            continue; 

        QPolygonF mappedBoundingRect = item->mapToScene(item->boundingRect());
        for (int i=0; i<mappedBoundingRect.size(); ++i) {
            QPointF p1 = mappedBoundingRect.at(i == 0 ? mappedBoundingRect.size()-1 : i-1);
            QPointF p2 = mappedBoundingRect.at(i);

            QLineF rectLine(p1, p2);
            QPointF intersectionPoint;
            QLineF::IntersectType intersectType = movementPath.intersect(rectLine, &intersectionPoint);

            if (intersectType == QLineF::BoundedIntersection) {
                movementPath.setP2(intersectionPoint);
                movementPath.setLength(movementPath.length() - cannonLength);
                nextPoint = movementPath.p2();
                collidedLine = rectLine;
            }
        }
    }

    // Don't go outside of map
    if (nextPoint.x() < sceneRect.left())
        nextPoint.rx() = sceneRect.left();
    if (nextPoint.x() > sceneRect.right())
        nextPoint.rx() = sceneRect.right();
    if (nextPoint.y() < sceneRect.top())
        nextPoint.ry() = sceneRect.top();
    if (nextPoint.y() > sceneRect.bottom())
        nextPoint.ry() = sceneRect.bottom();

    if (nextPoint != requestedPosition) {
        emit collision(collidedLine);        
    }

    return nextPoint;
}

QVariant TankItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange && scene())
        return tryMove(value.toPointF());
    else 
        return QGraphicsItem::itemChange(change, value);
}


void TankItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QRectF brect = boundingRect();

    painter->setBrush(m_color);
    painter->setPen(Qt::black);

    // body
    painter->drawRect(brect.adjusted(0.0, 4.0, -2.0, -4.0));

    // cannon
    QRectF cannonBase = brect.adjusted(10.0, 6.0, -12.0, -6.0);
    painter->drawEllipse(cannonBase);

    painter->drawRect(QRectF(QPointF(cannonBase.center().x(), cannonBase.center().y() - 2.0), 
                             QPointF(brect.right(), cannonBase.center().y() + 2.0)));
    
    // left track
    painter->setBrush(QBrush(Qt::black, Qt::VerPattern));
    QRectF leftTrackRect = QRectF(brect.topLeft(), QPointF(brect.right() - 2.0, brect.top() + 4.0));
    painter->fillRect(leftTrackRect, Qt::darkYellow);
    painter->drawRect(leftTrackRect);

    // right track
    QRectF rightTrackRect = QRectF(QPointF(brect.left(), brect.bottom() - 4.0),
                                   QPointF(brect.right() - 2.0, brect.bottom()));
    painter->fillRect(rightTrackRect, Qt::darkYellow);
    painter->drawRect(rightTrackRect);

    painter->setBrush(QColor::fromRgb(255, 0, 0, 128));
    painter->drawPolygon(m_brp);

}

QRectF TankItem::boundingRect() const
{
    return QRectF(-20.0, -10.0, 40.0, 20.0);
}

qreal TankItem::direction() const
{
    return m_currentDirection;
}

void TankItem::setDirection(qreal newDirection)
{
    m_currentDirection = newDirection;
    rotate(newDirection);
}

qreal TankItem::distanceToObstacle() const
{
    // ###

    return 0.0;
}