R-Type
R-Type project
MapScript.hpp
1 #pragma once
2 
3 #include "KapEngine.hpp"
4 #include "Debug.hpp"
5 #include "Lua.hpp"
6 #include "Script/EnemyScript.hpp"
7 #include "LuaException.hpp"
8 #include <vector>
9 
10 namespace RType::Script {
11  class Enemy;
12 }
13 
14 namespace RType {
15  struct SpawnEnemy {
16  std::string name; // Name of the enemy
17  int spawnTime; // Time when the enemy will spawn (in seconds)
18  float startPositionY; // Start position Y of the enemy
19  float startPositionX; // Start position X of the enemy
20  int enemyHp; // Enemy HP
21  };
22 
23  class MapScript {
24  private:
25  KapEngine::KEngine &engine;
26  bool isLoadedByServer;
27 
28  lua_State *L = nullptr;
29 
30  std::string name; // Name of the map
31  std::string author; // Author of the map
32  std::string description; // Description of the map
33  std::string bannerPath; // Path of the banner of the map
34 
35  std::vector<Script::Enemy *> newEnemies; // List of new enemies
36  std::vector<SpawnEnemy> spawnEnemies; // List of enemies to spawn
37 
38  public:
39  explicit MapScript(KapEngine::KEngine *_engine, bool _isLoadedByServer = false);
40  ~MapScript();
41 
47  void loadScript(const std::string &scriptPath);
48 
52  void closeScript();
53 
58  std::string getName() const { return name; }
59 
64  std::string getAuthor() const { return author; }
65 
70  std::string getDescription() const { return description; }
71 
76  std::string getBannerPath() const { return bannerPath; }
77 
82  std::vector<SpawnEnemy> getSpawnedEnemies() const { return spawnEnemies; }
83 
88  bool isModded() const;
89 
98  void spawnEnemy(KapEngine::SceneManagement::Scene &scene, const std::string &enemyName, float startPositionY, float startPositionX,
99  int enemyHp);
100 
101  // Internal functions for Lua
102  void _setMapName(const std::string &name);
103  void _setMapAuthor(const std::string &author);
104  void _setMapDescription(const std::string &description);
105  void _setMapBannerPath(const std::string &bannerPath);
106  void _registerNewEnemy(Script::Enemy *enemy);
107  void _registerSpawnEnemy(const std::string &name, int spawnTime, float startPositionY, float startPositionX, int enemyHp);
108  void _instanciatePrefab(const std::string &prefabName, float positionX, float positionY);
109 
110  KapEngine::Tools::Vector3 _updateEnemy(const std::string &enemyName, const KapEngine::Tools::Vector3 &position);
111 
112  private:
113  void executeScript(const std::string &script);
114 
115  void verifScript();
116 
117  Script::Enemy *getNewEnemy(const std::string &enemyName);
118 
119  void checkNewEnemy(Script::Enemy *enemy);
120 
121  void createNewEnemy(Script::Enemy *enemy);
122 
123  void initScript();
124 
125  void destroyPrefabEnemies();
126  };
127 } // namespace RType
RType::Script::Enemy
Definition: EnemyScript.hpp:17
RType::MapScript::isModded
bool isModded() const
Is Modded Map (new enemies, custom path, ...).
Definition: MapScript.cpp:445
RType::MapScript::getAuthor
std::string getAuthor() const
Get map author.
Definition: MapScript.hpp:64
RType::MapScript::getBannerPath
std::string getBannerPath() const
Get map banner path.
Definition: MapScript.hpp:76
RType::MapScript::spawnEnemy
void spawnEnemy(KapEngine::SceneManagement::Scene &scene, const std::string &enemyName, float startPositionY, float startPositionX, int enemyHp)
Spawn enemy.
Definition: MapScript.cpp:390
RType::MapScript::getDescription
std::string getDescription() const
Get map description.
Definition: MapScript.hpp:70
RType::MapScript::getName
std::string getName() const
Get map name.
Definition: MapScript.hpp:58
RType::MapScript::closeScript
void closeScript()
Close map script.
Definition: MapScript.cpp:365
RType::MapScript
Definition: MapScript.hpp:23
KapEngine::Tools::Vector3
Definition: Vectors.hpp:251
KapEngine::SceneManagement::Scene
Definition: Scene.hpp:37
RType::MapScript::loadScript
void loadScript(const std::string &scriptPath)
Load map script.
Definition: MapScript.cpp:20
RType::MapScript::getSpawnedEnemies
std::vector< SpawnEnemy > getSpawnedEnemies() const
Get spawn enemies.
Definition: MapScript.hpp:82
KapEngine::KEngine
Class of engine.
Definition: Engine.hpp:60
RType::SpawnEnemy
Definition: MapScript.hpp:15