| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2013 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 "pit.hpp" |
| 17 | #include "car.hpp" |
| 18 | |
| 19 | #include <MCCollisionEvent> |
| 20 | #include <MCPhysicsComponent> |
| 21 | #include <MCSeparationEvent> |
| 22 | #include <MCShape> |
| 23 | #include <MCShapeView> |
| 24 | #include <MCSurface> |
| 25 | |
| 26 | Pit::Pit(MCSurfacePtr surface) |
| 27 | : MCObject(surface, "pit") |
| 28 | { |
| 29 | physicsComponent().setMass(1, true); // Stationary |
| 30 | setIsPhysicsObject(false); |
| 31 | setIsTriggerObject(true); |
| 32 | shape()->view()->setHasShadow(false); |
| 33 | } |
| 34 | |
| 35 | void Pit::reset() |
| 36 | { |
| 37 | m_possiblyPittingCars.clear(); |
| 38 | } |
| 39 | |
| 40 | void Pit::collisionEvent(MCCollisionEvent & event) |
| 41 | { |
| 42 | // Cache type id integers. |
| 43 | static const auto carType = MCObject::typeId("car"); |
| 44 | if (event.collidingObject().typeId() == carType) |
| 45 | { |
| 46 | if (Car & car = static_cast<Car &>(event.collidingObject()); car.isHuman()) |
| 47 | { |
| 48 | m_possiblyPittingCars.insert(&car); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void Pit::separationEvent(MCSeparationEvent & event) |
| 54 | { |
| 55 | // Cache type id integers. |
| 56 | static const auto carType = MCObject::typeId("car"); |
| 57 | if (event.separatedObject().typeId() == carType) |
| 58 | { |
| 59 | if (Car & car = static_cast<Car &>(event.separatedObject()); car.isHuman()) |
| 60 | { |
| 61 | m_possiblyPittingCars.erase(&car); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void Pit::onStepTime(int) |
| 67 | { |
| 68 | auto iter = m_possiblyPittingCars.begin(); |
| 69 | while (iter != m_possiblyPittingCars.end()) |
| 70 | { |
| 71 | if ((*iter)->speedInKmh() < 25) |
| 72 | { |
| 73 | emit pitStop(**iter); |
| 74 | |
| 75 | iter = m_possiblyPittingCars.erase(iter); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | iter++; |
| 80 | } |
| 81 | } |
| 82 | } |