Functions from the base layer only

I am making this game but I am having a problem with structures. I created a class called Structure and other classes like Traps, Shelter, Fireplace inherit from this class. Tiles in the game have their own class (Tile) and have a list of structures on that tile. I can successfully build structures on the tiles that are listed. The problem comes when I try to access the functionality of classes like hooks, etc., it won't work. I can only use functions from the base class Structure.

List in tile:

class Tile
{
     public List<Structure> Structures = new List<Structure>();
}

      

How do I create a trap or other building:

        bool anyFireplace = Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.OfType<Shelter>().Any();
        if (!anyFireplace)
        {
            woodlogsCost = 4;
            if (Bundle.player.Woodlogs - woodlogsCost >= 0)
            {
                Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.Add(new Shelter(Bundle.player.X, Bundle.player.Y));
                Bundle.player.Woodlogs -= woodlogsCost;
            }
        }

      

When I draw Structures (this is where my problem is, notice the comments)

foreach (Structure s in Bundle.map.tile[x, y].Structures)
{
   if (s is Fireplace)
{
   //This is the function from base class Strucure
   s.ColorBody(g, 10, x - minx, y - miny, 0, Brushes.Firebrick);

   // The function that I wan´t to use but can´t be used
   //s.ColorBody(g, x - minx, y - miny); 
}
if (s is Shelter)
{
s.ColorBody(g, 10, x - minx, y - miny, 1, Brushes.ForestGreen);
}
if (s is Sleepingplace)
{
   s.ColorBody(g, 10, x - minx, y - miny, 2, Brushes.Brown);
}
if (s is Trap)
{
   s.ColorBody(g, 10, x - minx, y - miny, 3, Brushes.Silver);
}
if (s is Barricade)
{
   s.ColorBody(g, 10, x - minx, y - miny, 4, Brushes.DarkOliveGreen);
}
}

      

Soo ... I wonder how do I get access to features that I won't be using?

+3


source to share


3 answers


For computer s

use only Structure

. If you want to call a specific method that only a class has Fireplace

, but an abstract class Structure

does not (for example, a Fireplace

class might have a method BurnWood()

that doesn't make sense for Structure

), then you want to tell the computer that this Structure

one actually is also Fireplace

(for example). So you can do it by casting; eg:

((Fireplace)s).ColorBody(g, x - minx, y - miny); 

      

or



(s as Fireplace).ColorBody(g, x - minx, y - miny); 

      

See this post on the difference between casting and using operator as

.

+4


source


Add a virtual method to your base class;

public class Structure
{
   public virtual void ColorBody(Graphics g, int someParam1, int someParam2)
   {
       // do nothing in the base class
   }
}

      

and override the method in FireBody



public class FireBody : Structure
{
    public override void ColorBody(Graphics g, int someParam1, int someParam2)
    {
        // do something here for FireBody
    }
}

      

and if all classes inheriting from Structure

need it, make it abstract;

public abstract class Structure
{
   public abstract void ColorBody(Graphics g, int someParam1, int someParam2);
}

      

+6


source


As I can see from your question that you have two overloaded functions in two ie classes for base class and derived class, you have different numeric function functions

In this case you should get polymorphism type compilation support as shown below

 public BaseQueryClass
{
    public string QueryClassFunc(string mystring, int i)
    {
        return mystring + i.ToString();
    }
}

public QueryClassDerived : BaseQueryClass
{
    public string QueryClassFunc(string mystring)
    {
        return mystring;
    }
}

      

and you can call your methods like below.

 BaseQueryClass qClass = new BaseQueryClass();
        qClass.QueryClassFunc("mystring", 1);

        ((QueryClassDerived)qClass).QueryClassFunc("test");

      

Hope nothing is missing here

+1


source







All Articles