Inherited varibles are not reading correctly when using bitwise comparisons

Posted by Shawn B on Stack Overflow See other posts from Stack Overflow or by Shawn B
Published on 2010-03-30T17:18:58Z Indexed on 2010/03/30 17:23 UTC
Read the original article Hit count: 480

Filed under:
|
|
|
|

Hey,

I have a few classes set up for a game, with XMapObject as the base, and XEntity, XEnviron, and XItem inheriting it. MapObjects have a number of flags, one of them being MAPOBJECT_SOLID. My problem is, that XEntity is the only class that correctly detects MAPOBJECT_SOLID. Both Items are Environs are always considered solid by the game, regardless of the flag's state. What is important, is that Environs and Item should almost never be solid.

Here are the relevent code samples:

XMapObject:

class XMapObject : public XObject
{
    public:
    Uint8 MapObjectType,Location[2],MapObjectFlags;

    XMapObject *NextMapObject,*PrevMapObject;

    XMapObject();

    void CreateMapObject(Uint8 MapObjectType);
    void SpawnMapObject(Uint8 MapObjectLocation[2]);
    void RemoveMapObject();
    void DeleteMapObject();

    void MapObjectSetLocation(Uint8 Y,Uint8 X);
    void MapObjectMapLink();
    void MapObjectMapUnlink();
};

XEntity:

class XEntity : public XMapObject
{
    public:
    Uint8 Health,EntityFlags;
    float Speed,Time;
    XEntity *NextEntity,*PrevEntity;
    XItem *IventoryList;

    XEntity();

    void CreateEntity(Uint8 EntityType,Uint8 EntityLocation[2]);
    void DeleteEntity();

    void EntityLink();
    void EntityUnlink();

    Uint8 MoveEntity(Uint8 YOffset,Uint8 XOffset);
};

XEnviron:

class XEnviron : public XMapObject
{
    public:
    Uint8 Effect,TimeOut;

    void CreateEnviron(Uint8 Type,Uint8 Y,Uint8 X,Uint8 TimeOut);
};

XItem:

class XItem : public XMapObject
{
    public:
    void CreateItem(Uint8 Type,Uint8 Y,Uint8 X);
};

And lastly, the entity move code. Only entities are capable of moving themselves.

Uint8 XEntity::MoveEntity(Uint8 YOffset,Uint8 XOffset)
{
    Uint8 
      NewY = Location[0] + YOffset,
      NewX = Location[1] + XOffset;

    if((NewY >= 0 && NewY < MAPY) && (NewX >= 0 && NewX < MAPX))
    {
        XTile *Tile = GetTile(NewY,NewX);

        if(Tile->MapList != NULL)
        {
            XMapObject *MapObject = Tile->MapList;

            while(MapObject != NULL)
            {
                if(MapObject->MapObjectFlags & MAPOBJECT_SOLID)
                {
                    printf("solid\n");
                    return 0;
                }

                MapObject = MapObject->NextMapObject;
            }
        }

        if(Tile->Flags & TILE_SOLID && EntityFlags & ENTITY_CLIPPING)
        {
            return 0;
        }

        this->MapObjectSetLocation(NewY,NewX);

        return 1;
    }

    return 0;
}

What is wierd, is that the bitwise operator always returns true when the MapObject is an Environ or an Item, but it works correctly for Entities. For debug I am using the printf "Solid", and also a printf containing the value of the flag for both Environs and Items.

Any help is greatly appreciated, as this is a major bug for the small game I am working on.

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance