Triggar zones

Version:

1.0

Added on:

05 Feb 2013 01:24

Tags:

Description:
I adapted my Punish campers xd! script so that you can use it to trigger whatever kind of stuff you want. It basically lets you define tiles in a level that are triggar zones, and then provides you with three events where you can handle them: one when the triggerzone is entered, one when it is left, and one when its been occupied for longer than your defined limit (you can just put the gameticks column to 0 and ignore that event along with the triggarTime-array, if you want).
  1. /*
  2. Triggarzones v1.0
  3. Based on my Punish campers xd! script.
  4. */
  5.  
  6.  
  7. /** level settings **/
  8. const uint checkTriggarInterval=10;         //how often to check if a player is in the triggar zones
  9. array<uint> triggarTime;              //this array keeps track of how long a player has been in a triggar zone
  10.  
  11. const array<array<array<uint>>> triggarZone = {
  12.  /* A triggarzone is a rectangle referred to by its top left and bottom right corner
  13. x,y -----------
  14.     |         |
  15.     |         |
  16.     ----------- a,b                                                              
  17.   (x,y)      (a,b)      gameticks     id     */
  18.   {{5,14},     {5,14},     {400},      {0}}      //this is a triggar zone of one tile
  19.  
  20. /*
  21. -id's must be 0, 1, 2, etc, but several ones can have the same ID, meaning you can form any shape you want  in that way
  22. -it is enough to define gameticks once per id (the first time)
  23. */
  24.  
  25. };
  26. /******** end of level settings ********/
  27.  
  28.  
  29. /** Here are your events! **/
  30. //this one is called when a player is in a triggar zone
  31. void onInTriggar(uint pID, uint trigID, uint remainingSecs) {
  32.   switch (trigID) {
  33.       case 0:                 //the case for my only trigger!
  34.       if (jjGameTicks % 70 == 0) { //only show the text every second
  35.         jjPlayers[pID].showText("You are camping in gry's spot. Dead in " + formatInt(remainingSecs,"l") + " seconds.");
  36.       }
  37.     break;
  38.   }
  39. }
  40. //this one is called when a player leaves a triggar zone
  41. void onLeftTriggar(uint pID, uint trigID) {  
  42.   triggarTime[trigID] = 0;            //reset the timer whenever a player leaves the triggar zone
  43.   switch (trigID) {
  44.       case 0: jjPlayers[pID].showText(" "); break; //remove the camping-message
  45.   }
  46. }
  47. //this one is called when a player has been inside a triggar zone for longer than the defined amount of gameticks
  48. void onTriggared(uint pID, uint trigID) {            
  49.   switch (trigID) {
  50.       case 0:
  51.       if (jjPlayers[pID].health > 0) {                    //don't kill 10 times xd
  52.         jjChat("I camped for too long in gry's spot. Now I die :c");
  53.         jjPlayers[pID].kill();                        //kill the player
  54.         triggarTime[trigID] = 0;                      //reset timer
  55.       }
  56.       break;
  57.   }
  58. }
  59. /** end of triggar events **/
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. /** Don't edit past this point if you don't know what you're doing **/
  68. uint triggarZones = 0;
  69. uint triggarZonesRealLength;
  70.  
  71. void onLevelLoad() {
  72.   triggarZonesRealLength = triggarZone.length();
  73.   for (uint i=0; i < triggarZonesRealLength;i++) triggarZones = triggarZone[i][3][0];
  74.   triggarZones++;
  75.   triggarTime.resize(triggarZones);
  76. }
  77.  
  78. array<bool> isInTriggarZone(uint pID, uint triggarzone) {
  79.   array<bool> returnThis = {false, false, false};
  80.   uint maxCamp = 0;
  81.   uint xPos = jjPlayers[pID].xPos / 32;
  82.   uint yPos = jjPlayers[pID].yPos / 32;                        
  83.   for (uint j=0; j < triggarZonesRealLength;j++) {        
  84.       if (triggarZone[j][3][0] == triggarzone)  {  
  85.       if (maxCamp == 0) maxCamp = triggarZone[j][2][0];
  86.       if ((xPos >= triggarZone[j][0][0])&&(xPos <= triggarZone[j][1][0])&&(yPos >= triggarZone[j][0][1])&&(yPos <= triggarZone[j][1][1])) returnThis[0] = true;
  87.     }
  88.   }
  89.   if (!returnThis[0]) {
  90.     if (triggarTime[triggarzone] > 0) returnThis[2] = true;
  91.   }
  92.   else triggarTime[triggarzone] += checkTriggarInterval;
  93.   if (maxCamp < triggarTime[triggarzone]) {                                        
  94.     returnThis[1] = true;
  95.   }
  96.   return returnThis;
  97. }
  98.  
  99. void onPlayer() {
  100.   uint pID = p.playerID;
  101.   if (jjGameTicks % checkTriggarInterval == 0) {
  102.     for (uint i=0; i < triggarZones;i++) {                        
  103.       array<bool> isTrigging = isInTriggarZone(pID, i);            
  104.       if (isTrigging[0]) {
  105.         uint remainingSecs = ((triggarZone[i][2][0] - triggarTime[i]) / 70);
  106.         onInTriggar(pID, i, remainingSecs);
  107.         if (isTrigging[1]) onTriggared(pID, i);
  108.       }
  109.       else {
  110.         if (isTrigging[2]) onLeftTriggar(pID, i);
  111.       }
  112.     }
  113.   }
  114. }