|
| 1 | +using System; |
| 2 | +using Photon.Pun; |
| 3 | +using Unity.VisualScripting; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +public class MapHandler : MonoBehaviour |
| 7 | +{ |
| 8 | + private SpriteRenderer spriteRenderer; // 맵 스프라이트 렌더러 |
| 9 | + private Texture2D texture; // 맵 텍스쳐 |
| 10 | + [SerializeField] private Texture2D mapTexture; // 사용할 맵 텍스쳐 |
| 11 | + private float worldWidth; // 월드 너비 |
| 12 | + private float worldHeight; // 월드 높이 |
| 13 | + private int pixelWidth; // 픽셀 너비 |
| 14 | + private int pixelHeight; // 픽셀 높이 |
| 15 | + |
| 16 | + private PhotonView PV; // 포톤 뷰 |
| 17 | + |
| 18 | + private void Awake() |
| 19 | + { |
| 20 | + spriteRenderer = GetComponent<SpriteRenderer>(); |
| 21 | + PV = GetComponent<PhotonView>(); |
| 22 | + |
| 23 | + // 사용할 맵 텍스쳐로 생성 후 할당 |
| 24 | + texture = Instantiate(mapTexture); |
| 25 | + |
| 26 | + Debug.Log(texture.width); |
| 27 | + |
| 28 | + int width = (int)InGameManager.IT.MAX_WIDTH; |
| 29 | + int height = (int)InGameManager.IT.MAX_HEIGHT; |
| 30 | + for (int h=height; h<texture.height; h++) { |
| 31 | + for (int w=0; w<texture.width; w++) { |
| 32 | + texture.SetPixel(w, h, Color.clear); |
| 33 | + } |
| 34 | + } |
| 35 | + for (int w=width; w<texture.width; w++) { |
| 36 | + for (int h=0; h<texture.height; h++) { |
| 37 | + texture.SetPixel(w, h, Color.clear); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + texture.Apply(); |
| 42 | + SetSprite(); |
| 43 | + |
| 44 | + // 맵 크기 계산 |
| 45 | + worldWidth = spriteRenderer.bounds.size.x; |
| 46 | + worldHeight = spriteRenderer.bounds.size.y; |
| 47 | + pixelWidth = spriteRenderer.sprite.texture.width; |
| 48 | + pixelHeight = spriteRenderer.sprite.texture.height; |
| 49 | + |
| 50 | + gameObject.AddComponent<PolygonCollider2D>(); // 폴리곤 콜라이더 추가 |
| 51 | + } |
| 52 | + |
| 53 | + public void MakeRange(CircleCollider2D collider) |
| 54 | + { |
| 55 | + var center = collider.bounds.center; // 충돌한 콜라이더의 중심 위치 |
| 56 | + |
| 57 | + var attackRangeRatio = InGameManager.IT.isAttackRange ? 1.5f : 1.0f; // Range 아이템 사용 시 1.5배 증가 |
| 58 | + |
| 59 | + var radius = Mathf.RoundToInt(collider.bounds.size.x / 2 * pixelWidth / worldWidth * attackRangeRatio); // 반경 (rangeRatio 적용) |
| 60 | + |
| 61 | + PV.RPC(nameof(RPC_MakeRange), RpcTarget.All, center, radius); // RPC 호출 |
| 62 | + } |
| 63 | + |
| 64 | + [PunRPC] |
| 65 | + private void RPC_MakeRange(Vector3 colliderBoundsCenter, int radius) |
| 66 | + { |
| 67 | + var center = GetPixelPosition(colliderBoundsCenter); // 중심 위치 픽셀 좌표 |
| 68 | + |
| 69 | + int px, nx, py, ny, d; // 픽셀 변수들 |
| 70 | + |
| 71 | + // 반경 내의 픽셀들을 투명하게 처리 |
| 72 | + for (var i = 0; i < radius; i++) |
| 73 | + { |
| 74 | + d = Mathf.RoundToInt(Mathf.Sqrt(radius * radius - i * i)); |
| 75 | + |
| 76 | + for (var j = 0; j < d; j++) |
| 77 | + { |
| 78 | + px = center.x + i; |
| 79 | + nx = center.x - i; |
| 80 | + py = center.y + j; |
| 81 | + ny = center.y - j; |
| 82 | + |
| 83 | + texture.SetPixel(px, py, Color.clear); // 랜더러도 갱신됨 |
| 84 | + texture.SetPixel(nx, py, Color.clear); |
| 85 | + texture.SetPixel(px, ny, Color.clear); |
| 86 | + texture.SetPixel(nx, ny, Color.clear); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + texture.Apply(); // 바뀐 픽셀대로 텍스처를 바꿈 |
| 91 | + SetSprite(); // 맵밖 텍스처 색깔 적용 |
| 92 | + |
| 93 | + Destroy(gameObject.GetComponent<PolygonCollider2D>()); // 기존 폴리곤 콜라이더 삭제 |
| 94 | + gameObject.AddComponent<PolygonCollider2D>(); // 새로운 폴리곤 콜라이더 추가 |
| 95 | + |
| 96 | + EffectManager.IT.ActiveProjectileEffect(colliderBoundsCenter); // 포탄 이펙트 활성화 |
| 97 | + } |
| 98 | + |
| 99 | + private void OnTriggerEnter2D(Collider2D col) |
| 100 | + { |
| 101 | + // 포톤 뷰가 내 것이 아니면 리턴 |
| 102 | + if (!PV.IsMine) return; |
| 103 | + // 충돌한 콜라이더가 CircleCollider2D가 아니면 리턴 |
| 104 | + if (!col.GetComponent<CircleCollider2D>()) return; |
| 105 | + |
| 106 | + MakeRange(col.GetComponent<CircleCollider2D>()); // MakeRange 호출 |
| 107 | + |
| 108 | + // 반경 내에 있는 탱크에 거리에 따른 데미지 적용 |
| 109 | + var attackRangeRatio = InGameManager.IT.isAttackRange ? 1.5f : 1.0f; // Range 아이템 사용 시 1.5배 증가 |
| 110 | + |
| 111 | + InGameManager.IT.isAttackRange = false; // Range 아이템 사용 후 초기화 |
| 112 | + |
| 113 | + var radius = col.bounds.size.x / 2 * attackRangeRatio; // 반경 (rangeRatio 적용) |
| 114 | + |
| 115 | + var colliders = Physics2D.OverlapCircleAll(col.bounds.center, radius); // 반경 내의 모든 콜라이더 |
| 116 | + |
| 117 | + // 거리 변수 계산 |
| 118 | + var distance = Vector3.Distance(col.bounds.center, InGameManager.IT.initialPosition); |
| 119 | + |
| 120 | + var maxDistance = 50; // 최대 거리 (임의로 50으로 설정) |
| 121 | + // ratio // distance = 0 -> 1, distance > maxDistance -> 0.5 |
| 122 | + if (distance > maxDistance) distance = maxDistance; |
| 123 | + |
| 124 | + var distanceRatio = 1 - distance / maxDistance / 2; // 거리 변수 |
| 125 | + |
| 126 | + foreach (var collider in colliders) |
| 127 | + { |
| 128 | + // 탱크에게 데미지 적용 |
| 129 | + if (collider.CompareTag("Tank")) |
| 130 | + { |
| 131 | + var tank = collider.GetComponent<TankHandler>(); // 탱크 핸들러 |
| 132 | + |
| 133 | + // 범위 변수 계산 |
| 134 | + var range = Vector2.Distance(col.bounds.center, collider.bounds.center + new Vector3(0, 0.27f, 0)); |
| 135 | + range = Mathf.Round(range * 10) / 10; // 소수 첫째자리에서 반올림 |
| 136 | + |
| 137 | + // ratio // range = 0 -> 1, range = radius -> 0.5 |
| 138 | + if (radius < range) |
| 139 | + range = radius; |
| 140 | + |
| 141 | + var rangeRatio = 1 - range / radius / 2; // 범위 변수 |
| 142 | + |
| 143 | + |
| 144 | + // 데미지 증폭 아이템 유무 |
| 145 | + var damageRatio = InGameManager.IT.isAttackDamage ? 1.5f : 1f; |
| 146 | + |
| 147 | + var damage = 100 * damageRatio * distanceRatio * rangeRatio; // 데미지 계산 |
| 148 | + |
| 149 | + tank.Hit(damage); // 데미지 적용 |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private Vector2Int GetPixelPosition(Vector3 position) |
| 155 | + { |
| 156 | + var pixelPosition = Vector2Int.zero; // 픽셀 위치 초기화 |
| 157 | + |
| 158 | + var dx = position.x - transform.position.x; // x 거리 |
| 159 | + var dy = position.y - transform.position.y; // y 거리 |
| 160 | + |
| 161 | + pixelPosition.x = Mathf.RoundToInt(dx * (pixelWidth / worldWidth) + pixelWidth / 2); // x 픽셀 위치 계산 |
| 162 | + pixelPosition.y = Mathf.RoundToInt(dy * (pixelHeight / worldHeight) + pixelHeight / 2); // y 픽셀 위치 계산 |
| 163 | + |
| 164 | + return pixelPosition; |
| 165 | + } |
| 166 | + private void SetSprite() |
| 167 | + { |
| 168 | + spriteRenderer.sprite = Sprite.Create(texture, |
| 169 | + new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); // 스프라이트 설정 |
| 170 | + } |
| 171 | +} |
0 commit comments