LCOV - code coverage report
Current view: top level - simulation/components - TreasureCollector.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 43 52 82.7 %
Date: 2023-04-02 12:52:40 Functions: 7 8 87.5 %

          Line data    Source code
       1             : function TreasureCollector() {}
       2             : 
       3           2 : TreasureCollector.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             : 
      12           2 : TreasureCollector.prototype.Init = function()
      13             : {
      14             : };
      15             : 
      16             : /**
      17             :  * @return {Object} - Min/Max range at which this entity can claim a treasure.
      18             :  */
      19           2 : TreasureCollector.prototype.GetRange = function()
      20             : {
      21           4 :         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             :  */
      28           2 : TreasureCollector.prototype.CanCollect = function(target)
      29             : {
      30           0 :         let cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
      31           0 :         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             :  */
      40           2 : TreasureCollector.prototype.StartCollecting = function(target, callerIID)
      41             : {
      42           6 :         if (this.target)
      43           1 :                 this.StopCollecting();
      44             : 
      45           6 :         let cmpTreasure = Engine.QueryInterface(target, IID_Treasure);
      46           6 :         if (!cmpTreasure || !cmpTreasure.IsAvailable())
      47           1 :                 return false;
      48             : 
      49           5 :         let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
      50           5 :         if (cmpVisual)
      51           0 :                 cmpVisual.SelectAnimation("collecting_treasure", false, 1.0);
      52             : 
      53           5 :         this.target = target;
      54           5 :         this.callerIID = callerIID;
      55             : 
      56             :         // ToDo: Implement rate modifiers.
      57           5 :         let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
      58           5 :         this.timer = cmpTimer.SetTimeout(this.entity, IID_TreasureCollector, "CollectTreasure", cmpTreasure.CollectionTime(), null);
      59             : 
      60           5 :         return true;
      61             : };
      62             : 
      63             : /**
      64             :  * @param {string} reason - The reason why we stopped collecting, used to notify the caller.
      65             :  */
      66           2 : TreasureCollector.prototype.StopCollecting = function(reason)
      67             : {
      68           5 :         if (!this.target)
      69           0 :                 return;
      70             : 
      71           5 :         let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
      72           5 :         cmpTimer.CancelTimer(this.timer);
      73           5 :         delete this.timer;
      74             : 
      75           5 :         delete this.target;
      76             : 
      77           5 :         let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
      78           5 :         if (cmpVisual)
      79           0 :                 cmpVisual.SelectAnimation("idle", false, 1.0);
      80             : 
      81             :         // The callerIID component may start again,
      82             :         // replacing the callerIID, hence save that.
      83           5 :         let callerIID = this.callerIID;
      84           5 :         delete this.callerIID;
      85             : 
      86           5 :         if (reason && callerIID)
      87             :         {
      88           1 :                 let component = Engine.QueryInterface(this.entity, callerIID);
      89           1 :                 if (component)
      90           1 :                         component.ProcessMessage(reason, null);
      91             :         }
      92             : };
      93             : 
      94             : /**
      95             :  * @params - Data and lateness are unused.
      96             :  */
      97           2 : TreasureCollector.prototype.CollectTreasure = function(data, lateness)
      98             : {
      99           4 :         let cmpTreasure = Engine.QueryInterface(this.target, IID_Treasure);
     100           4 :         if (!cmpTreasure || !cmpTreasure.IsAvailable())
     101             :         {
     102           0 :                 this.StopCollecting("TargetInvalidated");
     103           0 :                 return;
     104             :         }
     105             : 
     106           4 :         if (!this.IsTargetInRange(this.target))
     107             :         {
     108           0 :                 this.StopCollecting("OutOfRange");
     109           0 :                 return;
     110             :         }
     111             : 
     112           4 :         cmpTreasure.Reward(this.entity);
     113           4 :         this.StopCollecting("TargetInvalidated");
     114             : };
     115             : 
     116             : /**
     117             :  * @param {number} - The entity ID of the target to check.
     118             :  * @return {boolean} - Whether this entity is in range of its target.
     119             :  */
     120           2 : TreasureCollector.prototype.IsTargetInRange = function(target)
     121             : {
     122           4 :         let range = this.GetRange();
     123           4 :         let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager);
     124           4 :         return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, range.max, false);
     125             : };
     126             : 
     127           2 : Engine.RegisterComponentType(IID_TreasureCollector, "TreasureCollector", TreasureCollector);

Generated by: LCOV version 1.14