| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef CHARACTERCONTROLLER_H |
| 5 | #define CHARACTERCONTROLLER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtQuick3DPhysics/qtquick3dphysicsglobal.h> |
| 19 | #include <QtQuick3DPhysics/private/qabstractphysicsbody_p.h> |
| 20 | #include <QtQml/QQmlEngine> |
| 21 | #include <QVector3D> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class Q_QUICK3DPHYSICS_EXPORT QCharacterController : public QAbstractPhysicsBody |
| 26 | { |
| 27 | Q_OBJECT |
| 28 | Q_PROPERTY(QVector3D movement READ movement WRITE setMovement NOTIFY movementChanged) |
| 29 | Q_PROPERTY(QVector3D gravity READ gravity WRITE setGravity NOTIFY gravityChanged) |
| 30 | Q_PROPERTY(bool midAirControl READ midAirControl WRITE setMidAirControl NOTIFY |
| 31 | midAirControlChanged) |
| 32 | Q_PROPERTY(Collisions collisions READ collisions NOTIFY collisionsChanged) |
| 33 | Q_PROPERTY(bool enableShapeHitCallback READ enableShapeHitCallback WRITE |
| 34 | setEnableShapeHitCallback NOTIFY enableShapeHitCallbackChanged) |
| 35 | QML_NAMED_ELEMENT(CharacterController) |
| 36 | public: |
| 37 | QCharacterController(); |
| 38 | |
| 39 | enum class Collision { |
| 40 | None = 0, |
| 41 | Side = 1 << 0, |
| 42 | Up = 1 << 1, |
| 43 | Down = 1 << 2, |
| 44 | }; |
| 45 | Q_DECLARE_FLAGS(Collisions, Collision) |
| 46 | Q_FLAG(Collisions) |
| 47 | |
| 48 | const QVector3D &movement() const; |
| 49 | void setMovement(const QVector3D &newMovement); |
| 50 | const QVector3D &gravity() const; |
| 51 | void setGravity(const QVector3D &newGravity); |
| 52 | QVector3D getDisplacement(float deltaTime); |
| 53 | bool getTeleport(QVector3D &position); |
| 54 | |
| 55 | bool midAirControl() const; |
| 56 | void setMidAirControl(bool newMidAirControl); |
| 57 | |
| 58 | Q_INVOKABLE void teleport(const QVector3D &position); |
| 59 | |
| 60 | const Collisions &collisions() const; |
| 61 | void setCollisions(const Collisions &newCollisions); |
| 62 | QAbstractPhysXNode *createPhysXBackend() final; |
| 63 | |
| 64 | Q_REVISION(6, 6) bool enableShapeHitCallback() const; |
| 65 | Q_REVISION(6, 6) void setEnableShapeHitCallback(bool newEnableShapeHitCallback); |
| 66 | |
| 67 | signals: |
| 68 | void movementChanged(); |
| 69 | void gravityChanged(); |
| 70 | |
| 71 | void midAirControlChanged(); |
| 72 | |
| 73 | void collisionsChanged(); |
| 74 | void enableShapeHitCallbackChanged(); |
| 75 | void shapeHit(QAbstractPhysicsNode *body, const QVector3D &position, const QVector3D &impulse, |
| 76 | const QVector3D &normal); |
| 77 | |
| 78 | private: |
| 79 | QVector3D m_movement; |
| 80 | QVector3D m_gravity; |
| 81 | bool m_midAirControl = true; |
| 82 | |
| 83 | QVector3D m_freeFallVelocity; // actual speed at start of next tick, if free fall |
| 84 | |
| 85 | QVector3D m_teleportPosition; |
| 86 | bool m_teleport = false; |
| 87 | Collisions m_collisions; |
| 88 | bool m_enableShapeHitCallback = false; |
| 89 | }; |
| 90 | |
| 91 | Q_DECLARE_OPERATORS_FOR_FLAGS(QCharacterController::Collisions) |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |
| 95 | #endif // CHARACTERCONTROLLER_H |
| 96 | |