-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgameplay_test.cpp
1740 lines (1468 loc) · 56.5 KB
/
gameplay_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gameplay_test.h"
#include "gameplay_ability.h"
#include "gameplay_ability_system.h"
#include "gameplay_attribute.h"
#include "gameplay_effect.h"
#include "gameplay_effect_magnitude.h"
#include "gameplay_tags.h"
#include <core/class_db.h>
#include <core/os/os.h>
#include <scene/main/scene_tree.h>
#include <scene/main/viewport.h>
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
namespace {
constexpr auto delta = 6.0f;
constexpr auto max_health = "max_health";
constexpr auto health = "health";
constexpr auto max_mana = "max_mana";
constexpr auto mana = "mana";
constexpr auto max_stamina = "max_stamina";
constexpr auto stamina = "stamina";
constexpr auto attack = "attack";
constexpr auto defence = "defence";
constexpr auto magic_attack = "magic_attack";
constexpr auto magic_defence = "magic_defence";
constexpr auto agility = "agility";
constexpr auto luck = "luck";
// final_action allows you to ensure something gets run at the end of a scope
template <class F>
class final_action {
public:
explicit final_action(F f) noexcept : f_(std::move(f)) {}
final_action(final_action &&other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_) {
other.invoke_ = false;
}
final_action(const final_action &) = delete;
final_action &operator=(const final_action &) = delete;
final_action &operator=(final_action &&) = delete;
~final_action() noexcept {
if (invoke_) f_();
}
private:
F f_;
bool invoke_{ true };
};
// finally() - convenience function to generate a final_action
template <class F>
final_action<F> finally(const F &f) noexcept {
return final_action<F>(f);
}
template <class F>
final_action<F> finally(F &&f) noexcept {
return final_action<F>(std::forward<F>(f));
}
class TestSceneTree : public SceneTree {
public:
virtual ~TestSceneTree() = default;
virtual void init() override {
SceneTree::init();
}
virtual bool idle(float p_time) override {
bool result = SceneTree::idle(p_time);
get_root()->propagate_notification(Node::NOTIFICATION_INTERNAL_PROCESS);
return result;
}
virtual bool iteration(float p_time) override {
bool result = SceneTree::iteration(p_time);
get_root()->propagate_notification(Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
return result;
}
virtual void finish() override {
SceneTree::finish();
}
};
class TestAttributeSet : public GameplayAttributeSet {
public:
virtual ~TestAttributeSet() = default;
const StringName max_health = "max_health";
const StringName health = "health";
const StringName max_mana = "max_mana";
const StringName mana = "mana";
const StringName max_stamina = "max_stamina";
const StringName stamina = "stamina";
const StringName attack = "attack";
const StringName defence = "defence";
const StringName magic_attack = "magic_attack";
const StringName magic_defence = "magic_defence";
const StringName agility = "agility";
const StringName luck = "luck";
TestAttributeSet() {
add_attribute(max_health, 100);
add_attribute(health, 100);
add_attribute(max_mana, 100);
add_attribute(mana, 100);
add_attribute(max_stamina, 100);
add_attribute(stamina, 100);
add_attribute(attack, 100);
add_attribute(defence, 100);
add_attribute(magic_attack, 100);
add_attribute(magic_defence, 100);
add_attribute(agility, 100);
add_attribute(luck, 100);
}
private:
static void _bind_methods() {}
};
class BaseTestAbility : public GameplayAbility {
GDCLASS(BaseTestAbility, ::GameplayAbility);
public:
Ref<ScalableFloat> const_1 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_5 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_10 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_25 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_50 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_75 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_100 = make_reference<ScalableFloat>();
Ref<ScalableFloat> const_200 = make_reference<ScalableFloat>();
BaseTestAbility() {
const_1->set_value(1);
const_5->set_value(5);
const_10->set_value(10);
const_25->set_value(25);
const_50->set_value(50);
const_75->set_value(75);
const_100->set_value(100);
const_200->set_value(200);
}
virtual ~BaseTestAbility() = default;
protected:
virtual void _on_activate_ability() {}
virtual void _on_end_ability(bool cancelled) {}
virtual void _on_gameplay_event(const Ref<GameplayEvent> &event) {}
virtual bool _can_event_activate_ability(const Ref<GameplayEvent> &event) { return true; }
virtual bool _can_activate_ability(Node *target) { return true; }
virtual void _on_wait_completed(WaitType::Type type, const Variant &data) {}
virtual void _on_wait_interrupted(const Variant &payload = {}) {}
virtual void _on_wait_cancelled(const Variant &payload = {}) {}
private:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("_on_activate_ability"), &BaseTestAbility::_on_activate_ability);
ClassDB::bind_method(D_METHOD("_on_end_ability", "cancelled"), &BaseTestAbility::_on_end_ability);
ClassDB::bind_method(D_METHOD("_on_gameplay_event", "event"), &BaseTestAbility::_on_gameplay_event);
ClassDB::bind_method(D_METHOD("_can_event_activate_ability", "event"), &BaseTestAbility::_can_event_activate_ability);
ClassDB::bind_method(D_METHOD("_can_activate_ability", "target"), &BaseTestAbility::_can_activate_ability);
ClassDB::bind_method(D_METHOD("_on_wait_completed", "type", "data"), &BaseTestAbility::_on_wait_completed);
ClassDB::bind_method(D_METHOD("_on_wait_interrupted", "payload"), &BaseTestAbility::_on_wait_interrupted);
ClassDB::bind_method(D_METHOD("_on_wait_cancelled", "type"), &BaseTestAbility::_on_wait_cancelled);
}
};
class AttackAbility : public BaseTestAbility {
Ref<GameplayEffect> damage_effect = make_reference<GameplayEffect>();
Ref<GameplayEffect> cooldown_effect = make_reference<GameplayEffect>();
Ref<GameplayEffect> cost_effect = make_reference<GameplayEffect>();
public:
bool wait_cancel = false;
bool wait_interrupt = false;
bool wait_for_event = false;
bool wait_for_delay = false;
bool got_cancelled = false;
const StringName event_tag = "event.collision";
AttackAbility() {
// Set damage effect.
Array modifiers;
modifiers.append(make_reference<GameplayEffectModifier>([this](Ref<GameplayEffectModifier> modifier) {
modifier->set_attribute(health);
modifier->set_modifier_operation(ModifierOperation::Subtract);
modifier->set_modifier_magnitude(const_10);
}));
damage_effect->set_modifiers(modifiers);
// Set cost effect.
modifiers = Array();
modifiers.push_back(make_reference<GameplayEffectModifier>([this](Ref<GameplayEffectModifier> modifier) {
modifier->set_attribute(stamina);
modifier->set_modifier_operation(ModifierOperation::Subtract);
modifier->set_modifier_magnitude(const_10);
}));
cost_effect->set_modifiers(modifiers);
set_cost_effect(cost_effect);
// Set cooldown effect.
auto tags = cooldown_effect->get_effect_tags();
tags->append("attack.cooldown");
cooldown_effect->set_duration_type(DurationType::HasDuration);
cooldown_effect->set_duration_magnitude(const_10);
set_cooldown_effect(cooldown_effect);
// Set blocked source tags.
set_source_blocked_tags(make_reference<GameplayTagContainer>([this](Ref<GameplayTagContainer> tags) {
tags->append("attack.blocked");
}));
// Set required source tags.
set_source_required_tags(make_reference<GameplayTagContainer>([this](Ref<GameplayTagContainer> tags) {
tags->append("equipment.weapon");
}));
// Set blocked target tags.
set_target_blocked_tags(make_reference<GameplayTagContainer>([this](Ref<GameplayTagContainer> tags) {
tags->append("attack.immune");
}));
}
virtual ~AttackAbility() = default;
virtual void _on_activate_ability() override {
if (wait_for_event) {
wait_event(event_tag);
} else if (wait_for_delay) {
wait_delay(delta + 0.1);
} else {
apply_effect_on_targets(filter_targets(), damage_effect);
commit_ability();
}
}
virtual void _on_end_ability(bool cancelled) override {
got_cancelled = cancelled;
}
virtual void _on_wait_completed(WaitType::Type type, const Variant &data) override {
if (type == WaitType::Event && data == event_tag) {
apply_effect_on_targets(filter_targets(), damage_effect);
commit_ability();
} else {
end_ability();
}
}
virtual void _on_wait_interrupted(const Variant &payload = {}) override {
wait_interrupt = is_active();
reset_wait_handle();
cancel_ability();
}
virtual void _on_wait_cancelled(const Variant &payload = {}) override {
wait_cancel = is_active();
reset_wait_handle();
cancel_ability();
}
};
class CancellationAbility : public BaseTestAbility {
public:
virtual ~CancellationAbility() = default;
virtual void _on_activate_ability() override {
auto cancel_effect = make_reference<GameplayEffect>();
cancel_effect->get_cancel_ability_tags()->append("ability.*");
apply_effect_on_source(cancel_effect);
commit_ability();
}
};
class ApplyEffectAbility : public BaseTestAbility {
public:
virtual ~ApplyEffectAbility() = default;
Vector<Ref<GameplayEffect> > source_effects;
Vector<Ref<GameplayEffect> > target_effects;
virtual void _on_activate_ability() override {
auto &&targets = filter_targets();
for (auto &&effect : source_effects) {
apply_effect_on_source(effect);
}
for (auto &&effect : target_effects) {
apply_effect_on_targets(targets, effect);
}
commit_ability();
}
};
class TestScriptInstance : public ScriptInstance {
public:
virtual ~TestScriptInstance() = default;
virtual bool set(const StringName &p_name, const Variant &p_value) override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool get(const StringName &p_name, Variant &r_ret) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void get_property_list(List<PropertyInfo> *p_properties) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = NULL) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void get_method_list(List<MethodInfo> *p_list) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool has_method(const StringName &p_method) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual Variant call(const StringName &p_method, VARIANT_ARG_LIST) override {
if (p_method == "_execute") {
if (p_arg4.get_type() == Variant::INT) {
return 10 * static_cast<int64_t>(p_arg4);
}
}
return {};
}
virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void notification(int p_notification) override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual Ref<Script> get_script() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual ScriptLanguage *get_language() override {
throw std::logic_error("The method or operation is not implemented.");
}
};
class TestScript : public Script {
public:
virtual ~TestScript() = default;
virtual bool can_instance() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual Ref<Script> get_base_script() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual StringName get_instance_base_type() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual ScriptInstance *instance_create(Object *p_this) override {
return memnew(TestScriptInstance);
}
virtual bool instance_has(const Object *p_this) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool has_source_code() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual String get_source_code() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void set_source_code(const String &p_code) override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual Error reload(bool p_keep_state = false) override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual MethodInfo get_method_info(const StringName &p_method) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool is_tool() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool is_valid() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual ScriptLanguage *get_language() const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool has_script_signal(const StringName &p_signal) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void get_script_signal_list(List<MethodInfo> *r_signals) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool get_property_default_value(const StringName &p_property, Variant &r_value) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void get_script_method_list(List<MethodInfo> *p_list) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual void get_script_property_list(List<PropertyInfo> *p_list) const override {
throw std::logic_error("The method or operation is not implemented.");
}
virtual bool has_method(const StringName &p_method) const override {
throw std::logic_error("The method or operation is not implemented.");
}
};
} // namespace
#pragma region ability activation
SCENARIO("check ability activation on single target with wait") {
auto scene_tree = make_gameplay_ptr<TestSceneTree>();
GIVEN("attack ability with cost and cooldown which waits") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_event = true;
});
source->add_ability(ability.get());
WHEN("ability activates on valid target but no event is fired") {
source->activate_ability(ability.get());
scene_tree->idle(delta);
THEN("ability is active and waits for collision event") {
CHECK(ability->is_active());
CHECK(ability->get_wait_handle().type == WaitType::Event);
REQUIRE(ability->get_wait_handle().data.get_type() == Variant::STRING);
}
}
WHEN("ability activates on valid target and event is fired") {
auto event = make_reference<GameplayEvent>([&](Ref<GameplayEvent> event) {
event->set_event_tag(ability->event_tag);
});
/** Activate ability and wait for event. */
source->activate_ability(ability.get());
scene_tree->idle(delta);
/** Process event and commit ability. */
ability->process_wait(WaitType::Event, ability->event_tag);
scene_tree->idle(delta);
THEN("ability was committed successfully and on cooldown") {
CHECK(!ability->is_active());
CHECK(ability->get_wait_handle().type != WaitType::Event);
CHECK(ability->get_remaining_cooldown() == 4);
CHECK(source->get_current_attribute_value(stamina) == 90);
REQUIRE(target->get_current_attribute_value(health) == 90);
}
}
WHEN("ability activates on valid target, event is fired and enough time passed") {
auto event = make_reference<GameplayEvent>([&](Ref<GameplayEvent> event) {
event->set_event_tag(ability->event_tag);
});
/** Activate ability and wait for event. */
source->activate_ability(ability.get());
scene_tree->idle(delta);
/** Process event and commit ability. */
ability->process_wait(WaitType::Event, ability->event_tag);
scene_tree->idle(delta);
/** Process one more time so that ability is no longer on cooldown. */
scene_tree->idle(delta);
THEN("ability was committed successfully and is no more on cooldown") {
CHECK(!ability->is_active());
CHECK(ability->get_remaining_cooldown() == 0);
CHECK(source->get_current_attribute_value(stamina) == 90);
REQUIRE(target->get_current_attribute_value(health) == 90);
}
}
}
}
SCENARIO("check ability tag requirements without wait") {
auto scene_tree = make_gameplay_ptr<TestSceneTree>();
GIVEN("attack ability without required source tags") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>();
source->add_ability(ability.get());
WHEN("tries to activate ability") {
source->activate_ability(ability.get());
scene_tree->idle(2 * delta);
THEN("activation was blocked") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(ability->can_activate_ability_on_target(target.get()));
CHECK(target->get_current_attribute_value(stamina) == 100);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
}
GIVEN("target which blocks attack ability") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
target->add_tag("attack.immune");
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>();
source->add_ability(ability.get());
WHEN("ability is activated on target") {
source->activate_ability(ability.get());
scene_tree->idle(delta);
THEN("ability was activated and is on cooldown but target is unaffected") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(ability->get_remaining_cooldown() > 0);
CHECK(!ability->can_activate_ability_on_target(target.get()));
CHECK(source->get_current_attribute_value(stamina) == 90);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
WHEN("ability is activated on target and cooldown period is over") {
source->activate_ability(ability.get());
scene_tree->idle(2 * delta);
THEN("ability is no longer on cooldown but target is unaffected") {
CHECK(!ability->is_active());
CHECK(ability->can_activate_ability());
CHECK(ability->get_remaining_cooldown() == 0);
CHECK(!ability->can_activate_ability_on_target(target.get()));
CHECK(source->get_current_attribute_value(stamina) == 90);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
}
GIVEN("source with blocking tag") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
source->add_tag("attack.blocked");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>();
source->add_ability(ability.get());
WHEN("tries to activate ability") {
source->activate_ability(ability.get());
scene_tree->idle(2 * delta);
THEN("ability activation was blocked") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(ability->can_activate_ability_on_target(target.get()));
CHECK(source->get_current_attribute_value(stamina) == 100);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
}
GIVEN("blocking source and target") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
source->add_tag("attack.blocked");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
target->add_tag("attack.immune");
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>();
source->add_ability(ability.get());
WHEN("tries to activate ability") {
source->activate_ability(ability.get());
scene_tree->idle(2 * delta);
THEN("ability activation was blocked") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(!ability->can_activate_ability_on_target(target.get()));
CHECK(source->get_current_attribute_value(stamina) == 100);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
}
GIVEN("ability which blocks another one") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto executing_ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_event = true;
ability->get_block_ability_tags()->append("ability.attack");
});
auto blocked_ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->get_ability_tags()->append("ability.attack");
});
source->add_ability(executing_ability.get());
source->add_ability(blocked_ability.get());
WHEN("blocking ability executes") {
// Start blocking ability.
source->activate_ability(executing_ability.get());
scene_tree->idle(delta);
THEN("block activation of blocked one") {
CHECK(executing_ability->is_active());
REQUIRE(!blocked_ability->can_activate_ability());
}
}
}
}
SCENARIO("check ability interruption while executing") {
auto scene_tree = make_gameplay_ptr<TestSceneTree>();
GIVEN("executing ability waiting for an event") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_event = true;
});
source->add_ability(ability.get());
WHEN("a different wait handle is called") {
// First activate wait handle.
source->activate_ability(ability.get());
scene_tree->idle(delta);
// Then interrupt it.
ability->wait_delay(delta + 0.1);
scene_tree->idle(delta);
THEN("ability got interrupted and cancelled") {
CHECK(!ability->is_active());
CHECK(ability->get_wait_handle().type == WaitType::None);
REQUIRE(ability->wait_interrupt);
}
}
}
GIVEN("executing ability waiting for a delay") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_delay = true;
});
source->add_ability(ability.get());
WHEN("explicitly call interrupt callback") {
// First activate wait handle.
source->activate_ability(ability.get());
scene_tree->idle(delta);
// Then interrupt it.
ability->_on_wait_interrupted();
THEN("ability got interrupted and cancelled") {
CHECK(!ability->is_active());
CHECK(ability->get_wait_handle().type == WaitType::None);
REQUIRE(ability->wait_interrupt);
}
}
}
}
SCENARIO("check ability cost") {
auto scene_tree = make_gameplay_ptr<TestSceneTree>();
GIVEN("ability with high cost") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
Ref<GameplayEffectModifier> modifier = ability->get_cost_effect()->get_modifiers().front();
modifier->set_modifier_magnitude(ability->const_200);
ability->wait_for_delay = true;
});
source->add_ability(ability.get());
WHEN("ability cost exceeds available resources") {
THEN("ability will not activate") {
REQUIRE(!ability->can_activate_ability());
}
}
WHEN("waiting ability will not activate if cost exceed resources") {
// First activate wait handle.
source->activate_ability(ability.get());
scene_tree->idle(delta);
THEN("ability will not activate") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(source->get_current_attribute_value(stamina) == 100);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
WHEN("instant ability will not execute if cost exceed resources") {
// First activate wait handle.
ability->wait_for_delay = false;
source->activate_ability(ability.get());
scene_tree->idle(delta);
THEN("do not execute ability") {
CHECK(!ability->is_active());
CHECK(!ability->can_activate_ability());
CHECK(source->get_current_attribute_value(stamina) == 100);
REQUIRE(target->get_current_attribute_value(health) == 100);
}
}
}
}
#pragma endregion
#pragma region ability cancellation
SCENARIO("cancel ability mid execution") {
auto scene_tree = make_gameplay_ptr<TestSceneTree>();
GIVEN("ability which executes and another one with cancellation tags") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto executing_ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_event = true;
ability->get_ability_tags()->append("ability.attack");
});
auto cancelling_ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->get_cancel_ability_tags()->append("ability.*");
});
source->add_ability(executing_ability.get());
source->add_ability(cancelling_ability.get());
// Start execution
source->activate_ability(executing_ability.get());
scene_tree->idle(delta);
WHEN("cancelling ability activates") {
// Cancelling ability executes
source->activate_ability(cancelling_ability.get());
scene_tree->idle(delta);
THEN("executing ability got cancelled") {
CHECK(executing_ability->got_cancelled);
REQUIRE(!executing_ability->is_active());
}
}
}
GIVEN("ability which executes and another one with cancellation effects") {
// Scene Tree
auto _ = finally([&scene_tree] { scene_tree->finish(); });
auto root = make_gameplay_ptr<Node>();
scene_tree->call("_change_scene", root.get());
scene_tree->init();
// Source
auto source_attributes = make_reference<TestAttributeSet>();
auto source = make_gameplay_ptr<GameplayAbilitySystem>();
source->set_attribute_set(source_attributes);
source->add_tag("equipment.weapon");
root->add_child(source.get());
// Target
auto target_attributes = make_reference<TestAttributeSet>();
auto target = make_gameplay_ptr<GameplayAbilitySystem>();
target->set_attribute_set(target_attributes);
source->add_target(target.get());
root->add_child(target.get());
// Ability
auto executing_ability = make_gameplay_ptr<AttackAbility>([](AttackAbility *ability) {
ability->wait_for_event = true;
ability->get_ability_tags()->append("ability.attack");
});
auto cancelling_ability = make_gameplay_ptr<CancellationAbility>();
source->add_ability(executing_ability.get());
source->add_ability(cancelling_ability.get());
// Start execution
source->activate_ability(executing_ability.get());
scene_tree->idle(delta);