Quellcode durchsuchen

Modified Player class. Made all gui scenes classes inherit virtual Scene class

Ivan Arkhipov vor 6 Jahren
Ursprung
Commit
fe3ee40ae9

+ 4 - 2
client.pro

@@ -39,7 +39,8 @@ SOURCES += \
     source/race.cpp \
     source/racemanager.cpp \
     source/units/mage.cpp \
-    source/units/warrior.cpp
+    source/units/warrior.cpp \
+    source/gui/scene.cpp
 
 HEADERS += \
     include/gui/uniticon.h \
@@ -55,7 +56,8 @@ HEADERS += \
     include/race.h \
     include/racemanager.h \
     include/units/mage.h \
-    include/units/warrior.h
+    include/units/warrior.h \
+    include/gui/scene.h
 
 FORMS += \
     include/gui/gui.ui \

+ 4 - 2
include/gui/guiscenemanager.h

@@ -1,6 +1,8 @@
 #ifndef INCLUDEGUIGUISCENEMANAGER_H
 #define INCLUDEGUIGUISCENEMANAGER_H
 
+#include "gui/scene.h"
+
 #include <QObject>
 
 #include <map>
@@ -20,7 +22,7 @@ public:
         return instance;
     }
 
-    bool registerScene(QString scene_name, QWidget* name);
+    bool registerScene(QString scene_name, Scene* name);
 
     bool changeScene(QString scene_name, QString args = "");
 
@@ -29,7 +31,7 @@ public:
     void destroyDetachedScene(QString scene_name);
 
 private:
-    std::map<QString, QWidget*> scenes_;
+    std::map<QString, Scene*> scenes_;
     QString current_scene_;
     GUI* gui_;
 

+ 5 - 1
include/gui/mainmenu.h

@@ -1,13 +1,15 @@
 #ifndef MAINMENU_H
 #define MAINMENU_H
 
+#include "gui/scene.h"
+
 #include <QWidget>
 
 namespace Ui {
 class MainMenu;
 }
 
-class MainMenu : public QWidget
+class MainMenu : public Scene
 {
     Q_OBJECT
 
@@ -15,6 +17,8 @@ public:
     explicit MainMenu(QWidget *parent = 0);
     ~MainMenu();
 
+    void ParseArgs(QString args) {}
+
 private slots:
 
     void on_exit_button_clicked();

+ 9 - 1
include/gui/mainmenu.ui

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ui version="4.0">
  <class>MainMenu</class>
- <widget class="QWidget" name="MainMenu">
+ <widget class="Scene" name="MainMenu">
   <property name="geometry">
    <rect>
     <x>0</x>
@@ -56,6 +56,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>

+ 0 - 45
include/gui/mainwindow.ui

@@ -1,45 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ui version="4.0">
- <class>Form</class>
- <widget class="QWidget" name="Form">
-  <property name="geometry">
-   <rect>
-    <x>0</x>
-    <y>0</y>
-    <width>940</width>
-    <height>720</height>
-   </rect>
-  </property>
-  <property name="windowTitle">
-   <string>Form</string>
-  </property>
-  <widget class="QLabel" name="divider_">
-   <property name="geometry">
-    <rect>
-     <x>0</x>
-     <y>170</y>
-     <width>940</width>
-     <height>91</height>
-    </rect>
-   </property>
-   <property name="text">
-    <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;img src=&quot;:/assets/recruitscene/divider.png&quot; width=&quot;940&quot;/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
-   </property>
-  </widget>
-  <widget class="QLabel" name="title_">
-   <property name="geometry">
-    <rect>
-     <x>0</x>
-     <y>100</y>
-     <width>941</width>
-     <height>101</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;br&gt;название-которой-мы-ещё-не-придумали&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
-   </property>
-  </widget>
- </widget>
- <resources/>
- <connections/>
-</ui>

+ 4 - 1
include/gui/playervsplayerintro.h

@@ -1,13 +1,15 @@
 #ifndef PLAYERVSPLAYERINTRO_H
 #define PLAYERVSPLAYERINTRO_H
 
+#include "gui/scene.h"
+
 #include <QWidget>
 
 namespace Ui {
 class PlayerVsPlayerIntro;
 }
 
-class PlayerVsPlayerIntro : public QWidget
+class PlayerVsPlayerIntro : public Scene
 {
     Q_OBJECT
 
@@ -15,6 +17,7 @@ public:
     explicit PlayerVsPlayerIntro(QWidget *parent = 0);
     ~PlayerVsPlayerIntro();
 
+    void ParseArgs(QString args) {}
 private slots:
     void on_back_to_menu_clicked();
 

+ 1 - 1
include/gui/playervsplayerintro.ui

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ui version="4.0">
  <class>PlayerVsPlayerIntro</class>
- <widget class="QWidget" name="PlayerVsPlayerIntro">
+ <widget class="Scene" name="PlayerVsPlayerIntro">
   <property name="geometry">
    <rect>
     <x>0</x>

+ 13 - 1
include/gui/recruitmentscene.h

@@ -1,17 +1,20 @@
 #ifndef RECRUITMENTSCENE_H
 #define RECRUITMENTSCENE_H
 
+#include "gui/scene.h"
+
 #include <QWidget>
 
 #include <vector>
 
 class UnitIcon;
+class Player;
 
 namespace Ui {
 class RecruitmentScene;
 }
 
-class RecruitmentScene : public QWidget
+class RecruitmentScene : public Scene
 {
     Q_OBJECT
 
@@ -19,10 +22,19 @@ public:
     explicit RecruitmentScene(QWidget *parent = 0);
     ~RecruitmentScene();
 
+    void ParseArgs(QString args) {
+        current_player_id_ = args.toInt();
+    }
+
+    void init() {
+
+    }
+
 private slots:
     void on_back_button_clicked();
 
 private:
+    int current_player_id_;
     Ui::RecruitmentScene *ui;
     UnitIcon* available_units_icons_[5];
     UnitIcon* chosen_units_icons_[10];

+ 7 - 1
include/gui/recruitmentscene.ui

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ui version="4.0">
  <class>RecruitmentScene</class>
- <widget class="QWidget" name="RecruitmentScene">
+ <widget class="Scene" name="RecruitmentScene">
   <property name="geometry">
    <rect>
     <x>0</x>
@@ -448,6 +448,12 @@
   <zorder>title_1</zorder>
  </widget>
  <customwidgets>
+  <customwidget>
+   <class>Scene</class>
+   <extends>QWidget</extends>
+   <header location="global">gui/scene.h</header>
+   <container>1</container>
+  </customwidget>
   <customwidget>
    <class>UnitIcon</class>
    <extends>QLabel</extends>

+ 23 - 0
include/gui/scene.h

@@ -0,0 +1,23 @@
+#ifndef INCLUDEGUISCENE_H
+#define INCLUDEGUISCENE_H
+
+#include <QWidget>
+
+class Scene : public QWidget
+{
+    Q_OBJECT
+public:
+    explicit Scene(QWidget *parent = nullptr);
+
+    virtual ~Scene() {}
+
+    virtual void parseArgs(QString args) {}
+
+    virtual void init() {}
+
+signals:
+
+public slots:
+};
+
+#endif // INCLUDEGUISCENE_H

+ 2 - 2
include/gui/uniticon.h

@@ -1,6 +1,8 @@
 #ifndef UNITICON_H
 #define UNITICON_H
 
+#include "units/unit.h"
+
 #include <QObject>
 #include <QWidget>
 #include <QLabel>
@@ -9,8 +11,6 @@
 #include <QPainter>
 #include <QTime>
 
-#include <unit.h>
-
 class UnitIcon : public QLabel {
 Q_OBJECT
 

+ 22 - 9
include/player.h

@@ -1,21 +1,35 @@
 #ifndef INCLUDEPLAYER_H
 #define INCLUDEPLAYER_H
 
+#include "units/unit.h"
+
 #include <QObject>
 
 #include <set>
 
-class Unit;
-class Faction;
+class Race;
 
 template<class Obj, class Identifier>
 class ObjectFactory;
 
-class Player : public QObject
-{
-    Q_OBJECT
+template<int id>
+class Player {
+private:
+    Player() {
+
+    }
+
 public:
-    explicit Player(QObject *parent = nullptr);
+    static Player& getInstance() {
+        static Player<id> instance;
+        return instance;
+    }
+
+    void init() {
+        player_name_ = "Игрок " + QString::number(id);
+        race_ = nullptr;
+        units_.clear();
+    }
 
 signals:
 
@@ -23,9 +37,8 @@ public slots:
 
 private:
     QString player_name_;
-    //std::set<Unit*> units_;
-    //Faction* faction_;
-
+    std::set<Unit> units_;
+    Race* race_;
 };
 
 #endif // INCLUDEPLAYER_H

+ 7 - 0
include/units/unit.h

@@ -129,6 +129,13 @@ private:
     void loadUnitPrevSpecs(QString unit_folder);
     void loadUnitUpgradeSpecs(QString unit_folder);
 
+public:
+    bool operator <(const Unit &b) {
+        if (base_class_id_ != b.base_class_id_)
+            return base_class_id_ < b.base_class_id_;
+        return unit_id_ < b.unit_id_;
+    }
+
 protected:
     std::vector <Spell> skills_;
 

+ 2 - 2
source/gui/guiscenemanager.cpp

@@ -19,7 +19,7 @@ GuiSceneManager::GuiSceneManager(QObject *parent) : QObject(parent) {
 GuiSceneManager::~GuiSceneManager() {}
 
 
-bool GuiSceneManager::registerScene(QString scene_name, QWidget* name) {
+bool GuiSceneManager::registerScene(QString scene_name, Scene* name) {
     if (scene_name == "none")
         return false;
 
@@ -40,7 +40,7 @@ bool GuiSceneManager::changeScene(QString scene_name, QString args) {
     if (current_scene_ != "none")
         scenes_[current_scene_]->hide();
 
-    //scenes_[scene_name]->parseArgs(args);
+    scenes_[scene_name]->parseArgs(args);
     scenes_[scene_name]->show();
     current_scene_ = scene_name;
     return true;

+ 1 - 1
source/gui/mainmenu.cpp

@@ -6,7 +6,7 @@
 #include <QApplication>
 
 MainMenu::MainMenu(QWidget *parent) :
-    QWidget(parent),
+    Scene(parent),
     ui(new Ui::MainMenu)
 {
     ui->setupUi(this);

+ 7 - 2
source/gui/playervsplayerintro.cpp

@@ -1,9 +1,12 @@
 #include "gui/playervsplayerintro.h"
 #include "gui/guiscenemanager.h"
+#include "player.h"
+#include "gui/scene.h"
+
 #include "ui_playervsplayerintro.h"
 
 PlayerVsPlayerIntro::PlayerVsPlayerIntro(QWidget *parent) :
-    QWidget(parent),
+    Scene(parent),
     ui(new Ui::PlayerVsPlayerIntro)
 {
     ui->setupUi(this);
@@ -21,5 +24,7 @@ void PlayerVsPlayerIntro::on_back_to_menu_clicked()
 
 void PlayerVsPlayerIntro::on_go_next_clicked()
 {
-    GuiSceneManager::getInstance().changeScene("recruit_army");
+    Player<1>::getInstance().init();
+    Player<2>::getInstance().init();
+    GuiSceneManager::getInstance().changeScene("recruit_army", "2");
 }

+ 4 - 1
source/gui/recruitmentscene.cpp

@@ -1,12 +1,13 @@
 #include <gui/recruitmentscene.h>
 #include <gui/uniticon.h>
 #include <gui/guiscenemanager.h>
+#include <gui/scene.h>
 
 #include <ui_recruitmentscene.h>
 #include <QHBoxLayout>
 
 RecruitmentScene::RecruitmentScene(QWidget *parent)
-    : QWidget(parent)
+    : Scene(parent)
     , ui(new Ui::RecruitmentScene)
 {
     ui->setupUi(this);
@@ -47,6 +48,8 @@ RecruitmentScene::RecruitmentScene(QWidget *parent)
     available_units_icons_[4]->setUnitIcon("norace", "nounit");
 }
 
+
+
 RecruitmentScene::~RecruitmentScene()
 {
     delete ui;

+ 6 - 0
source/gui/scene.cpp

@@ -0,0 +1,6 @@
+#include "gui/scene.h"
+
+Scene::Scene(QWidget *parent) : QWidget(parent)
+{
+
+}

+ 0 - 6
source/player.cpp

@@ -1,6 +0,0 @@
-#include <player.h>
-
-Player::Player(QObject *parent) : QObject(parent)
-{
-
-}