Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpRallyPointRenderer.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "CCmpRallyPointRenderer.h" | #include "CCmpRallyPointRenderer.h" | ||||
#include "ICmpIdentity.h" | |||||
#include <boost/algorithm/string.hpp> | |||||
std::string CCmpRallyPointRenderer::GetSchema() | std::string CCmpRallyPointRenderer::GetSchema() | ||||
{ | { | ||||
return | return | ||||
"<a:help>Displays a rally point marker where created units will gather when spawned</a:help>" | "<a:help>Displays a rally point marker where created units will gather when spawned</a:help>" | ||||
"<a:example>" | "<a:example>" | ||||
"<MarkerTemplate>special/rallypoint</MarkerTemplate>" | "<MarkerTemplate>special/rallypoint</MarkerTemplate>" | ||||
"<LineThickness>0.75</LineThickness>" | "<LineThickness>0.75</LineThickness>" | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | void CCmpRallyPointRenderer::UpdateMarkers() | ||||
for (size_t i = 0; i < m_RallyPoints.size(); ++i) | for (size_t i = 0; i < m_RallyPoints.size(); ++i) | ||||
{ | { | ||||
if (i >= m_MarkerEntityIds.size()) | if (i >= m_MarkerEntityIds.size()) | ||||
m_MarkerEntityIds.push_back(INVALID_ENTITY); | m_MarkerEntityIds.push_back(INVALID_ENTITY); | ||||
if (m_MarkerEntityIds[i] == INVALID_ENTITY) | if (m_MarkerEntityIds[i] == INVALID_ENTITY) | ||||
{ | { | ||||
// No marker exists yet, create one first | // No marker exists yet, create one first | ||||
CComponentManager& componentMgr = GetSimContext().GetComponentManager(); | |||||
// Allocate a new entity for the marker | // Allocate a new entity for the marker | ||||
if (!m_MarkerTemplate.empty()) | if (!m_MarkerTemplate.empty()) | ||||
{ | { | ||||
// We should not rely on the Identity component, as the entity may be captured, | |||||
// and the rally point needs to reflect the current player. | |||||
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); | |||||
if (cmpOwnership) | |||||
{ | |||||
player_id_t ownerId = cmpOwnership->GetOwner(); | |||||
if (ownerId != INVALID_PLAYER) | |||||
{ | |||||
CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity()); | |||||
if (cmpPlayerManager) | |||||
{ | |||||
CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(ownerId)); | |||||
if (cmpPlayer) | |||||
boost::replace_all(m_MarkerTemplate, L"{civ}", cmpPlayer->GetCiv()); | |||||
} | |||||
} | |||||
} | |||||
CComponentManager& componentMgr = GetSimContext().GetComponentManager(); | |||||
m_MarkerEntityIds[i] = componentMgr.AllocateNewLocalEntity(); | m_MarkerEntityIds[i] = componentMgr.AllocateNewLocalEntity(); | ||||
if (m_MarkerEntityIds[i] != INVALID_ENTITY) | if (m_MarkerEntityIds[i] != INVALID_ENTITY) | ||||
m_MarkerEntityIds[i] = componentMgr.AddEntity(m_MarkerTemplate, m_MarkerEntityIds[i]); | m_MarkerEntityIds[i] = componentMgr.AddEntity(m_MarkerTemplate, m_MarkerEntityIds[i]); | ||||
} | } | ||||
} | } | ||||
// The marker entity should be valid at this point, otherwise something went wrong trying to allocate it | // The marker entity should be valid at this point, otherwise something went wrong trying to allocate it | ||||
if (m_MarkerEntityIds[i] == INVALID_ENTITY) | if (m_MarkerEntityIds[i] == INVALID_ENTITY) | ||||
LOGERROR("Failed to create rally point marker entity"); | LOGERROR("Failed to create rally point marker entity"); | ||||
CmpPtr<ICmpPosition> markerCmpPosition(GetSimContext(), m_MarkerEntityIds[i]); | CmpPtr<ICmpPosition> markerCmpPosition(GetSimContext(), m_MarkerEntityIds[i]); | ||||
if (markerCmpPosition) | if (markerCmpPosition) | ||||
{ | { | ||||
if (m_Displayed && IsSet()) | if (m_Displayed && IsSet()) | ||||
{ | { | ||||
markerCmpPosition->MoveTo(m_RallyPoints[i].X, m_RallyPoints[i].Y); | markerCmpPosition->MoveTo(m_RallyPoints[i].X, m_RallyPoints[i].Y); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
markerCmpPosition->MoveOutOfWorld(); | markerCmpPosition->MoveOutOfWorld(); | ||||
} | } | ||||
} | } | ||||
// Set rally point flag selection based on player civilization | // Set rally point flag selection based on player civilization | ||||
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); | CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); | ||||
if (!cmpOwnership) | if (!cmpOwnership) | ||||
continue; | continue; | ||||
player_id_t ownerId = cmpOwnership->GetOwner(); | player_id_t ownerId = cmpOwnership->GetOwner(); | ||||
if (ownerId == INVALID_PLAYER || (ownerId == previousOwner && m_LastMarkerCount >= i)) | if (ownerId == INVALID_PLAYER || (ownerId == previousOwner && m_LastMarkerCount >= i)) | ||||
continue; | continue; | ||||
bb: Seems like we request the same component twice here | |||||
Done Inline ActionsWhat do you mean? Stan: What do you mean? | |||||
Done Inline Actionscompare L273 with L232 bb: compare L273 with L232 | |||||
m_LastOwner = ownerId; | m_LastOwner = ownerId; | ||||
CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity()); | |||||
// cmpPlayerManager should not be null as long as this method is called on-demand instead of at Init() time | |||||
// (we can't rely on component initialization order in Init()) | |||||
if (!cmpPlayerManager) | |||||
continue; | |||||
CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(ownerId)); | |||||
if (!cmpPlayer) | |||||
continue; | |||||
CmpPtr<ICmpVisual> cmpVisualActor(GetSimContext(), m_MarkerEntityIds[i]); | |||||
if (cmpVisualActor) | |||||
cmpVisualActor->SetVariant("civ", CStrW(cmpPlayer->GetCiv()).ToUTF8()); | |||||
} | } | ||||
m_LastMarkerCount = m_RallyPoints.size() - 1; | m_LastMarkerCount = m_RallyPoints.size() - 1; | ||||
} | } | ||||
void CCmpRallyPointRenderer::UpdateLineColor() | void CCmpRallyPointRenderer::UpdateLineColor() | ||||
{ | { | ||||
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); | CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle()); | ||||
if (!cmpOwnership) | if (!cmpOwnership) | ||||
▲ Show 20 Lines • Show All 757 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Seems like we request the same component twice here