| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #include "damageind.h" |
| 4 | |
| 5 | #include <base/color.h> |
| 6 | #include <base/math.h> |
| 7 | #include <base/vmath.h> |
| 8 | |
| 9 | #include <engine/client.h> |
| 10 | #include <engine/graphics.h> |
| 11 | |
| 12 | #include <generated/client_data.h> |
| 13 | |
| 14 | #include <game/client/gameclient.h> |
| 15 | |
| 16 | #include <algorithm> |
| 17 | |
| 18 | void CDamageInd::Create(vec2 Pos, vec2 Dir, float Alpha) |
| 19 | { |
| 20 | if(m_vItems.size() >= MAX_ITEMS) |
| 21 | return; |
| 22 | |
| 23 | m_vItems.emplace_back(args&: Pos, args: -Dir, args: 0.75f, args: -random_angle(), args: ColorRGBA(1.0f, 1.0f, 1.0f, Alpha)); |
| 24 | } |
| 25 | |
| 26 | void CDamageInd::OnRender() |
| 27 | { |
| 28 | if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK) |
| 29 | return; |
| 30 | |
| 31 | static float s_LastLocalTime = LocalTime(); |
| 32 | const float LifeAdjustment = (LocalTime() - s_LastLocalTime) * GameClient()->GetAnimationPlaybackSpeed(); |
| 33 | s_LastLocalTime = LocalTime(); |
| 34 | |
| 35 | Graphics()->TextureSet(Texture: GameClient()->m_GameSkin.m_aSpriteStars[0]); |
| 36 | |
| 37 | // Get screen edges to avoid rendering offscreen |
| 38 | CScreenRect ScreenRect = Graphics()->GetScreen(); |
| 39 | |
| 40 | // Expand screen slightly so items don't randomly pop in |
| 41 | ScreenRect.Expand(Size: m_ExpandSize); |
| 42 | |
| 43 | for(CItem &Item : m_vItems) |
| 44 | Item.m_RemainingLife -= LifeAdjustment; |
| 45 | |
| 46 | m_vItems.erase(first: std::remove_if(first: m_vItems.begin(), last: m_vItems.end(), |
| 47 | pred: [](const CItem &Item) { return Item.m_RemainingLife <= 0.0f; }), |
| 48 | last: m_vItems.end()); |
| 49 | |
| 50 | for(CItem &Item : m_vItems) |
| 51 | { |
| 52 | vec2 Pos = mix(a: Item.m_Pos + Item.m_Dir * 75.0f, b: Item.m_Pos, amount: std::clamp(val: (Item.m_RemainingLife - 0.60f) / 0.15f, lo: 0.0f, hi: 1.0f)); |
| 53 | if(!ScreenRect.Inside(Position: Pos)) |
| 54 | continue; |
| 55 | |
| 56 | const float LifeAlpha = Item.m_RemainingLife / 0.1f; |
| 57 | Graphics()->SetColor(Item.m_Color.WithMultipliedAlpha(alpha: LifeAlpha)); |
| 58 | Graphics()->QuadsSetRotation(Angle: Item.m_StartAngle + Item.m_RemainingLife * 2.0f); |
| 59 | Graphics()->RenderQuadContainerAsSprite(ContainerIndex: m_DamageIndQuadContainerIndex, QuadOffset: 0, X: Pos.x, Y: Pos.y); |
| 60 | } |
| 61 | |
| 62 | Graphics()->QuadsSetRotation(Angle: 0.0f); |
| 63 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f); |
| 64 | } |
| 65 | |
| 66 | void CDamageInd::OnInit() |
| 67 | { |
| 68 | Graphics()->QuadsSetRotation(Angle: 0.0f); |
| 69 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f); |
| 70 | |
| 71 | m_DamageIndQuadContainerIndex = Graphics()->CreateQuadContainer(AutomaticUpload: false); |
| 72 | float ScaleX, ScaleY; |
| 73 | Graphics()->GetSpriteScale(Id: SPRITE_STAR1, ScaleX, ScaleY); |
| 74 | Graphics()->QuadsSetSubset(TopLeftU: 0.0f, TopLeftV: 0.0f, BottomRightU: 1.0f, BottomRightV: 1.0f); |
| 75 | Graphics()->QuadContainerAddSprite(QuadContainerIndex: m_DamageIndQuadContainerIndex, Width: 48.0f * ScaleX, Height: 48.0f * ScaleY); |
| 76 | Graphics()->QuadContainerUpload(ContainerIndex: m_DamageIndQuadContainerIndex); |
| 77 | |
| 78 | m_ExpandSize = std::max(a: ScaleX, b: ScaleY) * 24.0f; |
| 79 | } |
| 80 | |
| 81 | void CDamageInd::OnReset() |
| 82 | { |
| 83 | m_vItems.clear(); |
| 84 | } |
| 85 | |