| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "bridge.hpp" |
| 17 | #include "../contrib/SimpleLogger/src/simple_logger.hpp" |
| 18 | #include "car.hpp" |
| 19 | #include "layers.hpp" |
| 20 | #include "renderer.hpp" |
| 21 | #include "tracktile.hpp" |
| 22 | |
| 23 | #include <MCAssetManager> |
| 24 | #include <MCCollisionEvent> |
| 25 | #include <MCObjectFactory> |
| 26 | #include <MCPhysicsComponent> |
| 27 | #include <MCRectShape> |
| 28 | #include <MCSeparationEvent> |
| 29 | #include <MCSurface> |
| 30 | #include <MCVector2d> |
| 31 | |
| 32 | namespace { |
| 33 | static const float RAIL_Z = 16; |
| 34 | } // namespace |
| 35 | |
| 36 | Bridge::ObjectStatusMap Bridge::m_onBridgeStatusMap; |
| 37 | |
| 38 | Bridge::ObjectStatusMap Bridge::m_underBridgeStatusMap; |
| 39 | |
| 40 | class UnderBridgeTrigger : public MCObject |
| 41 | { |
| 42 | public: |
| 43 | UnderBridgeTrigger(Bridge & bridge) |
| 44 | : MCObject("under_bridge_trigger") |
| 45 | , m_bridge(bridge) |
| 46 | { |
| 47 | setShape(std::make_shared<MCRectShape>(nullptr, TrackTile::width() - TrackTile::width() / 8, TrackTile::height() + TrackTile::height() / 8)); |
| 48 | |
| 49 | setCollisionLayer(-1); |
| 50 | setIsPhysicsObject(false); |
| 51 | setIsTriggerObject(true); |
| 52 | |
| 53 | physicsComponent().setMass(0, true); |
| 54 | } |
| 55 | |
| 56 | //! \reimp |
| 57 | void collisionEvent(MCCollisionEvent & event) override |
| 58 | { |
| 59 | if (!event.collidingObject().physicsComponent().isStationary()) |
| 60 | { |
| 61 | m_bridge.objectUnderBridge(event.collidingObject(), true); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | //! \reimp |
| 66 | void separationEvent(MCSeparationEvent & event) override |
| 67 | { |
| 68 | m_bridge.objectUnderBridge(event.separatedObject(), false); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | Bridge & m_bridge; |
| 73 | }; |
| 74 | |
| 75 | Bridge::Bridge() |
| 76 | : MCObject("bridge") |
| 77 | { |
| 78 | setShape(std::make_shared<MCRectShape>(nullptr, TrackTile::width(), TrackTile::height())); |
| 79 | |
| 80 | setCollisionLayer(-1); |
| 81 | setIsPhysicsObject(false); |
| 82 | setIsTriggerObject(true); |
| 83 | |
| 84 | physicsComponent().setMass(0, true); |
| 85 | |
| 86 | const auto bridgeRailId = "bridge_rail"; |
| 87 | auto && railSurface = MCAssetManager::instance().surfaceManager().surface("wallLong"); |
| 88 | const auto rail0 = std::make_shared<MCObject>(railSurface, bridgeRailId); |
| 89 | const int railYDisplacement = 42 * static_cast<int>(TrackTile::height()) / 100; |
| 90 | addChildObject(rail0, MCVector3dF(0, -railYDisplacement, RAIL_Z)); |
| 91 | const auto rail1 = std::make_shared<MCObject>(railSurface, bridgeRailId); |
| 92 | addChildObject(rail1, MCVector3dF(0, railYDisplacement, RAIL_Z)); |
| 93 | |
| 94 | const auto railLayer = static_cast<int>(Layers::Collision::BridgeRails); |
| 95 | rail0->setCollisionLayer(railLayer); |
| 96 | rail0->physicsComponent().setMass(0, true); |
| 97 | const auto railShader = Renderer::instance().program("defaultSpecular"); |
| 98 | rail0->shape()->view()->setShaderProgram(railShader); |
| 99 | rail1->setCollisionLayer(railLayer); |
| 100 | rail1->physicsComponent().setMass(0, true); |
| 101 | rail1->shape()->view()->setShaderProgram(railShader); |
| 102 | |
| 103 | const auto underBridgeTrigger = std::make_shared<UnderBridgeTrigger>(*this); |
| 104 | addChildObject(underBridgeTrigger, MCVector3dF(0, 0, 0)); |
| 105 | |
| 106 | MCMeshObjectData data("bridge"); |
| 107 | data.setMeshId("bridge"); |
| 108 | data.setSurfaceId("asphalt"); |
| 109 | data.setRestitution(0.9f); |
| 110 | |
| 111 | MCObjectFactory objectFactory(MCAssetManager::instance()); |
| 112 | auto bridgeMeshObject = objectFactory.build(data); |
| 113 | bridgeMeshObject->setIsPhysicsObject(false); |
| 114 | bridgeMeshObject->shape()->view()->setHasShadow(false); |
| 115 | addChildObject(bridgeMeshObject, MCVector3dF(0, 0, -bridgeMeshObject->shape()->view()->object()->minZ())); |
| 116 | } |
| 117 | |
| 118 | void Bridge::raiseObject(MCObject & object, bool raise) |
| 119 | { |
| 120 | const auto x = object.location().i(); |
| 121 | const auto y = object.location().j(); |
| 122 | const auto z = raise ? location().k() + RAIL_Z : 0.0f; |
| 123 | |
| 124 | const MCVector3dF newLocation(x, y, z); |
| 125 | object.translate(newLocation); |
| 126 | const auto s = object.shape()->shadowOffset(); |
| 127 | object.shape()->setShadowOffset(MCVector3dF(s.i(), s.j(), z)); |
| 128 | } |
| 129 | |
| 130 | void Bridge::collisionEvent(MCCollisionEvent & event) |
| 131 | { |
| 132 | if (!event.collidingObject().physicsComponent().isStationary()) |
| 133 | { |
| 134 | const auto object = &event.collidingObject(); |
| 135 | juzzlin::L().debug() << "Object " << object->index() << " collided with bridge " << index(); |
| 136 | if (!Bridge::m_underBridgeStatusMap[object].count(this)) |
| 137 | { |
| 138 | Bridge::m_onBridgeStatusMap[object].insert(this); |
| 139 | juzzlin::L().debug() << "Raising object " << object->index(); |
| 140 | object->setCollisionLayer(static_cast<int>(Layers::Collision::BridgeRails)); |
| 141 | raiseObject(*object, true); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void Bridge::separationEvent(MCSeparationEvent & event) |
| 147 | { |
| 148 | const auto object = &event.separatedObject(); |
| 149 | juzzlin::L().debug() << "Object " << object->index() << " separated with bridge " << index(); |
| 150 | Bridge::m_onBridgeStatusMap[object].erase(this); |
| 151 | if (Bridge::m_onBridgeStatusMap[object].empty()) |
| 152 | { |
| 153 | juzzlin::L().debug() << "Lowering object " << object->index(); |
| 154 | object->setCollisionLayer(0); // MCObject default collision layer |
| 155 | object->physicsComponent().preventSleeping(false); |
| 156 | raiseObject(*object, false); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void Bridge::objectUnderBridge(MCObject & object, bool isUnder) |
| 161 | { |
| 162 | if (isUnder) |
| 163 | { |
| 164 | if (!Bridge::m_onBridgeStatusMap[&object].count(this)) |
| 165 | { |
| 166 | juzzlin::L().debug() << "Object " << object.index() << " under bridge " << index(); |
| 167 | Bridge::m_underBridgeStatusMap[&object].insert(this); |
| 168 | } |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | juzzlin::L().debug() << "Object " << object.index() << " not under bridge " << index(); |
| 173 | Bridge::m_underBridgeStatusMap[&object].erase(this); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void Bridge::reset() |
| 178 | { |
| 179 | Bridge::m_onBridgeStatusMap.clear(); |
| 180 | Bridge::m_underBridgeStatusMap.clear(); |
| 181 | } |