NWengine 0.9
Loading...
Searching...
No Matches
Script.h
1#pragma once
2#include "GameObject.h"
3
4
5#define SCRIPT_CONSTR(scr) \
6 GameObject* goc; \
7 std::string __nwname = #scr; \
8 std::string GetName() { return __nwname; }; \
9 static Scriptable* GetScript(GameObject* goc); \
10 scr(GameObject* goc) { \
11 this->goc = goc; \
12}; \
13
18public:
19 GameObject* goc = nullptr;
20 std::string __nwname = "None";
27 Scriptable(GameObject* goc = nullptr, void* nws = nullptr) {
28 this->goc = goc;
29 };
30
31 virtual ~Scriptable() {};
36 virtual void Start() {};
37
41 virtual void Update() {};
42
46 virtual void Gui() {};
47
52 virtual void ShaderCode(void* sprite);
53
58 virtual std::string GetName() { return __nwname; };
59
60 void* nws = nullptr;
61};
62
66class Script : public GameComponent {
67public:
72 static std::string GetType() { return "Script";};
73
74 Script() {};
81
83 Scriptable* script = nullptr;
88 void Start() override;
89
93 void Update() override;
94
99 template<typename T>
100 void SetScript() {
101 delete script;
102 script = new T(this->attachedObj);
103 }
104
111 int Serialize(std::fstream* data, int offset) override;
112
119 int Deserialize(std::fstream* data, int offset) override;
120
121 static std::map<GameObject*, Script> componentList;
122};
Defines the GameObject class and its related components.
Base class for game components.
Definition GameObject.h:29
Class representing a game object.
Definition GameObject.h:68
The base class for script components.
Definition Script.h:66
Scriptable * script
Definition Script.h:83
static std::string GetType()
Gets the type of the script component.
Definition Script.h:72
int Serialize(std::fstream *data, int offset) override
Serializes the script data.
Definition Script.cpp:37
int Deserialize(std::fstream *data, int offset) override
Deserializes the script data.
Definition Script.cpp:55
static std::map< GameObject *, Script > componentList
Definition Script.h:121
void Update() override
Called every frame to update the script.
Definition Script.cpp:21
void SetScript()
Sets the script to the specified type.
Definition Script.h:100
GameObject * attachedObj
Definition Script.h:82
void Start() override
Called when the script is started.
Definition Script.cpp:16
The base class for scriptable components.
Definition Script.h:17
virtual std::string GetName()
Gets the name of the script.
Definition Script.h:58
std::string __nwname
Definition Script.h:20
GameObject * goc
Definition Script.h:19
virtual void Update()
Called every frame to update the script.
Definition Script.h:41
virtual void Start()
Called when the script is started.
Definition Script.h:36
virtual void ShaderCode(void *sprite)
Called to generate shader code for the script.
Definition Script.cpp:75
Scriptable(GameObject *goc=nullptr, void *nws=nullptr)
Constructs a Scriptable object.
Definition Script.h:27
virtual void Gui()
Called to draw the script's GUI.
Definition Script.h:46
void * nws
Definition Script.h:60