载入中...
搜索中...
未找到
SkillAction.cpp
浏览该文件的文档.
1#include "rpg/SkillAction.h"
2#include <string>
3#include <utility>
4
5namespace eve::rpg {
6namespace {
7
8eve::Result<eve::LogicalId> skillActionId(const SkillDefinition& skill) {
9 if (skill.id.empty())
11 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "skill id must not be empty", "skill.id"));
12 const auto id = eve::LogicalId::fromParts("rpg", "skill." + skill.id);
13 if (!id)
15 eve::DiagnosticCode::InvalidArgument, "skill id cannot form a valid action logical id", "skill.id"));
17}
18
19eve::Result<eve::Duration> seconds(float value, const char* path) {
20 auto duration = eve::Duration::fromSeconds(static_cast<double>(value));
21 if (!duration) {
22 const eve::Status status = duration.status();
23 (void)status;
25 }
26 if (duration.value().nanoseconds() < 0)
28 eve::DiagnosticCode::InvalidArgument, "skill action duration must be non-negative", path));
29 return duration;
30}
31
32} // namespace
33
35 auto id = skillActionId(skill);
37 auto windup = seconds(skill.castTime, "skill.castTime");
38 if (!windup) return eve::Result<action::ActionDefinition>::failure(windup.status());
39 auto cooldown = seconds(skill.cooldown, "skill.cooldown");
40 if (!cooldown) return eve::Result<action::ActionDefinition>::failure(cooldown.status());
41
42 action::ActionDefinition definition;
43 definition.id = std::move(id).takeValue();
44 definition.timing.windup = std::move(windup).takeValue();
45 definition.condition = skill.castCondition;
46 definition.targetingMode =
48 definition.cost = skill.cost;
49 definition.effectIds = skill.grantedEffects;
50 // A skill is an Active operation even when it currently has no effect id;
51 // the injected executor may emit a settlement/event for that operation.
52 definition.activeExecutionRequired = true;
53 definition.metadata.emplace("cooldown", eve::Value(cooldown.value().seconds()));
54 definition.metadata.emplace("targetType", eve::Value(skill.targetType));
55 definition.metadata.emplace("skillId", eve::Value(skill.id));
56
57 auto valid = definition.validate();
59 return eve::Result<action::ActionDefinition>::success(std::move(definition));
60}
61
63 std::optional<ecs::EntityHandle> source,
64 std::optional<ecs::EntityHandle> target,
65 eve::SimulationTick requestedTick) {
66 auto definition = makeDefinition(skill);
67 if (!definition) return eve::Result<action::ActionRequest>::failure(definition.status());
68 auto checkedDefinition = std::move(definition).takeValue();
69
70 if (checkedDefinition.targetingMode == action::TargetingMode::None && target)
72 eve::DiagnosticCode::InvalidArgument, "self skill request cannot carry an explicit target", "target"));
73 if (checkedDefinition.targetingMode == action::TargetingMode::Explicit && !target)
76 "non-self skill request requires an explicit target", "target"));
77
79 request.actionId = checkedDefinition.id;
80 request.source = std::move(source);
81 request.requestedTick = requestedTick;
82 if (target) request.targetEntities.push_back(*target);
83 auto valid = request.validate(checkedDefinition);
86}
87
89 const SkillDefinition& skill,
90 std::optional<ecs::EntityHandle> source,
91 std::optional<ecs::EntityHandle> target,
92 eve::SimulationTick requestedTick) {
93 auto definition = makeDefinition(skill);
94 if (!definition) return eve::Result<action::ActionExecutionId>::failure(definition.status());
95 auto request = makeRequest(skill, std::move(source), std::move(target), requestedTick);
97 return runtime.submit(std::move(definition).takeValue(), std::move(request).takeValue());
98}
99
100} // namespace eve::rpg
LogicalId target
double value
float duration
const GltfImportRequest & request
wgpu::PopErrorScopeStatus status
bool valid
std::string path
Definition PlayHost.cpp:110
RPG Skill to renderer-independent Action adapter.
const UnitySourceAsset & source
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
static Result< Duration > fromSeconds(double seconds)
Convert finite seconds to the nearest nanosecond.
Definition Time.cpp:16
static std::optional< LogicalId > fromParts(std::string_view namespaceName, std::string_view name)
Builds a logical ID from separate namespace and name components.
Definition Identity.cpp:48
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Structured status and zero or more diagnostics for an operation.
Definition Status.h:68
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Deterministic action lifecycle coordinator.
Definition Action.h:485
static eve::Result< action::ActionRequest > makeRequest(const SkillDefinition &skill, std::optional< ecs::EntityHandle > source=std::nullopt, std::optional< ecs::EntityHandle > target=std::nullopt, eve::SimulationTick requestedTick=eve::SimulationTick::zero())
Build a request for one skill invocation.
static eve::Result< action::ActionExecutionId > submit(action::ActionRuntime &runtime, const SkillDefinition &skill, std::optional< ecs::EntityHandle > source=std::nullopt, std::optional< ecs::EntityHandle > target=std::nullopt, eve::SimulationTick requestedTick=eve::SimulationTick::zero())
Translate and submit one skill to an existing lifecycle owner.
static eve::Result< action::ActionDefinition > makeDefinition(const SkillDefinition &skill)
Build the common definition for one RPG skill.
@ None
The action has no target selection.
@ Explicit
The request already owns the selected ECS target handles.
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
Immutable-by-convention definition of one action kind.
Definition Action.h:90
One caller-owned request to instantiate an action execution.
Definition Action.h:127
LogicalId actionId
Definition identity this request intends to execute.
Definition Action.h:129
SkillDefinition public API.
Definition Skill.h:28
float castTime
0 = 瞬发
Definition Skill.h:31
std::string targetType
Definition Skill.h:32
std::vector< std::string > grantedEffects
命中/释放后施加给目标的效果 id 列表
Definition Skill.h:35
eve::decision::Condition castCondition
Side-effect-free condition tree required before this skill can cast.
Definition Skill.h:38
std::optional< eve::resource::CostSpec > cost
Canonical resource cost; AttributeSet is an adapter, not a second cost model.
Definition Skill.h:34