西部牛仔

This commit is contained in:
杨号敬 2025-01-09 15:47:49 +08:00
parent 12d3f86068
commit f21d17dc53
7 changed files with 1746 additions and 56 deletions

File diff suppressed because it is too large Load Diff

View File

@ -140,10 +140,10 @@ public class FishingPK : MonoBehaviour
Debug.Log(fishResponse.data.countdown_type);
if(fishResponse.data.countdown_type==0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
@ -151,11 +151,11 @@ public class FishingPK : MonoBehaviour
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
@ -196,21 +196,21 @@ public class FishingPK : MonoBehaviour
Debug.Log(fishResponse2.data.countdown_type);
if (fishResponse2.data.countdown_type == 0)
{
photoMovement.type = true;
photoMovement.Del1();
photoMovement.To1();
photoMovement1.type = true;
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.type = false;
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.type = false;
photoMovement1.To2();
}

View File

@ -23,54 +23,92 @@ public class PhotoMovement : MonoBehaviour
public bool type=false;
public Sprite[] sprites;
int count = 0;
// 控制 To1 和 To2 的执行
private bool isTo1Running = false; // 标识 To1 是否正在执行
private bool isTo2Running = false; // 标识 To2 是否正在执行
private void Start()
{
image.gameObject.SetActive(false);
}
public async void To1()
{
if (type == true)
{
To1();
}
// 如果 To1 已经在执行,则返回,不执行新的 To1
if (isTo1Running || isTo2Running)
return;
isTo1Running = true; // 设置 To1 为正在执行状态
// 执行 To1 的操作
shuaigan.gameObject.SetActive(true);
var time=shuaigan.GetComponent<SpriteAniation>().sprites.Count * shuaigan.GetComponent<SpriteAniation>().Aintime;
var time = shuaigan.GetComponent<SpriteAniation>().sprites.Count * shuaigan.GetComponent<SpriteAniation>().Aintime;
await Task.Delay((int)time);
daiji.gameObject.SetActive(true);
shuaigan.gameObject.SetActive(false);
//await Task.Delay(1000);
// 延迟 1000 毫秒(或者你可以自定义)
daiji.gameObject.SetActive(false);
shougan.gameObject.SetActive(true);
var time1 = shougan.GetComponent<SpriteAniation>().sprites.Count * shougan.GetComponent<SpriteAniation>().Aintime;
// 开始沿路径移动
StartCoroutine(MoveAlongPath());
currentPathIndex = 0;
image.gameObject.SetActive(true);
image.GetComponent<Image>().sprite = sprites[count% sprites.Length];
count++;
await Task.Delay((int)time1-100);
Del1();
// 随机选择一个 sprite
int randomIndex = Random.Range(0, sprites.Length); // 获取随机索引
image.GetComponent<Image>().sprite = sprites[randomIndex]; // 使用随机的 sprite
count++;
await Task.Delay((int)time1 - 100); // 延迟结束,准备执行下一个步骤
Del1(); // 结束当前操作
isTo1Running = false; // 执行完 To1恢复为未执行状态
// 如果没有正在执行 To2则继续执行 To1
if (!isTo2Running)
{
To1();
}
}
public void Del1()
{
image.gameObject.SetActive(false);
shuaigan.gameObject.SetActive(false);
daiji.gameObject.SetActive(false) ;
shougan.gameObject .SetActive(false) ;
daiji.gameObject.SetActive(false);
shougan.gameObject.SetActive(false);
currentPathIndex = 0;
}
public async void To2()
{
// 如果 To1 正在执行,打断 To1 的执行
isTo2Running = true; // 标记 To2 为正在执行
// 等待 To1 完成执行(如果 To1 正在执行)
while (isTo1Running)
{
await Task.Delay(100); // 检查 To1 的执行状态
}
// 执行 To2 的操作
shuaigan.gameObject.SetActive(true);
var time = shuaigan.GetComponent<SpriteAniation>().sprites.Count * shuaigan.GetComponent<SpriteAniation>().Aintime;
await Task.Delay((int)time);
daiji.gameObject.SetActive(true);
shuaigan.gameObject.SetActive(false);
// 执行完 To2 后,恢复 To2 的标志为 false
isTo2Running = false;
}
private IEnumerator MoveAlongPath()
{
// 持续沿着路径点移动
@ -86,19 +124,21 @@ public class PhotoMovement : MonoBehaviour
// 移动到下一个路径点
currentPathIndex++;
}
image.gameObject.SetActive(false);
shougan.gameObject.SetActive(false);
image.transform.localPosition=new Vector3 (467, -144, 0);
image.transform.localPosition = new Vector3(467, -144, 0);
}
public void change()
{
StartCoroutine(MoveAlongPath());
currentPathIndex = 0;
image.gameObject.SetActive(true);
}
public void Update()
{
}
}

View File

@ -30,6 +30,7 @@ public class WebFishingPK : MonoBehaviour
/// </summary>
public Text lefttext;
public Text righttext;
public Text balance;
public Button TouruBtn;
public Button selleftbtn;
public Button selrightbtn;
@ -44,6 +45,12 @@ public class WebFishingPK : MonoBehaviour
/// </summary>
///
public PhotoMovement photoMovement;
public PhotoMovement photoMovement1;
public Button returnbtn;
private async void Awake()
{
await ConnectWebSocket();
}
private async void Start()
{
TimeText.gameObject.SetActive(false);
@ -70,7 +77,10 @@ public class WebFishingPK : MonoBehaviour
{
await SendJsonMessage(int.Parse(Dropdown.options[Dropdown.value].text), pos);
});
returnbtn.onClick.AddListener(async () =>
{
await SendJsonMessage("{ \"code\": \"FISHING_PK\", \"content\": \"{\\\"action\\\":\\\"LEAVE\\\"}\" }");
});
}
private void ChangeButtonColor(Button btn, UnityEngine.Color color)
{
@ -132,6 +142,7 @@ public class WebFishingPK : MonoBehaviour
if (fishResponse?.data != null)
{
Debug.Log(fishResponse.data.balance);
balance.text= fishResponse.data.balance.ToString();
Debug.Log(fishResponse.data.intro_text);
introtext.text = fishResponse.data.intro_text;
Debug.Log(fishResponse.data.countdown);
@ -142,11 +153,16 @@ public class WebFishingPK : MonoBehaviour
{
photoMovement.Del1();
photoMovement.To1();
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.To2();
}
Debug.Log(fishResponse.data.amount_left);
@ -165,6 +181,7 @@ public class WebFishingPK : MonoBehaviour
if (fishResponse1 != null && fishResponse1.data != null)
{
Debug.Log(fishResponse1.data.balance);
balance.text = fishResponse1.data.balance.ToString();
}
else
{
@ -187,11 +204,15 @@ public class WebFishingPK : MonoBehaviour
{
photoMovement.Del1();
photoMovement.To1();
photoMovement1.Del1();
photoMovement1.To1();
}
else
{
photoMovement.Del1();
photoMovement.To2();
photoMovement1.Del1();
photoMovement1.To2();
}
Debug.Log(fishResponse2.data.amount_left);
Debug.Log(fishResponse2.data.amount_right);
@ -210,6 +231,7 @@ public class WebFishingPK : MonoBehaviour
if (fishResponse3 != null && fishResponse3.data != null)
{
Debug.Log(fishResponse3.data.balance);
balance.text = fishResponse3.data.balance.ToString();
Debug.Log(fishResponse3.data.status);
Debug.Log(fishResponse3.data.amount);
Debug.Log(fishResponse3.data.reward);

View File

@ -38,6 +38,10 @@ public class WebJoinroom : MonoBehaviour
public GameObject yupanel;
public Image zhanshiyu;
public Button returnbtn;
private async void Awake()
{
await ConnectWebSocket();
}
private async void Start()
{
yupanel.SetActive(false);
@ -45,7 +49,6 @@ public class WebJoinroom : MonoBehaviour
Debug.Log(PlayerPrefs.GetString("UserToken"));
AuthorizationValue = PlayerPrefs.GetString("UserToken");
await ConnectWebSocket();
returnbtn.onClick.AddListener(async () =>
{
await SendJsonMessage("{ \"code\": \"FISHING\", \"content\": \"{\\\"action\\\":\\\"LEAVE\\\"}\" }");

View File

@ -0,0 +1,270 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;
using System.Drawing;
using UnityEngine.UI;
using DG.Tweening;
public class Westerncowboy : MonoBehaviour
{
private ClientWebSocket _webSocket;
private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
private string AuthorizationValue;
public int count = 0;
//界面元素
public Text balance;
public Text intro_text;
public Text surpluscount_text;
public Button Tourubtn;
private float lastCallTime = 0f;
float remainingTime = 0f;
public Text TimeText;
private float interval = 1f; // 每秒调用一次
public Button returnbtn;
private async void Awake()
{
await ConnectWebSocket();
}
private async void Start()
{
TimeText.gameObject.transform.parent.gameObject.SetActive(false);
Debug.Log(PlayerPrefs.GetString("UserToken"));
AuthorizationValue = PlayerPrefs.GetString("UserToken");
returnbtn.onClick.AddListener(async () =>
{
await SendJsonMessage("{ \"code\": \"WEST_COWBOY\", \"content\": \"{\\\"action\\\":\\\"LEAVE\\\"}\" }");
});
Tourubtn.onClick.AddListener(async () =>
{
await SendJsonMessage(10,1);
});
// 调用发送方法
await SendJsonMessage("{ \"code\": \"WEST_COWBOY\", \"content\": \"{\\\"action\\\":\\\"INFO\\\"}\" }");
}
private async Task ConnectWebSocket()
{
_webSocket = new ClientWebSocket();
// 添加 Authorization 头
_webSocket.Options.SetRequestHeader("Authorization", "Bearer " + AuthorizationValue);
// _webSocket.Options.SetRequestHeader("client-info",);
try
{
Debug.Log("正在连接到 WebSocket...");
await _webSocket.ConnectAsync(new Uri(WebSocketUri), CancellationToken.None);
Debug.Log("WebSocket 连接成功!");
// 开始接收消息
_ = ReceiveMessages();
}
catch (Exception e)
{
Debug.LogError($"WebSocket 连接失败:{e.Message}");
}
}
private async Task ReceiveMessages()
{
var buffer = new byte[1024];
try
{
while (_webSocket.State == WebSocketState.Open)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Debug.Log("WebSocket 连接已被服务器关闭。");
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "关闭连接", CancellationToken.None);
}
else
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Debug.Log($"接收到消息:{message}");
var baseResponse = JsonConvert.DeserializeObject<BaseResponse>(message);
Promptmgr.Instance.PromptBubble(baseResponse.message);
if (baseResponse != null)
{
switch (baseResponse.code)
{
// 解析消息为 Fishresponse 对象
case "FISHING_INFO":
// 解析为 FishJoinroomresponse 类型
FishJoinroomresponse fishResponse = JsonConvert.DeserializeObject<FishJoinroomresponse>(message);
if (fishResponse?.data != null)
{
}
break;
case "FISHING_BETTING":
// 解析消息为 Fishresponse 对象
FishBetonresponse fishResponse1 = JsonConvert.DeserializeObject<FishBetonresponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse1.message);
// 检查是否成功反序列化
if (fishResponse1 != null && fishResponse1.data != null)
{
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
case "FISHING_FISHING":
// 解析消息为 Fishresponse 对象
Fishingresponse fishResponse2 = JsonConvert.DeserializeObject<Fishingresponse>(message);
//Promptmgr.Instance.PromptBubble(fishResponse2.message);
// 检查是否成功反序列化
if (fishResponse2 != null && fishResponse2.data != null)
{
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
case "BALANCE":
break;
}
}
}
}
}
catch (Exception e)
{
Debug.LogError($"接收消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(int count,int pos)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var message = new
{
code = "WEST_COWBOY",
content = $"{{\"action\":\"BETTING\",\"amount\":{count},\"count\":{pos}}}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonConvert.SerializeObject(message);
var encodedMessage = Encoding.UTF8.GetBytes(jsonMessage);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{jsonMessage}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(string json)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var encodedMessage = Encoding.UTF8.GetBytes(json);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{json}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
private async void OnApplicationQuit()
{
if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "应用程序退出", CancellationToken.None);
_webSocket.Dispose();
Debug.Log("WebSocket 连接已关闭。");
}
}
public void disbalance(float detail)
{
balance.text = detail.ToString();
}
public void dissurplus(float detail, int p)
{
surpluscount_text.text = (p * (int)detail).ToString() + "金币";
}
void UpdateCountdownText(float remainingTime)
{
// 将剩余时间转换为小时、分钟和秒
int hours = Mathf.FloorToInt(remainingTime / 3600);
int minutes = Mathf.FloorToInt((remainingTime % 3600) / 60);
int seconds = Mathf.FloorToInt(remainingTime % 60);
// 使用格式化字符串显示倒计时00:00:00
TimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
}
private void Update()
{
if (Time.time - lastCallTime >= interval)
{
// 每秒调用一次的代码
if (remainingTime > 0)
{
remainingTime -= 1;
UpdateCountdownText(remainingTime);
Debug.Log("进入倒计时");
}
if (remainingTime <= 0)
{
TimeText.gameObject.transform.parent.gameObject.SetActive(false);
}
// 更新上次调用时间
lastCallTime = Time.time;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9163bcd1e1639854aae4695bd6c798d4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: