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 Entities;                          use Entities; 
  10. with Hashed_Strings;                    use Hashed_Strings; 
  11. with Objects;                           use Objects; 
  12.  
  13. limited with Games; 
  14.  
  15. private with Object_Factory; 
  16.  
  17. pragma Elaborate_All( Object_Factory ); 
  18.  
  19. package Actors is 
  20.  
  21.     -- Actor is an abstract class which defines the behavior of an Entity. It 
  22.     -- should be subclassed and implemented once for each Entity subclass to be 
  23.     -- controlled. Actor subclasses register an allocator and the class name of 
  24.     -- the Entity they can control with the Actor Factory so that an appropriate 
  25.     -- Actor can be instantiated, given an Entity to control. 
  26.     type Actor is abstract new Limited_Object with private; 
  27.     type A_Actor is access all Actor'Class; 
  28.  
  29.     -- Creates a new actor, given a controllable entity's class name. The 
  30.     -- allocator used for creation is registered at elaboration time. 
  31.     function Create_Actor( entityClass : String ) return A_Actor; 
  32.     pragma Precondition( entityClass'Length > 0 ); 
  33.  
  34.     -- Handles Impulses given by a player. 
  35.     procedure Impulse( this : access Actor; name : Hashed_String ) is abstract; 
  36.  
  37.     -- This will be called when the game is paused. Override this to stop any 
  38.     -- on-going actions begun with a previous impulse so the entity doesn't 
  39.     -- continue doing something while gameplay is paused or when gameplay is 
  40.     -- resumed and the previously on-going action is no longer appropriate. 
  41.     procedure Pause( this : access Actor ) is null; 
  42.  
  43.     -- Returns True if the actor's lifespan is confined to the lifespan of the 
  44.     -- world in which its entity exists. The default for actors is True unless 
  45.     -- overridden. 
  46.     function Is_Temporal( this : access Actor ) return Boolean; 
  47.  
  48.     -- Sets the entity that the actor controls, returning True on acceptance. 
  49.     -- For non-temporal actors, this can be 'null' between worlds or before 
  50.     -- loading a world. The concrete subclasses should override this function to 
  51.     -- verify the entity's class before calling the base Set_Entity procedure. 
  52.     function Set_Entity( this : access Actor; e : A_Entity ) return Boolean; 
  53.  
  54.     -- Sets the entity that the actor controls. The actor must be able to 
  55.     -- control entities of the given class. 
  56.     procedure Set_Entity( this : not null access Actor'Class; e : A_Entity ); 
  57.  
  58.     -- Sets the Actor's reference to its parent Game object. 
  59.     procedure Set_Game( this : not null access Actor'Class; 
  60.                         game : not null access Games.Game'Class ); 
  61.  
  62.     -- Deletes an Actor object. 
  63.     procedure Delete( this : in out A_Actor ); 
  64.  
  65.     ---------------------------------------------------------------------------- 
  66.  
  67.     -- Initializes the Actor factory for use. This should be called after all 
  68.     -- allocators have been registered. 
  69.     procedure Initialize; 
  70.  
  71.     -- Finalizes the Actor factory. This should be called before exiting. 
  72.     procedure Finalize; 
  73.  
  74. private 
  75.  
  76.     type Actor is abstract new Limited_Object with 
  77.         record 
  78.             entity : A_Entity := null;                 -- this is not owned by the actor 
  79.             game   : access Games.Game'Class := null;  -- the actor's parent game object 
  80.         end record; 
  81.  
  82.     ---------------------------------------------------------------------------- 
  83.  
  84.     package Factory is new Object_Factory( Actor, A_Actor, Delete ); 
  85.  
  86. end Actors;