Commit d3f7db1e authored by lijin's avatar lijin

增加注释

parent e269b470
......@@ -16,7 +16,7 @@ public class EffectItem : MonoBehaviour
MainData md = GameMgr.Ins.md;
Transform p = Rect.parent;
Rect.SetParent(md.imgpng.transform);
Rect.localPosition = v - new Vector3(md.png.width / 2, md.png.height / 2);
Rect.localPosition = v - new Vector3(md.json.width / 2, md.json.height / 2);
Rect.SetParent(p);
MainModule mm = ps.main;
mm.startColor = c;
......
......@@ -21,12 +21,13 @@ public class GameMgr : MonoBehaviour
void Update()
{
Save();//自动保存逻辑
Click();//点击事件逻辑
Play();//涂色重播逻辑
Save();//自动保存逻辑
}
//初始化,主逻辑入口
#region 外部接口
//逻辑接口,主逻辑入口,初始化
public void Init(string key, Texture2D png, Sprite jpg, ResJsonData json, DynamicData dynamic, Texture2D map)
{
ins = this;
......@@ -39,7 +40,6 @@ public class GameMgr : MonoBehaviour
{
gray = _gray;
}
//选组接口,传入组ID
public void ChooseGroup(int groupId)
{
......@@ -63,8 +63,9 @@ public class GameMgr : MonoBehaviour
{
md.AutoClick();
}
#endregion
#region 点击
#region 点击事件逻辑
public static readonly Vector2 gunSize = new Vector2(3, 10);
private float time = 0;
private Vector2 pos = Vector2.zero;
......@@ -99,8 +100,8 @@ public class GameMgr : MonoBehaviour
isLongPress = -1;
RectTransformUtility.ScreenPointToLocalPointInRectangle(md.rect, Input.mousePosition, Camera.main, out Vector2 v);
Debug.Log("长按:" + v);
v.x += md.png.width / 2;
v.y += md.png.height / 2;
v.x += md.json.width / 2;
v.y += md.json.height / 2;
v.x = (int)v.x;
v.y = (int)v.y;
md.OnLongPressPos(v);
......@@ -140,13 +141,13 @@ public class GameMgr : MonoBehaviour
//点击由坐标系转图内像素坐标
public bool OnClickPixel(Vector2 v)
{
v.x += md.png.width / 2;
v.y += md.png.height / 2;
return md.OnClickPos(v);
v.x += md.json.width / 2;
v.y += md.json.height / 2;
return md.OnClickFun(v);
}
#endregion
#region 重播逻辑
#region 涂色重播逻辑
private int stepIndex = -1;//重播判断值,-1不处理,0及以上为重播到第几步
private static readonly float delay = 0.5f;//重播延迟
private static readonly float maxTime = 0.1f;//重播步间隔
......@@ -174,7 +175,7 @@ public class GameMgr : MonoBehaviour
}
#endregion
#region 保存逻辑
#region 自动保存逻辑
private float saveTime = -1;//保存判断值,由正变负则保存
//保存接口,点击涂色触发
public void SaveTime()
......@@ -193,7 +194,7 @@ public class GameMgr : MonoBehaviour
saveTime -= Time.deltaTime;
if (saveTime <= 0)
{
md.Save();
SaveManager.Instance.SaveRes(md.key, md.dynamic, md.png);
Debug.Log("自动保存");
}
}
......
......@@ -9,184 +9,129 @@ using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
//sw.Start();
//sw.Stop();
//Debug.Log("耗时:" + sw.ElapsedMilliseconds);
public class MainData : MonoBehaviour
{
public static Vector4 config = new Vector4(0, 150, 725f, 725f);
public string key = "";
public static Vector2 config = new Vector2(725f, 1250f);//基于开发分辨率设置进游戏时的缩放比例
public Texture2D png = null;
public Sprite jpg = null;
public Texture2D map = null;
public ResJsonData json;
public DynamicData dynamic;
//资源
public string key = "";//当前资源key
public Texture2D png = null;//线框图
public Sprite jpg = null;//彩色图
public Texture2D map = null;//点阵图
public ResJsonData json;//json配置
public DynamicData dynamic;//动态数据
public Image imgpng;
public Image imgjpg;
public Image imgpng;//线框图组件
public Image imgjpg;//彩色图组件
public RectTransform rect;
public int curGroupId = -1;
public RectTransform rect;//本节点
public int curGroupId = -1;//当前选组ID
//初始化逻辑,获取资源,解析资源,初始化显示
public void Init(string _key, Texture2D _png, Sprite _jpg, ResJsonData _json, DynamicData _dynamic, Texture2D _map)
{
ChooseGroup();
ChooseGroup();//用于清空当前选组
//资源赋值
key = _key;
png = _png;
jpg = _jpg;
map = _map;
json = _json;
dynamic = _dynamic;
json.JoinMap(map);//把点阵数据放进json配置以快速获取某个区域的所有点
json.JoinMap(map);
imgjpg.sprite = jpg;//由layout组件自动适应大小
imgpng.sprite = Sprite.Create(png, new Rect(0, 0, json.width, json.height), new Vector2(0.5f, 0.5f));
imgpng.GetComponent<RectTransform>().sizeDelta = new Vector2(json.width, json.height);//自动适应大小基础
imgpng.sprite = Sprite.Create(png, new Rect(0, 0, png.width, png.height), new Vector2(0.5f, 0.5f));
imgpng.GetComponent<RectTransform>().sizeDelta = new Vector2(png.width, png.height);
imgjpg.sprite = jpg;
imgjpg.GetComponent<RectTransform>().sizeDelta = new Vector2(png.width, png.height);
GameMgr.Ins.miMgr.Init(json, dynamic, png);//初始化遮罩
GameMgr.Ins.tiMgr.Init(json, dynamic, png);//初始化文本
float w = config.z / png.width;
float h = config.w / png.height;
float w = config.x / json.width;
float h = config.y / json.height;
float s = Math.Min(w, h);
transform.parent.localScale = Vector3.one * s;
transform.parent.localPosition = new Vector3(config.x, config.y, 0);
GameMgr.Ins.miMgr.Init(json, dynamic, png);
GameMgr.Ins.tiMgr.Init(json, dynamic, png);
GameMgr.Ins.tiMgr.SizeChange(s, true);
GameMgr.Ins.tiMgr.SizeChange(s, true);//初始化缩放
EventDispatcher.Dispatch(EventName.Event.Event_GroupListCreate, json.roomGroupsData, dynamic);
EventDispatcher.Dispatch(EventName.Event.Event_GroupListCreate, json.roomGroupsData, dynamic);//抛事件初始化组按钮
}
public bool OnClickPos(Vector2 v, bool b = false)
//点击逻辑,v图内像素坐标,b是否跳过当前选组ID判断,返回bool以判断散弹枪是否终止
public bool OnClickFun(Vector2 v, bool b = false)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
if (v.x < 0 || v.x >= png.width)
{
//Debug.Log("x越界:" + v.x);
return false;
}
if (v.y < 0 || v.y >= png.height)
{
//Debug.Log("y越界:" + v.y);
return false;
}
if (png.GetPixel((int)v.x, (int)v.y) != Color.white)
{
//Debug.Log("涂过了");
return false;
}
if (FunDo(v, b))
{
sw.Stop();
Debug.Log("涂色全耗时:" + sw.ElapsedMilliseconds);
return true;
}
return false;
}
public bool FunDo(Vector2 v, bool b = false)
{
int index = (int)(v.x + v.y * png.width);
Color[] c_map = map.GetPixels();
Color c = c_map[index];
//x越界
if (v.x < 0 || v.x >= json.width) { return false; }
//y越界
if (v.y < 0 || v.y >= json.height) { return false; }
//涂过了,或点击到线
if (png.GetPixel((int)v.x, (int)v.y) != Color.white) { return false; }
int index = (int)(v.x + v.y * json.width);//图内像素坐标转图内像素索引
Color[] c_map = map.GetPixels();//点阵数据
Color c = c_map[index];//点击的点阵数据:rgba,rg为房间ID,ba为组ID
int roomId = ResJsonData.ColorToV(c).x;
RoomDatas rd = json.roomDatas[roomId];
if (rd == null)
{
Debug.LogError("沒找到點:" + v.x + "-" + v.y);
return false;
}
if (dynamic.step.Contains(rd.id))
//涂过了
if (dynamic.step.Contains(rd.id)) { return false; }
//判断是否当前选组
if (rd.groupId != curGroupId && !b) { return false; }
//染色逻辑
Color[] c_png = png.GetPixels();
for (int i = 0; i < rd.posIndex.Count; i++)
{
Debug.LogError("點過了:" + rd.id);
return false;
c_png[rd.posIndex[i]] = Color.clear;
}
if (rd.groupId == curGroupId || b)
{
Color[] c_png = png.GetPixels();
for (int i = 0; i < rd.posIndex.Count; i++)
{
c_png[rd.posIndex[i]] = Color.clear;
}
png.SetPixels(c_png);
png.Apply();
png.SetPixels(c_png);
png.Apply();
dynamic.AddData(rd.groupId, rd.id);
RoomGroupsData rgd = json.roomGroupsData[rd.groupId - 1];
GameMgr.Ins.eiMgr.Create(new Color(rgd.R / 255f, rgd.G / 255f, rgd.B / 255f, 1), v);
GameMgr.Ins.tiMgr.HideId(rd.id);
GameMgr.Ins.miMgr.Play(v, rd.id);
EventDispatcher.Dispatch(EventName.Event.Event_GropuItemChange, rgd, dynamic);
GameMgr.Ins.SaveTime();
return true;
}
return false;
dynamic.AddData(rd.groupId, rd.id);//更新动态数据
RoomGroupsData rgd = json.roomGroupsData[rd.groupId - 1];
GameMgr.Ins.eiMgr.Create(new Color(rgd.R / 255f, rgd.G / 255f, rgd.B / 255f, 1), v);//创建点击特效
GameMgr.Ins.tiMgr.HideId(rd.id);//隐藏当前文本
GameMgr.Ins.miMgr.Play(v, rd.id);//播放当前遮罩
GameMgr.Ins.SaveTime();//触发自动保存
EventDispatcher.Dispatch(EventName.Event.Event_GropuItemChange, rgd, dynamic);//抛事件更新组按钮
return true;
}
//长按逻辑,v图内像素坐标
public void OnLongPressPos(Vector2 v)
{
//x越界
if (v.x < 0 || v.x >= json.width) { return; }
//y越界
if (v.y < 0 || v.y >= json.height) { return; }
//涂过了,或点击到线
if (png.GetPixel((int)v.x, (int)v.y) != Color.white) { return; }
int index = (int)(v.x + v.y * json.width);//图内像素坐标转图内像素索引
Color[] c_map = map.GetPixels();//点阵数据
Color c = c_map[index];//点击的点阵数据:rgba,rg为房间ID,ba为组ID
int groupId = ResJsonData.ColorToV(c).y;
ChooseGroup(groupId);
}
//选组逻辑,id组ID
public void ChooseGroup(int id = -1)
{
if (id == -1)
{
curGroupId = id;
GameMgr.Ins.miMgr.Show();
}
Debug.Log("选中组:" + id);
if (curGroupId == id)
{
return;
GameMgr.Ins.miMgr.Show(curGroupId);//选组-1隐藏全部遮罩
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
if (curGroupId == id) { return; }
curGroupId = id;
GameMgr.Ins.miMgr.Show(curGroupId);
//GameMgr.Ins.miMgr.CircleAll();
//List<int> roomIds = json.roomGroupsData[curGroupId - 1].roomIds;
//for (int i = 0; i < roomIds.Count; i++)
//{
// RoomDatas rd = json.roomDatas[roomIds[i]];
// if (!dynamic.step.Contains(rd.id))
// {
// ExRoomDatas erd = new ExRoomDatas();
// for (int j = 0; j < rd.posIndex.Count; j++)
// {
// erd.AddData(rd.posIndex[j], png.width);
// }
// GameMgr.Ins.miMgr.Create(this, erd, rd.id, rd.groupId);
// }
//}
sw.Stop();
Debug.Log("选组耗时:" + sw.ElapsedMilliseconds);
EventDispatcher.Dispatch(EventName.Event.Event_ChoiseGroupByPic, curGroupId);
GameMgr.Ins.miMgr.Show(curGroupId);//显示当前选组遮罩
EventDispatcher.Dispatch(EventName.Event.Event_ChoiseGroupByPic, curGroupId);//抛事件反选组按钮
}
public void OnLongPressPos(Vector2 v)
{
if (v.x < 0 || v.x >= png.width)
{
//Debug.Log("x越界:" + v.x);
return;
}
if (v.y < 0 || v.y >= png.height)
{
//Debug.Log("y越界:" + v.y);
return;
}
if (png.GetPixel((int)v.x, (int)v.y) != Color.white)
{
//Debug.Log("涂过了");
return;
}
int index = (int)(v.x + v.y * png.width);
Color[] c_map = map.GetPixels();
Color c = c_map[index];
int groupId = ResJsonData.ColorToV(c).y;
ChooseGroup(groupId);
}
//重置逻辑
public void Clear()
{
dynamic = new DynamicData();
dynamic = new DynamicData();//动态数据直接舍弃
//重置染色
Color[] c = png.GetPixels();
for (int i = 0; i < c.Length; i++)
{
......@@ -197,38 +142,23 @@ public class MainData : MonoBehaviour
}
png.SetPixels(c);
png.Apply();
SaveManager.Instance.SaveRes(key, dynamic, png);
SaveManager.Instance.SaveRes(key, dynamic, png);//保存
}
//重播逻辑
public void RePlay()
{
ChooseGroup();
ChooseGroup();//用于清空当前选组
GameMgr.Ins.miMgr.Replay();
//ExRoomDatas erd = new ExRoomDatas();
//for (int i = 0; i < dynamic.step.Count; i++)
//{
// RoomDatas rd = json.roomDatas[dynamic.step[i]];
// erd.Init();
// for (int j = 0; j < rd.posIndex.Count; j++)
// {
// erd.AddData(rd.posIndex[j], png.width);
// }
// GameMgr.Ins.miMgr.Create(this, erd, rd.id, rd.groupId);
//}
}
public void Save()
{
SaveManager.Instance.SaveRes(key, dynamic, png);
}
//辅助涂色逻辑
public void AutoClick()
{
List<int> list = json.roomGroupsData[curGroupId - 1].roomIds;
List<int> list = json.roomGroupsData[curGroupId - 1].roomIds;//当前选中组全部房间ID
for (int i = 0; i < list.Count; i++)
{
if (!dynamic.step.Contains(list[i]))
if (!dynamic.step.Contains(list[i]))//已涂过的跳过
{
OnClickPos(new Vector2(json.roomDatas[list[i]].px, json.roomDatas[list[i]].py));
OnClickFun(new Vector2(json.roomDatas[list[i]].px, json.roomDatas[list[i]].py));
return;
}
}
......
......@@ -58,12 +58,12 @@ public class MaskItem : MonoBehaviour
//坐标计算
Transform p = Rect.parent;
Rect.SetParent(md.imgpng.transform);
Rect.localPosition = v - new Vector3(md.png.width / 2, md.png.height / 2);
Rect.localPosition = v - new Vector3(md.json.width / 2, md.json.height / 2);
Rect.SetParent(p);
Rect.localScale = Vector2.one;
SPRect.SetParent(md.imgpng.transform);
SPRect.localPosition = new Vector3(erd.size.x + (erd.size.z + 1 - erd.size.x) / 2 - (md.png.width + 1) / 2, erd.size.y + (erd.size.w + 1 - erd.size.y) / 2 - (md.png.height + 1) / 2, 0);
SPRect.localPosition = new Vector3(erd.size.x + (erd.size.z + 1 - erd.size.x) / 2 - (md.json.width + 1) / 2, erd.size.y + (erd.size.w + 1 - erd.size.y) / 2 - (md.json.height + 1) / 2, 0);
SPRect.SetParent(Rect);
SPRect.localScale = Vector2.one;
......@@ -87,7 +87,7 @@ public class MaskItem : MonoBehaviour
Transform p = Rect.parent;
Rect.SetParent(md.imgpng.transform);
Rect.localPosition = v - new Vector3(md.png.width / 2, md.png.height / 2);
Rect.localPosition = v - new Vector3(md.json.width / 2, md.json.height / 2);
Rect.SetParent(p);
SPRect.SetParent(Rect);
......
......@@ -115,7 +115,7 @@ public class MaskItemMgr : MonoBehaviour
ExRoomDatas erd = new ExRoomDatas();
for (int k = 0; k < rd.posIndex.Count; k++)
{
erd.AddData(rd.posIndex[k], GameMgr.Ins.md.png.width);
erd.AddData(rd.posIndex[k], GameMgr.Ins.md.json.width);
}
return Create(GameMgr.Ins.md, erd, rd.id, rd.groupId);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment