Quellcode durchsuchen

Added PlayerManager class, partly implemented army recruitment scene

Ivan Arkhipov vor 6 Jahren
Ursprung
Commit
f984ff2407

+ 6 - 3
client.pro

@@ -31,7 +31,6 @@ SOURCES += \
     source/gui/gui.cpp \
     source/gui/recruitmentscene.cpp \
     source/units/unit.cpp \
-    source/player.cpp \
     source/gui/mainmenu.cpp \
     source/gui/guiscenemanager.cpp \
     source/gui/detatchedwidget.cpp \
@@ -40,7 +39,9 @@ SOURCES += \
     source/racemanager.cpp \
     source/units/mage.cpp \
     source/units/warrior.cpp \
-    source/gui/scene.cpp
+    source/gui/scene.cpp \
+    source/gui/raceicon.cpp \
+    source/playermanager.cpp
 
 HEADERS += \
     include/gui/uniticon.h \
@@ -57,7 +58,9 @@ HEADERS += \
     include/racemanager.h \
     include/units/mage.h \
     include/units/warrior.h \
-    include/gui/scene.h
+    include/gui/scene.h \
+    include/gui/raceicon.h \
+    include/playermanager.h
 
 FORMS += \
     include/gui/gui.ui \

+ 9 - 1
include/gui/playervsplayerintro.ui

@@ -107,7 +107,7 @@
     <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:12pt; color:#b12d2d;&quot;&gt;Кол-во монет:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
    </property>
   </widget>
-  <widget class="QLineEdit" name="lineEdit">
+  <widget class="QLineEdit" name="money_">
    <property name="geometry">
     <rect>
      <x>462</x>
@@ -121,6 +121,14 @@
    </property>
   </widget>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>Scene</class>
+   <extends>QWidget</extends>
+   <header location="global">gui/scene.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>

+ 64 - 0
include/gui/raceicon.h

@@ -0,0 +1,64 @@
+#ifndef RACEICON_H
+#define RACEICON_H
+
+#include "units/unit.h"
+
+#include <QObject>
+#include <QWidget>
+#include <QLabel>
+#include <QImage>
+#include <QPixmap>
+#include <QPainter>
+#include <QTime>
+
+class RaceIcon : public QLabel {
+Q_OBJECT
+
+public:
+    explicit RaceIcon(QWidget* parent = 0, int width = 64, int height = 64);
+    ~RaceIcon();
+
+    void setRaceIcon(QString racename);
+    void setRaceIcon(QImage icon);
+    void resize(int w, int h);
+    void unsetRaceIcon();
+
+    void deactivate();
+    void activate();
+
+
+protected:
+    void mousePressEvent(QMouseEvent* event);
+    void enterEvent(QEvent* event);
+    void leaveEvent(QEvent* event);
+
+
+signals:
+    void clicked();
+    void doubleclicked();
+    void hovered();
+    void unhovered();
+
+private:
+    void drawIcon();
+
+    int width_;
+    int height_;
+
+    int state_; // 0 = not initialized;
+                // 1 = default;
+                // 2 = hover;
+                // 3 = active;
+
+    QPixmap pixmap_;
+    QImage active_border_;
+    QImage hover_border_;
+    QImage default_border_;
+    QImage icon_;
+
+    QTime previous_click_time_;
+
+    Unit* unit_;
+};
+
+#endif // RACEICON_H

+ 14 - 6
include/gui/recruitmentscene.h

@@ -8,6 +8,7 @@
 #include <vector>
 
 class UnitIcon;
+class RaceIcon;
 class Player;
 
 namespace Ui {
@@ -22,22 +23,29 @@ public:
     explicit RecruitmentScene(QWidget *parent = 0);
     ~RecruitmentScene();
 
-    void ParseArgs(QString args) {
-        current_player_id_ = args.toInt();
-    }
+    void parseArgs(QString args) override;
+    void init() override;
 
-    void init() {
-
-    }
+private:
+    void initAvailableRaces();
+    void initAvailableUnits();
+    void initChosenUnits();
 
 private slots:
     void on_back_button_clicked();
 
 private:
+    int available_money_;
+    int current_money_;
+
     int current_player_id_;
+    Player* current_player_;
+
     Ui::RecruitmentScene *ui;
     UnitIcon* available_units_icons_[5];
     UnitIcon* chosen_units_icons_[10];
+
+    RaceIcon* available_races_icons_[3];
 };
 
 #endif // RECRUITMENTSCENE_H

+ 2 - 16
include/gui/recruitmentscene.ui

@@ -92,9 +92,9 @@
   <widget class="QLabel" name="title_">
    <property name="geometry">
     <rect>
-     <x>-3</x>
+     <x>0</x>
      <y>0</y>
-     <width>941</width>
+     <width>940</width>
      <height>71</height>
     </rect>
    </property>
@@ -416,19 +416,6 @@
     </rect>
    </property>
   </widget>
-  <widget class="QLabel" name="title_1">
-   <property name="geometry">
-    <rect>
-     <x>270</x>
-     <y>350</y>
-     <width>941</width>
-     <height>71</height>
-    </rect>
-   </property>
-   <property name="text">
-    <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:24pt; color:#b12d2d;&quot;&gt;Набор юнитов&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
-   </property>
-  </widget>
   <zorder>divider_</zorder>
   <zorder>chosen_race_text_</zorder>
   <zorder>chosen_race_image_</zorder>
@@ -445,7 +432,6 @@
   <zorder>apply</zorder>
   <zorder>unit_spec_widget_</zorder>
   <zorder>widget</zorder>
-  <zorder>title_1</zorder>
  </widget>
  <customwidgets>
   <customwidget>

+ 2 - 0
include/gui/uniticon.h

@@ -17,7 +17,9 @@ Q_OBJECT
 public:
     explicit UnitIcon(QWidget* parent = 0, int width = 64, int height = 64);
     ~UnitIcon();
+
     void setUnitIcon(QString racename, QString unitname);
+    void setUnitIcon(QImage icon);
     void resize(int w, int h);
     void unsetUnitIcon();
 

+ 18 - 10
include/player.h

@@ -2,6 +2,7 @@
 #define INCLUDEPLAYER_H
 
 #include "units/unit.h"
+#include "racemanager.h"
 
 #include <QObject>
 
@@ -12,25 +13,32 @@ class Race;
 template<class Obj, class Identifier>
 class ObjectFactory;
 
-template<int id>
 class Player {
-private:
+public:
     Player() {
 
     }
 
-public:
-    static Player& getInstance() {
-        static Player<id> instance;
-        return instance;
-    }
-
-    void init() {
+    void init(int id) {
         player_name_ = "Игрок " + QString::number(id);
-        race_ = nullptr;
+        race_ = RaceManager::getInstance().getRace(
+                    RaceManager::getInstance().getAvailableRacesList()[0]
+                );
         units_.clear();
     }
 
+    QString getPlayerName() {
+        return player_name_;
+    }
+
+    const std::set<Unit>& getUnits() {
+        return units_;
+    }
+
+    Race* getRace() {
+        return race_;
+    }
+
 signals:
 
 public slots:

+ 33 - 0
include/playermanager.h

@@ -0,0 +1,33 @@
+#ifndef INCLUDEPLAYERMANAGER_H
+#define INCLUDEPLAYERMANAGER_H
+
+#include <player.h>
+
+#include <QObject>
+
+#include <vector>
+
+class PlayerManager : public QObject
+{
+    Q_OBJECT
+private:
+    explicit PlayerManager(QObject *parent = nullptr);
+public:
+    static PlayerManager& getInstance() {
+        static PlayerManager instance;
+        return instance;
+    }
+
+    Player* getPlayer(int id);
+    int getPlayersNum();
+    void setPlayersNum(int num);
+
+private:
+    std::vector<Player> players_;
+
+signals:
+
+public slots:
+};
+
+#endif // INCLUDEPLAYERMANAGER_H

+ 11 - 0
include/race.h

@@ -3,6 +3,7 @@
 
 #include <QObject>
 #include <QString>
+#include <QImage>
 
 #include <vector>
 
@@ -19,11 +20,21 @@ public:
     Unit* createUnit(QString unit_name);
 
     const std::vector<QString> &getAvailableUnitsList();
+    const std::vector<Unit*> &getAllUnitsList();
+
+    QString getRaceId();
+    QString getRaceName();
+    QImage getRaceIcon();
+    QString getRaceDescr();
 
 private:
+    QString race_id_;
     QString race_name_;
+    QImage race_icon_;
+    QString race_descr_;
     ObjectFactory<Unit, QString> units_factory_;
     std::vector<QString> available_units_list_;
+    std::vector<Unit*> all_units_list_;
 
 signals:
 

+ 5 - 5
include/units/unit.h

@@ -110,11 +110,11 @@ public:
     //----------------GUI section------------------//
     //---------------------------------------------//
 
-    QString getUnitName();
-    QString getUnitDescr();
-    QString getUnitBaseClassId();
-    std::vector<QString> getUnitTraits();
-    QImage getUnitIcon();
+    QString getUnitName() const;
+    QString getUnitDescr() const;
+    QString getUnitBaseClassId() const;
+    std::vector<QString> getUnitTraits() const;
+    QImage getUnitIcon() const;
 
     //---------------------------------------------//
     //-----------Parameters load section-----------//

+ 4 - 4
source/gui/playervsplayerintro.cpp

@@ -1,6 +1,6 @@
 #include "gui/playervsplayerintro.h"
 #include "gui/guiscenemanager.h"
-#include "player.h"
+#include "playermanager.h"
 #include "gui/scene.h"
 
 #include "ui_playervsplayerintro.h"
@@ -24,7 +24,7 @@ void PlayerVsPlayerIntro::on_back_to_menu_clicked()
 
 void PlayerVsPlayerIntro::on_go_next_clicked()
 {
-    Player<1>::getInstance().init();
-    Player<2>::getInstance().init();
-    GuiSceneManager::getInstance().changeScene("recruit_army", "2");
+    PlayerManager::getInstance().setPlayersNum(2);
+    /// TODO - Check if money input correct
+    GuiSceneManager::getInstance().changeScene("recruit_army", "1|" + ui->money_->text());
 }

+ 122 - 0
source/gui/raceicon.cpp

@@ -0,0 +1,122 @@
+#include <gui/raceicon.h>.h>
+#include <QDebug>
+#include <QTime>
+
+RaceIcon::RaceIcon(QWidget* parent, int width, int height)
+    : QLabel(parent)
+{
+    state_ = 0;
+    width_ = width;
+    height_ = height;
+
+    setAttribute(Qt::WA_Hover, true);
+    installEventFilter(this);
+
+    default_border_.load(":/assets/common/unit_icon_default.png");
+    hover_border_.load(":/assets/common/unit_icon_hover.png");
+    active_border_.load(":/assets/common/unit_icon_active.png");
+
+    drawIcon();
+}
+
+RaceIcon::~RaceIcon()
+{}
+
+void RaceIcon::drawIcon() {
+    QPixmap combined(width_, height_);
+    QPainter p(&combined);
+
+    if (state_ == 0) {
+        p.setBrush(Qt::NoBrush);
+        p.setPen(Qt::black);
+        p.drawRect(0, 0, width_, height_);
+    } else {
+        p.drawImage(QPoint(0, 0), icon_.scaled(width_, height_, Qt::KeepAspectRatio));
+    }
+
+    if (state_ == 0 || state_ == 1)
+        p.drawImage(QPoint(0, 0), default_border_.scaled(width_, height_, Qt::KeepAspectRatio));
+
+    if (state_ == 2)
+        p.drawImage(QPoint(0, 0), hover_border_.scaled(width_, height_, Qt::KeepAspectRatio));
+
+    if (state_ == 3)
+        p.drawImage(QPoint(0, 0), active_border_.scaled(width_, height_, Qt::KeepAspectRatio));
+
+    p.end();
+    setPixmap(combined);
+}
+
+void RaceIcon::setRaceIcon(QString racename) {
+    icon_.load(":/assets/units/" + racename + "/icon.png");
+    if (state_ == 0)
+        state_ = 1;
+    drawIcon();
+}
+
+void RaceIcon::setRaceIcon(QImage icon) {
+    icon_ = icon;
+    if (state_ == 0)
+        state_ = 1;
+    drawIcon();
+}
+
+
+void RaceIcon::unsetRaceIcon() {
+    state_ = 0;
+    drawIcon();
+}
+
+void RaceIcon::resize(int w, int h) {
+    width_ = w;
+    height_ = h;
+    drawIcon();
+}
+
+void RaceIcon::deactivate() {
+    if (state_ > 1)
+        state_ = 1;
+    drawIcon();
+}
+
+void RaceIcon::activate() {
+    state_ = 3;
+    drawIcon();
+}
+
+void RaceIcon::mousePressEvent(QMouseEvent*)
+{
+    if (state_ == 0)
+        return;
+
+    activate();
+
+    QTime current_time = QTime::currentTime();
+    if (current_time.msecsSinceStartOfDay() - previous_click_time_.msecsSinceStartOfDay() < 200) {
+        deactivate();
+        emit doubleclicked();
+    } else {
+        previous_click_time_ = current_time;
+        emit clicked();
+    }
+}
+
+void RaceIcon::enterEvent(QEvent*)
+{
+    if (state_ == 0 || state_ == 3)
+        return;
+
+    state_ = 2;
+    drawIcon();
+    emit hovered();
+}
+
+void RaceIcon::leaveEvent(QEvent*)
+{
+    if (state_ == 0 || state_ == 3)
+        return;
+
+    state_ = 1;
+    drawIcon();
+    emit unhovered();
+}

+ 52 - 2
source/gui/recruitmentscene.cpp

@@ -1,10 +1,19 @@
 #include <gui/recruitmentscene.h>
 #include <gui/uniticon.h>
+#include <gui/raceicon.h>
 #include <gui/guiscenemanager.h>
 #include <gui/scene.h>
+#include <playermanager.h>
+#include <player.h>
+#include <racemanager.h>
+#include <race.h>
 
 #include <ui_recruitmentscene.h>
 #include <QHBoxLayout>
+#include <QString>
+#include <QStringList>
+
+#include <cassert>
 
 RecruitmentScene::RecruitmentScene(QWidget *parent)
     : Scene(parent)
@@ -48,13 +57,54 @@ RecruitmentScene::RecruitmentScene(QWidget *parent)
     available_units_icons_[4]->setUnitIcon("norace", "nounit");
 }
 
-
-
 RecruitmentScene::~RecruitmentScene()
 {
     delete ui;
 }
 
+void RecruitmentScene::parseArgs(QString args) {
+    QStringList list = args.split("|");
+    assert(list.size() >= 2);
+
+    current_player_id_ = list[0].toInt();
+    current_player_ = PlayerManager::getInstance().getPlayer(current_player_id_);
+    available_money_ = list[1].toInt();
+}
+
+void RecruitmentScene::init() {
+    current_money_ = available_money_;
+    initAvailableRaces();
+    initAvailableUnits();
+    initChosenUnits();
+}
+
+void RecruitmentScene::initAvailableRaces() {
+    auto races = RaceManager::getInstance().getAvailableRacesList();
+
+    int race_id = 0;
+    for (QString race_name : races) {
+        assert(race_id < 5);
+        auto race = RaceManager::getInstance().getRace(race_name);
+        available_races_icons_[race_id]->setRaceIcon(race_name);
+        race_id++;
+    }
+
+
+}
+
+void RecruitmentScene::initAvailableUnits() {
+    int unit_id = 0;
+    for (Unit* unit : current_player_->getRace()->getAllUnitsList()) {
+        available_units_icons_[unit_id]->setUnitIcon(unit->getUnitIcon());
+        unit_id++;
+    }
+}
+
+void RecruitmentScene::initChosenUnits() {
+
+}
+
+
 void RecruitmentScene::on_back_button_clicked()
 {
     GuiSceneManager::getInstance().changeScene("main_menu");

+ 7 - 0
source/gui/uniticon.cpp

@@ -55,6 +55,13 @@ void UnitIcon::setUnitIcon(QString racename, QString unitname) {
     drawIcon();
 }
 
+void UnitIcon::setUnitIcon(QImage icon) {
+    icon_ = icon;
+    if (state_ == 0)
+        state_ = 1;
+    drawIcon();
+}
+
 void UnitIcon::unsetUnitIcon() {
     state_ = 0;
     drawIcon();

+ 0 - 0
source/player.cpp


+ 23 - 0
source/playermanager.cpp

@@ -0,0 +1,23 @@
+#include "playermanager.h"
+
+#include <cassert>
+
+PlayerManager::PlayerManager(QObject *parent) : QObject(parent)
+{
+
+}
+
+Player* PlayerManager::getPlayer(int id) {
+    assert(id < players_.size());
+    return &players_[id];
+}
+
+int PlayerManager::getPlayersNum() {
+    return players_.size();
+}
+
+void PlayerManager::setPlayersNum(int num) {
+    players_.resize(num);
+    for (int i = 0; i < num; i++)
+        players_[i].init(i);
+}

+ 37 - 7
source/race.cpp

@@ -13,32 +13,62 @@
 
 Race::Race(QString race_name, QObject *parent) : QObject(parent)
 {
-    race_name_ = race_name;
+    race_id_ = race_name;
+
+    units_factory_.addClass<Warrior, QString>("Warrior");
+    units_factory_.addClass<Warrior, QString>("Mage");
 
     QFile file(":/assets/units/" + race_name + "/units.txt");
     QTextStream in(&file);
     QString unit_name = in.readLine();
 
-    units_factory_.addClass<Warrior, QString>("Warrior");
-    units_factory_.addClass<Warrior, QString>("Mage");
-
     while (!in.atEnd()) {
+        all_units_list_.push_back(createUnit(unit_name));
         available_units_list_.push_back(unit_name);
-        race_name = in.readLine();
+        unit_name = in.readLine();
     }
+
+    QFile file0(":/assets/units/" + race_name + "/racename.txt");
+    QTextStream in0(&file);
+    race_name_ = in0.readLine();
+
+    QFile file1(":/assets/units/" + race_name + "/descr.txt");
+    QTextStream in1(&file);
+    race_descr_ = in1.readAll();
+
+    race_icon_.load(":/assets/units/" + race_name + "/descr.txt");
 }
 
+QString Race::getRaceId() {
+    return race_id_;
+}
+
+QString Race::getRaceName() {
+    return race_name_;
+}
+
+QImage Race::getRaceIcon() {
+    return race_icon_;
+}
+
+QString Race::getRaceDescr() {
+    return race_descr_;
+}
 
 Unit* Race::createUnit(QString unit_name) {
     assert(std::count(available_units_list_.begin(), available_units_list_.end(), unit_name) != 0);
 
-    QFile file(":/asserts/units/" + race_name_ + "/" + unit_name + "/baseclass.txt");
+    QFile file(":/asserts/units/" + race_id_ + "/" + unit_name + "/baseclass.txt");
     QTextStream in(&file);
     QString class_id = in.readLine();
 
-    return units_factory_.createObject<QString>(class_id, race_name_ + "|" + unit_name);
+    return units_factory_.createObject<QString>(class_id, race_id_ + "|" + unit_name);
 }
 
 const std::vector<QString>& Race::getAvailableUnitsList() {
     return available_units_list_;
 }
+
+const std::vector<Unit*> &Race::getAllUnitsList() {
+    return all_units_list_;
+}

+ 5 - 5
source/units/unit.cpp

@@ -213,19 +213,19 @@ void Unit::moveToCell(Cell* destination) {
 	}
 }
 
-QString Unit::getUnitName() {
+QString Unit::getUnitName() const {
     return unit_name_;
 }
 
-QString Unit::getUnitDescr() {
+QString Unit::getUnitDescr() const {
     return unit_descr_;
 }
 
-QString Unit::getUnitBaseClassId() {
+QString Unit::getUnitBaseClassId() const {
     return base_class_id_;
 }
 
-std::vector<QString> Unit::getUnitTraits() {
+std::vector<QString> Unit::getUnitTraits() const {
     return {
             QString::number(health_points_),
             QString::number(attack_range_),
@@ -234,6 +234,6 @@ std::vector<QString> Unit::getUnitTraits() {
     };
 }
 
-QImage Unit::getUnitIcon() {
+QImage Unit::getUnitIcon() const {
     return unit_icon_;
 }