Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.11 KB | None | 0 0
  1. #include "GameCore.h"
  2. #include "CommandParser.h"
  3. #include <stdio.h>
  4. #include <cstring>
  5. #include <string>
  6. #include <vector>
  7.  
  8. namespace TextGame
  9. {
  10.     RoomData CreateRoom(const std::string& inName, const std::string& inDescription)
  11.     {
  12.         RoomData room = {};
  13.         room.Name = inName;
  14.         room.Description = inDescription;
  15.  
  16.         return room;
  17.     }
  18.  
  19.     ItemData CreateItem(ItemType itemType,
  20.         int inPositionX,
  21.         int inPositionY,
  22.         int quantity,
  23.         char icon,
  24.         const std::string& name)
  25.     {
  26.         ItemData item = {};
  27.         item.Type = itemType;
  28.         item.Icon = icon;
  29.         item.Name = name;
  30.         item.Quantity = quantity;
  31.         item.ItemPosition.X = inPositionX;
  32.         item.ItemPosition.Y = inPositionY;
  33.  
  34.         return item;
  35.     }
  36.  
  37.     void InitializeGame(PlayerState& playerState, WorldState& worldState)
  38.     {
  39.         printf("Welcome to...\n");
  40.         printf("The ASCII Temple of Doom by ME!\n\n");
  41.  
  42.         playerState.WantsDescription = true;
  43.         playerState.CurrentRoomIndex = 0;
  44.         playerState.CurrentPosition.X = playerState.NewPosition.X = 3;
  45.         playerState.CurrentPosition.Y = playerState.NewPosition.Y = 2;
  46.  
  47.         worldState.Rooms.clear();
  48.  
  49.         // 0
  50.         {
  51.             RoomData room = CreateRoom("INNER HALL", "After a long day of chasing children around in humid, 80 degree weather, the cherry on top is that you get to clean up their mess, too! What fun.\n\n\nRobin,\nPlease take care of the church and its premises after our yearly Easter event. And, if it's not too much trouble, do you mind gathering up any leftover eggs from the hunt? It seems as though there are about six missing from their bowls on the left side of the inner hall. I'll see you after my holiday in The Bahamas!\nGod bless,\nPaster Carl\n\n\nThanks, Pastor Carl. ");
  52.  
  53.             room.RoomMapWidth = 8;
  54.             room.RoomMap =
  55.                 "###..###"
  56.                 "#8.....#"
  57.                 "#8......"
  58.                 "#8.....#"
  59.                 "###..###";
  60.             room.RoomPosition.X = 0;
  61.             room.RoomPosition.Y = 0;
  62.  
  63.             ItemData item = CreateItem(ItemType_EGG, 6, 1, 1, 'O', "White Egg");
  64.             /*ItemData item = {};
  65.             item.Type = ItemType_BOOK;
  66.             item.Icon = 'b';
  67.             item.Name = "Book of Cooking + 1";
  68.             item.Quantity = 1;
  69.             item.ItemPosition.X = 2;
  70.             item.ItemPosition.Y = 1;*/
  71.  
  72.             room.Inventory.push_back(item);
  73.            
  74.  
  75.             worldState.Rooms.push_back(room);
  76.         }
  77.  
  78.         // 1
  79.         {
  80.             RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
  81.  
  82.             room.RoomMapWidth = 10;
  83.             room.RoomMap =
  84.                 "###...####"
  85.                 "#.....#..#"
  86.                 "#..####..#"
  87.                 "#........#"
  88.                 "##########";
  89.             room.RoomPosition.X = 0;
  90.             room.RoomPosition.Y = 1;
  91.  
  92.             ItemData item = CreateItem(ItemType_EGG2, 8, 1, 1, 'O', "Striped Egg");
  93.             room.Inventory.push_back(item);
  94.             worldState.Rooms.push_back(room);
  95.         }
  96.  
  97.         // 2
  98.         {
  99.             RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
  100.  
  101.             room.RoomMapWidth = 8;
  102.             room.RoomMap =
  103.                 "########"
  104.                 ".#...#.#"
  105.                 "...#...#"
  106.                 "###..###";
  107.             room.RoomPosition.X = 1;
  108.             room.RoomPosition.Y = 0;
  109.  
  110.             ItemData item = CreateItem(ItemType_EGG3, 6, 1, 1, 'O', "Spotted Egg");
  111.             room.Inventory.push_back(item);
  112.             worldState.Rooms.push_back(room);
  113.         }
  114.         // 3
  115.         {
  116.             RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
  117.  
  118.             room.RoomMapWidth = 6;
  119.             room.RoomMap =
  120.                 "###..#"
  121.                 "##...#"
  122.                 "###..#"
  123.                 "#...##"
  124.                 "#..###"
  125.                 "#...##"
  126.                 "######";
  127.             room.RoomPosition.X = 1;
  128.             room.RoomPosition.Y = 1;
  129.  
  130.             ItemData item = CreateItem(ItemType_EGG4, 3, 5, 1, 'O', "Golden Egg");
  131.             room.Inventory.push_back(item);
  132.             worldState.Rooms.push_back(room);
  133.         }
  134.         // 4
  135.         {
  136.             RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
  137.  
  138.             room.RoomMapWidth = 6;
  139.             room.RoomMap =
  140.                 "######"
  141.                 "#....#"
  142.                 "#....#"
  143.                 "#....."
  144.                 "#....#"
  145.                 "#....#"
  146.                 "#....#"
  147.                 "#....#"
  148.                 "#....#"
  149.                 "##..##";
  150.             room.RoomPosition.X = 0;
  151.             room.RoomPosition.Y = -1;
  152.  
  153.             ItemData item = CreateItem(ItemType_EGG5, 2, 7, 1, 'O', "Crystal Egg");
  154.             room.Inventory.push_back(item);
  155.             worldState.Rooms.push_back(room);
  156.         }
  157.         // 4
  158.         {
  159.             RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
  160.  
  161.             room.RoomMapWidth = 14;
  162.             room.RoomMap =
  163.                 "##############"
  164.                 "#............#"
  165.                 "#............#"
  166.                 ".............#"
  167.                 "##############";
  168.             room.RoomPosition.X = 0;
  169.             room.RoomPosition.Y = -1;
  170.  
  171.             ItemData item = CreateItem(ItemType_EGG6, 7, 1, 1, 'O', "Rotten Egg");
  172.             room.Inventory.push_back(item);
  173.             worldState.Rooms.push_back(room);
  174.         }
  175.     }
  176.  
  177.     void GetInput(PlayerState& playerState, const WorldState& worldState)
  178.     {
  179.         playerState.WantsToExit = false;
  180.         playerState.WantsDescription = false;
  181.         playerState.WantsInventoryListed = false;
  182.         playerState.WantsToGet = false;
  183.         playerState.WinCondition = false;
  184.        
  185.         printf("What do you do?\n");
  186.         printf("> ");
  187.         TextAdventureCommand command = ParseAdventureCommand();
  188.  
  189.         if (command.CommandType == CommandType_QUIT)
  190.         {
  191.             playerState.WantsToExit = true;
  192.         }
  193.         else if (command.CommandType == CommandType_NORTH)
  194.         {
  195.             playerState.NewPosition.Y = playerState.CurrentPosition.Y - 1;
  196.         }
  197.         else if (command.CommandType == CommandType_SOUTH)
  198.         {
  199.             playerState.NewPosition.Y = playerState.CurrentPosition.Y + 1;
  200.         }
  201.         else if (command.CommandType == CommandType_WEST)
  202.         {
  203.             playerState.NewPosition.X = playerState.CurrentPosition.X - 1;
  204.         }
  205.         else if (command.CommandType == CommandType_EAST)
  206.         {
  207.             playerState.NewPosition.X = playerState.CurrentPosition.X + 1;
  208.         }
  209.         else if (command.CommandType == CommandType_LOOK)
  210.         {
  211.             playerState.WantsDescription = true;
  212.         }
  213.         else if (command.CommandType == CommandType_INVENTORY)
  214.         {
  215.             playerState.WantsInventoryListed = true;
  216.         }
  217.         else if (command.CommandType == CommandType_GET)
  218.         {
  219.             playerState.WantsToGet = true;
  220.         }
  221.         else if (command.CommandType == CommandType_HELP)
  222.         {
  223.             printf("Command List: look, quit, inventory, get, north, south, west, east\n");
  224.             printf("Key:\n");
  225.             printf("  @ - Player\n");
  226.             printf("  8 - Cups\n");
  227.             printf("  O - Egg\n");
  228.             printf("  . - Floor\n");
  229.             printf("  # - Wall\n");
  230.             printf("  H - Door (Locked)\n");
  231.         }
  232.  
  233.         if(command.CommandType == CommandType_NONE)
  234.         {
  235.             printf("I don't understand\n");
  236.         }
  237.         printf("\n");
  238.  
  239.     }
  240.  
  241.     void RenderGame(const PlayerState& playerState, const WorldState& worldState)
  242.     {
  243.         if (playerState.WantsDescription)
  244.         {
  245.             const RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex];
  246.             printf("================================================\n");
  247.             printf("LOCATION: %s\n", currRoom.Name.c_str());
  248.             printf("%s\n\n", currRoom.Description.c_str());
  249.        
  250.             std::string mapString = "";
  251.             unsigned int currentSpace = 0;
  252.             while (currentSpace < currRoom.RoomMap.size())
  253.             {
  254.                 char characterToDisplay = currRoom.RoomMap[currentSpace];
  255.  
  256.                 for (unsigned int i = 0; i < currRoom.Inventory.size(); i++)
  257.                 {
  258.                     const ItemData& itemData = currRoom.Inventory[i];
  259.                     if (currentSpace == PositionToIndex(itemData.ItemPosition, currRoom.RoomMapWidth))
  260.                     {
  261.                         characterToDisplay = itemData.Icon;
  262.                     }
  263.                 }
  264.  
  265.                 if (currentSpace == PositionToIndex(playerState.CurrentPosition, currRoom.RoomMapWidth))
  266.                 {
  267.                     characterToDisplay = '@';
  268.                 }
  269.  
  270.                 mapString += characterToDisplay;
  271.                
  272.                 if(currentSpace % currRoom.RoomMapWidth == (currRoom.RoomMapWidth - 1))
  273.                 {
  274.                     mapString += '\n';
  275.                 }
  276.  
  277.                 currentSpace++;
  278.             }
  279.             printf("%s\n", mapString.c_str());
  280.  
  281.             printf("\n");
  282.         }
  283.         else if (playerState.WantsInventoryListed)
  284.         {
  285.             printf("================================================\n");
  286.             printf("INVENTORY:\n");
  287.  
  288.             if (playerState.Inventory.size() == 0)
  289.             {
  290.                 printf("Not even pocket lint. Figures.\n");
  291.             }
  292.             else
  293.             {
  294.                 for (unsigned int i = 0; i < playerState.Inventory.size(); i++)
  295.                 {
  296.                     const ItemData& currItem = playerState.Inventory[i];
  297.                     printf("%s: %d\n", currItem.Name.c_str(), currItem.Quantity);
  298.                 }
  299.             }
  300.  
  301.             printf("\n");
  302.         }
  303.     }
  304.  
  305.     void UpdateGame(PlayerState& playerState, WorldState& worldState)
  306.     {
  307.         RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex];
  308.  
  309.         //Player Movement Code
  310.         if (playerState.NewPosition != playerState.CurrentPosition)
  311.         {
  312.             if (IsSpaceOutsideMap(playerState.NewPosition, currRoom))
  313.             {
  314.                 int newRoomIndex = 0;
  315.  
  316.                 int playerExitType = 0;
  317.                 Position targetRoomPosition = currRoom.RoomPosition;
  318.                 if (playerState.NewPosition.X < 0)
  319.                 {
  320.                     //Exited West
  321.                     targetRoomPosition.X--;
  322.                     playerExitType = 1;
  323.                 }
  324.                 else if(playerState.NewPosition.X >= (int)currRoom.RoomMapWidth)
  325.                 {
  326.                     //Exited East
  327.                     targetRoomPosition.X++;
  328.                     playerExitType = 2;
  329.                 }
  330.                 else if (playerState.NewPosition.Y < 0)
  331.                 {
  332.                     //Exited North
  333.                     targetRoomPosition.Y--;
  334.                     playerExitType = 3;
  335.                 }
  336.                 else if (playerState.NewPosition.Y >= (int)(currRoom.RoomMap.size() / currRoom.RoomMapWidth))
  337.                 {
  338.                     //Exited South
  339.                     targetRoomPosition.Y++;
  340.                     playerExitType = 4;
  341.                 }
  342.  
  343.                 bool foundNewRoom = false;
  344.                 for (unsigned int i = 0; i < worldState.Rooms.size(); ++i)
  345.                 {
  346.                     if (worldState.Rooms[i].RoomPosition == targetRoomPosition)
  347.                     {
  348.                         foundNewRoom = true;
  349.                         newRoomIndex = i;
  350.                         break;
  351.                     }
  352.                 }
  353.  
  354.                 if (foundNewRoom)
  355.                 {
  356.                     playerState.CurrentRoomIndex = newRoomIndex;
  357.                     playerState.WantsDescription = true;
  358.  
  359.                     switch (playerExitType)
  360.                     {
  361.                     case 1:
  362.                         playerState.CurrentPosition.X = worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth - 1;
  363.                         break;
  364.                     case 2:
  365.                         playerState.CurrentPosition.X = 0;
  366.                         break;
  367.                     case 3:
  368.                         playerState.CurrentPosition.Y = (int)(worldState.Rooms[playerState.CurrentRoomIndex].RoomMap.size() / worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth) - 1;
  369.                         break;
  370.                     case 4:
  371.                         playerState.CurrentPosition.Y = 0;
  372.                         break;
  373.                     default:
  374.                         break;
  375.                     }
  376.                 }
  377.                 else
  378.                 {
  379.                     printf("There is nothing out there for you...\n");
  380.                     playerState.NewPosition = playerState.CurrentPosition;
  381.                 }
  382.             }
  383.             else if (IsSpaceOpenForMovement(playerState.NewPosition, currRoom))
  384.             {
  385.                 playerState.CurrentPosition = playerState.NewPosition;
  386.                 playerState.WantsDescription = true;
  387.             }
  388.             else
  389.             {
  390.                 printf("The path is blocked! Curses!\n");
  391.                 playerState.NewPosition = playerState.CurrentPosition;
  392.             }
  393.         }
  394.  
  395.         //Get Item Code
  396.         if (playerState.WantsToGet)
  397.         {
  398.             for (unsigned int i = 0; i < currRoom.Inventory.size(); i++)
  399.             {
  400.                 const ItemData& currItem = currRoom.Inventory[i];
  401.                 if (currItem.ItemPosition == playerState.CurrentPosition)
  402.                 {
  403.                     printf("Got a %s!\n", currItem.Name.c_str());
  404.  
  405.                     bool itemInInventory = false;
  406.                     for (unsigned int j = 0; j < playerState.Inventory.size(); i++)
  407.                     {
  408.                         if (playerState.Inventory[j].Type == currItem.Type)
  409.                         {
  410.                             playerState.Inventory[j].Quantity += currItem.Quantity;
  411.                             itemInInventory = true;
  412.                             break;
  413.                         }
  414.                     }
  415.                     if (!itemInInventory)
  416.                     {
  417.                         playerState.Inventory.push_back(currItem);
  418.                     }
  419.                    
  420.                     currRoom.Inventory.erase(currRoom.Inventory.begin() + i);
  421.                     break;
  422.                 }
  423.             }
  424.         }
  425.  
  426.         if (playerState.Inventory.size() == 6)
  427.         {
  428.             printf("You've done it! You found all six! You place the eggs back into the bowls in the inner hall. Now it's time to EXIT this church and binge watch seasons 1 through 7 of The Golden Girls. Hooray!");
  429.         }
  430.     }
  431.  
  432.  
  433.  
  434.  
  435. void CleanupGame(PlayerState& playerState, WorldState& worldState)
  436.     {
  437.         worldState.Rooms.clear();
  438.     }
  439.  
  440.     int PositionToIndex(const Position& position, int roomWidth)
  441.     {
  442.         return position.Y * roomWidth + position.X;
  443.     }
  444.  
  445.     bool IsSpaceOutsideMap(const Position& position, const RoomData& currRoom)
  446.     {
  447.         return position.Y < 0 ||
  448.             position.X < 0 ||
  449.             position.X >= (int)currRoom.RoomMapWidth ||
  450.             position.Y >= (int)(currRoom.RoomMap.size() / currRoom.RoomMapWidth);
  451.     }
  452.  
  453.     bool IsSpaceOpenForMovement(const Position& position, const RoomData& currRoom)
  454.     {
  455.         int spaceIndex = PositionToIndex(position, currRoom.RoomMapWidth);
  456.         char currentSpace = currRoom.RoomMap[spaceIndex];
  457.  
  458.         /*
  459.         if(currentSpace == '.')
  460.         {
  461.             return true;
  462.         }
  463.         else
  464.         {
  465.             return false;
  466.         }
  467.         */
  468.         return currentSpace == '.';
  469.     }
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement