Skip to content

Commit

Permalink
V1.0.0.20241015 code files
Browse files Browse the repository at this point in the history
  • Loading branch information
Hgnim committed Oct 15, 2024
1 parent 2b692ed commit 515194b
Show file tree
Hide file tree
Showing 6 changed files with 641 additions and 0 deletions.
152 changes: 152 additions & 0 deletions Assets/Scripts/Game/NumberDataControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using static PublicClass.DLL;
using Debug = UnityEngine.Debug;

public class NumberDataControl : MonoBehaviour
{
public Text ScoreUI_Text;
public Text TimeUI_Text;
public GameObject single_ClickTarget;

public static bool gameIsStart = false;
/// <summary>
/// 游戏剩余时间,单位: 10ms
/// </summary>
private static int gameTime = 3000;
public static int GetGameTime { get { return gameTime; } }
/// <summary>
/// 更改游戏剩余时间且更新UI显示<br/>
/// 将值设置为-1时只刷新UI,不设置值
/// </summary>
public int GameTimeUI
{
get { return gameTime; }
set
{
if(value!=-1)
gameTime = value;
string timeStr = gameTime.ToString().PadLeft(4, '0');
TimeUI_Text.text = timeStr[..^2] + ":" + timeStr.Substring(timeStr.Length - 2, 2);
}
}
/// <summary>
/// 游戏得分
/// </summary>
private static int gameScore = 0;
public static int GetGameScore
{
get { return gameScore; }
}
int GameScore
{
get { return gameScore; }
set
{
gameScore = value;
ScoreUI_Text.text = gameScore.ToString();
}
}

/// <summary>
/// 表示鼠标点击了某物
/// </summary>
/// <param name="isHit">是否命中目标</param>
internal delegate void MouseClickSomething(bool isHit);
internal static event MouseClickSomething MouseClickST_event;
internal static void Trigger_MouseClickST_event(bool isHit_)
{
MouseClickST_event(isHit_);
}

/// <summary>
/// 全写: MouseClickSomething<br/>
/// 表示鼠标点击了某物
/// </summary>
/// <param name="isHit">是否命中目标</param>
internal void MouseClickST(bool isHit)
{
if (!gameIsStart)
{
gameIsStart = true;
countdownThread = new(CountdownThread);
countdownUIThread = new(CountdownUIThread);
countdownThread.Start();
countdownUIThread.Start();
}
if (isHit)
GameScore++;
else
GameScore--;

}
Thread countdownThread;
Thread countdownUIThread;
void CountdownThread()
{
while (gameTime > 0)
{
Sleep(10);
gameTime--;
}
gameTime = 0;

MessageBox(IntPtr.Zero, "最后得分为: " + gameScore.ToString(), "游戏结束", 0);
{
bool wait = false;
ThreadDelegate.QueueOnMainThread((param) =>
{
single_ClickTarget.transform.localPosition = new Vector2(0, 0);
GameScore = 0;
gameIsStart = false;
GameTimeUI = 3000;
wait = true;
}, null);
while (!wait) ;
}
}
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 += MouseClickST;
}
/// <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);
}
}
}
35 changes: 35 additions & 0 deletions Assets/Scripts/Game/Single_ClickTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Single_ClickTarget : MonoBehaviour
{
const float minX = -8.7f;
const float minY = -4.4f;
const float maxX = 8.7f;
const float maxY = 4.4f;

void Update()
{
if (Input.GetMouseButtonDown(0)||Input.GetMouseButtonDown(1))
{
if (NumberDataControl.GetGameTime != 0)
{
// 射线检测碰撞器是否被点击
Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(clickPosition, Vector2.zero);
// 不为null,则认为有物体撞到
if (hit.collider != null)
{
var hitObj = hit.collider.gameObject;
// 自行逻辑处理

transform.localPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
NumberDataControl.Trigger_MouseClickST_event(true);
}
else
NumberDataControl.Trigger_MouseClickST_event(false);
}
}
}
}
Loading

0 comments on commit 515194b

Please sign in to comment.