File: debug_mode.ss

package info (click to toggle)
opensurge 0.6.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,404 kB
  • sloc: ansic: 62,066; sh: 739; makefile: 193; java: 110; xml: 66; javascript: 53
file content (229 lines) | stat: -rw-r--r-- 6,450 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// -----------------------------------------------------------------------------
// File: debug_mode.ss
// Description: Debug Mode
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------

/*

The Debug Mode lets you edit elements of the level. It may be accessed:
- on Mobile: via the Mobile Menu at the Pause Screen
- on Desktop: press Shift+F12 during gameplay

You leave it by pressing the back button on your smartphone / gamepad or the
ESC key on your keyboard.

You can extend the Debug Mode by creating your own plugins. Doing so is as
simple as creating objects tagged "debug-mode-plugin" and saving them to the
debug_mode/plugins/ folder. Example:

// File: debug_mode/plugins/my_plugin.ss
object "Debug Mode - My Plugin" is "debug-mode-plugin"
{
    state "main"
    {
        Console.print("Hello from My Plugin!");
    }
}

In order to keep compatibility with the official releases of the engine, do not
change the scripts of the Debug Mode directly. Create your own plugins instead.

Visible elements are usually rendered in screen space. Generally, you should
add the tags "detached", "private" and "entity" to the objects you wish to see.
Study the plugins of the Debug Mode for practical examples.

Plugins may optionally implement functions onLoad() and onUnload() for
initialization and deinitialization of resources, respectively. Example:

// File: debug_mode/plugins/my_other_plugin.ss
object "Debug Mode - My Other Plugin" is "debug-mode-plugin"
{
    fun onLoad(debugMode) // debugMode is the "Debug Mode" object below
    {
        Console.print("Loading this plugin");
    }

    fun onUnload(debugMode)
    {
        Console.print("Unloading this plugin");
    }
}

Plugins can also interact with each other. I'll let you figure this out.

Study how the existing plugins are made. Some are simple and short. The
possibilities are limitless!

Happy hacking! ;)

*/

using SurgeEngine.Level;
using SurgeEngine.Vector2;

// The Debug Mode object manages its plugins
object "Debug Mode" is "entity", "awake", "private"
{
    plugins = [];
    indicesOfPluginsScheduledForRemoval = [];
    cleared = false;

    state "main"
    {
        // spawn all plugins in ascending order
        pluginNames = System.tags.select("debug-mode-plugin").sort(null);
        for(i = 0; i < pluginNames.length; i++) {
            plugin = spawn(pluginNames[i]);
            plugins.push(plugin);
        }

        // load all plugins
        // (ideally we would have a dependency graph)
        foreach(plugin in plugins)
            _loadPlugin(plugin);

        // done!
        state = "active";
    }

    state "active"
    {
        // do nothing
        if(indicesOfPluginsScheduledForRemoval.length == 0)
            return;

        // unload plugins
        for(i = 0; i < indicesOfPluginsScheduledForRemoval.length; i++) {
            index = indicesOfPluginsScheduledForRemoval[i];
            _unloadPlugin(plugins[index]);
        }

        // destroy plugins
        for(i = 0; i < indicesOfPluginsScheduledForRemoval.length; i++) {
            index = indicesOfPluginsScheduledForRemoval[i];
            plugins[index].destroy();

            // remove from the plugins array
            if(index < plugins.length - 1)
                plugins[index] = plugins.pop(); // length -= 1
            else
                plugins.pop();
        }

        // no more plugins scheduled for removal
        indicesOfPluginsScheduledForRemoval.clear();
    }

    state "exit"
    {
        // unload all plugins
        foreach(plugin in plugins)
            _unloadPlugin(plugin);

        // destroy all plugins
        foreach(plugin in plugins)
            plugin.destroy();
        plugins.clear();

        // exit debug mode
        cleared = true;
        destroy();
    }



    // exit the debug mode
    fun exit()
    {
        // exit() may be called twice or more...
        state = "exit"; // don't destroy() the object right away
    }

    // get a plugin
    fun plugin(pluginName)
    {
        plugin = this.child(pluginName);
        if(plugin !== null && plugins.indexOf(plugin) >= 0)
            return plugin;

        Application.crash("Can't find Debug Mode Plugin: " + pluginName);
        return null;
    }

    // check if a plugin is in use
    fun hasPlugin(pluginName)
    {
        plugin = this.child(pluginName);
        return (plugin !== null && plugins.indexOf(plugin) >= 0);
    }

    // remove a plugin
    fun removePlugin(pluginName)
    {
        for(i = plugins.length - 1; i >= 0; i--) {
            if(plugins[i].__name == pluginName) {

                // don't allow duplicates (this function may be called repeatedly)
                if(indicesOfPluginsScheduledForRemoval.indexOf(i) < 0)
                    indicesOfPluginsScheduledForRemoval.push(i);

                break;

            }
        }
    }

    // internal use
    fun constructor()
    {
        // validate
        assert(parent === Level);
        assert(parent.children(this.__name).length == 1);

        // enable the Debug Mode flag
        Level.debugMode = true;
    }

    fun destructor()
    {
        // disable the Debug Mode flag
        Level.debugMode = false;

        // did the user destroy() the Debug Mode object?
        if(!cleared) {
            // plugin.onUnload() is not being called...
            Console.print("Use exit() to leave the debug mode!");
        }

        // note: the engine may be closed while the debug mode is activated
    }

    fun _loadPlugin(plugin)
    {
        if(plugin.findObjectWithTag("debug-mode-plugin") !== null)
            Application.crash("Debug Mode: nested plugins are not acceptable.");

        uiComponents = plugin.findObjectsWithTag("debug-mode-ui-component");
        foreach(uiComponent in uiComponents) {
            if(uiComponent.hasFunction("onLoad"))
                uiComponent.onLoad(this);
        }

        if(plugin.hasFunction("onLoad"))
            plugin.onLoad(this);
    }

    fun _unloadPlugin(plugin)
    {
        if(plugin.hasFunction("onUnload"))
            plugin.onUnload(this);

        uiComponents = plugin.findObjectsWithTag("debug-mode-ui-component");
        foreach(uiComponent in uiComponents) {
            if(uiComponent.hasFunction("onUnload"))
                uiComponent.onUnload(this);
        }
    }
}