Brenta Engine 1.2
Loading...
Searching...
No Matches
script.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <tenno/utility.hpp>
9#include <tenno/memory.hpp>
10
11#include <string>
12#include <filesystem>
13#include <optional>
14
15class lua_State;
16
17namespace brenta
18{
19
20class Node;
21
22//
23// Script
24// ------
25//
26// Scripts are an iterface for storing and running lua programs.
27//
28class Script
29{
30public:
31
32 friend class Node;
33
34 Script() = default;
35 Script(tenno::weak_ptr<Node> node, const std::string &source)
36 : source(source), node(node)
37 {
38 this->init();
39 }
40 Script(tenno::weak_ptr<Node> node, const std::filesystem::path &path)
41 : path(path), node(node)
42 {
43 this->init();
44 }
45 Script(Script&& other)
46 {
47 this->source = tenno::move(other.source);
48 this->path = tenno::move(other.path);
49 this->node = tenno::move(other.node);
50 this->state = other.state;
51 other.state = nullptr;
52 }
53 ~Script();
54
55 Script& operator=(Script&& other)
56 {
57 this->source = tenno::move(other.source);
58 this->path = tenno::move(other.path);
59 this->node = tenno::move(other.node);
60 this->state = other.state;
61 other.state = nullptr;
62
63 return *this;
64 }
65
66 void reload();
67 void update(float delta_time);
68
69private:
70
71 std::optional<std::string> source;
72 std::optional<std::filesystem::path> path;
73 lua_State *state = nullptr;
74 tenno::weak_ptr<Node> node;
75
76 void init();
77
78 static int lua_get_position(lua_State* L);
79 static int lua_set_position(lua_State* L);
80 static Node* current_node; // big non-thread safe hack for now
81};
82
83} // namespace brenta