1. with Ada.Streams;                       use Ada.Streams; 
  2. with Entities;                          use Entities; 
  3. with Entities.Players;                  use Entities.Players; 
  4. with Events;                            use Events; 
  5. with Events.Corrals;                    use Events.Corrals; 
  6. with Events.Entities;                   use Events.Entities; 
  7. with Events.Listeners;                  use Events.Listeners; 
  8. with Game_States;                       use Game_States; 
  9. with Objects;                           use Objects; 
  10. with Processes;                         use Processes; 
  11. with Processes.Managers;                use Processes.Managers; 
  12. with Tiles.Libraries;                   use Tiles.Libraries; 
  13.  
  14. private with Ada.Containers; 
  15. private with Ada.Containers.Hashed_Sets; 
  16. private with Ada.Containers.Ordered_Maps; 
  17. private with Ada.Real_Time; 
  18. private with Ada.Strings.Unbounded; 
  19. private with Maps; 
  20. private with Tiles; 
  21.  
  22. package Worlds is 
  23.  
  24.     -- A World is an object which contains a map and entities. It can be 
  25.     -- created, loaded and saved as a simple data object and it can be copied. 
  26.     -- 
  27.     -- Worlds are also Processes and Event_Listeners which can be attached to 
  28.     -- the game framework. Only one World at a time can be attached. This is 
  29.     -- enforced by the Game logic. Attaching a World to the game framework means 
  30.     -- giving it a Corral so it can register to receive events and attaching it 
  31.     -- to a Process_Manager so it can execute code. An attached World object 
  32.     -- then has behavior because it listens for events, queues events, and 
  33.     -- executes code as a Process. When a world is attached, its dispatching 
  34.     -- On_Attach procedure is called, which may queue load-time events and 
  35.     -- register the object as an event listener. When a world is detached from 
  36.     -- the game framework, its dispatching On_Detach procedure is called to 
  37.     -- unregister the object as an event listener, etc. A World object can't be 
  38.     -- copied while it is attached to the Game framework. 
  39.     -- 
  40.     -- An unattached World should not have any independent behavior and should 
  41.     -- only do as it is instructed by calling the class procedures. 
  42.     type World_Object is abstract new Object and Event_Listener and Process with private; 
  43.     type A_World is access all World_Object'Class; 
  44.  
  45.     -- Creates a new empty world. An exception is raised on error. 
  46.     function Create_World( width, 
  47.                            height  : Positive; 
  48.                            libName, 
  49.                            domain  : String ) return A_World; 
  50.     pragma Precondition( libName'Length > 0 ); 
  51.     pragma Precondition( domain'Length > 0 ); 
  52.     pragma Postcondition( Create_World'Result /= null ); 
  53.  
  54.     -- Loads a world from disk. An exception is raised on error. 
  55.     function Load_World( name : String ) return A_World; 
  56.     pragma Postcondition( Load_World'Result /= null ); 
  57.  
  58.     -- Attaches the world to the game framework to send and receive events and 
  59.     -- execute as a process. 
  60.     procedure Attach_To_Framework( this   : not null access World_Object'Class; 
  61.                                    game   : not null A_Game_State; 
  62.                                    corral : not null A_Corral; 
  63.                                    pman   : not null A_Process_Manager ); 
  64.  
  65.     -- Detaches the world from the game framework, removing itself as an event 
  66.     -- listener and ceasing execution as a process. 
  67.     procedure Detach_From_Framework( this : not null access World_Object'Class ); 
  68.  
  69.     -- Iterate through all of the entities in the world, examining each. 
  70.     procedure Examine_Entities( this    : not null access World_Object'Class; 
  71.                                 examine : not null access procedure( e : not null A_Entity ) ); 
  72.  
  73.     -- Returns a reference to an entity with the given id, or null if the entity 
  74.     -- doesn't exist. 
  75.     function Get_Entity( this : access World_Object; id : Entity_Id ) return A_Entity; 
  76.  
  77.     -- The name of the file that this world was loaded from (including the file 
  78.     -- extension), if it was loaded. If this world was not loaded from disk or 
  79.     -- has not been saved to the disk yet then an empty string will be returned. 
  80.     function Get_Filename( this : access World_Object ) return String; 
  81.  
  82.     -- Returns the World's attached Game class, if it has been attached to the 
  83.     -- framework. 
  84.     function Get_Game_State( this : not null access World_Object'Class ) return A_Game_State; 
  85.  
  86.     -- Returns the actual map height in pixels. 
  87.     function Get_Height( this : not null access World_Object'Class ) return Positive; 
  88.  
  89.     -- Returns the height of the map in tiles. 
  90.     function Get_Height_Tiles( this : not null access World_Object'Class ) return Positive; 
  91.  
  92.     -- Returns the world's reference to its tile library. Do not modify it. 
  93.     function Get_Library( this : not null access World_Object'Class ) return A_Tile_Library; 
  94.  
  95.     -- Returns the name of the background music track. 
  96.     function Get_Music( this : not null access World_Object'Class ) return String; 
  97.  
  98.     -- Returns a reference to the world's current player entity. Do not modify 
  99.     -- it. 
  100.     function Get_Player( this : not null access World_Object'Class ) return A_Player; 
  101.  
  102.     -- Returns the id of the tile at the specified location. 
  103.     function Get_Tile_Id( this  : not null access World_Object'Class; 
  104.                           layer, 
  105.                           x, y  : Integer ) return Natural; 
  106.  
  107.     -- Returns the title of the world. 
  108.     function Get_Title( this  : not null access World_Object'Class ) return String; 
  109.  
  110.     -- Returns the actual map width in pixels. 
  111.     function Get_Width( this : not null access World_Object'Class) return Positive; 
  112.  
  113.     -- Returns the width of the map in tiles. 
  114.     function Get_Width_Tiles( this : not null access World_Object'Class ) return Positive; 
  115.  
  116.     -- Resizes the map. An exception is raised on error. 
  117.     procedure Resize( this   : not null access World_Object'Class; 
  118.                       width, 
  119.                       height : Positive ); 
  120.  
  121.     -- Writes the world in its current state to a file on disk. An exception is 
  122.     -- raised on error. 
  123.     procedure Save( this      : not null access World_Object'Class; 
  124.                     name      : String; 
  125.                     overwrite : Boolean := True ); 
  126.     pragma Precondition( name'Length > 0 ); 
  127.  
  128.     -- basic properties: 
  129.     -- "filename" : the filename 
  130.     -- "domain"   : determines player movement, physical rules 
  131.     -- "title"    : the readable name of the world 
  132.     -- "music"    : the background music track name 
  133.     procedure Set_Property( this : access World_Object; name, value : String ); 
  134.     pragma Precondition( name'Length > 0 ); 
  135.  
  136.     -- If 'notify' is True, Tile_Changed and World_Modified events will be sent. 
  137.     procedure Set_Tile( this   : access World_Object; 
  138.                         layer  : Integer; 
  139.                         x, y   : Integer; 
  140.                         id     : Natural; 
  141.                         notify : Boolean := True ); 
  142.  
  143.     -- x, y are in pixel coordinates, not tile coordinates. If 'notify' is True, 
  144.     -- Tile_Changed and World_Modified events will be sent. 
  145.     procedure Set_Tile( this   : access World_Object; 
  146.                         layer  : Integer; 
  147.                         x, y   : Float; 
  148.                         id     : Natural; 
  149.                         notify : Boolean := True ); 
  150.  
  151.     -- Spawns an entity of the given class id into the world. If 'width' or 
  152.     -- 'height' are equal to 0, the entity's natural width and height will be 
  153.     -- used. 
  154.     procedure Spawn_Entity( this   : access World_Object; 
  155.                             id     : String; 
  156.                             x, y   : Float; 
  157.                             width, 
  158.                             height : Natural := 0; 
  159.                             xv, yv : Float := 0.0 ); 
  160.     pragma Precondition( id'Length > 0 ); 
  161.  
  162.     -- Returns the width of each tile in the world, in pixels. 
  163.     function Tile_Width( this : not null access World_Object'Class ) return Positive; 
  164.  
  165.     -- Implement this function to Construct and Read instances of the concrete 
  166.     -- subclass from a stream. 
  167.     function Object_Input( stream : access Root_Stream_Type'Class ) return World_Object is abstract; 
  168.  
  169.     -- Returns a deep copy of 'src'. 
  170.     function Copy( src : A_World ) return A_World; 
  171.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  172.  
  173.     -- Deletes the World. 
  174.     procedure Delete( this : in out A_World ); 
  175.     pragma Postcondition( this = null ); 
  176.  
  177.     ---------------------------------------------------------------------------- 
  178.  
  179.     -- Returns True if 'domain' is a registered domain name. Case sensitive. 
  180.     function Valid_Domain( domain : String ) return Boolean; 
  181.  
  182.     -- Returns the file extension for World files, without a leading dot. 
  183.     function World_Extension return String; 
  184.     pragma Postcondition( World_Extension'Result'Length > 0 ); 
  185.  
  186.     FILE_NOT_FOUND, 
  187.     READ_EXCEPTION, 
  188.     WRITE_EXCEPTION : exception; 
  189.  
  190. private 
  191.  
  192.     use Ada.Containers; 
  193.     use Ada.Real_Time; 
  194.     use Ada.Strings.Unbounded; 
  195.     use Maps; 
  196.     use Tiles; 
  197.  
  198.     -- Maps Entity_Ids to Entity objects. 
  199.     package Entity_Map is new Ada.Containers.Ordered_Maps( Entity_Id, A_Entity, "<", "=" ); 
  200.  
  201.     -- There are two kinds of tile animation: looping animations and one-shot. 
  202.     -- 
  203.     -- In the case of a one-shot animation, each frame progresses to the next 
  204.     -- after a fixed amount of time, beginning the moment the tile is set into 
  205.     -- the world. Generally, the last frame of a one-shot animation will end by 
  206.     -- progressing to a non-animated tile. It is possible for a frame in a 
  207.     -- one-shot animation to jump back to a previous frame, creating a frame 
  208.     -- cycle, but this is generally not very useful. 
  209.     -- 
  210.     -- Looping animations have a list of frames and a single delay. The biggest 
  211.     -- difference between looping and one-shot animations is that the animation 
  212.     -- frame is globally updated at specific time slices, based on the animation 
  213.     -- frame delay. This means that two world locations with the same tile id 
  214.     -- will always update simultaneously, regardless of being set into the map 
  215.     -- at different times or not. 
  216.     -- 
  217.     -- An animation is looping if the frames field of the Animated_Info is 
  218.     -- non-null, otherwise it's a one-shot. 
  219.     type Animated_Info is 
  220.         record 
  221.             layer      : Integer;           -- layer of animated tile 
  222.             x, y       : Integer;           -- coordinates of animated tile 
  223.             nextUpdate : Time;              -- next time to update the frame 
  224.             frameDelay : Time_Span;         -- (for looping) animation frame delay 
  225.             frames     : A_Tile_Id_Array;   -- (for looping) frame list 
  226.         end record; 
  227.  
  228.     -- Returns True if 'l' and 'r' share the same world location. 
  229.     function Equivalent( l, r : Animated_Info ) return Boolean; 
  230.  
  231.     -- Returns a hash value of the Animated_Info record. 
  232.     function Hash( a : Animated_Info ) return Hash_Type; 
  233.  
  234.     -- Returns True if the animation is played as a loop. 
  235.     function Is_Looping( ai : Animated_Info ) return Boolean; 
  236.  
  237.     function Animated_Info_Input( stream : access Root_Stream_Type'Class ) return Animated_Info; 
  238.     for Animated_Info'Input use Animated_Info_Input; 
  239.  
  240.     procedure Animated_Info_Output( stream : access Root_Stream_Type'Class; info : Animated_Info ); 
  241.     for Animated_Info'Output use Animated_Info_Output; 
  242.  
  243.     package Animated_Set is new Ada.Containers.Hashed_Sets( Animated_Info, 
  244.                                                             Hash, Equivalent, 
  245.                                                             "=" ); 
  246.  
  247.     ---------------------------------------------------------------------------- 
  248.  
  249.     type World_Object is abstract new Object and Event_Listener and Process with 
  250.         record 
  251.             -- the following fields are Not streamed 
  252.             attached : Boolean := False; 
  253.             game     : A_Game_State; 
  254.             corral   : A_Corral; 
  255.             pman     : A_Process_Manager; 
  256.  
  257.             filename : Unbounded_String; 
  258.             lib      : A_Tile_Library := null; 
  259.             player   : Entity_Id := INVALID_ID; 
  260.  
  261.             -- the following fields are streamed 
  262.             map      : A_Map := null; 
  263.             libName  : Unbounded_String; 
  264.             domain   : Unbounded_String; 
  265.             title    : Unbounded_String; 
  266.             music    : Unbounded_String; 
  267.             entities : Entity_Map.Map; 
  268.             animated : Animated_Set.Set; 
  269.         end record; 
  270.  
  271.     procedure Adjust( this : access World_Object ); 
  272.  
  273.     -- Construction with an empty filename, map, libName and domain is allowed 
  274.     -- if the object is being constructed as part of a streaming read operation. 
  275.     -- The fields should be be immediately read in before the World is returned 
  276.     -- to the application for use. 
  277.     procedure Construct( this     : access World_Object; 
  278.                          filename : String; 
  279.                          map      : in out A_Map; 
  280.                          libName, 
  281.                          domain   : String ); 
  282.  
  283.     procedure Delete( this : in out World_Object ); 
  284.  
  285.     -- Performs initialization immediately after load that can't be performed 
  286.     -- during construction, like passing a reference to 'this' to child objects 
  287.     -- (ie: entitites) that need it. An overriding implementation should call 
  288.     -- this first. 
  289.     procedure Initialize( this : access World_Object ); 
  290.  
  291.     -- Returns the name of the World as a Process. 
  292.     function Get_Process_Name( this : access World_Object ) return String; 
  293.     pragma Postcondition( Get_Process_Name'Result'Length > 0 ); 
  294.  
  295.     -- Handles a Delete_Entity event. 
  296.     procedure Handle( this : access World_Object; evt : not null A_Delete_Entity_Event ); 
  297.  
  298.     -- Handles an Entities_Collided event. 
  299.     procedure Handle( this : access World_Object; evt : not null A_Entities_Collided_Event ); 
  300.  
  301.     -- Handles an Entities_Separated event. 
  302.     procedure Handle( this : access World_Object; evt : not null A_Entities_Separated_Event ); 
  303.  
  304.     -- Handles an Entity_Face event. 
  305.     procedure Handle( this : access World_Object; evt : not null A_Entity_Face_Event ); 
  306.  
  307.     -- Handles an Entity_Grounded event. 
  308.     procedure Handle( this : access World_Object; evt : not null A_Entity_Grounded_Event ); 
  309.  
  310.     -- Handles an Entity_Hit_Wall event. 
  311.     procedure Handle( this : access World_Object; evt : not null A_Entity_Hit_Wall_Event ); 
  312.  
  313.     -- Handles an Entity_Moved event. 
  314.     procedure Handle( this : access World_Object; evt : not null A_Entity_Moved_Event ); 
  315.  
  316.     -- Handles an Entity_Resized event. 
  317.     procedure Handle( this : access World_Object; evt : not null A_Entity_Resized_Event ); 
  318.  
  319.     -- Handles a Set_Entity_Attribute event. 
  320.     procedure Handle( this : access World_Object; evt : not null A_Set_Entity_Attribute_Event ); 
  321.  
  322.     -- Handles a Spawn_Entity event. 
  323.     procedure Handle( this : access World_Object; evt : not null A_Spawn_Entity_Event ); 
  324.  
  325.     -- Handles all events the World receives and dispatches to the specific 
  326.     -- Handle procedure. If this procedure is overridden, the implementation 
  327.     -- should handle 'evt' if it's special. If the subclass doesn't do anything 
  328.     -- special with it, then it should call this implementation to dispatch it. 
  329.     procedure Handle_Event( this : access World_Object; 
  330.                             evt  : in out A_Event; 
  331.                             resp : out Response_Type ); 
  332.     pragma Precondition( evt /= null ); 
  333.  
  334.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out World_Object ); 
  335.     for World_Object'Read use Object_Read; 
  336.  
  337.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : World_Object ); 
  338.     for World_Object'Write use Object_Write; 
  339.  
  340.     -- This is called when the World is attached to the Game framework, to 
  341.     -- register for events and execute entities' On_Load handlers. Override this 
  342.     -- procedure to register the world to listen for special events or to queue 
  343.     -- any special events on load. An overriding implementation should call this 
  344.     -- first. 
  345.     procedure On_Attach( this : access World_Object ); 
  346.  
  347.     -- This is called when the World is removed from the Game framework, to 
  348.     -- unregister for the events it previously registered for with On_Attach. 
  349.     -- If On_Attach is overridden, On_Detach should be overridden too. The 
  350.     -- world can't remain registered as a listener for any event type on detach. 
  351.     -- An overriding implementation should call this first. 
  352.     procedure On_Detach( this : access World_Object ); 
  353.  
  354.     -- Queues world loaded events in order to send the world to the game views. 
  355.     -- This includes the events New_World, World_Property_Changed, 
  356.     -- Entity_Created, and Follow_Entity. This procedure is called as part of 
  357.     -- On_Attach. An overriding implementation should call this first. 
  358.     procedure Queue_Load_Events( this : access World_Object ); 
  359.  
  360.     -- Executes one frame of the world behavior; updates entities and animated 
  361.     -- tiles. An overriding implementation should call this first. 
  362.     procedure Tick( this : access World_Object; time : Tick_Time ); 
  363.  
  364.     -- Verifies a file format header before reading the World representation. 
  365.     -- If the header is invalid, READ_EXCEPTION will be raised. Raises an 
  366.     -- exception on streaming error. 
  367.     function A_World_Input( stream : access Root_Stream_Type'Class ) return A_World; 
  368.     for A_World'Input use A_World_Input; 
  369.  
  370.     -- Writes a file format header and then the World representation. 
  371.     procedure A_World_Output( stream : access Root_Stream_Type'Class; world : A_World ); 
  372.     for A_World'Output use A_World_Output; 
  373.  
  374.     -- Reads the concrete World tag and instantiates it. A Constraint_Error will 
  375.     -- be raised if the class is unknown. Raises an exception on streaming 
  376.     -- error. 
  377.     procedure A_World_Read( stream : access Root_Stream_Type'Class; world : out A_World ); 
  378.     for A_World'Read use A_World_Read; 
  379.  
  380.     -- Writes the conrete World tag. 
  381.     procedure A_World_Write( stream : access Root_Stream_Type'Class; world : A_World ); 
  382.     for A_World'Write use A_World_Write; 
  383.  
  384.     ---------------------------------------------------------------------------- 
  385.  
  386.     -- An allocator function for Creating a new World. 'width' and 'height' are 
  387.     -- the size of the world in tiles. 'libName' is the library that contains 
  388.     -- the tiles used in the world. 'domain' is the world domain, which 
  389.     -- determines physics constants, the player entity, and available enemy 
  390.     -- types. 
  391.     type Allocator is 
  392.         access function( width, 
  393.                          height  : Positive; 
  394.                          libName, 
  395.                          domain  : String ) return A_World; 
  396.  
  397.     -- Registers all the information pertinent to the application's concrete 
  398.     -- World implementation: an allocator for creating a new World instance, 
  399.     -- the world format identifier string, and the World format version number. 
  400.     procedure Register( allocate      : not null Allocator; 
  401.                         identifier    : String; 
  402.                         read_version, 
  403.                         write_version : Positive ); 
  404.     pragma Precondition( identifier'Length > 0 ); 
  405.  
  406.     -- Registers 'domain' as a valid domain for Worlds. Case sensitive. 
  407.     procedure Register_Domain( domain : String ); 
  408.     pragma Precondition( domain'Length > 0 ); 
  409.  
  410.     -- Returns a string that identifies the world index file format. It is the 
  411.     -- first data found in the file. 
  412.     function World_Identifier return String; 
  413.  
  414. end Worlds;