diff --git a/src/zaxage_demo_01.prog.abap b/src/zaxage_demo_01.prog.abap index 3266607..d484cb1 100644 --- a/src/zaxage_demo_01.prog.abap +++ b/src/zaxage_demo_01.prog.abap @@ -15,45 +15,36 @@ REPORT zaxage_demo_01 NO STANDARD PAGE HEADING. INCLUDE zaxage_game_engine. - - CLASS main DEFINITION. PUBLIC SECTION. METHODS constructor. - METHODS interprete - IMPORTING - command TYPE clike - RETURNING - VALUE(result) TYPE REF TO result. METHODS init_view IMPORTING container TYPE REF TO cl_gui_container. METHODS init_inventory IMPORTING container TYPE REF TO cl_gui_container. - METHODS is_completed + METHODS interprete + IMPORTING + command TYPE clike RETURNING - VALUE(result) TYPE abap_bool. + VALUE(result) TYPE REF TO result. + METHODS show_inventory. METHODS get_location RETURNING VALUE(result) TYPE string. + METHODS is_completed + RETURNING + VALUE(result) TYPE abap_bool. PRIVATE SECTION. - DATA player TYPE REF TO actor. DATA bill_developer TYPE REF TO actor. DATA mark_consultant TYPE REF TO actor. - DATA map TYPE REF TO map. - DATA actors TYPE REF TO thinglist. DATA container TYPE REF TO cl_gui_container. DATA inventory_container TYPE REF TO cl_gui_container. DATA text TYPE REF TO cl_gui_textedit. DATA inventory TYPE REF TO cl_gui_textedit. - DATA mission_completed TYPE abap_bool. - METHODS show_inventory. - METHODS cmd_look - IMPORTING - result TYPE REF TO result - cmd2 TYPE string OPTIONAL. + DATA engine TYPE REF TO engine. ENDCLASS. @@ -79,16 +70,16 @@ CLASS main IMPLEMENTATION. METHOD constructor. - map = NEW #( ). + engine = NEW #( ). DATA(entrance) = NEW room( name = 'Entrance' descr = 'You are in the entrance area. Welcome.' ). DATA(developer) = NEW room( name = 'Developers office' descr = 'The developers area. be quiet!' ). DATA(consulting) = NEW room( name = 'Consulting Department' descr = 'This is the area where the consultants work. Bring coffee!' ). - map->add_room( entrance ). - map->add_room( developer ). - map->add_room( consulting ). - map->set_floor_plan( VALUE #( + engine->map->add_room( entrance ). + engine->map->add_room( developer ). + engine->map->add_room( consulting ). + engine->map->set_floor_plan( VALUE #( ( `+--------------------+ +--------------------+` ) ( `| | | |` ) ( `| | | |` ) @@ -129,8 +120,7 @@ CLASS main IMPLEMENTATION. needed = needed_to_open_box ). consulting->things->add( card_box ). - player = NEW #( name = 'PLAYER' descr = 'player name' ). - player->set_location( entrance ). + engine->player->set_location( entrance ). bill_developer = NEW #( name = 'Bill' descr = 'An ABAP developer' ). bill_developer->set_location( developer ). @@ -143,256 +133,38 @@ CLASS main IMPLEMENTATION. mark_consultant->add_sentences( VALUE #( ( |Hello, My name is Mark and I am an SAP consultant| ) ( |You can ask me anything about SAP processes.| ) ) ). - actors = NEW #( ). - actors->add( bill_developer ). - actors->add( mark_consultant ). + + engine->actors->add( bill_developer ). + engine->actors->add( mark_consultant ). ENDMETHOD. + + METHOD interprete. - DATA cmd TYPE c LENGTH 100. - - result = NEW #( ). - - cmd = to_upper( command ). - - SPLIT cmd AT space INTO DATA(cmd1) DATA(cmd2). - - CASE cmd1. - WHEN 'MAP'. - result->addtab( map->show( ) ). - - WHEN 'N' OR 'NORTH'. - IF player->location->north = room=>no_exit. - result->add( 'you cannot go there.' ). - ELSE. - player->set_location( player->location->north ). - ENDIF. - cmd_look( result ). - - WHEN 'S' OR 'SOUTH'. - IF player->location->south = room=>no_exit. - result->add( 'you cannot go there.' ). - ELSE. - player->set_location( player->location->south ). - ENDIF. - cmd_look( result ). - - WHEN 'E' OR 'EAST'. - IF player->location->east = room=>no_exit. - result->add( 'you cannot go there.' ). - ELSE. - player->set_location( player->location->east ). - ENDIF. - cmd_look( result ). - - WHEN 'W' OR 'WEST'. - IF player->location->west = room=>no_exit. - result->add( 'you cannot go there.' ). - ELSE. - player->set_location( player->location->west ). - ENDIF. - cmd_look( result ). - - WHEN 'HELP'. - - result->add( |N or NORTH Go to the room on the north side| ). - result->add( |E or EAST Go to the room on the east side| ). - result->add( |S or SOUTH Go to the room on the south side| ). - result->add( |W or WEST Go to the room on the west side| ). - result->add( |MAP Show map/ floor plan/ world| ). - result->add( || ). - result->add( |INV or INVENTARY Show everything you carry| ). - result->add( |LOOK Look what''s in the room| ). - result->add( |LOOK Have a closer look at the object in the room or in your inventory| ). - result->add( |TAKE Take object in the room| ). - result->add( |DROP Drop an object that you carry| ). - result->add( |OPEN Open something that is in the room| ). - result->add( || ). - result->add( |ASK Ask a person to tell you something| ). - - - WHEN 'LOOK'. - cmd_look( - result = result - cmd2 = cmd2 ). - - WHEN 'TAKE'. - IF player->location->things->get_list( ) IS INITIAL. - result->add( 'There is nothing you can take' ). - ELSE. - IF player->location->things->exists( cmd2 ). - result->add( |You take the { cmd2 }| ). - player->things->add( player->location->things->get( cmd2 ) ). - player->location->things->delete( cmd2 ). - ELSE. - result->add( |You cannot take the { cmd2 }| ). - ENDIF. - ENDIF. - - WHEN 'DROP'. - IF player->things->get_list( ) IS INITIAL. - result->add( 'There is nothing you can drop' ). - ELSE. - IF player->things->exists( cmd2 ). - result->add( |You drop the { cmd2 }| ). - player->location->things->add( player->things->get( cmd2 ) ). - player->things->delete( cmd2 ). - ELSE. - result->add( |You cannot drop the { cmd2 }| ). - ENDIF. - ENDIF. - - WHEN 'INV' OR 'INVENTORY'. - IF player->things->get_list( ) IS INITIAL. - result->add( 'You don''t carry anything' ). - ELSE. - result->add( 'You carry' ). - result->addtab( player->things->show( ) ). - ENDIF. - - WHEN 'OPEN'. - IF cmd2 IS INITIAL. - result->add( 'Open what?' ). - ELSEIF player->things->get_list( ) IS INITIAL - AND player->location->things->get_list( ) IS INITIAL. - result->add( 'There is nothing you can open...' ). - ELSE. - IF player->things->exists( cmd2 ). - DATA(thing) = player->things->get( cmd2 ). - ELSEIF player->location->things->exists( cmd2 ). - thing = player->location->things->get( cmd2 ). - ENDIF. - - IF thing IS INSTANCE OF openable_thing. - DATA(thing_to_open) = CAST openable_thing( thing ). - DATA finds TYPE string_table. - result->add( thing_to_open->open( player->things )->get( ) ). - IF thing_to_open->is_open( ). - - LOOP AT thing_to_open->get_content( )->get_list( ) INTO DATA(content). - APPEND |a { content->name }| TO finds. - ENDLOOP. - result->add( |The { thing->name } contains:| ). - result->addtab( finds ). - player->things->add( content ). - ENDIF. - ELSEif thing is bound. - result->add( |{ thing->name } cannot be opened!| ). - else. - result->add( |You cannot open { cmd2 }| ). - ENDIF. - ENDIF. - - WHEN 'ASK'. - DATA actors_in_the_room TYPE STANDARD TABLE OF REF TO actor. - DATA actor TYPE REF TO actor. - LOOP AT actors->get_list( ) INTO thing. - actor ?= thing. - IF actor->get_location( ) = player->location. - APPEND actor TO actors_in_the_room. - ENDIF. - ENDLOOP. - - IF actors_in_the_room IS INITIAL. - result->add( 'There is no one here to ask...' ). - ELSE. - IF cmd2 IS INITIAL. - result->add( 'Whom do you want to ask?' ). - ELSE. - LOOP AT actors_in_the_room INTO actor. - IF to_upper( actor->name ) = cmd2. - result->addtab( actor->speak( ) ). - ELSE. - result->add( |You cannot ask { cmd2 }| ). - ENDIF. - ENDLOOP. - ENDIF. - ENDIF. - - WHEN OTHERS. - result->add( 'You cannot do that' ). - ENDCASE. - -* result->add( |You are in the { player->location->name }.| ). " { player->location->description }|. + result = engine->interprete( command ). + + result->add( |You are in the { engine->player->location->name }.| ). " { player->location->description }|. text->set_textstream( result->get( ) ). - IF player->location->things->exists( 'RFC' ). - mission_completed = abap_true. + IF engine->player->location->things->exists( 'RFC' ). + engine->mission_completed = abap_true. ENDIF. show_inventory( ). ENDMETHOD. - METHOD is_completed. - result = mission_completed. - ENDMETHOD. - - METHOD show_inventory. - - DATA inv TYPE string. - LOOP AT player->things->get_list( ) INTO DATA(thing_inv). - IF inv IS INITIAL. - inv = |You are carrying:{ cl_abap_char_utilities=>cr_lf }|. - ENDIF. - inv = |{ inv }{ thing_inv->name } - { thing_inv->description }{ cl_abap_char_utilities=>cr_lf }|. - ENDLOOP. - IF inv IS INITIAL. - inv = |Your inventory is empty...|. - ENDIF. - inventory->set_textstream( inv ). - + inventory->set_textstream( engine->get_inventory( )->get( ) ). ENDMETHOD. - METHOD get_location. - result = player->location->name. + result = engine->get_location( ). ENDMETHOD. - - METHOD cmd_look. - - IF cmd2 IS INITIAL. - DATA actor TYPE REF TO actor. - LOOP AT actors->get_list( ) INTO DATA(thing). - actor ?= thing. - IF actor->get_location( ) = player->location. - result->add( |There is { actor->name }, { actor->description }| ). - ENDIF. - ENDLOOP. - - IF player->location->things->get_list( ) IS INITIAL. - result->add( 'There is nothing interesting to see...' ). - ELSE. - result->add( |You see| ). - result->addtab( player->location->things->show( ) ). - ENDIF. - - IF player->location->east <> room=>no_exit. - result->add( 'There is a door on the east side' ). - ENDIF. - IF player->location->west <> room=>no_exit. - result->add( 'There is a door on the west side' ). - ENDIF. - IF player->location->north <> room=>no_exit. - result->add( 'There is a door on the north side' ). - ENDIF. - IF player->location->south <> room=>no_exit. - result->add( 'There is a door on the south side' ). - ENDIF. - - ELSE. - IF player->location->things->exists( cmd2 ). - result->add( |It's { player->location->things->get( cmd2 )->description }| ). - ELSEIF player->things->exists( cmd2 ). - result->add( |It's { player->things->get( cmd2 )->description }| ). - ELSE. - result->add( |You cannot look at the { cmd2 }| ). - ENDIF. - ENDIF. - + METHOD is_completed. + result = engine->is_completed( ). ENDMETHOD. ENDCLASS. diff --git a/src/zaxage_game_engine.prog.abap b/src/zaxage_game_engine.prog.abap index 3f5c2bc..2ef982c 100644 --- a/src/zaxage_game_engine.prog.abap +++ b/src/zaxage_game_engine.prog.abap @@ -342,3 +342,285 @@ CLASS actor IMPLEMENTATION. my_sentences = sentences. ENDMETHOD. ENDCLASS. + +CLASS engine DEFINITION. + PUBLIC SECTION. + METHODS constructor. + METHODS interprete + IMPORTING + command TYPE clike + RETURNING + VALUE(result) TYPE REF TO result. + METHODS is_completed + RETURNING + VALUE(result) TYPE abap_bool. + METHODS get_location + RETURNING + VALUE(result) TYPE string. + METHODS get_inventory + returning + value(result) type ref to result. + DATA player TYPE REF TO actor. + DATA map TYPE REF TO map. + DATA actors TYPE REF TO thinglist. + DATA mission_completed TYPE abap_bool. + + PRIVATE SECTION. + DATA inventory_container TYPE REF TO cl_gui_container. + + METHODS cmd_look + IMPORTING + result TYPE REF TO result + cmd2 TYPE string OPTIONAL. + +ENDCLASS. + +CLASS engine IMPLEMENTATION. + + METHOD constructor. + + map = NEW #( ). + player = NEW #( name = 'PLAYER' descr = 'player name' ). + actors = NEW #( ). + + ENDMETHOD. + + METHOD interprete. + DATA cmd TYPE c LENGTH 100. + + result = NEW #( ). + + cmd = to_upper( command ). + + SPLIT cmd AT space INTO DATA(cmd1) DATA(cmd2). + + CASE cmd1. + WHEN 'MAP'. + result->addtab( map->show( ) ). + + WHEN 'N' OR 'NORTH'. + IF player->location->north = room=>no_exit. + result->add( 'you cannot go there.' ). + ELSE. + player->set_location( player->location->north ). + ENDIF. + cmd_look( result ). + + WHEN 'S' OR 'SOUTH'. + IF player->location->south = room=>no_exit. + result->add( 'you cannot go there.' ). + ELSE. + player->set_location( player->location->south ). + ENDIF. + cmd_look( result ). + + WHEN 'E' OR 'EAST'. + IF player->location->east = room=>no_exit. + result->add( 'you cannot go there.' ). + ELSE. + player->set_location( player->location->east ). + ENDIF. + cmd_look( result ). + + WHEN 'W' OR 'WEST'. + IF player->location->west = room=>no_exit. + result->add( 'you cannot go there.' ). + ELSE. + player->set_location( player->location->west ). + ENDIF. + cmd_look( result ). + + WHEN 'HELP'. + + result->add( |N or NORTH Go to the room on the north side| ). + result->add( |E or EAST Go to the room on the east side| ). + result->add( |S or SOUTH Go to the room on the south side| ). + result->add( |W or WEST Go to the room on the west side| ). + result->add( |MAP Show map/ floor plan/ world| ). + result->add( || ). + result->add( |INV or INVENTARY Show everything you carry| ). + result->add( |LOOK Look what''s in the room| ). + result->add( |LOOK Have a closer look at the object in the room or in your inventory| ). + result->add( |TAKE Take object in the room| ). + result->add( |DROP Drop an object that you carry| ). + result->add( |OPEN Open something that is in the room| ). + result->add( || ). + result->add( |ASK Ask a person to tell you something| ). + + + WHEN 'LOOK'. + cmd_look( + result = result + cmd2 = cmd2 ). + + WHEN 'TAKE'. + IF player->location->things->get_list( ) IS INITIAL. + result->add( 'There is nothing you can take' ). + ELSE. + IF player->location->things->exists( cmd2 ). + result->add( |You take the { cmd2 }| ). + player->things->add( player->location->things->get( cmd2 ) ). + player->location->things->delete( cmd2 ). + ELSE. + result->add( |You cannot take the { cmd2 }| ). + ENDIF. + ENDIF. + + WHEN 'DROP'. + IF player->things->get_list( ) IS INITIAL. + result->add( 'There is nothing you can drop' ). + ELSE. + IF player->things->exists( cmd2 ). + result->add( |You drop the { cmd2 }| ). + player->location->things->add( player->things->get( cmd2 ) ). + player->things->delete( cmd2 ). + ELSE. + result->add( |You cannot drop the { cmd2 }| ). + ENDIF. + ENDIF. + + WHEN 'INV' OR 'INVENTORY'. + IF player->things->get_list( ) IS INITIAL. + result->add( 'You don''t carry anything' ). + ELSE. + result->add( 'You carry' ). + result->addtab( player->things->show( ) ). + ENDIF. + + WHEN 'OPEN'. + IF cmd2 IS INITIAL. + result->add( 'Open what?' ). + ELSEIF player->things->get_list( ) IS INITIAL + AND player->location->things->get_list( ) IS INITIAL. + result->add( 'There is nothing you can open...' ). + ELSE. + IF player->things->exists( cmd2 ). + DATA(thing) = player->things->get( cmd2 ). + ELSEIF player->location->things->exists( cmd2 ). + thing = player->location->things->get( cmd2 ). + ENDIF. + + IF thing IS INSTANCE OF openable_thing. + DATA(thing_to_open) = CAST openable_thing( thing ). + DATA finds TYPE string_table. + result->add( thing_to_open->open( player->things )->get( ) ). + IF thing_to_open->is_open( ). + + LOOP AT thing_to_open->get_content( )->get_list( ) INTO DATA(content). + APPEND |a { content->name }| TO finds. + ENDLOOP. + result->add( |The { thing->name } contains:| ). + result->addtab( finds ). + player->things->add( content ). + ENDIF. + ELSEif thing is bound. + result->add( |{ thing->name } cannot be opened!| ). + else. + result->add( |You cannot open { cmd2 }| ). + ENDIF. + ENDIF. + + WHEN 'ASK'. + DATA actors_in_the_room TYPE STANDARD TABLE OF REF TO actor. + DATA actor TYPE REF TO actor. + LOOP AT actors->get_list( ) INTO thing. + actor ?= thing. + IF actor->get_location( ) = player->location. + APPEND actor TO actors_in_the_room. + ENDIF. + ENDLOOP. + + IF actors_in_the_room IS INITIAL. + result->add( 'There is no one here to ask...' ). + ELSE. + IF cmd2 IS INITIAL. + result->add( 'Whom do you want to ask?' ). + ELSE. + LOOP AT actors_in_the_room INTO actor. + IF to_upper( actor->name ) = cmd2. + result->addtab( actor->speak( ) ). + ELSE. + result->add( |You cannot ask { cmd2 }| ). + ENDIF. + ENDLOOP. + ENDIF. + ENDIF. + + WHEN OTHERS. + result->add( 'You cannot do that' ). + ENDCASE. + + ENDMETHOD. + + METHOD is_completed. + result = mission_completed. + ENDMETHOD. + + METHOD get_inventory. + + data inv type abap_bool. + + result = new #( ). + LOOP AT player->things->get_list( ) INTO DATA(thing_inv). + IF inv IS INITIAL. + result->add( |You are carrying:| ). + ENDIF. + result->add( |{ thing_inv->name } - { thing_inv->description }| ). + ENDLOOP. + + IF sy-subrc > 0. + result->add( |Your inventory is empty...| ). + ENDIF. + + ENDMETHOD. + + + METHOD get_location. + result = player->location->name. + ENDMETHOD. + + + METHOD cmd_look. + + IF cmd2 IS INITIAL. + DATA actor TYPE REF TO actor. + LOOP AT actors->get_list( ) INTO DATA(thing). + actor ?= thing. + IF actor->get_location( ) = player->location. + result->add( |There is { actor->name }, { actor->description }| ). + ENDIF. + ENDLOOP. + + IF player->location->things->get_list( ) IS INITIAL. + result->add( 'There is nothing interesting to see...' ). + ELSE. + result->add( |You see| ). + result->addtab( player->location->things->show( ) ). + ENDIF. + + IF player->location->east <> room=>no_exit. + result->add( 'There is a door on the east side' ). + ENDIF. + IF player->location->west <> room=>no_exit. + result->add( 'There is a door on the west side' ). + ENDIF. + IF player->location->north <> room=>no_exit. + result->add( 'There is a door on the north side' ). + ENDIF. + IF player->location->south <> room=>no_exit. + result->add( 'There is a door on the south side' ). + ENDIF. + + ELSE. + IF player->location->things->exists( cmd2 ). + result->add( |It's { player->location->things->get( cmd2 )->description }| ). + ELSEIF player->things->exists( cmd2 ). + result->add( |It's { player->things->get( cmd2 )->description }| ). + ELSE. + result->add( |You cannot look at the { cmd2 }| ). + ENDIF. + ENDIF. + + ENDMETHOD. + +ENDCLASS.