86 lines
3.6 KiB
C#
86 lines
3.6 KiB
C#
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;
|
|
[SerializeField] private RectTransform dealCardPlace;
|
|
private Vector3 dealCardPlaceWorldPosition;
|
|
|
|
// ========== Private ======================
|
|
private List<GameObject> characterList = new();
|
|
private readonly List<Vector3> characterPositionList = new();
|
|
|
|
public void SetupBaseMessage(List<GameObject> characters)
|
|
{
|
|
if (mainCamera == null) mainCamera = Camera.main;
|
|
RectTransformUtility.ScreenPointToWorldPointInRectangle(
|
|
rect: dealCardPlace,
|
|
screenPoint: dealCardPlace.position,
|
|
cam: mainCamera,
|
|
out dealCardPlaceWorldPosition);
|
|
characterList = characters;
|
|
MathWorldPositionPoint();
|
|
}
|
|
|
|
|
|
public List<Card> RenderCharacterSprite()
|
|
{
|
|
List<Card> fightCharacterOwnedCard = new();
|
|
for (var i = 0; i < characterList.Count; i++)
|
|
{
|
|
Character.CharacterRenderData renderData;
|
|
renderData.IS_ENEMY = false;
|
|
renderData.RENDER_POSITION = characterPositionList[i];
|
|
renderData.ROTATION = Quaternion.identity;
|
|
GameObject characterPrefab = Instantiate<GameObject>(
|
|
characterList[i].gameObject,
|
|
position: renderData.RENDER_POSITION,
|
|
rotation: Quaternion.identity,
|
|
parent: characterContainer
|
|
);
|
|
int characterID = (i + 1) * 100;
|
|
characterPrefab.name = $"Character_{characterID}";
|
|
Character characterComponent = characterPrefab.GetComponent<Character>();
|
|
characterComponent.SetupBaseMessage(characterID);
|
|
characterComponent.RecordRenderData(renderData);
|
|
// var character_card = characterList[i].SetCardID();
|
|
// fightCharacterOwnedCard.AddRange(character_card);
|
|
}
|
|
|
|
return fightCharacterOwnedCard;
|
|
}
|
|
|
|
public 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;
|
|
}
|
|
}
|
|
} |