@ts541933433
2020-04-28T02:18:45.000000Z
字数 3980
阅读 999
《蒙琦游戏》
- 使用(Touch Script)插件
- SubwayProjectionMain.cs(地幕端主逻辑,负责加载游戏配置文件、与电视机端通讯、显示不同的地幕模块)
- BaseBlock.cs (地幕功能块基类)
- AnswerLineBlock.cs (地幕功能块子类,表示各类功能区)
- ClickElement.cs (地幕元素基类)
- NumberBubble.cs (地幕元素子类,表示各种元素)
- SubwayEvent.cs (定义的事件)
- EventManager.cs (事件管理)
- IEventHandler.cs (时间手柄接口)
- PlayGame.cs (最终实现类)
SubwayProjectionMain.cs
public class SubwayProjectionMain : MonoBehaviour, IEventHandler{private BaseBlock curBlock = null; //当前显示的区块public AnswerLineBlock answerLineBlock;private BaseBlock GetPlayBlock(){return answerLineBlock;}void Start(){//赋值当前区块curBlock = GetPlayBlock();if (TouchManager.Instance != null){//监听输入源事件(TouchSript已定义的事件,直接进行监听)TouchManager.Instance.PointersAdded += pointersUpdateHandler;TouchManager.Instance.PointersUpdated += pointersUpdateHandler;TouchManager.Instance.PointersRemoved += pointersRemovedHandler;}}//当检测到有输入源进入时private void pointersUpdateHandler(object sender, PointerEventArgs e){if (curBlock && curBlock.gameObject.activeSelf){//遍历接收到的点foreach (var pointer in e.Pointers){//对每个点进行更新curBlock.OnUpdatePointer(pointer);}}}}
BaseBlock.cs
//区块基类public class BaseBlock : MonoBehaviour{public virtual void OnUpdatePointer(Pointer pointer){//区块基类虚方法}}
AnswerLineBlock.cs
//区块子类public class AnswerLineBlock : BaseBlock{private List<NumberBubble> NumberBubbleList= new List<NumberBubble>(); //储存所有数字块public override void OnUpdatePointer(Pointer pointer){base.OnUpdatePointer(pointer);foreach (NumberBubble number in NumberBubbleList){if (number == null) continue;number.OnUpdatePointer(pointer);}}}
ClickElement.cs
public class ClickElement{protected float pressedTime = 0; //按下时的计时时间public float pressToClickTime = 0.8f; //按下多久算点击protected int pressID = -1; //硬件对应的点的idpublic void OnUpdatePointer(Pointer pointer){if (!this.gameObject.activeInHierarchy) return;float w, h;//将踏入点坐标从屏幕坐标转换为世界坐标var pos = Camera.main.ScreenToWorldPoint(pointer.Position);//获取该元素的宽高w = this.getWidth();h = this.getHeight();//判断踏入点是否在元素的范围内if (pos.x > this.transform.position.x - w / 2 && pos.x < this.transform.position.x + w / 2 && pos.y > this.transform.position.y - h / 2 && pos.y < this.transform.position.y + h / 2){//如果是,执行按下处理。OnPressed(pointer.Id);}else{//如果不是,但之前记录过这个点是按下的,释放该点if (this.pressID == pointer.Id){OnPointerOut(pointer.Id);}}}//按下处理public void OnPressed(int id){if (!this.gameObject.activeInHierarchy) return;if (pressID == -1){pressedTime = Time.time;pressID = id;}}//释放处理public void OnReleased(int id){if (pressID == id){pressedTime = 0;pressID = -1;}}//实时检测是否满足点击protected virtual void Update(){if (pressedTime > 0){if (Time.time - pressedTime >= pressToClickTime){pressedTime = 0;OnClickHandler();}}}//触发点击事件protected virtual void OnClickHandler(){print(gameObject.name + " clicked");}}
NumberBubble.cs
public class NumberBubble : ClickElement{//在子类Update中实时检测是否满足点击protected override void Update(){base.Update();}//满足点击后触发事件protected override void OnClickHandler(){base.OnClickHandler();//事件处理器EventManager.GetInstance().DispachEvent(SubwayEvent.ENTER_GAME);}}
SubwayEvent.cs
public class SubwayEvent{public const string ENTER_GAME = "Enter_Game"; //进入游戏public const string Exit_GAME = "Exit_Game"; //退出游戏}
EventManager.cs
//事件处理器public class EventManager{//单例private static EventManager instance;public static EventManager GetInstance(){if (instance == null){instance = new EventManager();}return instance;}//定义一个value为事件手柄列表的字典private Dictionary<string, List<IEventHandler>> dicHandler;// 注册事件监听public void AddEventListener(string type, IEventHandler listher){if (!dicHandler.ContainsKey(type)){dicHandler.Add(type, new List<IEventHandler>());}dicHandler[type].Add(listher);}//派发事件public void DispachEvent(string type, object data = null){List<IEventHandler> list = dicHandler[type];for (int i = 0; i < list.Count; i++){list[i].OnEvent(type, data);}}}
IEventHandler.cs
public interface IEventHandler{//事件手柄void OnEvent(string type, object data);}
PlayGame.cs
public class PlayGame : IEventHandler{//事件接口处理public void OnEvent(string type, object data){switch (type){case SubwayEvent.ENTER_GAME://开始游戏break;case SubwayEvent.EXIT_GAME://退出游戏break;}}}