#include "gameitem.h" #include #include GameItem::GameItem(QObject *parent) : QObject(parent) { } QPointF GameItem::tryMove(const QPointF &requestedPosition, QLineF *collidedLine, QGraphicsItem **collidedItem) 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()))); QList itemsInRect = scene()->items(boundingRectPath, Qt::IntersectsItemBoundingRect); QPointF nextPoint = requestedPosition; QRectF sceneRect = scene()->sceneRect(); foreach (QGraphicsItem *item, itemsInRect) { if (item == static_cast(this)) continue; QPolygonF mappedBoundingRect = item->mapToScene(item->boundingRect()); for (int i=0; isceneRect().topLeft(), scene()->sceneRect().bottomLeft()); } if (nextPoint.x() > sceneRect.right()) { nextPoint.rx() = sceneRect.right(); if (collidedLine != 0) *collidedLine = QLineF(scene()->sceneRect().topRight(), scene()->sceneRect().bottomRight()); } if (nextPoint.y() < sceneRect.top()) { nextPoint.ry() = sceneRect.top(); if (collidedLine != 0) *collidedLine = QLineF(scene()->sceneRect().topLeft(), scene()->sceneRect().topRight()); } if (nextPoint.y() > sceneRect.bottom()) { nextPoint.ry() = sceneRect.bottom(); if (collidedLine != 0) *collidedLine = QLineF(scene()->sceneRect().bottomLeft(), scene()->sceneRect().bottomRight()); } return nextPoint; }