Triggar zones

Version:

1.0

Added on:

05 Feb 2013 00: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).
/*
Triggarzones v1.0
Based on my Punish campers xd! script.
*/


/** level settings **/
const uint checkTriggarInterval=10;         //how often to check if a player is in the triggar zones
array<uint> triggarTime;              //this array keeps track of how long a player has been in a triggar zone

const array<array<array<uint>>> triggarZone = {
 /* A triggarzone is a rectangle referred to by its top left and bottom right corner
x,y -----------
    |         |
    |         |
    ----------- a,b                                                              
  (x,y)      (a,b)      gameticks     id     */
  {{5,14},     {5,14},     {400},      {0}}      //this is a triggar zone of one tile
  
/*
-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
-it is enough to define gameticks once per id (the first time)
*/
  
};
/******** end of level settings ********/


/** Here are your events! **/
//this one is called when a player is in a triggar zone
void onInTriggar(uint pID, uint trigID, uint remainingSecs) {
  switch (trigID) {
      case 0:                 //the case for my only trigger!
      if (jjGameTicks % 70 == 0) { //only show the text every second
        jjPlayers[pID].showText("You are camping in gry's spot. Dead in " + formatInt(remainingSecs,"l") + " seconds."); 
      }
    break;
  }
}
//this one is called when a player leaves a triggar zone
void onLeftTriggar(uint pID, uint trigID) {  
  triggarTime[trigID] = 0;            //reset the timer whenever a player leaves the triggar zone
  switch (trigID) {
      case 0: jjPlayers[pID].showText(" "); break; //remove the camping-message
  }
}
//this one is called when a player has been inside a triggar zone for longer than the defined amount of gameticks
void onTriggared(uint pID, uint trigID) {            
  switch (trigID) {
      case 0: 
      if (jjPlayers[pID].health > 0) {                    //don't kill 10 times xd
        jjChat("I camped for too long in gry's spot. Now I die :c");
        jjPlayers[pID].kill();                        //kill the player
        triggarTime[trigID] = 0;                      //reset timer
      }
      break;
  }
}
/** end of triggar events **/







/** Don't edit past this point if you don't know what you're doing **/
uint triggarZones = 0;
uint triggarZonesRealLength;

void onLevelLoad() {
  triggarZonesRealLength = triggarZone.length();
  for (uint i=0; i < triggarZonesRealLength;i++) triggarZones = triggarZone[i][3][0];
  triggarZones++;
  triggarTime.resize(triggarZones);
}

array<bool> isInTriggarZone(uint pID, uint triggarzone) {
  array<bool> returnThis = {false, false, false};
  uint maxCamp = 0;
  uint xPos = jjPlayers[pID].xPos / 32;
  uint yPos = jjPlayers[pID].yPos / 32;                        
  for (uint j=0; j < triggarZonesRealLength;j++) {        
      if (triggarZone[j][3][0] == triggarzone)  {  
      if (maxCamp == 0) maxCamp = triggarZone[j][2][0];
      if ((xPos >= triggarZone[j][0][0])&&(xPos <= triggarZone[j][1][0])&&(yPos >= triggarZone[j][0][1])&&(yPos <= triggarZone[j][1][1])) returnThis[0] = true;
    }
  }
  if (!returnThis[0]) {
    if (triggarTime[triggarzone] > 0) returnThis[2] = true;
  }
  else triggarTime[triggarzone] += checkTriggarInterval;
  if (maxCamp < triggarTime[triggarzone]) {                                        
    returnThis[1] = true; 
  }
  return returnThis;
}

void onPlayer() {
  uint pID = p.playerID;
  if (jjGameTicks % checkTriggarInterval == 0) {
    for (uint i=0; i < triggarZones;i++) {                        
      array<bool> isTrigging = isInTriggarZone(pID, i);            
      if (isTrigging[0]) {
        uint remainingSecs = ((triggarZone[i][2][0] - triggarTime[i]) / 70);
        onInTriggar(pID, i, remainingSecs); 
        if (isTrigging[1]) onTriggared(pID, i);
      }
      else {
        if (isTrigging[2]) onLeftTriggar(pID, i);
      }
    }
  }
}