Skip to content

Commit 3efc42a

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
增加事件&状态模块
1 parent dd44b6f commit 3efc42a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2728
-1
lines changed

Assets/GameFramework.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GameFramework/Base.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #游戏模块的管理类# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月22日 14点59分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections.Generic;
12+
13+
namespace GameFramework.Taurus
14+
{
15+
public static class GameFrameworkMode
16+
{
17+
#region 属性
18+
//所有的子模块
19+
private static readonly Dictionary<int, GameFrameworkModule> _allGameModules = new Dictionary<int, GameFrameworkModule>();
20+
//所有渲染帧函数
21+
private static List<IUpdate> _allUpdates = new List<IUpdate>();
22+
//所有的固定帧函数
23+
private static List<IFixedUpdate> _allFixedUpdates = new List<IFixedUpdate>();
24+
#endregion
25+
26+
#region 外部接口
27+
/// <summary>
28+
/// 获取模块
29+
/// </summary>
30+
/// <typeparam name="T">游戏模块</typeparam>
31+
/// <returns></returns>
32+
public static T GetModule<T>() where T : GameFrameworkModule, new()
33+
{
34+
return (T)GetModule(typeof(T));
35+
}
36+
37+
/// <summary>
38+
/// 渲染帧
39+
/// </summary>
40+
public static void Update()
41+
{
42+
foreach (var item in _allUpdates)
43+
item.OnUpdate();
44+
}
45+
46+
/// <summary>
47+
/// 固定帧
48+
/// </summary>
49+
public static void FixedUpdate()
50+
{
51+
foreach (var item in _allFixedUpdates)
52+
item.OnFixedUpdate();
53+
}
54+
55+
/// <summary>
56+
/// 关闭游戏的所有模块
57+
/// </summary>
58+
public static void ShutDown()
59+
{
60+
foreach (var item in _allGameModules.Values)
61+
item.OnClose();
62+
63+
_allUpdates.Clear();
64+
_allFixedUpdates.Clear();
65+
_allGameModules.Clear();
66+
}
67+
68+
#endregion
69+
70+
71+
#region 内部函数
72+
73+
//获取模块
74+
private static GameFrameworkModule GetModule(Type type)
75+
{
76+
int hashCode = type.GetHashCode();
77+
GameFrameworkModule module = null;
78+
if (_allGameModules.TryGetValue(hashCode, out module))
79+
return module;
80+
module = CreateModule(type);
81+
return module;
82+
}
83+
84+
//创建模块
85+
private static GameFrameworkModule CreateModule(Type type)
86+
{
87+
int hashCode = type.GetHashCode();
88+
GameFrameworkModule module = (GameFrameworkModule)Activator.CreateInstance(type);
89+
_allGameModules[hashCode] = module;
90+
//整理含IUpdate的模块
91+
var update = module as IUpdate;
92+
if (update != null)
93+
_allUpdates.Add(update);
94+
//整理含IFixed的模块
95+
var fixedUpdate = module as IFixedUpdate;
96+
if (fixedUpdate != null)
97+
_allFixedUpdates.Add(fixedUpdate);
98+
return module;
99+
}
100+
101+
#endregion
102+
103+
}
104+
}

Assets/GameFramework/Base/GameFrameworkMode.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #游戏模块的基类# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月22日 14点59分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
namespace GameFramework.Taurus
11+
{
12+
public abstract class GameFrameworkModule
13+
{
14+
/// <summary>
15+
/// 关闭当前模块
16+
/// </summary>
17+
public abstract void OnClose();
18+
19+
}
20+
}

Assets/GameFramework/Base/GameFrameworkModule.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #固定帧调用接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月22日 15点02分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
namespace GameFramework.Taurus
11+
{
12+
public interface IFixedUpdate
13+
{
14+
void OnFixedUpdate();
15+
}
16+
}

Assets/GameFramework/Base/IFixedUpdate.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GameFramework/Base/IUpdate.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #渲染帧调用的接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月22日 15点01分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
namespace GameFramework.Taurus
11+
{
12+
public interface IUpdate
13+
{
14+
void OnUpdate();
15+
}
16+
}

Assets/GameFramework/Base/IUpdate.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GameFramework/Event.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #事件管理器# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年6月22日 15点38分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections.Generic;
12+
13+
namespace GameFramework.Taurus
14+
{
15+
16+
public sealed class EventManager:GameFrameworkModule
17+
{
18+
#region 属性
19+
//所有的事件
20+
private readonly Dictionary<int, Action<object, IEventArgs>> _allActions = new Dictionary<int, Action<object, IEventArgs>>();
21+
#endregion
22+
23+
#region 外部接口
24+
/// <summary>
25+
/// 添加事件监听函数
26+
/// </summary>
27+
/// <typeparam name="T">事件的类型</typeparam>
28+
/// <param name="handler">监听事件的回调函数</param>
29+
public void AddListener<T>(Action<object, IEventArgs> handler) where T : IEventArgs
30+
{
31+
int eventId = typeof(T).GetHashCode();
32+
Action<object, IEventArgs> eventHandler = null;
33+
if (!_allActions.TryGetValue(eventId, out eventHandler))
34+
_allActions[eventId] = handler;
35+
else
36+
{
37+
eventHandler += handler;
38+
_allActions[eventId] = eventHandler;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// 移除监听函数
44+
/// </summary>
45+
/// <typeparam name="T">BaseEventArgs</typeparam>
46+
/// <param name="handler"></param>
47+
public void RemoveListener<T>(Action<object, IEventArgs> handler) where T : IEventArgs
48+
{
49+
int eventId = typeof(T).GetHashCode();
50+
Action<object, IEventArgs> eventHandler = null;
51+
if (!_allActions.TryGetValue(eventId, out eventHandler))
52+
return;
53+
else
54+
{
55+
if (eventHandler != null)
56+
{
57+
eventHandler -= handler;
58+
if (eventHandler == null)
59+
_allActions.Remove(eventId);
60+
else
61+
_allActions[eventId] = eventHandler;
62+
}
63+
}
64+
}
65+
66+
/// <summary>
67+
/// 触发事件 带事件类型 事件默认为null
68+
/// </summary>
69+
/// <typeparam name="T">事件类</typeparam>
70+
/// <param name="sender">触发事件的对象</param>
71+
public void Trigger<T>(object sender) where T : IEventArgs
72+
{
73+
int eventId = typeof(T).GetHashCode();
74+
HanleEvent(sender, eventId);
75+
}
76+
77+
/// <summary>
78+
/// 触发事件
79+
/// </summary>
80+
/// <param name="sender">触发事件的对象</param>
81+
/// <param name="value">事件参数</param>
82+
public void Trigger(object sender, IEventArgs value)
83+
{
84+
HanleEvent(sender, value);
85+
}
86+
87+
#endregion
88+
89+
90+
#region 内部函数
91+
//处理事件
92+
private void HanleEvent(object sender, IEventArgs args)
93+
{
94+
if (args == null)
95+
return;
96+
Action<object, IEventArgs> eventHandler = null;
97+
int Id = args.Id;
98+
if (_allActions.TryGetValue(Id, out eventHandler))
99+
{
100+
if (eventHandler != null)
101+
eventHandler(sender, args);
102+
}
103+
}
104+
//处理事件 不带参数
105+
private void HanleEvent(object sender, int eventId)
106+
{
107+
Action<object, IEventArgs> eventHandler = null;
108+
if (_allActions.TryGetValue(eventId, out eventHandler))
109+
{
110+
if (eventHandler != null)
111+
eventHandler(sender, null);
112+
}
113+
}
114+
#endregion
115+
116+
117+
public override void OnClose()
118+
{
119+
_allActions.Clear();
120+
}
121+
}
122+
}

Assets/GameFramework/Event/EventManager.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)