Debian Codegraph @

src/game/crashoverlay.cpp

raw symbols SCIP
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 "crashoverlay.hpp"
17 #include "car.hpp"
18
19 #include <MCAssetManager>
20 #include <MCGLColor>
21 #include <MCSurface>
22
23 CrashOverlay::CrashOverlay()
24 : m_surface(MCAssetManager::instance().surfaceManager().surface("crashOverlay"))
25 , m_alpha(1.0f)
26 , m_car(nullptr)
27 , m_isTriggered(false)
28 {
29 m_surface->material()->setAlphaBlend(true);
30 }
31
32 void CrashOverlay::setDimensions(int width, int height)
33 {
34 OverlayBase::setDimensions(width, height);
35 }
36
37 void CrashOverlay::setCarToFollow(Car & car)
38 {
39 m_car = &car;
40 }
41
42 void CrashOverlay::render()
43 {
44 if (m_isTriggered)
45 {
46 glDisable(GL_DEPTH_TEST);
47
48 const auto halfWidth = width() / 2;
49 const auto halfHeight = height() / 2;
50
51 m_surface->setColor({ 1.0, 1.0, 1.0, m_alpha });
52 m_surface->setSize(width(), height());
53 m_surface->render(nullptr, MCVector3dF(halfWidth, halfHeight, 0), 0);
54 }
55 }
56
57 bool CrashOverlay::update()
58 {
59 if (m_car && m_car->hadHardCrash())
60 {
61 m_isTriggered = true;
62 m_alpha = 0.75f;
63 }
64 else if (m_isTriggered)
65 {
66 m_alpha *= 0.97f;
67 if (m_alpha < 0.01f)
68 {
69 m_isTriggered = false;
70 }
71 }
72
73 return false;
74 }