227 lines
6.9 KiB
C#
227 lines
6.9 KiB
C#
using Assets.MyGame.Scripts.Net;
|
|
using Assets.MyGame.Scripts.Net.API;
|
|
using Assets.MyGame.Scripts.Net.WebResponses;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class BullitMove : MonoBehaviour
|
|
{
|
|
public string userid;
|
|
public float moveSpeed = 5;
|
|
public float desTime = 10f;
|
|
public string playerID;
|
|
public GameObject effectPrefab;
|
|
public GameObject diePrefab;
|
|
float currShowTime = 0f;
|
|
Vector3 vector3_startPosition;
|
|
public bool isTriggerFirst = true;
|
|
MonsterContral target;
|
|
public Transform targetTra;
|
|
public string bulletID;
|
|
// Start is called before the first frame update
|
|
void Awake()
|
|
{
|
|
Init();
|
|
//Destroy(gameObject, desTime);
|
|
}
|
|
bool isLock = false;
|
|
public void Lock(MonsterContral monsterContral,Transform endTra)
|
|
{
|
|
foreach (var item in GetComponents<Collider2D>())
|
|
{
|
|
item.enabled = false;
|
|
}
|
|
isLock = true;
|
|
target = monsterContral;
|
|
targetTra = endTra;
|
|
}
|
|
public void NotLock()
|
|
{
|
|
foreach (var item in GetComponents<Collider2D>())
|
|
{
|
|
item.enabled = true;
|
|
}
|
|
isLock = false;
|
|
target = null;
|
|
}
|
|
/// <summary>
|
|
/// 对象池用
|
|
/// </summary>
|
|
public void Init()
|
|
{
|
|
vector3_startPosition = transform.position;
|
|
vector3_startPosition.z = -400;
|
|
transform.position = vector3_startPosition;
|
|
}
|
|
//子弹同步分为两种一种是锁定目标一种是不锁定目标
|
|
// Update is called once per frame
|
|
float ABdistanceTemp=999999999;
|
|
public float a = 0.1f;
|
|
void Update()
|
|
{
|
|
if (isLock)
|
|
{
|
|
// 目标已被销毁或失效 → 解除锁定,变为普通子弹
|
|
if (target == null || targetTra == null || !target.gameObject.activeInHierarchy || !targetTra.gameObject.activeInHierarchy)
|
|
{
|
|
NotLock();
|
|
return;
|
|
}
|
|
Vector3 v3bullet = transform.position;
|
|
v3bullet.z = 0;
|
|
Vector3 v3target = targetTra.transform.position;
|
|
v3target.z = 0;
|
|
var ABdistance = Vector3.Distance(v3bullet, v3target);
|
|
var rotateSpeed = 1/(ABdistance/3);
|
|
Vector3 targetPoint = (targetTra.transform.position-transform.position).normalized * moveSpeed*Time.deltaTime;
|
|
targetPoint.z = transform.position.z;
|
|
//transform.up = Vector2.Lerp(transform.up, targetPoint, rotateSpeed);
|
|
transform.up = Vector2.Lerp(transform.up, targetPoint, a);
|
|
targetPoint = transform.up.normalized * moveSpeed * Time.deltaTime;
|
|
targetPoint.z = 0;
|
|
transform.position+= targetPoint;
|
|
|
|
//if (ABdistance <= 2)
|
|
if (ABdistance > ABdistanceTemp)
|
|
{
|
|
AttackFish(target);
|
|
}
|
|
ABdistanceTemp = ABdistance;
|
|
}
|
|
else
|
|
{
|
|
//方便帧同步
|
|
currShowTime += Time.deltaTime;
|
|
Vector3 targetPoint = transform.up.normalized * currShowTime * moveSpeed;
|
|
targetPoint.z = 0;
|
|
transform.position = vector3_startPosition + targetPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (isTriggerFirst)
|
|
{
|
|
if (collision.transform.tag == "Fish")
|
|
{
|
|
isTriggerFirst = false;
|
|
//击中特效
|
|
if (effectPrefab != null)
|
|
{
|
|
Instantiate(effectPrefab, transform.position, Quaternion.identity, transform.parent);
|
|
}
|
|
|
|
// 只有子弹所属的本地玩家才处理怪物
|
|
if (userid == GameManager.Instance.user.id)
|
|
{
|
|
var monster = collision.transform.GetComponentInParent<MonsterContral>();
|
|
monster.monsterData.charactersHealth.OnHurt(10, CharacterHurtType.none);
|
|
Destroy(monster.gameObject);
|
|
}
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
}
|
|
}
|
|
public static Quaternion RotationFromVector2(Vector2 dir)
|
|
{
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
|
|
return Quaternion.Euler(0, 0, angle);
|
|
}
|
|
// 计算反射角度
|
|
Vector2 GetReflectionAngle(Vector2 velocity, Vector2 normal)
|
|
{
|
|
// 反射向量
|
|
return Vector2.Reflect(velocity.normalized, normal.normalized);
|
|
|
|
}
|
|
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":
|
|
v3.z = -v3.z+180;
|
|
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);
|
|
}
|
|
|
|
// 只有子弹所属的本地玩家才向服务器发送 KillFish,其他人只播放特效
|
|
if (userid == GameManager.Instance.user.id)
|
|
{
|
|
var v2 = GameManager.Instance.gameData.currRoomData;
|
|
WebSocketSendList.KillFish(v2.roomId, userid, monster.monsterData.fishTypeConfig.fishNumber, monster.guid, (int)CreateFishManager.Instance.currGameType.currSitData.consumeGoldNum, bulletID, "63215f0afa3f5b25");
|
|
}
|
|
|
|
|
|
|
|
//受激
|
|
|
|
|
|
|
|
//受激
|
|
|
|
//monster.monsterData.charactersHealth.OnHurt(10, CharacterHurtType.none);
|
|
//monster.HP--;
|
|
//if (monster.HP == 0)
|
|
//{
|
|
// Instantiate(monster.effect_Die == null ? diePrefab : monster.effect_Die, monster.transform.position, Quaternion.identity, transform.parent);
|
|
// Destroy(monster.gameObject);
|
|
//}
|
|
//else
|
|
//{
|
|
monster.OnHurt();
|
|
//}
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
}
|