1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Scripting;                         use Scripting; 
  10.  
  11. private with Scripting.Evaluators; 
  12.  
  13. package Entities.Triggers is 
  14.  
  15.     -- A Trigger is an entity that executes a script when a condition is met at 
  16.     -- a configurable time. For example, when a player collides with a trigger, 
  17.     -- a new level may be loaded. 
  18.     type Trigger is abstract new Entity and Evaluation_Node with private; 
  19.     type A_Trigger is access all Trigger'Class; 
  20.  
  21.     -- A class pattern that matches all trigger class ids registered in the 
  22.     -- entity factory. 
  23.     CLASS_PATTERN : constant String := "Entities.Triggers.*"; 
  24.  
  25. private 
  26.  
  27.     use Scripting.Evaluators; 
  28.  
  29.     -- Valid values for the "When" attribute are: 
  30.     --   OnActivate - The player manually activated a trigger 
  31.     --   OnLoad     - The world was loaded 
  32.     --   OnTouch    - The player touched the trigger (once per collision) 
  33.  
  34.     ATTR_WHEN      : constant String := "When";       -- when the trigger may trip 
  35.     ATTR_CONDITION : constant String := "Condition";  -- the necessary condition 
  36.     ATTR_ACTION    : constant String := "Action";     -- what the trigger will do (a script) 
  37.  
  38.     type Trigger is abstract new Entity and Evaluation_Node with 
  39.         record 
  40.             activatable : Boolean := True; 
  41.  
  42.             -- ** the following fields are not streamed ** 
  43.  
  44.             evaluator : A_Evaluator := Create_Evaluator; 
  45.             activator : A_Entity;            -- entity that activated the 
  46.                                              --   trigger (if any); set only 
  47.                                              --   while executing the action 
  48.         end record; 
  49.  
  50.     -- Raises an exception if 'icon' can't be found in library 'libName'. 
  51.     procedure Construct( this    : access Trigger; 
  52.                          libName : String; 
  53.                          icon    : String ); 
  54.  
  55.     procedure Delete( this : in out Trigger ); 
  56.  
  57.     -- This is called when the trigger's action script is evaluated, to 
  58.     -- implement the behavior of any script functions the map designer wrote. 
  59.     -- It evaluates function 'name' (as a script function), given 'arguments'. 
  60.     -- If the trigger doesn't recognize the function, it will ask its World to 
  61.     -- evaluate it. If function 'name' isn't recognized at all, then Null will 
  62.     -- be returned. The default implementation of this doesn't implement any 
  63.     -- functions. 
  64.     -- 
  65.     -- Override this function as necessary to allow triggers to do things when 
  66.     -- they're activated. Call this function from the overriding implementation 
  67.     -- if 'name' is not recognized. 
  68.     function Evaluate_Function( this      : access Trigger; 
  69.                                 name      : String; 
  70.                                 arguments : Value_Array ) return Value_Ptr; 
  71.  
  72.     -- Evaluates the symbol 'symbol' (as a script variable), resolving its value 
  73.     -- and returning it. If the trigger doesn't recognize 'symbol', it will ask 
  74.     -- the world to resolve the symbol. If the symbol can't be resolved, Null 
  75.     -- will be returned. 
  76.     function Evaluate_Symbol( this   : access Trigger; 
  77.                               symbol : String ) return Value_Ptr; 
  78.  
  79.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Trigger ); 
  80.     for Trigger'Read use Object_Read; 
  81.  
  82.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Trigger ); 
  83.     for Trigger'Write use Object_Write; 
  84.  
  85.     -- Called when the trigger is manually activated by entity 'activator'. 
  86.     -- Don't override this procedure. 
  87.     procedure On_Activate( this      : access Trigger; 
  88.                            activator : not null A_Entity ); 
  89.  
  90.     -- Called when the trigger is touched by an entity. Don't override this 
  91.     -- procedure. 
  92.     procedure On_Collide( this : access Trigger; e : not null A_Entity ); 
  93.  
  94.     -- Called when the triggers's world is loaded from disk. Don't override this 
  95.     -- procedure. 
  96.     procedure On_Load( this : access Trigger ); 
  97.  
  98. end Entities.Triggers;