Line data Source code
1 : function Treasure() {}
2 :
3 2 : Treasure.prototype.Schema =
4 : "<a:help>Provides a bonus when taken. E.g. a supply of resources.</a:help>" +
5 : "<a:example>" +
6 : "<CollectTime>1000</CollectTime>" +
7 : "<Resources>" +
8 : "<Food>1000</Food>" +
9 : "</Resources>" +
10 : "</a:example>" +
11 : "<element name='CollectTime' a:help='Amount of milliseconds that it takes to collect this treasure.'>" +
12 : "<ref name='nonNegativeDecimal'/>" +
13 : "</element>" +
14 : "<optional>" +
15 : "<element name='Resources' a:help='Amount of resources that are in this.'>" +
16 : Resources.BuildSchema("positiveDecimal") +
17 : "</element>" +
18 : "</optional>";
19 :
20 2 : Treasure.prototype.Init = function()
21 : {
22 : };
23 :
24 2 : Treasure.prototype.ComputeReward = function()
25 : {
26 2 : for (const resource in this.template.Resources)
27 : {
28 2 : const amount = ApplyValueModificationsToEntity("Treasure/Resources/" + resource, this.template.Resources[resource], this.entity);
29 2 : if (!amount)
30 0 : continue;
31 2 : if (!this.resources)
32 2 : this.resources = {};
33 2 : this.resources[resource] = amount;
34 : }
35 : };
36 :
37 : /**
38 : * @return {Object} - The resources given by this treasure.
39 : */
40 2 : Treasure.prototype.Resources = function()
41 : {
42 0 : return this.resources || {};
43 : };
44 :
45 : /**
46 : * @return {number} - The time in milliseconds it takes to collect this treasure.
47 : */
48 2 : Treasure.prototype.CollectionTime = function()
49 : {
50 1 : return +this.template.CollectTime;
51 : };
52 :
53 : /**
54 : * @param {number} entity - The entity collecting us.
55 : * @return {boolean} - Whether the reward was granted.
56 : */
57 2 : Treasure.prototype.Reward = function(entity)
58 : {
59 4 : if (this.isTaken)
60 1 : return false;
61 :
62 3 : const cmpPlayer = QueryOwnerInterface(entity);
63 3 : if (!cmpPlayer)
64 1 : return false;
65 :
66 2 : const cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
67 2 : if (cmpFogging)
68 0 : cmpFogging.Activate();
69 :
70 2 : if (this.resources)
71 2 : cmpPlayer.AddResources(this.resources);
72 :
73 2 : const cmpStatisticsTracker = QueryOwnerInterface(entity, IID_StatisticsTracker);
74 2 : if (cmpStatisticsTracker)
75 0 : cmpStatisticsTracker.IncreaseTreasuresCollectedCounter();
76 :
77 2 : const cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
78 2 : cmpTrigger.CallEvent("OnTreasureCollected", {
79 : "player": cmpPlayer.GetPlayerID(),
80 : "treasure": this.entity
81 : });
82 :
83 2 : this.isTaken = true;
84 2 : Engine.DestroyEntity(this.entity);
85 2 : return true;
86 : };
87 :
88 : /**
89 : * We might live long enough for a collecting entity
90 : * to find us again after taking us.
91 : * @return {boolean} - Whether we are taken already.
92 : */
93 2 : Treasure.prototype.IsAvailable = function()
94 : {
95 2 : return !this.isTaken;
96 : };
97 :
98 2 : Treasure.prototype.OnOwnershipChanged = function(msg)
99 : {
100 2 : if (msg.to != INVALID_PLAYER)
101 2 : this.ComputeReward();
102 : };
103 :
104 2 : Treasure.prototype.OnValueModification = function(msg)
105 : {
106 0 : if (msg.component != "Treasure")
107 0 : return;
108 0 : this.ComputeReward();
109 : };
110 :
111 2 : Engine.RegisterComponentType(IID_Treasure, "Treasure", Treasure);
|