Skip to content

Commit

Permalink
v1.1.1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Hgnim committed Nov 24, 2024
1 parent 0413af7 commit c0e51e5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 232 deletions.
90 changes: 35 additions & 55 deletions Assets/Scripts/Game/NumberDataControl.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static PublicClass.DLL;
using Debug = UnityEngine.Debug;
using Cysharp.Threading.Tasks;
using System;
using System.Collections;
using System.Timers;

public class NumberDataControl : MonoBehaviour
{
Expand Down Expand Up @@ -76,31 +77,44 @@ internal void MCB_Click(bool isHit, int id)
if (!gameIsStart)
{
gameIsStart = true;
countdownThread = new(CountdownThread);
/*countdownThread = new(CountdownThread);
countdownUIThread = new(CountdownUIThread);
countdownThread.Start();
countdownUIThread.Start();
}
countdownUIThread.Start();*/
//因为WebGL不支持多线程,所以改为协程运行
StartCoroutine( CountdownThread());
//CountdownUIThread();
}
if (isHit)
GameScore+=id;
else
GameScore--;

}
Thread countdownThread;
Thread countdownUIThread;
void CountdownThread()
IEnumerator CountdownThread()
{
while (gameTime > 0)
{
Sleep(10);
gameTime--;
}
static double tenms(DateTime startTime, DateTime endTime)
{
TimeSpan secondSpan = new(endTime.Ticks - startTime.Ticks);
return secondSpan.TotalMilliseconds/10;
}
DateTime start= DateTime.Now;
int gameTimeStartValue = gameTime;
while (gameTime > 0)
{
//await TaskSleep(10);
//await UniTask.Delay(10);
yield return new WaitForSeconds(0.01f);
//gameTime--;
gameTime = gameTimeStartValue-(int)tenms(start,DateTime.Now);
GameTimeUI = -1;
}
gameTime = 0;
GameTimeUI = 0;

ThreadDelegate.QueueOnMainThread((param) =>
{
if (Application.isPlaying)
/*ThreadDelegate.QueueOnMainThread((param) =>
{*/
if (Application.isPlaying)
{
GameObject gom = GameObject.Find("GameOverMenu_Root").transform.Find("GameOverMenu").gameObject;
gom.SetActive(true);
Expand All @@ -114,45 +128,11 @@ void CountdownThread()
gom.SetActive(false);
});
}
}, null);
/*}, null);*/
}
void CountdownUIThread()
{
while(gameTime > 0)
{
bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
GameTimeUI = -1;
wait = true;
}, null);
Sleep(10);
while (!wait) ;
}
ThreadDelegate.QueueOnMainThread((param) =>
{
GameTimeUI = 0;
}, null);
}
private void Start()
{
NumberDataControl.MouseClickST_event += MCB_Click;
}
/// <summary>
/// 更精准的Sleep函数
/// </summary>
/// <param name="ms">毫秒</param>
static void Sleep(int ms)
{
var sw = Stopwatch.StartNew();
var sleepMs = ms - 16;
if (sleepMs > 0)
{
Thread.Sleep(sleepMs);
}
while (sw.ElapsedMilliseconds < ms)
{
Thread.Sleep(0);
}
}

}
60 changes: 31 additions & 29 deletions Assets/Scripts/KeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using System.Collections;

public class KeyManager : MonoBehaviour
{
Thread keyListenThread;
void Start()
{
keyListenThread = new(KeyListenThread);
keyListenThread.Start();
}
/*keyListenThread = new(KeyListenThread);
keyListenThread.Start();*/
StartCoroutine(KeyListenThread());
}
public static class WaitLock
{
internal static bool exitGameWaitLock = false;
}

void KeyListenThread()
IEnumerator KeyListenThread()
{
bool CheckEscKey()
static bool CheckEscKey()
{
bool tmp = false;
/*bool tmp = false;
bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
tmp = GameKey.EscClick();
wait = true;
}, null);
while (!wait) ;
return tmp;
}
while (keyListenThread.IsAlive)
return tmp;*/
return GameKey.EscClick();
}
while (Application.isPlaying)
{
if (!WaitLock.exitGameWaitLock &&
CheckEscKey())
Expand All @@ -41,16 +44,16 @@ bool CheckEscKey()
//ExitGameWait();
ExitGame();
}
Thread.Sleep(10);
}
yield return new WaitForSeconds(0.01f);
}
}
/// <summary>
/// 长按esc退出游戏
/// </summary>
void ExitGameWait()
{
ThreadDelegate.QueueOnMainThread((param) =>
{
/*ThreadDelegate.QueueOnMainThread((param) =>
{*/
GameObject quitText = GameObject.Find("MainCamera").transform.Find("Quit_Text").gameObject;
quitText.SetActive(true);
Text quitText_text = quitText.transform.Find("Quit_Text_text").gameObject.GetComponent<Text>();
Expand All @@ -64,15 +67,15 @@ bool keyCheck()
{
bool wait = false;
bool tmp = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
/*ThreadDelegate.QueueOnMainThread((param) =>
{*/
quitText_text.text = mTxt.PadRight(mTxt.Length + i, '.');
if (!GameKey.EscClick())
tmp = false;
else
tmp = true;
wait = true;
}, null);
/*}, null);*/
while (!wait) ;
return tmp;
}
Expand All @@ -93,35 +96,34 @@ bool keyCheck()
exit:;//停止退出程序
{
bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
/*ThreadDelegate.QueueOnMainThread((param) =>
{*/
quitText_text.text = "";
quitText.SetActive(false);
wait = true;
}, null);
/*}, null);*/
while (!wait) ;
}
over:;

});
runThread.Start();
}, null);
/*}, null);*/
}
void ExitGame()
{
bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
/*bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{*/
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
wait = true;
}, null);
while (!wait) ;

WaitLock.exitGameWaitLock = false;
/*wait = true;
}, null);
while (!wait) ;*/
WaitLock.exitGameWaitLock = false;
}
}
public static class GameKey
Expand Down
26 changes: 11 additions & 15 deletions Assets/Scripts/MainMenu/MainMenu_StartGameText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@

public class MainMenu_StartGameText : MonoBehaviour
{
public const string version = "1.1.0.20241122";
public const string version = "1.1.1.20241124";

private void Start()
{
GameObject.Find("TextUI/About").GetComponent<Text>().text =
@$"°æ±¾: {version}
Copyright (C) 2024 Hgnim, All rights reserved.
Github: https://github.com/Hgnim/CursorTraining";
}


private void Update()
private void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKey(KeyCode.A))
{
MessageBox(IntPtr.Zero,
"游戏名: 光标训练(CursorTraining)\r\n" +
"版本: V"+version+"\r\n" +
"Copyright (C) 2024 Hgnim, All rights reserved.\r\n" +
"Github: https://github.com/Hgnim/CursorTraining"
, "关于", 0);
goto exitVoid;
}
else if(Input.GetKey(KeyCode.Escape))
if(Input.GetKey(KeyCode.Escape))
goto exitVoid;


DontDestroyOnLoad(GameObject.Find("KeyManager"));
DontDestroyOnLoad(GameObject.Find("ThreadDelegate"));
//DontDestroyOnLoad(GameObject.Find("ThreadDelegate"));
SceneManager.LoadScene("Game");
}
exitVoid:;
Expand Down
Loading

0 comments on commit c0e51e5

Please sign in to comment.