77 lines
3.2 KiB
C#
Raw Normal View History

2025-11-20 17:06:04 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterManager : MonoBehaviour
{
// ========== SerializeField ==============
[SerializeField] private Camera mainCamera;
[SerializeField] private Transform characterContainer;
[SerializeField] private float screenOffsetLeft = 8f;
[SerializeField] private float screenOffsetRight = 6f;
// ========== Private ======================
private List<Character> characterList = new();
private readonly List<Vector3> characterPositionList = new();
private void Start()
{
if (mainCamera == null) mainCamera = Camera.main;
MathWorldPositionPoint(); // 计算每个单位的站位
}
public void SetFightCharacter(List<Character> characters) => characterList = characters;
public List<Card> RenderCharacterSprite()
{
List<Card> fightCharacterOwnedCard = new();
for (var i = 0; i < characterList.Count; i++)
{
DATACLASS_CHARACTER_RENDER_DATA render_data;
render_data.IS_ENEMY = false;
render_data.RENDER_POSITION = characterPositionList[i];
render_data.ROTATION = Quaternion.identity;
GameObject character_prefab = Instantiate<GameObject>(
characterList[i].gameObject,
position: render_data.RENDER_POSITION,
rotation: Quaternion.identity,
parent: characterContainer
);
character_prefab.name = $"Character_{i}";
characterList[i].RecordRenderData(render_data);
var character_card = characterList[i].SetCardID();
fightCharacterOwnedCard.AddRange(character_card);
}
return fightCharacterOwnedCard;
}
private void MathWorldPositionPoint()
{
// 计算屏幕边界
float screen_half = Screen.width / 2f;
float screen_offset_right = screen_half / screenOffsetRight; // 屏幕右边的偏移
float screen_offset_left = screen_half / screenOffsetLeft; // 屏幕左边的偏移
float in_fact_space = screen_half - screen_offset_right - screen_offset_left; // 屏幕内实际可用空间
float sprite_space = in_fact_space / 3f; // 3张牌的实际可用空间
float sprite_offset = (sprite_space * 0 + sprite_space * 1) / 2f; // 每个角色偏移一个单位
for (int i = 0; i < 3; i++)
{
float position_x = screen_offset_left + i * sprite_space + sprite_offset;
characterPositionList.Add(switch_screen_position_to_world_position(position_x));
// float position_x_enemy = screen_half + screen_offset_right + i * sprite_space + _sprite_offset;
// enemyPositionList.Add(switch_screen_position_to_world_position(position_x_enemy));
}
Vector3 switch_screen_position_to_world_position(float screen_position_x)
{
Vector3 world_position = mainCamera!.ScreenToWorldPoint(
new Vector3(screen_position_x, Screen.height / 2f, 0));
world_position.y += characterContainer.transform.position.y;
world_position.z = 0;
return world_position;
}
}
}