From 1abac470be794e5091146da87e80bac30a3a73f0 Mon Sep 17 00:00:00 2001 From: yhh <359807859@qq.com> Date: Wed, 17 Jul 2019 14:59:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLEditor/core/EditorCommands.cs | 16 +-- CLEditor/viewmodel/MainViewModel.cs | 4 + .../framework/FrameworkManager.cs | 77 +++++++++++++ .../CLEngine.Core/framework/IDataFramework.cs | 19 ++++ Engine/CLEngine.Core/framework/ItemManager.cs | 25 +++-- .../CLEngine.Core/framework/SkillManager.cs | 103 ++++++++++++++++++ 6 files changed, 225 insertions(+), 19 deletions(-) create mode 100644 Engine/CLEngine.Core/framework/FrameworkManager.cs create mode 100644 Engine/CLEngine.Core/framework/IDataFramework.cs create mode 100644 Engine/CLEngine.Core/framework/SkillManager.cs diff --git a/CLEditor/core/EditorCommands.cs b/CLEditor/core/EditorCommands.cs index 8ccb3c6..e07809f 100644 --- a/CLEditor/core/EditorCommands.cs +++ b/CLEditor/core/EditorCommands.cs @@ -12,6 +12,7 @@ using CLEngine.Editor.controls; using CLEngine.Editor.model; using CLEngine.Editor.windows; using CLEngine.Core; +using CLEngine.Core.framework; using Clipboard = System.Windows.Clipboard; using MessageBox = System.Windows.Forms.MessageBox; @@ -172,14 +173,12 @@ namespace CLEngine.Editor.core { SceneManager.GameProject = CProject.Load(filename); - //File.Copy("Farseer Engine MonoGame OpenGL.dll", SceneManager.GameProject.ProjectPath + "\\Farseer Engine MonoGame OpenGL.dll", true); File.Copy("CLEngine.Core.dll", SceneManager.GameProject.ProjectPath + "\\CLEngine.Core.dll", true); File.Copy("Project Templates\\CLEngine.Core.xml", SceneManager.GameProject.ProjectPath + "\\CLEngine.Core.xml", true); File.Copy("Project Templates\\CLEngine.Windows.exe", SceneManager.GameProject.ProjectPath + "\\CLEngine.Windows.exe", true); CHelper.CopyDirectory("Project Templates\\libs", SceneManager.GameProject.ProjectPath + "", true); File.Copy("Xceed.Wpf.Toolkit.dll", SceneManager.GameProject.ProjectPath + "\\Xceed.Wpf.Toolkit.dll", true); - //File.Copy("OpenTK.dll", SceneManager.GameProject.ProjectPath + "\\OpenTK.dll", true); - // load user settings: + if (!File.Exists(SceneManager.GameProject.ProjectPath + "\\_userPrefs.pgb")) { UserPreferences.Instance = new UserPreferences(); @@ -196,7 +195,10 @@ namespace CLEngine.Editor.core EditorHandler.SceneTreeView.CreateView(); EditorHandler.ProjectTreeView.CreateView(); - CompilerWindow cf = new CompilerWindow(); + // 加载游戏框架数据库 + FrameworkManager.LoadAllData(); + + CompilerWindow cf = new CompilerWindow(); cf.ShowDialog(); bool success = cf.Result; @@ -206,13 +208,11 @@ namespace CLEngine.Editor.core { LoadLastScene(); - EditorCommands.ShowOutputMessage("工程加载成功"); - - //kryptonNavigator1.SelectedIndex = 1; + ShowOutputMessage("工程加载成功"); } else { - EditorCommands.ShowOutputMessage("项目加载脚本错误"); + ShowOutputMessage("项目加载脚本错误"); } EditorHandler.Settings = new IniFile(SceneManager.GameProject.ProjectPath + "\\settings.ini"); diff --git a/CLEditor/viewmodel/MainViewModel.cs b/CLEditor/viewmodel/MainViewModel.cs index 0c47662..05274f0 100644 --- a/CLEditor/viewmodel/MainViewModel.cs +++ b/CLEditor/viewmodel/MainViewModel.cs @@ -10,6 +10,7 @@ using Microsoft.Xna.Framework; using System.Collections.Generic; using System.Windows.Forms; using System.Windows.Media; +using CLEngine.Core.framework; namespace CLEngine.Editor.ViewModel { @@ -50,6 +51,9 @@ namespace CLEngine.Editor.ViewModel /// Ϸݿ /// public RelayCommand DataBaseCommand { get; set; } + /// + /// ¼֪ͨ + /// public NotificationMessageManager Manager { get; set; } = App.Manager; public MainViewModel() diff --git a/Engine/CLEngine.Core/framework/FrameworkManager.cs b/Engine/CLEngine.Core/framework/FrameworkManager.cs new file mode 100644 index 0000000..d220041 --- /dev/null +++ b/Engine/CLEngine.Core/framework/FrameworkManager.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; + +namespace CLEngine.Core.framework +{ + /// + /// 框架管理器 + /// + public static class FrameworkManager + { + private static Dictionary _dataFrameworks; + + static FrameworkManager() + { + _dataFrameworks = new Dictionary(); + RegisterDataFramework("item", new ItemManager()); + RegisterDataFramework("skill", new SkillManager()); + } + + /// + /// 注册事件框架 + /// + /// + public static void RegisterDataFramework(string name, IDataFramework framework) + { + _dataFrameworks.Add(name, framework); + } + + /// + /// 取消注册事件框架 + /// + /// 框架名 + /// 是否成功 + public static bool UnRegisterDataFramework(string name) + { + return _dataFrameworks.Remove(name); + } + + /// + /// 获取数据框架 + /// + /// 数据类型 + /// 数据名称 + /// 数据 + public static T GetDataFramework(string name) where T : IDataFramework + { + return (T)_dataFrameworks[name]; + } + + /// + /// 加载所有数据 + /// + public static void LoadAllData() + { + foreach (var dataFramework in _dataFrameworks) + dataFramework.Value.LoadData(); + } + + /// + /// 保存所有数据 + /// + public static void SaveAllData() + { + foreach (var dataFramework in _dataFrameworks) + dataFramework.Value.SaveData(); + } + + /// + /// 改变指定数据 + /// + /// 名称 + /// 数据 + public static void ChangeData(string name, T data) where T : IDataFramework + { + _dataFrameworks[name] = data; + } + } +} \ No newline at end of file diff --git a/Engine/CLEngine.Core/framework/IDataFramework.cs b/Engine/CLEngine.Core/framework/IDataFramework.cs new file mode 100644 index 0000000..e15dece --- /dev/null +++ b/Engine/CLEngine.Core/framework/IDataFramework.cs @@ -0,0 +1,19 @@ +namespace CLEngine.Core.framework +{ + /// + /// 数据框架接口 + /// 用于读取/加载数据 + /// + public interface IDataFramework + { + /// + /// 加载数据 + /// + void LoadData(); + + /// + /// 保存数据 + /// + void SaveData(); + } +} \ No newline at end of file diff --git a/Engine/CLEngine.Core/framework/ItemManager.cs b/Engine/CLEngine.Core/framework/ItemManager.cs index 795b62f..a562e0a 100644 --- a/Engine/CLEngine.Core/framework/ItemManager.cs +++ b/Engine/CLEngine.Core/framework/ItemManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.Serialization; using Path = System.IO.Path; namespace CLEngine.Core.framework @@ -8,20 +9,24 @@ namespace CLEngine.Core.framework /// /// 物品管理器 /// - public static class ItemManager +#if WIN + [Serializable] +#endif + [DataContract] + public class ItemManager : IDataFramework { /// /// 全局物品 /// - private static Dictionary _worldItem; + [DataMember] private static Dictionary _worldItem; /// /// 玩家背包 /// - private static List _playerItem; + [DataMember] private static List _playerItem; /// /// 全局物品Id /// - public static int WorldId { get; private set; } + [DataMember] public static int WorldId { get; private set; } static ItemManager() { @@ -78,7 +83,7 @@ namespace CLEngine.Core.framework /// /// 保存全局物品 /// - public static void SaveWorldItem() + public void SaveData() { if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) throw new Exception("未加载工程前无法保存物品信息"); @@ -87,8 +92,7 @@ namespace CLEngine.Core.framework Directory.CreateDirectory(itemPath); try { - CHelper.SerializeObject(Path.Combine(itemPath, "worldItem.db"), _worldItem); - CHelper.SerializeObject(Path.Combine(itemPath, "worldId.db"), WorldId); + CHelper.SerializeObject(Path.Combine(itemPath, "worldItem.db"), this); Logger.Info("物品保存成功"); } catch (Exception e) @@ -100,7 +104,7 @@ namespace CLEngine.Core.framework /// /// 加载全局物品 /// - private static void LoadWorldItem() + public void LoadData() { if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) throw new Exception("未加载工程前无法加载物品信息"); @@ -108,9 +112,8 @@ namespace CLEngine.Core.framework var itemPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Items"); try { - _worldItem = - (Dictionary) CHelper.DeserializeObject(Path.Combine(itemPath, "worldItem.db")); - WorldId = (int) CHelper.DeserializeObject(Path.Combine(itemPath, "worldId.db")); + var itemManager = (ItemManager) CHelper.DeserializeObject(Path.Combine(itemPath, "worldItem.db")); + FrameworkManager.ChangeData("item", itemManager); Logger.Info("物品加载成功"); } catch (Exception e) diff --git a/Engine/CLEngine.Core/framework/SkillManager.cs b/Engine/CLEngine.Core/framework/SkillManager.cs new file mode 100644 index 0000000..b66d867 --- /dev/null +++ b/Engine/CLEngine.Core/framework/SkillManager.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization; + +namespace CLEngine.Core.framework +{ + /// + /// 技能管理器 + /// +#if WIN + [Serializable] +#endif + [DataContract] + public class SkillManager : IDataFramework + { + /// + /// 全局技能 + /// + [DataMember] private static Dictionary _worldSkill; + + static SkillManager() + { + _worldSkill = new Dictionary(); + } + + /// + /// 添加技能 + /// + /// 技能名 + /// 技能 + public static SkillObject AddSkill(string name) + { + var skill = new SkillObject(); + _worldSkill.Add(name, skill); + + return skill; + } + + /// + /// 删除技能 + /// + /// 技能名 + public static void RemoveSkill(string name) + { + _worldSkill.Remove(name); + } + + /// + /// 获取技能列表 + /// + /// 技能列表 + public static List GetSkillList(){ + var skillList = new List(); + foreach (var skill in _worldSkill) + skillList.Add(skill.Value); + + return skillList; + } + + /// + /// 加载数据 + /// + public void LoadData() + { + if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) + throw new Exception("未加载工程前无法加载物品信息"); + + var skillPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Skills"); + try + { + var skillManager = (ItemManager)CHelper.DeserializeObject(Path.Combine(skillPath, "worldSkill.db")); + FrameworkManager.ChangeData("skill", skillManager); + Logger.Info("技能加载成功"); + } + catch (Exception e) + { + throw new Exception(e.Message, e); + } + } + + /// + /// 保存数据 + /// + public void SaveData() + { + if (string.IsNullOrEmpty(SceneManager.GameProject.ProjectPath)) + throw new Exception("未加载工程前无法保存物品信息"); + + var itemPath = Path.Combine(SceneManager.GameProject.ProjectPath, "Content", "Skills"); + Directory.CreateDirectory(itemPath); + try + { + CHelper.SerializeObject(Path.Combine(itemPath, "worldSkill.db"), this); + Logger.Info("技能保存成功"); + } + catch (Exception e) + { + throw new Exception(e.Message, e); + } + } + } +} \ No newline at end of file -- Gitee