2026-06-20 15:00:15 +08:00
|
|
|
using Assets.MyGame.Scripts.Net;
|
2026-02-03 11:35:19 +08:00
|
|
|
using Assets.MyGame.Scripts.Net.API;
|
|
|
|
|
using Assets.MyGame.Scripts.Net.WebResponses;
|
|
|
|
|
using System.Collections;
|
2025-12-26 11:15:56 +08:00
|
|
|
using System.Collections.Generic;
|
2026-05-27 19:47:06 +08:00
|
|
|
using System.Text;
|
2025-12-26 11:15:56 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class BullitMove : MonoBehaviour
|
|
|
|
|
{
|
2026-03-18 17:26:14 +08:00
|
|
|
public string userid;
|
2025-12-26 11:15:56 +08:00
|
|
|
public float moveSpeed = 5;
|
|
|
|
|
public float desTime = 10f;
|
|
|
|
|
public string playerID;
|
2025-12-26 14:43:20 +08:00
|
|
|
public GameObject effectPrefab;
|
2026-02-03 11:35:19 +08:00
|
|
|
public GameObject diePrefab;
|
2025-12-26 11:15:56 +08:00
|
|
|
float currShowTime = 0f;
|
|
|
|
|
Vector3 vector3_startPosition;
|
2026-02-03 11:35:19 +08:00
|
|
|
public bool isTriggerFirst = true;
|
|
|
|
|
MonsterContral target;
|
2026-05-27 19:47:06 +08:00
|
|
|
public Transform targetTra;
|
2026-03-09 09:25:01 +08:00
|
|
|
public string bulletID;
|
2026-06-14 19:07:11 +08:00
|
|
|
|
|
|
|
|
// 转向参数
|
|
|
|
|
public float maxTurnSpeed = 15f; // 最大转向速度(度/秒)
|
|
|
|
|
public float minTurnSpeed = 2f; // 最小转向速度
|
|
|
|
|
public float distanceForMaxTurn = 1f; // 在此距离内达到最大转向速度
|
|
|
|
|
public float hitDistance = 0.5f; // 命中判定距离
|
|
|
|
|
|
2025-12-26 11:15:56 +08:00
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
Init();
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
bool isLock = false;
|
2026-06-14 19:07:11 +08:00
|
|
|
public void Lock(MonsterContral monsterContral, Transform endTra)
|
2026-02-03 11:35:19 +08:00
|
|
|
{
|
|
|
|
|
foreach (var item in GetComponents<Collider2D>())
|
|
|
|
|
{
|
|
|
|
|
item.enabled = false;
|
|
|
|
|
}
|
|
|
|
|
isLock = true;
|
|
|
|
|
target = monsterContral;
|
2026-05-27 19:47:06 +08:00
|
|
|
targetTra = endTra;
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
public void NotLock()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in GetComponents<Collider2D>())
|
|
|
|
|
{
|
|
|
|
|
item.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
isLock = false;
|
|
|
|
|
target = null;
|
2026-06-14 19:07:11 +08:00
|
|
|
targetTra = null;
|
2025-12-26 11:15:56 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2025-12-26 11:15:56 +08:00
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
vector3_startPosition = transform.position;
|
2026-05-27 19:47:06 +08:00
|
|
|
vector3_startPosition.z = -400;
|
|
|
|
|
transform.position = vector3_startPosition;
|
2025-12-26 11:15:56 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2025-12-26 11:15:56 +08:00
|
|
|
void Update()
|
|
|
|
|
{
|
2026-06-20 15:00:15 +08:00
|
|
|
// 子弹生命周期:超时自动销毁(默认10秒)
|
|
|
|
|
currShowTime += Time.deltaTime;
|
|
|
|
|
if (currShowTime >= desTime)
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
if (isLock)
|
|
|
|
|
{
|
2026-06-14 19:07:11 +08:00
|
|
|
// 目标无效 → 解除锁定
|
2026-05-30 19:17:51 +08:00
|
|
|
if (target == null || targetTra == null || !target.gameObject.activeInHierarchy || !targetTra.gameObject.activeInHierarchy)
|
2026-02-03 11:35:19 +08:00
|
|
|
{
|
|
|
|
|
NotLock();
|
2026-05-30 19:17:51 +08:00
|
|
|
return;
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
2026-05-27 19:47:06 +08:00
|
|
|
|
2026-06-14 19:07:11 +08:00
|
|
|
Vector3 myPos = transform.position;
|
|
|
|
|
myPos.z = 0;
|
|
|
|
|
Vector3 targetPos = targetTra.position;
|
|
|
|
|
targetPos.z = 0;
|
|
|
|
|
|
|
|
|
|
float distance = Vector3.Distance(myPos, targetPos);
|
|
|
|
|
|
|
|
|
|
// 命中判定
|
|
|
|
|
if (distance <= hitDistance)
|
2026-02-03 11:35:19 +08:00
|
|
|
{
|
|
|
|
|
AttackFish(target);
|
2026-06-14 19:07:11 +08:00
|
|
|
return;
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
|
|
|
|
// 计算指向目标的方向
|
|
|
|
|
Vector3 targetDir = (targetPos - myPos).normalized;
|
|
|
|
|
|
|
|
|
|
// 根据距离动态计算转向速度:距离越近转向越快
|
|
|
|
|
float t = Mathf.Clamp01(1f - (distance / distanceForMaxTurn));
|
|
|
|
|
float turnSpeed = Mathf.Lerp(minTurnSpeed, maxTurnSpeed, t);
|
|
|
|
|
// 使用 RotateTowards 平滑转向(角度/秒)
|
|
|
|
|
float currentAngle = Vector2.SignedAngle(transform.up, targetDir);
|
|
|
|
|
float newAngle = Mathf.MoveTowardsAngle(currentAngle, 0f, turnSpeed * Time.deltaTime);
|
|
|
|
|
transform.up = Quaternion.Euler(0, 0, newAngle) * transform.up;
|
|
|
|
|
|
|
|
|
|
// 沿当前方向移动
|
|
|
|
|
transform.position += transform.up * moveSpeed * Time.deltaTime;
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-06-14 19:07:11 +08:00
|
|
|
// 非锁定模式:保持原有直线 + 反弹逻辑(不变)
|
2026-02-03 11:35:19 +08:00
|
|
|
Vector3 targetPoint = transform.up.normalized * currShowTime * moveSpeed;
|
2026-05-27 19:47:06 +08:00
|
|
|
targetPoint.z = 0;
|
2026-02-03 11:35:19 +08:00
|
|
|
transform.position = vector3_startPosition + targetPoint;
|
|
|
|
|
}
|
2025-12-26 11:15:56 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
2025-12-26 11:15:56 +08:00
|
|
|
{
|
|
|
|
|
if (isTriggerFirst)
|
|
|
|
|
{
|
2026-02-03 11:35:19 +08:00
|
|
|
if (collision.transform.tag == "Fish")
|
2025-12-26 11:15:56 +08:00
|
|
|
{
|
2026-02-03 11:35:19 +08:00
|
|
|
isTriggerFirst = false;
|
|
|
|
|
if (effectPrefab != null)
|
|
|
|
|
{
|
|
|
|
|
Instantiate(effectPrefab, transform.position, Quaternion.identity, transform.parent);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 19:17:51 +08:00
|
|
|
if (userid == GameManager.Instance.user.id)
|
|
|
|
|
{
|
|
|
|
|
var monster = collision.transform.GetComponentInParent<MonsterContral>();
|
|
|
|
|
monster.monsterData.charactersHealth.OnHurt(10, CharacterHurtType.none);
|
|
|
|
|
Destroy(monster.gameObject);
|
|
|
|
|
}
|
2025-12-26 11:15:56 +08:00
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-03 11:35:19 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
public static Quaternion RotationFromVector2(Vector2 dir)
|
|
|
|
|
{
|
|
|
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
|
|
|
|
|
return Quaternion.Euler(0, 0, angle);
|
|
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
Vector2 GetReflectionAngle(Vector2 velocity, Vector2 normal)
|
|
|
|
|
{
|
|
|
|
|
return Vector2.Reflect(velocity.normalized, normal.normalized);
|
|
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
public void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
|
{
|
|
|
|
|
if (isTriggerFirst)
|
|
|
|
|
{
|
|
|
|
|
if (collision.tag == "Fish")
|
|
|
|
|
{
|
|
|
|
|
isTriggerFirst = false;
|
|
|
|
|
var monster = collision.transform.GetComponentInParent<MonsterContral>();
|
|
|
|
|
AttackFish(monster);
|
|
|
|
|
}
|
|
|
|
|
else if (collision.tag == "Wall")
|
|
|
|
|
{
|
|
|
|
|
Vector3 v3 = transform.eulerAngles;
|
|
|
|
|
switch (collision.name)
|
|
|
|
|
{
|
|
|
|
|
case "up":
|
2026-06-14 19:07:11 +08:00
|
|
|
v3.z = -v3.z + 180;
|
2026-02-03 11:35:19 +08:00
|
|
|
break;
|
|
|
|
|
case "down":
|
|
|
|
|
v3.z = -v3.z + 180;
|
|
|
|
|
break;
|
|
|
|
|
case "left":
|
|
|
|
|
v3.z = -v3.z;
|
|
|
|
|
break;
|
|
|
|
|
case "right":
|
|
|
|
|
v3.z = -v3.z;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
vector3_startPosition = transform.position;
|
|
|
|
|
currShowTime = 0;
|
|
|
|
|
transform.eulerAngles = v3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AttackFish(MonsterContral monster)
|
|
|
|
|
{
|
|
|
|
|
if (effectPrefab != null)
|
|
|
|
|
{
|
|
|
|
|
Instantiate(effectPrefab, transform.position, Quaternion.identity, transform.parent);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 19:17:51 +08:00
|
|
|
if (userid == GameManager.Instance.user.id)
|
2026-02-09 10:44:38 +08:00
|
|
|
{
|
2026-05-30 19:17:51 +08:00
|
|
|
var v2 = GameManager.Instance.gameData.currRoomData;
|
2026-05-27 19:47:06 +08:00
|
|
|
WebSocketSendList.KillFish(v2.roomId, userid, monster.monsterData.fishTypeConfig.fishNumber, monster.guid, (int)CreateFishManager.Instance.currGameType.currSitData.consumeGoldNum, bulletID, "63215f0afa3f5b25");
|
2026-02-09 10:44:38 +08:00
|
|
|
}
|
2026-05-27 19:47:06 +08:00
|
|
|
|
2026-02-03 11:35:19 +08:00
|
|
|
monster.OnHurt();
|
|
|
|
|
Destroy(gameObject);
|
2025-12-26 11:15:56 +08:00
|
|
|
}
|
2026-06-14 19:07:11 +08:00
|
|
|
}
|