TH1/Unity/Assets/Scripts/TH1Renderer/ProjectileManager.cs
2025-08-15 00:39:22 +08:00

154 lines
6.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using ET;
using UnityEngine;
using RuntimeData;
using Logic;
using TMPro;
using UI;
using Unity.Collections;
namespace TH1Renderer
{
public enum ProjectileType { Arrow, Bomb, MagicFireBall,CityExp,Coin, Faith,RemiliaAttack,PatchouliAttack}
public enum ProjectileMoveType { Straight, Parabola,HighParabola,CityExpHighParabola, CoinParabola,Spin, Curved,Bounce,SpecialAnimation}
public class ProjectileData
{
public MapData MapData;
public uint Id;
public ProjectileType ProjectileType;
public ProjectileMoveType ProjectileMoveType;
public Vector3 StartPos;
public Vector3 EndPos;
public ProjectileData(MapData mapData,uint id,ProjectileType projectileType,ProjectileMoveType moveType,Vector3 startPos,Vector3 endPos)
{
MapData = mapData;
Id = id;
ProjectileType = projectileType;
ProjectileMoveType = moveType;
StartPos = startPos;
EndPos = endPos;
}
}
public class ProjectileManager
{
public uint IdCalculater;
public Dictionary<uint, ProjectileData> ProjectileDataDict;
public Dictionary<uint, ProjectileRenderer> ROProjectileMap;
//存储的projectileRenderMap对象(在主界面挂在谁下面)和projectilePrefab对象(用来实例化)
private GameObject _projectilePrefab;
private Transform _projectileRenderMap;
private Transform _coinTransform;
public void Init(Transform trans)
{
IdCalculater = 0;
_projectileRenderMap = trans;
ProjectileDataDict = new Dictionary<uint, ProjectileData>();
ROProjectileMap = new Dictionary<uint, ProjectileRenderer>();
_projectilePrefab = Resources.Load<GameObject>($"Prefab/projectilePrefab");
}
public void Update()
{
foreach(var t in ROProjectileMap)
t.Value.Update();
}
//返回coin要改飞往的地方
public Vector3 GetCoinPos()
{
if (_coinTransform == null)
{
_coinTransform = UIManager.Instance.ROUIManager?.transform.Find("TopBarPanel/MoneyPanel/Icon")?.transform;
}
return _coinTransform.position;
}
//创建一个飞行道具,要传入起始位置和结束位置,然后传入飞行道具类型,飞行方式
public void CreateProjectile(Vector3 startPos, Vector3 endPos,
ProjectileType projectileType, ProjectileMoveType moveType, int para = -1)
{
var mapData = Main.MapData;
//如果不在视野,不处理
mapData.GridMap.GetGridDataByVector3Pos(startPos, out var g1);
mapData.GridMap.GetGridDataByVector3Pos(endPos,out var g2);
if (g1 == null || g2 == null) return;
if (!mapData.PlayerMap.SelfPlayerData.Sight.CheckIsInSight(g1.Id)
&& mapData.PlayerMap.SelfPlayerData.Sight.CheckIsInSight(g2.Id)) return;
//先创建新的projectileData数据
var t = new ProjectileData(mapData, IdCalculater++, projectileType, moveType, startPos, endPos);
ProjectileDataDict[t.Id] = t;
//再创建对应projectileRender对象以及绑定的prefab
ROProjectileMap[t.Id] = new ProjectileRenderer(_projectilePrefab, _projectileRenderMap, t);
if (projectileType == ProjectileType.Faith)
ROProjectileMap[t.Id].GetROProjectile().transform.Find("Faith/Text").GetComponent<TextMeshPro>().text = "+" + para;
}
//TODO 把这里multi函数和上面的projectile函数全部换成便捷方式(下方的Coin和Faith
public void CreateProjectileMulti(Vector3 startPos, Vector3 endPos,
ProjectileType projectileType, ProjectileMoveType moveType,int count, float interval = 0.1f)
{
var mapData = Main.MapData;
if (count == 0)
return;
//如果不在视野,不处理
if(mapData.GridMap.GetGridDataByVector3Pos(startPos, out var g1)
&& mapData.GridMap.GetGridDataByVector3Pos(endPos,out var g2))
if (!mapData.PlayerMap.SelfPlayerData.Sight.CheckIsInSight(g1.Id)
&& mapData.PlayerMap.SelfPlayerData.Sight.CheckIsInSight(g2.Id))
return;
count--;
//先创建新的projectileData数据
var t = new ProjectileData(mapData, IdCalculater++, projectileType, moveType, startPos, endPos);
ProjectileDataDict[t.Id] = t;
//再创建对应projectileRender对象以及绑定的prefab
ROProjectileMap[t.Id] = new ProjectileRenderer(_projectilePrefab, _projectileRenderMap, t);
float tt = 0f;
while (count > 0)
{
count--;
tt += interval;
Timer.Instance.TimerRegister(this, () => {
var ttt = new ProjectileData(mapData, IdCalculater++, projectileType, moveType, startPos, endPos);
ProjectileDataDict[t.Id] = t;
//再创建对应projectileRender对象以及绑定的prefab
ROProjectileMap[ttt.Id] = new ProjectileRenderer(_projectilePrefab, _projectileRenderMap, t);
}
,tt,"ProjectileManager_CreateMulti");
//先创建新的projectileData数据
}
}
//发起coin获得动画的便捷函数
public void CreateCoinProjectile(Vector3 startPos,int count)
{
CreateProjectileMulti(startPos, GetCoinPos(),
ProjectileType.Coin, ProjectileMoveType.CoinParabola, count: count, interval: 0.05f);
}
//发起faith获得动画的便捷函数
public void CreateFaithProjectile(Vector3 startPos,int num)
{
}
}
}