aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Script.h
blob: e6fbb5c8b95a8342cfc924853ecde10286f741fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include <vector>

namespace crepe {

class ScriptSystem;
class BehaviorScript;

/**
 * \brief Script interface
 *
 * This class is used as a base class for user-defined scripts that can be
 * added to game objects using the \c BehaviorScript component.
 */
class Script {
	friend class crepe::ScriptSystem;

protected:
	/**
	 * \brief Script initialization function
	 *
	 * This function is called during the ScriptSystem::update() routine *before*
	 * Script::update() if it (a) has not yet been called and (b) the \c
	 * BehaviorScript component holding this script instance is active.
	 */
	virtual void init() {}
	/**
	 * \brief Script update function
	 *
	 * This function is called during the ScriptSystem::update() routine if the
	 * \c BehaviorScript component holding this script instance is active.
	 */
	virtual void update() {}
	// NOTE: additional *events* (like unity's OnDisable and OnEnable) should be
	// implemented as member methods in derivative user script classes and
	// registered in init(), otherwise this class will balloon in size with each
	// added event.

protected:
	/**
	 * \brief Get single component of type \c T on this game object (utility)
	 *
	 * \tparam T Type of component
	 *
	 * \returns Reference to component
	 *
	 * \throws nullptr if this game object does not have a component matching
	 * type \c T
	 */
	template <typename T>
	T & get_component() const;

	/**
	 * \brief Get all components of type \c T on this game object (utility)
	 *
	 * \tparam T Type of component
	 *
	 * \returns List of component references
	 */
	template <typename T>
	std::vector<std::reference_wrapper<T>> get_components() const;

private:
	friend class crepe::BehaviorScript;
	BehaviorScript * parent = nullptr;
};

} // namespace crepe

#include "Script.hpp"