summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/tankgame/tankitem.cpp
blob: 5506a7ec0be43089d98f8ff2b7388909b69826c0 (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
#include "tankitem.h"

#include <QPainter>
#include <QGraphicsScene>
#include <QDebug>

#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)
    {
        m_reverse = m_distance < 0.0;
    }

    bool apply(qreal timeDelta) 
    {
        qreal dist = timeDelta * item()->speed() * (m_reverse ? -1.0 : 1.0);

        bool done = false;        
        if (qAbs(m_distance) < qAbs(dist)) {            
            done = true;
            dist = m_distance;
        } 
        m_distance -= dist;

        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 !done;
    }

private:
    qreal m_distance;
    bool m_reverse;
};

class TurnAction: public Action
{
public:
    TurnAction(TankItem *item, qreal distance)
        : Action(item), m_distance(distance)
    {
        m_reverse = m_distance < 0.0;
    }

    bool apply(qreal timeDelta) 
    {
        qreal dist = timeDelta * item()->angularSpeed() * (m_reverse ? -1.0 : 1.0);
        bool done = false;        
        if (qAbs(m_distance) < qAbs(dist)) {
            done = true;
            dist = m_distance;
        } 
        m_distance -= dist;

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

private:
    qreal m_distance;
    bool m_reverse;
};

TankItem::TankItem(QObject *parent) 
    : GameItem(parent), m_currentAction(0), m_currentDirection(0.0), m_enabled(true)
{
    connect(this, SIGNAL(cannonFired()), this, SIGNAL(actionCompleted()));
}

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

            QGraphicsItem *item = 0;
            qreal distance = distanceToObstacle(&item);
            if (TankItem *tankItem = qgraphicsitem_cast<TankItem *>(item))
                emit tankSpotted(tankItem->direction(), distance);        
        }
    }
}

void TankItem::hitByRocket()
{
    deleteLater();
}

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

    m_currentAction = newAction;
}

void TankItem::fireCannon()
{
    emit cannonFired();
}

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

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

void TankItem::turn(qreal degrees)
{
    setAction(new TurnAction(this, degrees));
}

void TankItem::turnTo(qreal degrees)
{
    setAction(new TurnAction(this, degrees - direction()));
}

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

QVariant TankItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange && scene()) {
        QPointF requestedPosition = value.toPointF();
        QLineF collidedLine;
        QPointF nextPoint = tryMove(requestedPosition, &collidedLine);
        if (nextPoint != requestedPosition)
            emit collision(collidedLine);
        return nextPoint;
    } 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);

    if (!m_enabled) {
        painter->setPen(QPen(Qt::red, 5));

        painter->drawEllipse(brect);

        QPainterPath path;
        path.addEllipse(brect);
        painter->setClipPath(path);
        painter->drawLine(brect.topRight(), brect.bottomLeft());
    }
}

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)
{
    int fullRotations = int(newDirection) / 360;    
    newDirection -= fullRotations * 360.0;

    qreal diff = newDirection - m_currentDirection;
    m_currentDirection = newDirection;
    rotate(diff);
}

qreal TankItem::distanceToObstacle(QGraphicsItem **obstacle) const
{
    qreal dist = sqrt(pow(scene()->sceneRect().width(), 2) + pow(scene()->sceneRect().height(), 2));

    qreal a = m_currentDirection * M_PI / 180.0;

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

    QPointF requestedPosition = pos() + QPointF(xd, yd);
    QGraphicsItem *collidedItem = 0;
    QPointF nextPosition = tryMove(requestedPosition, 0, &collidedItem);
    if (collidedItem != 0) {
        if (obstacle != 0)
            *obstacle = collidedItem;        
        
        QPointF d = nextPosition - pos();
        return sqrt(pow(d.x(), 2) + pow(d.y(), 2));
    } else {
        return 0.0;
    }
}

qreal TankItem::distanceToObstacle() const
{
    return distanceToObstacle(0);
}