Advertisement
Guest User

Untitled

a guest
Nov 29th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function AttackWarning() {}
  2.  
  3. AttackWarning.prototype.Schema =
  4.     "<a:component type='system'/><empty/>";
  5.  
  6. AttackWarning.prototype.Init = function()
  7. {
  8.     this.suppressionRange = 150;
  9.     this.suppressionSeconds = 40;
  10.     // Other attacks within this time and distance will not trigger the warning
  11.    
  12.     this.suppressionTransferRange = 50;
  13.     // Any attacks within this range will replace the previous attack suppression
  14.    
  15.     this.suppressedList = [];
  16. };
  17.  
  18. AttackWarning.prototype.NotifyAttack = function(attacker, target)
  19. {
  20.     var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
  21.     var cmpOwnership = Engine.QueryInterface(target, IID_Ownership);
  22.     if (cmpPlayer.GetPlayerID() != cmpOwnership.GetOwner()) {
  23.         // Only warn about people attacking me
  24.         return;
  25.     }
  26.  
  27.     var cmpPosition = Engine.QueryInterface(target, IID_Position);
  28.     if (!cmpPosition || !cmpPosition.IsInWorld())
  29.         return;
  30.     var entityPosition = cmpPosition.GetPosition();
  31.    
  32.     var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
  33.     var currentTime = cmpTimer.GetTime();
  34.    
  35.     for (var i = 0; i < this.suppressedList.length; i++)
  36.     {
  37.         var event = this.suppressedList[i];
  38.        
  39.         // Check if the current event has timed out
  40.         if (currentTime - event.time > 1000 * this.suppressionSeconds)
  41.         {
  42.             this.suppressedList.splice(i, 1);
  43.             i--;
  44.             continue;
  45.         }
  46.        
  47.         // If this is within suppression distance of the event then check if the event should be updated
  48.         // and then return
  49.         var dist = Distance(event.position, entityPosition);
  50.         if (dist < this.suppressionRange)
  51.         {
  52.             if (dist < this.suppressionTransferRange)
  53.             {
  54.                 event.position = {x: entityPosition.x, z: entityPosition.z};
  55.                 event.time = currentTime;
  56.             }
  57.             return;
  58.         }
  59.     }
  60.    
  61.     warn("Player " + cmpPlayer.GetPlayerID() + ": You are under attack!");
  62.     this.suppressedList.push({position:{x: entityPosition.x, z: entityPosition.z}, time: currentTime});
  63. };
  64.  
  65. Engine.RegisterComponentType(IID_AttackWarning, "AttackWarning", AttackWarning);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement