开发者

Plugin architecture with multple plugin types (XNA)

开发者 https://www.devze.com 2023-03-25 20:54 出处:网络
I\'m looking to design a multi purpose RPG engine for myself in XNA, and would like to make it reliant on plugins. For the most part, I understand the whole general concept... and I have a basic IPlug

I'm looking to design a multi purpose RPG engine for myself in XNA, and would like to make it reliant on plugins. For the most part, I understand the whole general concept... and I have a basic IPlugin interface.

However, the problem arises when you realize you need several types of plugins for different systems, and a way to accommodate it all. In fact, you could say I开发者_开发百科'm trying to make everything modulated. Should I use multiple interfaces, that implement different things? Different host interfaces? Any tips are appreciated. :)


Depends how you gonna manage and also look from performance perspective on this. BUt yu can do something like this. Pseudocode:

public class Host {

   HostEventDispatcher dispatcher = new HostEventDispatcher ();
   MenuTypes {File, Tools, Options, Help}
   ToolBarTypes {MainBar}

   AddSubMenuToMainMenu(IMenu menu, MenuTypes hostMenu);
   AddSubMenuToMainMenu(IToolbar toolbar, ToolBarTypes hostToolbar);   

   public void LoadPlugin(IPlugin plugin) 
   {
       plugin.Dispatcher = dispatcher;
   }
}

interface IMenu {/* control warpper implementation */} 

interface IToolBar {/* control wrapper implementation */}

public HostEventDispatcher {
   //raises events to subscribers (Host, plugins)

   /***** event list ***/
}

public interface IPlugin 
{
    Dispatcher {get;set} //using this plugin can raise/recieve evets to/from Host.
}

Very basic idea. But it for sure will become more complicated in real implementation, and can easily become much more complicated after. Keep attention on complexity as it affects performance cause it's something that easily can be missed following "maximum flexibility" pattern.

Regards.


You don't have a IPlugin interface. You have an IEnemy interface and an IWeapon interface etc.


You can use components, either your own or gameComponents, that are by default inactive. Then you can yourself choose which ones to activate with for example a xml doc.

<Plugins>
  <Plugin name="MyNamespace.CombatManager"/>
  <Plugin name="MyNamespace.GuiManager"/>
</Plugins>

And then you can activate the selected plugins:

using System.Xml.Linq;
public void ActivatePlugins(string filePath)
{
    XElement plugins = XElement.Load(filePath);
    foreach(var plugin in plugins.Elements("Plugin"))
    {
        for(int i = 0; i < Components.Count; i++)
        {
            if(Type.GetType(plugin.Value) == Components[i].GetType()) 
            { ((GameCompoent)Components[i]).Enabled = true; }
            break;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消