@ts541933433
2020-04-28T10:18:45.000000Z
字数 3980
阅读 878
《蒙琦游戏》
- 使用(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; //硬件对应的点的id
public 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;
}
}
}