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 Objects;                           use Objects; 
  9. with Processes;                         use Processes; 
  10. with Processes.Managers;                use Processes.Managers; 
  11. with Tiles.Libraries;                   use Tiles.Libraries; 
  12.  
  13. private with Ada.Containers; 
  14. private with Ada.Containers.Hashed_Sets; 
  15. private with Ada.Containers.Ordered_Maps; 
  16. private with Ada.Real_Time; 
  17. private with Ada.Strings.Unbounded; 
  18. private with Maps; 
  19. private with Tiles; 
  20.  
  21. package Worlds is 
  22.  
  23.     -- A World is an object which contains a map and entities. It can be 
  24.     -- created, loaded and saved as a simple data object and it can be copied. 
  25.     -- 
  26.     -- Worlds are also Processes and Event_Listeners which can be attached to 
  27.     -- the game framework. Only one World at a time can be attached. This is 
  28.     -- enforced by the Game logic. Attaching a World to the game framework means 
  29.     -- giving it a Corral so it can register to receive events and attaching it 
  30.     -- to a Process_Manager so it can execute code. An attached World object 
  31.     -- then has behavior because it listens for events, queues events, and 
  32.     -- executes code as a Process. When a world is attached, its dispatching 
  33.     -- On_Attach procedure is called, which may queue load-time events and 
  34.     -- register the object as an event listener. When a world is detached from 
  35.     -- the game framework, its dispatching On_Detach procedure is called to 
  36.     -- unregister the object as an event listener, etc. A World object can't be 
  37.     -- copied while it is attached to the Game framework. 
  38.     -- 
  39.     -- An unattached World should not have any independent behavior and should 
  40.     -- only do as it is instructed by calling the class procedures. 
  41.  
  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.                                    corral : not null A_Corral; 
  62.                                    pman   : not null A_Process_Manager ); 
  63.  
  64.     -- Detaches the world from the game framework, removing itself as an event 
  65.     -- listener and ceasing execution as a process. 
  66.     procedure Detach_From_Framework( this : not null access World_Object'Class ); 
  67.  
  68.     -- Iterate through all of the entities in the world, examining each. 
  69.     procedure Examine_Entities( this    : not null access World_Object'Class; 
  70.                                 examine : not null access procedure( e : not null A_Entity ) ); 
  71.  
  72.     -- Returns a reference to an entity with the given id, or null if the entity 
  73.     -- doesn't exist. 
  74.     function Get_Entity( this : access World_Object; id : Entity_Id ) return A_Entity; 
  75.  
  76.     -- The name of the file that this world was loaded from (including the file 
  77.     -- extension), if it was loaded. If this world was not loaded from disk or 
  78.     -- has not been saved to the disk yet then an empty string will be returned. 
  79.     function Get_Filename( this : access World_Object ) return String; 
  80.  
  81.     -- Returns the actual map height in pixels. 
  82.     function Get_Height( this : not null access World_Object'Class ) return Positive; 
  83.  
  84.     -- Returns the height of the map in tiles. 
  85.     function Get_Height_Tiles( this : not null access World_Object'Class ) return Positive; 
  86.  
  87.     -- Returns the world's reference to its tile library. Do not modify it. 
  88.     function Get_Library( this : not null access World_Object'Class ) return A_Tile_Library; 
  89.  
  90.     -- Returns the name of the background music track. 
  91.     function Get_Music( this : not null access World_Object'Class ) return String; 
  92.  
  93.     -- Returns a reference to the world's current player entity. Do not modify 
  94.     -- it. 
  95.     function Get_Player( this : not null access World_Object'Class ) return A_Player; 
  96.  
  97.     -- Returns the id of the tile at the specified location. 
  98.     function Get_Tile_Id( this  : not null access World_Object'Class; 
  99.                           layer, 
  100.                           x, y  : Integer ) return Natural; 
  101.  
  102.     -- Returns the title of the world. 
  103.     function Get_Title( this  : not null access World_Object'Class ) return String; 
  104.  
  105.     -- Returns the actual map width in pixels. 
  106.     function Get_Width( this : not null access World_Object'Class) return Positive; 
  107.  
  108.     -- Returns the width of the map in tiles. 
  109.     function Get_Width_Tiles( this : not null access World_Object'Class ) return Positive; 
  110.  
  111.     -- Resizes the map. An exception is raised on error. 
  112.     procedure Resize( this   : not null access World_Object'Class; 
  113.                       width, 
  114.                       height : Positive ); 
  115.  
  116.     -- Writes the world in its current state to a file on disk. An exception is 
  117.     -- raised on error. 
  118.     procedure Save( this      : not null access World_Object'Class; 
  119.                     name      : String; 
  120.                     overwrite : Boolean := True ); 
  121.     pragma Precondition( name'Length > 0 ); 
  122.  
  123.     -- basic properties: 
  124.     -- "filename" : the filename 
  125.     -- "domain"   : determines player movement, physical rules 
  126.     -- "title"    : the readable name of the world 
  127.     -- "music"    : the background music track name 
  128.     procedure Set_Property( this : access World_Object; name, value : String ); 
  129.     pragma Precondition( name'Length > 0 ); 
  130.  
  131.     -- If 'notify' is True, Tile_Changed and World_Modified events will be sent. 
  132.     procedure Set_Tile( this   : access World_Object; 
  133.                         layer  : Integer; 
  134.                         x, y   : Integer; 
  135.                         id     : Natural; 
  136.                         notify : Boolean := True ); 
  137.  
  138.     -- Spawns an entity of the given class id into the world. If 'width' or 
  139.     -- 'height' are equal to 0, the entity's natural width and height will be 
  140.     -- used. 
  141.     procedure Spawn_Entity( this   : access World_Object; 
  142.                             id     : String; 
  143.                             x, y   : Float; 
  144.                             width, 
  145.                             height : Natural := 0; 
  146.                             xv, yv : Float := 0.0 ); 
  147.     pragma Precondition( id'Length > 0 ); 
  148.  
  149.     function Tile_Width( this : not null access World_Object'Class ) return Positive; 
  150.  
  151.     function Object_Input( stream : access Root_Stream_Type'Class ) return World_Object is abstract; 
  152.  
  153.     function Copy( src : A_World ) return A_World; 
  154.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  155.  
  156.     procedure Delete( this : in out A_World ); 
  157.     pragma Postcondition( this = null ); 
  158.  
  159.     ---------------------------------------------------------------------------- 
  160.  
  161.     function Valid_Domain( domain : String ) return Boolean; 
  162.  
  163.     function World_Extension return String; 
  164.     pragma Postcondition( World_Extension'Result'Length > 0 ); 
  165.  
  166.     FILE_NOT_FOUND, 
  167.     READ_EXCEPTION, 
  168.     WRITE_EXCEPTION : exception; 
  169.  
  170. private 
  171.  
  172.     use Ada.Containers; 
  173.     use Ada.Real_Time; 
  174.     use Ada.Strings.Unbounded; 
  175.     use Maps; 
  176.     use Tiles; 
  177.  
  178.     package Entity_Map is new Ada.Containers.Ordered_Maps( Entity_Id, A_Entity, "<", "=" ); 
  179.  
  180.     -- There are two kinds of tile animation: looping animations and one-shot. 
  181.     -- 
  182.     -- In the case of a one-shot animation, each frame progresses to the next 
  183.     -- after a fixed amount of time, beginning the moment the tile is set into 
  184.     -- the world. Generally, the last frame of a one-shot animation will end by 
  185.     -- progressing to a non-animated tile. It is possible for a frame in a 
  186.     -- one-shot animation to jump back to a previous frame, creating a frame 
  187.     -- cycle, but this is generally not very useful. 
  188.     -- 
  189.     -- Looping animations have a list of frames and a single delay. The biggest 
  190.     -- difference between looping and one-shot animations is that the animation 
  191.     -- frame is globally updated at specific time slices, based on the animation 
  192.     -- frame delay. This means that two world locations with the same tile id 
  193.     -- will always update simultaneously, regardless of being set into the map 
  194.     -- at different times or not. 
  195.     -- 
  196.     -- An animation is looping if the frames field of the Animated_Info is 
  197.     -- non-null, otherwise it's a one-shot. 
  198.     type Animated_Info is 
  199.         record 
  200.             layer      : Integer;           -- layer of animated tile 
  201.             x, y       : Integer;           -- coordinates of animated tile 
  202.             nextUpdate : Time;              -- next time to update the frame 
  203.             frameDelay : Time_Span;         -- (for looping) animation frame delay 
  204.             frames     : A_Tile_Id_Array;   -- (for looping) frame list 
  205.         end record; 
  206.  
  207.     function Equivalent( l, r : Animated_Info ) return Boolean; 
  208.  
  209.     function Hash( a : Animated_Info ) return Hash_Type; 
  210.  
  211.     function Is_Looping( ai : Animated_Info ) return Boolean; 
  212.  
  213.     function Animated_Info_Input( stream : access Root_Stream_Type'Class ) return Animated_Info; 
  214.     for Animated_Info'Input use Animated_Info_Input; 
  215.  
  216.     procedure Animated_Info_Output( stream : access Root_Stream_Type'Class; info : Animated_Info ); 
  217.     for Animated_Info'Output use Animated_Info_Output; 
  218.  
  219.     package Animated_Set is new Ada.Containers.Hashed_Sets( Animated_Info, 
  220.                                                             Hash, Equivalent, 
  221.                                                             "=" ); 
  222.  
  223.     ---------------------------------------------------------------------------- 
  224.  
  225.     type World_Object is abstract new Object and Event_Listener and Process with 
  226.         record 
  227.             attached : Boolean := False; 
  228.             corral   : A_Corral; 
  229.             pman     : A_Process_Manager; 
  230.  
  231.             filename : Unbounded_String; 
  232.             lib      : A_Tile_Library := null; 
  233.             player   : Entity_Id := INVALID_ID; 
  234.  
  235.             -- the following fields are streamed 
  236.             map      : A_Map := null; 
  237.             libName  : Unbounded_String; 
  238.             domain   : Unbounded_String; 
  239.             title    : Unbounded_String; 
  240.             music    : Unbounded_String; 
  241.             entities : Entity_Map.Map; 
  242.             animated : Animated_Set.Set; 
  243.         end record; 
  244.  
  245.     procedure Adjust( this : access World_Object ); 
  246.  
  247.     -- Construction with an empty filename, map, libName and domain is allowed 
  248.     -- if the object is being constructed as part of a streaming read operation. 
  249.     -- The fields should be be immediately read in before the World is returned 
  250.     -- to the application for use. 
  251.     procedure Construct( this     : access World_Object; 
  252.                          filename : String; 
  253.                          map      : in out A_Map; 
  254.                          libName, 
  255.                          domain   : String ); 
  256.  
  257.     procedure Delete( this : in out World_Object ); 
  258.  
  259.     procedure Initialize( this : access World_Object ); 
  260.  
  261.     function Get_Process_Name( this : access World_Object ) return String; 
  262.     pragma Postcondition( Get_Process_Name'Result'Length > 0 ); 
  263.  
  264.     procedure Handle( this : access World_Object; evt : not null A_Delete_Entity_Event ); 
  265.  
  266.     procedure Handle( this : access World_Object; evt : not null A_Entities_Collided_Event ); 
  267.  
  268.     procedure Handle( this : access World_Object; evt : not null A_Entities_Separated_Event ); 
  269.  
  270.     procedure Handle( this : access World_Object; evt : not null A_Entity_Face_Event ); 
  271.  
  272.     procedure Handle( this : access World_Object; evt : not null A_Entity_Grounded_Event ); 
  273.  
  274.     procedure Handle( this : access World_Object; evt : not null A_Entity_Hit_Wall_Event ); 
  275.  
  276.     procedure Handle( this : access World_Object; evt : not null A_Entity_Moved_Event ); 
  277.  
  278.     procedure Handle( this : access World_Object; evt : not null A_Entity_Resized_Event ); 
  279.  
  280.     procedure Handle( this : access World_Object; evt : not null A_Set_Entity_Attribute_Event ); 
  281.  
  282.     procedure Handle( this : access World_Object; evt : not null A_Spawn_Entity_Event ); 
  283.  
  284.     procedure Handle_Event( this : access World_Object; 
  285.                             evt  : in out A_Event; 
  286.                             resp : out Response_Type ); 
  287.     pragma Precondition( evt /= null ); 
  288.  
  289.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out World_Object ); 
  290.     for World_Object'Read use Object_Read; 
  291.  
  292.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : World_Object ); 
  293.     for World_Object'Write use Object_Write; 
  294.  
  295.     -- Calls Queue_Load_Events 
  296.     procedure On_Attach( this : access World_Object ); 
  297.  
  298.     procedure On_Detach( this : access World_Object ); 
  299.  
  300.     -- Queues world loaded events in order to send the world to the game views. 
  301.     procedure Queue_Load_Events( this : access World_Object ); 
  302.  
  303.     procedure Tick( this : access World_Object; upTime, dt : Time_Span ); 
  304.  
  305.     function A_World_Input( stream : access Root_Stream_Type'Class ) return A_World; 
  306.     for A_World'Input use A_World_Input; 
  307.  
  308.     procedure A_World_Output( stream : access Root_Stream_Type'Class; world : A_World ); 
  309.     for A_World'Output use A_World_Output; 
  310.  
  311.     procedure A_World_Read( stream : access Root_Stream_Type'Class; world : out A_World ); 
  312.     for A_World'Read use A_World_Read; 
  313.  
  314.     procedure A_World_Write( stream : access Root_Stream_Type'Class; world : A_World ); 
  315.     for A_World'Write use A_World_Write; 
  316.  
  317.     ---------------------------------------------------------------------------- 
  318.  
  319.     type Allocator is 
  320.         access function( width, 
  321.                          height  : Positive; 
  322.                          libName, 
  323.                          domain  : String ) return A_World; 
  324.  
  325.     procedure Register( allocate      : not null Allocator; 
  326.                         identifier    : String; 
  327.                         read_version, 
  328.                         write_version : Positive ); 
  329.     pragma Precondition( identifier'Length > 0 ); 
  330.  
  331.     procedure Register_Domain( domain : String ); 
  332.     pragma Precondition( domain'Length > 0 ); 
  333.  
  334.     function World_Identifier return String; 
  335.  
  336. end Worlds;