using Stein.Commons.Bundles;
using Stein.Commons.Network;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(UITexture))]
public class QuestBrushStroke : ASteinUIBase
{
private UITexture mUITex;
GameObject m_DOne;
GameObject m_Fulfill;
private Texture2D PreTex;
private Texture2D MyTex;
public int Radius = 30;
private Color Col = new Color(0, 0, 0, 0);
Color[] s2;
GameObject m_Guidance;
float num = 0;
float count = 0.04f;
bool fulfill = true;
Vector2 posA;
Vector3 worldPos;
Vector2 posA1;
private Vector3 stwp;
private GameObject clone;
private LineRenderer line;
private int i;
private GameObject obs;
int mGameId;
GameObject mLinePrant;
CausualGameInfo resp;
UILabel mexplain;
bool fromOrgan;
OrganGame organGame;
byte[][] mArrays;
GameObject guideArrow;
Vector3[] Gui = { new Vector3(-24, -94, 0), new Vector3(-83, 56, 0), new Vector3(-106, -156, 0), new Vector3(178, -91, 0), new Vector3(20, -226, 0),new Vector3 (3,-185,0) };
int guiindx=0;
public override void OnStart (object param = null)
{
resp = param as CausualGameInfo;
CommonTools.ShowOrHideUI(false);
this.gameObject.GetComponent<UITexture>().enabled = false;
mGameId = resp.mDetail.id;
fromOrgan = resp.mDetail.fromOrgan;
organGame = resp.mOrgan;
IAsyncResult<GameObject> result = BundleResources.GetResources().LoadAssetAsync<GameObject>(SteinResType.UI.ToString() + "/SmallGameszc" + 302);
result.OnCallback((r) =>
{
if (!string.IsNullOrEmpty(r.Error))
{
Debug.LogWarningFormat("加载UI资源 [{0}] 失败.Error:{1}", "SmallGameszc" + resp.mDetail.scratchResId);
return;
}
if (Module.GetOpennedUi(this.mUIIndex) == null)
{
return;
}
mUITex.mainTexture = r.Result.GetComponentInChildren<UITexture>(true).mainTexture;
mUITex.alpha = 0.01f;
PreTex = mUITex.mainTexture as Texture2D;
//获取遮罩图片的字节数
mArrays = new byte[mUITex.width][];
for (int i = 0 ; i < mUITex.width; i ++)
{
mArrays[i] = new byte[mUITex.height];
}
CommonTools.ShowOrHideUI(false);
});
SetOverEffer("26007", m_DOne);
mexplain.text = resp.mDetail.gestureCrossItem.name;
SetGUI();
}
void SetGUI()
{
//一个引导动画
guiindx++;
if (guiindx < Gui.Length)
{
TweenPosition tp = TweenPosition.Begin(guideArrow, 1, Gui[guiindx]);
tp.from = Gui[guiindx - 1];
tp.to = Gui[guiindx];
Invoke("SetGUI", 1);
}
else
{
guideArrow.SetActive(false);
}
}
public override void OnPostWidgetAssemble (WidgetBase widget)
{
base.OnPostWidgetAssemble(widget);
switch (widget.widgetGo.name)
{
case "Cap":
mUITex = widget.widgetGo.GetComponent<UITexture>();
break;
case "effer":
m_DOne = widget.widgetGo;
break;
case "Line":
obs = widget.widgetGo;
break;
case "LinePrant":
mLinePrant = widget.widgetGo;
break;
case "Guidance":
guideArrow = widget.widgetGo;
break;
case "explain":
mexplain = widget.widgetGo.GetComponent<UILabel>();
break;
case "Reset":
m_Fulfill = widget.widgetGo;
UIEventListener.Get(widget.widgetGo).onClick = Resetone;
break;
}
}
public override bool FullScreen
{
get
{
return true;
}
}
void Update ()
{
if (fulfill)
{
if (Input.GetMouseButtonDown(0))
{
clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一个带有LineRender的物体
line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件
line.materials[0].renderQueue = this.GetComponent<UIPanel>().startingRenderQueue + 1000;
clone.AddComponent<Son>();
line.transform.parent = mLinePrant.transform;
//line.SetColors(Color.blue, Color.blue);//设置颜色
line.SetWidth(0.03f, 0.03f);//设置宽度
posA1 = Vector2.zero;
guideArrow.SetActive(false);
guiindx = 6;
i = 0;
}
if (Input.GetMouseButton(0))
{
worldPos = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
if (Mathf.Abs(worldPos.x) < 1.4f && Mathf.Abs(worldPos.y) < 0.8f)
{
i++;
line.SetVertexCount(i);//设置顶点数
line.SetPosition(i - 1, worldPos);
}
posA = WorldPos2Pix(worldPos);
ChangePixelColorByCircle((int)posA.x, (int)posA.y, Radius);
//Achieve();
}
if (Input.GetMouseButtonUp(0))
{
Achieve();
}
if (posA != null && posA1 != posA && posA != Vector2.zero)
{
if(posA1 == Vector2.zero)
{
posA1 = posA;
return;
}
float dist = Vector2.Distance(posA, posA1);
int offset = 40;
for (int i = offset; i < dist; i += offset)
{
Vector2 pV = Vector2.Lerp(posA, posA1, i / dist);
ChangePixelColorByCircle((int)pV.x, (int)pV.y, Radius);
}
posA1 = posA;
}
}
}
Vector2 WorldPos2Pix (Vector3 worldPos)
{
var temp = transform.InverseTransformPoint(worldPos);
var pos = new Vector2(temp.x + mUITex.width / 2, temp.y + mUITex.height / 2);
float rateX = mUITex.width / (float)PreTex.width;
float rateY = mUITex.height / (float)PreTex.height;
return new Vector2((pos.x / rateX), (pos.y / rateY));
}
void ChangePixelColorByCircle (int x, int y, int radius)
{
for (int i = -Radius; i < Radius; i++)
{
var py = y + i;
if (py < 0 || py >= mUITex.height)
{
continue;
}
for (int j = -Radius; j < Radius; j++)
{
var px = x + j;
if (px < 0 || px >= mUITex.width)
{
continue;
}
mArrays[px][py] = 1;
}
}
}
void Achieve()
{
if (fulfill)
{
num = 0;
for (int i = 0; i < mArrays.Length; i++)
{
for(int j = 0; j < mArrays[i].Length; j++)
{
if (mArrays[i][j] == 1)
{
num++;
}
}
}
//Debug.LogError(num / (float)(mUITex.width * mUITex.height));
if (num / (float)(mUITex.width * mUITex.height) >= count)
{
m_Fulfill.transform.parent.gameObject.SetActive(false);
fulfill = false;
mLinePrant.SetActive(false);
SetOverEffer(resp.mDetail.gestureCrossItem.effectId, m_DOne);
StartCoroutine(Delayed());
}
}
}
IEnumerator Delayed ()//在这里做个延时 以免太突兀
{
yield return new WaitForSeconds(1.8f);
mUITex.transform.parent.GetComponent<TweenAlpha>().enabled = true;
yield return new WaitForSeconds(1.2f);
CommonTools.SendCompleteQuest(mGameId, organGame, true,false);
}
ParticleEffectProxy mRoleEffec4;
ParticleEffectProxy mRoleEffec5;
public void SetOverEffer (string tx, GameObject effer)
{
if (mRoleEffec4 != null)
{
mRoleEffec4.Terminate();
mRoleEffec4 = null;
}
if (mRoleEffec4 == null)
mRoleEffec4 = new ParticleEffectProxy(effer);
mRoleEffec4.SetAttachToPanel(true);
mRoleEffec4.SetScale(1.0f);
mRoleEffec4.Play(tx);
}
public void SetStarEffer (string tx, GameObject effer)
{
if (mRoleEffec5 != null)
{
mRoleEffec5.Terminate();
mRoleEffec5 = null;
}
if (mRoleEffec5 == null)
mRoleEffec5 = new ParticleEffectProxy(effer);
mRoleEffec5.SetAttachToPanel(true);
mRoleEffec5.SetScale(1.0f);
mRoleEffec5.Play(tx);
}
private void Resetone (GameObject obj)
{
BroadcastMessage("DestoryMe");
IAsyncResult<GameObject> result = BundleResources.GetResources().LoadAssetAsync<GameObject>(SteinResType.UI.ToString() + "/SmallGameszc" + 302);
mUITex.mainTexture = null;
result.OnCallback((r) =>
{
if (!string.IsNullOrEmpty(r.Error))
{
Debug.LogWarningFormat("加载UI资源 [{0}] 失败.Error:{1}", "SmallGameszc" + resp.mDetail.scratchResId);
return;
}
if (Module.GetOpennedUi(this.mUIIndex) == null)
{
return;
}
mUITex.mainTexture = r.Result.GetComponentInChildren<UITexture>(true).mainTexture;
mUITex.alpha = 0.01f;
});
for (int i = 0; i < mArrays.Length; i++)
{
for (int j = 0; j < mArrays[i].Length; j++)
{
mArrays[i][j] = 0;
}
}
num = 0;
}
public override void OnOpenBegin ()
{
}
public override void OnStop (object param = null)
{
mUITex.mainTexture = PreTex;
if (MyTex != null)
DestroyImmediate(MyTex);
MyTex = null;
CommonTools.ShowOrHideUI(true);
}
public override string DefaultLayer
{
get
{
return XBModuleDefine.LAYER_MAIN;
}
}
public override string Displayer
{
get
{
return XBModuleDefine.DISPLAYER_MAIN;
}
}
public void OnClickClose ()
{
SelfClose();
}
public void OnClickFulfill ()
{
ProtoCasualGames.C1734_CasualGamesCompleteReqMessage casualGames = new ProtoCasualGames.C1734_CasualGamesCompleteReqMessage();
casualGames.id = mGameId;
Notification msg = GameServerHelper.GetInstance().GetMessage(GameMessageCommands.MSG_CasualGamesComplete);
msg.SetData<ProtoCasualGames.C1734_CasualGamesCompleteReqMessage>(casualGames);
GameServerHelper.GetInstance().SendMessage(msg);
SelfClose();
}
public override void OnCloseComplete()
{
base.OnCloseComplete();
CommonTools.ShowOrHideUI(true);
}
}
一款画图形的小游戏
最新推荐文章于 2023-04-28 15:40:57 发布