@@ -7,7 +7,9 @@ use vectarine_plugin_sdk::plugininterface::PluginInterface;
77
88use crate :: {
99 console:: print_warn,
10- game_resource:: { Resource , ResourceId , Status , script_resource:: ScriptResource } ,
10+ game_resource:: {
11+ Resource , ResourceId , ResourceManager , Status , script_resource:: ScriptResource ,
12+ } ,
1113 graphics:: batchdraw:: BatchDraw2d ,
1214 io:: { fs:: ReadOnlyFileSystem , process_events} ,
1315 lua_env:: { LuaEnvironment , lua_screen, print_lua_error_from_error} ,
@@ -27,6 +29,8 @@ pub struct Game {
2729 pub main_script_path : String ,
2830
2931 pub metrics_holder : Rc < RefCell < MetricsHolder > > ,
32+
33+ pub plugin_env : PluginEnvironment ,
3034}
3135
3236impl Game {
@@ -58,27 +62,39 @@ impl Game {
5862 project_info. default_screen_height ,
5963 ) ;
6064
65+ // Create all the things we need for a game
6166 let batch = BatchDraw2d :: new ( & gl) . expect ( "Failed to create batch 2d" ) ;
6267 let metrics = Rc :: new ( RefCell :: new ( MetricsHolder :: new ( ) ) ) ;
63- let lua_env = LuaEnvironment :: new ( batch , file_system, project_dir, metrics . clone ( ) ) ;
64- let mut game = Game :: from_lua ( & gl , lua_env , project_info. main_script_path . clone ( ) , metrics ) ;
65- game . load ( video , window ) ;
68+ let resources = Rc :: new ( ResourceManager :: new ( file_system, project_dir) ) ;
69+ let plugin_env = PluginEnvironment :: load_plugins ( & project_info. plugins , & resources ) ;
70+ let lua_env = LuaEnvironment :: new ( batch , metrics . clone ( ) , resources ) ;
6671
67- let plugin_env =
68- PluginEnvironment :: load_plugins ( & project_info. plugins , & game. lua_env . resources ) ;
69- plugin_env. init ( PluginInterface {
72+ // Make the game!
73+ let mut game = Game :: from_lua (
74+ & gl,
75+ lua_env,
76+ project_info. main_script_path . clone ( ) ,
77+ metrics,
78+ plugin_env,
79+ ) ;
80+
81+ game. load ( video, window) ;
82+ game. plugin_env . init ( PluginInterface {
7083 lua : & game. lua_env . lua ,
7184 } ) ;
7285
86+ // Load the starting script
7387 let path = Path :: new ( & game. main_script_path ) ;
7488 game. lua_env . resources . load_resource :: < ScriptResource > (
7589 path,
7690 gl,
7791 game. lua_env . lua . clone ( ) ,
7892 game. lua_env . default_events . resource_loaded_event . clone ( ) ,
7993 ) ;
80- // New game means new sounds, so we discard the previous ones.
94+
95+ // New game means new sounds, so we discard the previous ones (this is useful only for the editor).
8196 sound:: flush_all_samples ( ) ;
97+
8298 callback ( Ok ( game) ) ;
8399 }
84100
@@ -87,13 +103,15 @@ impl Game {
87103 lua_env : LuaEnvironment ,
88104 main_script_path : String ,
89105 metrics_holder : Rc < RefCell < MetricsHolder > > ,
106+ plugin_env : PluginEnvironment ,
90107 ) -> Self {
91108 Game {
92109 gl : gl. clone ( ) ,
93110 lua_env,
94111 was_main_script_executed : false ,
95112 main_script_path,
96113 metrics_holder,
114+ plugin_env,
97115 }
98116 }
99117
0 commit comments