source: ps/trunk/binaries/data/mods/public/simulation/components/TreasureCollecter.js@ 25206

Last change on this file since 25206 was 25206, checked in by Freagarach, 3 years ago

Gather using ResourceGatherer instead of UnitAI.

Moves the gathering logic from UnitAI to ResourceGatherer.
Makes it easier for modders to change gathering behaviour, e.g. letting structures gather.
Refs. #4293 by optimising a bit.

Differential revision: D2662
Comments by: @bb, @Stan, @wraitii

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 3.5 KB
Line 
1function TreasureCollecter() {}
2
3TreasureCollecter.prototype.Schema =
4 "<a:help>Defines the treasure collecting abilities.</a:help>" +
5 "<a:example>" +
6 "<MaxDistance>2.0</MaxDistance>" +
7 "</a:example>" +
8 "<element name='MaxDistance' a:help='The maximum treasure taking distance in m.'>" +
9 "<ref name='positiveDecimal'/>" +
10 "</element>";
11
12TreasureCollecter.prototype.Init = function()
13{
14};
15
16/**
17 * @return {Object} - Min/Max range at which this entity can claim a treasure.
18 */
19TreasureCollecter.prototype.GetRange = function()
20{
21 return { "min": 0, "max": +this.template.MaxDistance };
22};
23
24/**
25 * @param {number} target - Entity ID of the target.
26 * @return {boolean} - Whether we can collect from the target.
27 */
28TreasureCollecter.prototype.CanCollect = function(target)
29{
30 let cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
31 return cmpTreasure && cmpTreasure.IsAvailable();
32};
33
34/**
35 * @param {number} target - The target to collect.
36 * @param {number} callerIID - The IID to notify on specific events.
37 *
38 * @return {boolean} - Whether we started collecting.
39 */
40TreasureCollecter.prototype.StartCollecting = function(target, callerIID)
41{
42 if (this.target)
43 this.StopCollecting();
44
45 let cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
46 if (!cmpTreasure || !cmpTreasure.IsAvailable())
47 return false;
48
49 let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
50 if (cmpVisual)
51 cmpVisual.SelectAnimation("collecting_treasure", false, 1.0);
52
53 this.target = target;
54 this.callerIID = callerIID;
55
56 // ToDo: Implement rate modifiers.
57 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
58 this.timer = cmpTimer.SetTimeout(this.entity, IID_TreasureCollecter, "CollectTreasure", cmpTreasure.CollectionTime(), null);
59
60 return true;
61};
62
63/**
64 * @param {string} reason - The reason why we stopped collecting, used to notify the caller.
65 */
66TreasureCollecter.prototype.StopCollecting = function(reason)
67{
68 if (this.timer)
69 {
70 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
71 cmpTimer.CancelTimer(this.timer);
72 delete this.timer;
73 }
74 delete this.target;
75
76 let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
77 if (cmpVisual)
78 cmpVisual.SelectAnimation("idle", false, 1.0);
79
80 // The callerIID component may start collecting again,
81 // replacing the callerIID, hence save that.
82 let callerIID = this.callerIID;
83 delete this.callerIID;
84
85 if (reason && callerIID)
86 {
87 let component = Engine.QueryInterface(this.entity, callerIID);
88 if (component)
89 component.ProcessMessage(reason, null);
90 }
91};
92
93/**
94 * @params - Data and lateness are unused.
95 */
96TreasureCollecter.prototype.CollectTreasure = function(data, lateness)
97{
98 let cmpTreasure = Engine.QueryInterface(this.target, IID_Treasure);
99 if (!cmpTreasure || !cmpTreasure.IsAvailable())
100 {
101 this.StopCollecting("TargetInvalidated");
102 return;
103 }
104
105 if (!this.IsTargetInRange(this.target))
106 {
107 this.StopCollecting("OutOfRange");
108 return;
109 }
110
111 cmpTreasure.Reward(this.entity);
112 this.StopCollecting("TargetInvalidated");
113};
114
115/**
116 * @param {number} - The entity ID of the target to check.
117 * @return {boolean} - Whether this entity is in range of its target.
118 */
119TreasureCollecter.prototype.IsTargetInRange = function(target)
120{
121 let range = this.GetRange();
122 let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager);
123 return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, range.max, false);
124};
125
126Engine.RegisterComponentType(IID_TreasureCollecter, "TreasureCollecter", TreasureCollecter);
Note: See TracBrowser for help on using the repository browser.