Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with C++ OOP...
#11
Heh OOP is great! I've found a new and much better way to handle characters now Wink (Actually I read a similar idea in a book of mine and modified it somewhat...)

Here's the declaration for my CCharacter class (just to maybe inspire typosoft with some ideas for implementing OOP coding in his RPG... Wink... and to show off)

Code:
// CCHARACTER CLASS
// ----------------
// Character (NPC, Player, etc.) manager class

#include "RPGEngine.h"

#ifndef __CCHAR_INCLUDED__
#define __CCHAR_INCLUDED__

#define SOUTH    1
#define NORTH    2
#define WEST    3
#define EAST    4

#define CHAR_PLAYER        0
#define CHAR_NPC        1
#define CHAR_ENEMY        2

#define MAX_CHARS        256
#define MAX_CHAR_DEF    256
#define MAX_CHAR_ANIM    16

#define AI_RANDOM        0
#define AI_MOVE            1
#define AI_TARGET        2
#define AI_ATTACK        3
#define AI_RANGE        4
#define AI_ATTACKSTOP    5
#define AT_RANGESTOP    6

typedef struct sCharAnim {
    String Name;
    short start;            // Start tile index
    short frames;            // # of frames
    short delay;            // Delay time (in frames) between frame changing
    bool loop;                // Animation looping
    bool alldir;            // Animation includes all directions
    bool updatemove;        // Only update when in motion

    sCharAnim() {
        start = 0;
        frames = 0;
        delay = 0;
        alldir = false;
        loop = false;
        updatemove = false;
    }
} sCharAnim;

typedef struct sCharDef {
    String Name;            // Definition name
    CDXSurface* Sprites;    // Character sprites
    sCharAnim* Animation;    // Animation sequences
    short Level;            // Experience level
    short Experience;        // Experience points
    short Speed;            // Walking speed

    short HP;                // Health points
    short MP;                // Mana points
    short ToHit;            // Chance to hit
    float ChargeRate;        // Countdown rate to attack
    
    short Agility;            // Ability stats
    short Attack;
    short Defense;
    short Resistance;
    short Intelligence;

    short Weapon;            // Equipment
    short Armour;
    short Shield;
    short Accessory;

    sCharDef() {
        Sprites = new CDXSurface[128];
        Animation = new sCharAnim[MAX_CHAR_ANIM];
    }

    ~sCharDef() {
        delete[] Sprites;
        delete[] Animation;
    }
} sCharDef;

typedef struct sChar {
    bool Used;                // Used or not
    bool Enabled;            // Enabled or not (for updates)
    short Definition;        // Character definition number

    short x, y;                // Position
    short Direction;        // Facing direction
    short MoveLeft;            // Amount of pixels left to move
    short CurrentAni;        // Current animation sequence
    short CurrentFrame;        // Current frame in the animation sequence
    bool DoneAni;            // Done one loop of the animation
    short FrameWait;        // Frame delay counter

    short Type;                // Player, NPC, enemy
    short AI;                // Behaviour (only for NPC and enemy)

    sCharDef* Def;            // Definition structure

    short HP;                // Current health points
    short MP;                // Current mana points
    long Ailments;            // Ailments affecting this character;
    float Charge;            // Attack charge

    short ActionTimer;        // Action countdown timer

    String Message;            // Overlaid text message
    long MessageTimer;        // Time left to display message
    long MessageColour;        // Colour to display message

    sChar() {
        Used = false;
        Enabled = false;
        Definition = 0;
        Type = CHAR_NPC;
        AI = 0;
        x = y = 0;
        CurrentFrame = 0;
        FrameWait = 0;
        Direction = NORTH;
        Def = NULL;
        Charge = 0.0f;
        MoveLeft = 0;
        DoneAni = false;
    }

    ~sChar() {
        delete Def;
    }
} sChar;
    
class CCharacter
{
public:
    CCharacter(CEngine* engine);
    virtual ~CCharacter();

private:
    CEngine*    m_Engine;            // Engine class

    sChar*        m_Chars;            // Array of characters
    sCharDef*    m_CharDef;            // Array of character definitions

public:
    short Add(short x, short y, short direction, short type,
                short AI, String definition);
    short AddDefinition(String Name, String SpriteFile, short HP,
                short MP);
    void RemoveDefinition(String Name);
    short GetDefinitionIndex(String Name);
    void SetAnimation(String Definition, String Animation, short start,
                short frames, short delay, bool alldir, bool updatemove, bool loop);
    void Remove(short index);
    inline short GetCharX(short index)                { return m_Chars[index].x; }
    inline short GetCharY(short index)                { return m_Chars[index].y; }
    void GetCharPosition(short index, short* x, short* y);
    inline short GetCharDir(short index)                { return m_Chars[index].Direction; }
    inline short GetCharHP(short index)                { return m_Chars[index].HP; }
    inline short GetCharMP(short index)                { return m_Chars[index].MP; }
    inline void SetCharX(short index, short x)            { m_Chars[index].x = x; }
    inline void SetCharY(short index, short y)            { m_Chars[index].y = y; }
    void SetCharPosition(short index, short x, short y);
    inline void SetCharDir(short index, short direction)        { m_Chars[index].Direction = direction; }
    inline void SetCharHP(short index, short newhp)            { m_Chars[index].HP = newhp; }
    inline void SetCharMP(short index, short newmp)            { m_Chars[index].MP = newmp; }
    void SetCharAni(short index, String Animation);
    inline void SetEnabled(short index, bool Enable)        { m_Chars[index].Enabled = Enable; }
    void MoveChar(short index, short amount, short direction);
    void MoveChar(short index, short amount);
    bool MapCollide(short index);
    void SetMessage(short index, String MessageText, long Timer, long colour);
    void DrawChar(short index, CDXSurface* destSurf);
    void DrawChars(CDXSurface* destSurf);
    void UpdateCharAni(short index);
    void UpdateChar(short index);
    void UpdateChars();
    inline sChar* GetChar(short index)                { return &m_Chars[index]; }
    inline sCharDef* GetDefinition(short index)            { return &m_CharDef[index]; }
};

#endif

Big Grin
Reply
#12
heh, that's cool. I see what it says and everything, I just don't see how it would work in a program (because I don't know much about oop). Can you tell me what book it was that inspired you?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)