Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: c++ How to handle massive case type  (Read 1236 times)

0 Members and 1 Guest are viewing this topic.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
c++ How to handle massive case type
« on: July 20, 2013, 08:01:46 pm »
Hello.
What i am doing is a Class that is called "Interact", the point of class is to allow for type checking.
I am making 2d tile map, and i want to allow for player to "Interact" with map as in(case isBerryBush = harvest)(case Stone = mine(check if item(Pickaxe...)))
I hope i had provided you with what my idea is.

What i am having in mind
//Type of "Addon" on tile (BerryBush, Stone...)
int GetAddonType(TileClass &tile)//TileClass::Addon is defined as int
switch(tile.Addon)
{
type::BerryBush: return my_enum::Hand; break;
type::Stone: return my_enum::Hacking; break;//As in swing to use
}

//Has tool for
bool HasToolFor(my_enum type)
{
//Check player inventory if he has tool
}

//Is the tile ready for harvest
bool IsReadyForHarvest(int TileID)
{
//Returns if the tile has grown since last harvest/planting...
}

//Somewhere in main loop
if(Interact == pressed)//Ilustration
{
  if(GetAddonType(CurrentTileStandingOn) == Hand)
  {
    if(IsReadyForHarvest(CurrentTileStandingOn) == true)
    {
      //Add item to inventory, set tile to harvested...
    }
  }
  else if(GetAddonType(CurrentTileStandingOn) == HackingHand)
  {
    if(IsReadyForHarvest(CurrentTileStandingOn) == true)
    {
      if(HasToolFor(GetAddonType(CurrentTileStandingOn)) == true)
      {
        //Add item to inventory, set tile to harvested...
      }
    }
  }
}
 

The issues with this is that its syntax is horrible imo.
Wanna share a better way? pretty please!
« Last Edit: July 20, 2013, 10:53:56 pm by Laurent »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: c++ How to handle massive case type
« Reply #1 on: July 20, 2013, 11:36:54 pm »
That's exactly why people start using script language at this point. Having all the different checks in the actual game code, will bloat things quite a bit and you'll have to recompile every time. With scripting languages you could have all the not so nice parts external and organize it in a better way.
On the other hand scripts can also be overkill, when all it needs is a good objects abstraction with the possibility of polymorphous. To get to such a nice object design is not easy, but an essential part of programming, well at that point you'll notice that programming is often less about typing code and more about designing code structures and processes, thus the word "software engineer" fits better than "programmer".

I'm not sure if anyone will help you with getting a usable design but if there are such people around, you might want to help them and provide what you actually have, what the plan is and what you need. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/