ketura

EventSubscription

Sep 22nd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1.  
  2.  
  3. //////////////////////////////////
  4. //                              //
  5. //  EVENT SUBSCRIPTION EXAMPLE  //
  6. //                              //
  7. //////////////////////////////////
  8.  
  9. using System;
  10.  
  11. namespace RenegadeCore
  12. {
  13.     public partial class Weather
  14.     {
  15.         //This is called by the core weather system from other parts of the game.
  16.         [GeneratesEvent("SpawnWeatherCheck")]
  17.         public bool SpawnWeather(Weather weather, ChunkColumn column)
  18.         {
  19.             if(column.HasWeather())
  20.                 return;
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26. namespace AwesomeMod
  27. {
  28.     public partial class ArceusThunderstorm
  29.     {
  30.         public int ThunderstormCount { get; protected set; }
  31.  
  32.         public ArceusThunderstorm()
  33.         {
  34.             ThunderstormCount = 0;
  35.         }
  36.  
  37.         [PrependEvent("SpawnWeatherCheck", Priority.Highest)]
  38.         public bool PreWeatherSpawn(Weather weather, ChunkColumn column)
  39.         {
  40.             if(ThunderstormCount >= 100)
  41.             {
  42.                 SpawnArceus(column.zero);
  43.                 ThunderstormCount = 0;
  44.                 return false;
  45.             }
  46.  
  47.             return true;
  48.         }
  49.  
  50.         [AppendEvent("SpawnWeatherCheck")]
  51.         public void PostWeatherSpawn(Weather weather, ChunkColumn column)
  52.         {
  53.             ThunderstormCount++;
  54.         }
  55.     }
  56. }
  57.  
  58. //results in:
  59. using System;
  60.  
  61. namespace RenegadeCore
  62. {
  63.     public partial class Weather
  64.     {
  65.         public class SpawnWeatherCheckEventArgs : XGEFEventArgs
  66.         {
  67.             public Weather weather { get; set; }
  68.             public ChunkColumn column { get; set; }
  69.         }
  70.  
  71.         //This is called by the core weather system from other parts of the game.
  72.         public void SpawnWeather(Weather weather, ChunkColumn column)
  73.         {
  74.             //Automatically inserted
  75.             SpawnWeatherCheckEventArgs args = new SpawnWeatherCheckEventArgs() { weather = weather, column = column };
  76.             args = XGEF.Events.EventHandler.Instance.InvokeEvent(this, args);
  77.             if(args.Cancel)
  78.                 return;
  79.  
  80.             if(column.HasWeather())
  81.                 return;
  82.  
  83.             //Automatically inserted
  84.             XGEF.Events.EventHandler.Instance.InvokePostEvent("SpawnWeatherCheck", this, args);
  85.         }
  86.  
  87.         //Automatically generated if it didn't exist
  88.         public Weather()
  89.         {
  90.             XGEF.Events.EventHandler.Instance.CreateEvent("SpawnWeatherCheck", this, "WeatherMod", SpawnWeatherCheckEventArgs);
  91.         }
  92.     }
  93. }
  94.  
  95. namespace AwesomeMod
  96. {
  97.     public partial class ArceusThunderstorm
  98.     {
  99.         public int ThunderstormCount { get; protected set; }
  100.  
  101.         public ArceusThunderstorm()
  102.         {
  103.             ThunderstormCount = 0;
  104.             //Automatically inserted
  105.             XGEF.Events.EventHandler.Instance.RegisterPreEvent("SpawnWeatherCheck", PreWeatherSpawn, Priority.Highest);
  106.             XGEF.Events.EventHandler.Instance.RegisterPostEvent("SpawnWeatherCheck", PostWeatherSpawn, Priority.Normal);
  107.         }
  108.  
  109.         public void PreWeatherSpawn(Weather weather, ChunkColumn column)
  110.         {
  111.             if(ThunderstormCount >= 100)
  112.             {
  113.                 SpawnArceus(column.zero);
  114.                 ThunderstormCount = 0;
  115.                 args.Cancel = true;
  116.                 return;
  117.             }
  118.             args.Cancel = false;
  119.             return;
  120.         }
  121.  
  122.         public void PostWeatherSpawn(Weather weather, ChunkColumn column)
  123.         {
  124.             ThunderstormCount++;
  125.         }
  126.     }
  127. }
Add Comment
Please, Sign In to add comment