File indexing completed on 2024-05-12 05:39:49

0001 /***************************************************************************
0002  *   Copyright (C) 2015 by Renaud Guezennec                                *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #ifndef CHARACTER_H
0022 #define CHARACTER_H
0023 
0024 #include "characterstate.h"
0025 #include "network_global.h"
0026 #include "person.h"
0027 
0028 class CharacterSheet;
0029 class RolisteamImageProvider;
0030 
0031 #include <QList>
0032 #include <QMovie>
0033 
0034 class NETWORK_EXPORT CharacterField
0035 {
0036 public:
0037     enum Type
0038     {
0039         Shape,
0040         Action,
0041         Property
0042     };
0043     CharacterField();
0044     virtual ~CharacterField();
0045 
0046     virtual Type getType()= 0;
0047     virtual QVariant getData(int col, int role)= 0;
0048     virtual bool setData(int col, QVariant value, int role)= 0;
0049 };
0050 
0051 class NETWORK_EXPORT CharacterShape : public CharacterField
0052 {
0053 public:
0054     CharacterShape();
0055 
0056     bool hasMovie();
0057 
0058     QString name() const;
0059     void setName(const QString& name);
0060 
0061     const QImage& image() const;
0062     void setImage(const QImage& image);
0063 
0064     const QMovie& movie() const;
0065     void setMovie(const QMovie& movie);
0066 
0067     QString uri() const;
0068     void setUri(const QString& uri);
0069 
0070     virtual QVariant getData(int col, int role);
0071     virtual bool setData(int col, QVariant value, int role);
0072 
0073     virtual Type getType() { return Shape; }
0074     void setSize(int size);
0075 
0076 protected:
0077     void updateImage();
0078 
0079 private:
0080     QString m_name;
0081     QString m_uri;
0082     QImage m_image;
0083     QMovie m_movie;
0084     int m_size= 64;
0085 };
0086 
0087 class NETWORK_EXPORT CharacterAction : public CharacterField
0088 {
0089 public:
0090     CharacterAction();
0091 
0092     QString name() const;
0093     void setName(const QString& name);
0094 
0095     QString command() const;
0096     void setCommand(const QString& command);
0097 
0098     bool isValid() { return !m_command.isEmpty(); }
0099 
0100     virtual QVariant getData(int col, int role);
0101     virtual bool setData(int col, QVariant value, int role);
0102 
0103     virtual Type getType() { return Action; }
0104 
0105 private:
0106     QString m_name;
0107     QString m_command;
0108 };
0109 
0110 class NETWORK_EXPORT CharacterProperty : public CharacterField
0111 {
0112 public:
0113     CharacterProperty();
0114 
0115     QString name() const;
0116     void setName(const QString& name);
0117 
0118     QString value() const;
0119     void setValue(const QString& value);
0120 
0121     virtual QVariant getData(int col, int role);
0122     virtual bool setData(int col, QVariant value, int role);
0123 
0124     virtual Type getType() { return Property; }
0125 
0126 private:
0127     QString m_name;
0128     QString m_value;
0129 };
0130 
0131 /**
0132  * @brief Represents PCs and NPCs.
0133  *
0134  * They are stored inside the Player who own them.
0135  */
0136 class NETWORK_EXPORT Character : public Person
0137 {
0138     Q_OBJECT
0139     Q_PROPERTY(
0140         int healthPoints READ getHealthPointsCurrent WRITE setHealthPointsCurrent NOTIFY currentHealthPointsChanged)
0141     Q_PROPERTY(int maxHP READ getHealthPointsMax WRITE setHealthPointsMax NOTIFY maxHPChanged)
0142     Q_PROPERTY(int minHP READ getHealthPointsMin WRITE setHealthPointsMin NOTIFY minHPChanged)
0143     // Q_PROPERTY(QString avatarPath READ getAvatarPath WRITE setAvatarPath NOTIFY avatarPathChanged)
0144     Q_PROPERTY(bool isNpc READ isNpc WRITE setNpc NOTIFY npcChanged)
0145     Q_PROPERTY(int initiative READ getInitiativeScore WRITE setInitiativeScore NOTIFY initiativeChanged)
0146     Q_PROPERTY(qreal distancePerTurn READ getDistancePerTurn WRITE setDistancePerTurn NOTIFY distancePerTurnChanged)
0147     Q_PROPERTY(QString stateId READ stateId WRITE setStateId NOTIFY stateIdChanged)
0148     Q_PROPERTY(QColor lifeColor READ getLifeColor WRITE setLifeColor NOTIFY lifeColorChanged)
0149     Q_PROPERTY(QString initCommand READ initCommand WRITE setInitCommand NOTIFY initCommandChanged)
0150     Q_PROPERTY(bool hasInitiative READ hasInitScore WRITE setHasInitiative NOTIFY hasInitScoreChanged)
0151 
0152 public:
0153     // enum HeathState {Healthy,Lightly,Seriously,Dead,Sleeping,Bewitched};
0154     Character(QObject* parent= nullptr);
0155     /**
0156      * @brief construtor
0157      */
0158     Character(const QString& name, const QColor& getColor, bool NPC= false, int number= 0);
0159     /**
0160      * @brief Character
0161      * @param uuid
0162      * @param name
0163      * @param color
0164      */
0165     Character(const QString& uuid, const QString& name, const QColor& getColor, bool NPC= false, int number= 0);
0166     /**
0167      * @brief ~Character
0168      */
0169     virtual ~Character() override;
0170     /**
0171      * @brief isBool
0172      * @return
0173      */
0174     bool isNpc() const;
0175     /**
0176      * @brief setNpc
0177      */
0178     void setNpc(bool);
0179     /**
0180      * @brief number
0181      * @return
0182      */
0183     int number() const;
0184     /**
0185      * @brief setNumber
0186      * @param n
0187      */
0188     void setNumber(int n);
0189     /**
0190      * @brief getHeathState
0191      * @return
0192      */
0193     QString stateId() const;
0194     /**
0195      * @brief setListOfCharacterState
0196      */
0197     static void setListOfCharacterState(QList<CharacterState*>*);
0198 
0199     /**
0200      * @brief getCharacterStateList
0201      * @return
0202      */
0203     static QList<CharacterState*>* getCharacterStateList();
0204     /**
0205      * @brief getStateFromLabel
0206      * @param label
0207      * @return
0208      */
0209     static CharacterState* getStateFromLabel(QString label);
0210     /**
0211      * @brief getStateFromIndex
0212      * @param i
0213      * @return
0214      */
0215     static CharacterState* getStateFromIndex(int i);
0216     void setStateId(const QString& stateId);
0217 
0218     QImage currentStateImage() const;
0219     QString currentStateLabel() const;
0220 
0221     bool hasInitScore() const;
0222     void setHasInitiative(bool b);
0223 
0224     CharacterSheet* getSheet() const;
0225     void setSheet(CharacterSheet* sheet);
0226 
0227     Player* getParentPlayer() const;
0228     QString getParentId() const;
0229 
0230     virtual QHash<QString, QString> getVariableDictionnary() override;
0231 
0232     int indexOfState(CharacterState* state);
0233 
0234     void insertAction(const QString& name, const QImage& img, const QKeySequence& seq);
0235     void removeAction(const QString& name);
0236     void clearActions();
0237 
0238     int getHealthPointsMax() const;
0239     void setHealthPointsMax(int hpMax);
0240 
0241     int getHealthPointsMin() const;
0242     void setHealthPointsMin(int hpMin);
0243 
0244     int getHealthPointsCurrent() const;
0245     void setHealthPointsCurrent(int hpCurrent);
0246 
0247     int getInitiativeScore() const;
0248     void setInitiativeScore(int intiativeScore);
0249 
0250     qreal getDistancePerTurn() const;
0251     void setDistancePerTurn(const qreal& distancePerTurn);
0252 
0253     RolisteamImageProvider* getImageProvider() const;
0254     void setImageProvider(RolisteamImageProvider* imageProvider);
0255 
0256     QColor getLifeColor() const;
0257     void setLifeColor(QColor color);
0258 
0259     QString initCommand() const;
0260     void setInitCommand(const QString& init);
0261 
0262     QList<CharacterAction*> actionList() const;
0263     QList<CharacterShape*> shapeList() const;
0264     QList<CharacterProperty*> propertiesList() const;
0265 
0266     void defineActionList(const QList<CharacterAction*>& actions);
0267     void defineShapeList(const QList<CharacterShape*>& shape);
0268     void definePropertiesList(const QList<CharacterProperty*>& props);
0269 
0270     CharacterShape* currentShape() const;
0271     void setCurrentShape(CharacterShape* shape);
0272 
0273     virtual QByteArray avatar() const override;
0274 
0275 public slots:
0276     void setCurrentShape(int index);
0277     void addShape(const QString& name, const QString& path);
0278     void addShape(CharacterShape* shape);
0279     void addAction(CharacterAction* action);
0280     void addProperty(CharacterProperty* property);
0281 signals:
0282     void currentHealthPointsChanged();
0283     void maxHPChanged();
0284     void minHPChanged();
0285     void npcChanged();
0286     void initiativeChanged();
0287     void distancePerTurnChanged();
0288     void stateIdChanged(QString);
0289     void lifeColorChanged();
0290     void initCommandChanged();
0291     void hasInitScoreChanged();
0292 
0293 private:
0294     bool m_isNpc= true;
0295     int m_number= 0;
0296     static QList<CharacterState*>* m_stateList;
0297     QList<CharacterShape*> m_shapeList;
0298     QList<CharacterAction*> m_actionList;
0299     QList<CharacterProperty*> m_propertyList;
0300     QString m_stateId;
0301     CharacterSheet* m_sheet= nullptr;
0302     int m_healthPointsMax= 100;
0303     int m_healthPointsMin= 0;
0304     int m_healthPointsCurrent= 100;
0305     RolisteamImageProvider* m_imageProvider= nullptr;
0306     int m_initiativeScore= 0;
0307     CharacterAction m_initiativeRoll;
0308     qreal m_distancePerTurn= 0;
0309     QColor m_lifeColor= QColor(Qt::green);
0310     bool m_hasInitScore= false;
0311     CharacterShape* m_currentShape= nullptr;
0312 };
0313 Q_DECLARE_METATYPE(Character*)
0314 #endif // CHARACTER_H