thelostpenny

Untitled

Feb 17th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerCollision : MonoBehaviour
  4. {
  5. private Texture2D texture;
  6. public int redDamageAmount = 10;
  7. public int blueBlockAmount = 5;
  8. public float cooldown = 2.0f;
  9. private float lastCollisionTime = -1f;
  10. public ContactPointVisualizer playerVisualizer;
  11. public ContactPointVisualizer enemyVisualizer;
  12.  
  13. void Start()
  14. {
  15. SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
  16. if (spriteRenderer != null && spriteRenderer.sprite != null)
  17. {
  18. texture = spriteRenderer.sprite.texture;
  19. }
  20.  
  21. if (playerVisualizer == null)
  22. {
  23. playerVisualizer = gameObject.AddComponent<ContactPointVisualizer>();
  24. playerVisualizer.gizmoColor = Color.black;
  25. }
  26. }
  27.  
  28. private void OnCollisionEnter2D(Collision2D collision)
  29. {
  30. if (collision.gameObject.CompareTag("Enemy"))
  31. {
  32. if (enemyVisualizer == null)
  33. {
  34. enemyVisualizer = collision.gameObject.AddComponent<ContactPointVisualizer>();
  35. enemyVisualizer.gizmoColor = Color.black;
  36. }
  37.  
  38. if (Time.time - lastCollisionTime < cooldown && lastCollisionTime >= 0) return;
  39.  
  40. foreach (ContactPoint2D contact in collision.contacts)
  41. {
  42. Vector2 contactPoint = contact.point;
  43. Vector2 localPointThis = transform.InverseTransformPoint(contactPoint);
  44. Vector2 localPointOther = collision.transform.InverseTransformPoint(contactPoint);
  45.  
  46. Debug.Log($"Contact point: {contactPoint}, Local Point (Player): {localPointThis}, Local Point (Enemy): {localPointOther}");
  47.  
  48. // Rileva il colore del primo pixel non trasparente partendo dal punto di contatto verso l'interno della sprite
  49. Color thisPixelColor = GetNearestColor(texture, localPointThis);
  50. Color otherPixelColor = GetNearestColor(collision.gameObject.GetComponent<SpriteRenderer>().sprite.texture, localPointOther);
  51.  
  52. string thisPixelColorName = GetColorName(thisPixelColor);
  53. string otherPixelColorName = GetColorName(otherPixelColor);
  54.  
  55. Debug.Log($"Player contact color: {thisPixelColorName}, Enemy contact color: {otherPixelColorName}");
  56.  
  57. // Aggiorna i punti di contatto per i gizmo
  58. playerVisualizer.contactPoint = localPointThis;
  59. enemyVisualizer.contactPoint = localPointOther;
  60.  
  61. if (thisPixelColor.a > 0 && otherPixelColor.a > 0)
  62. {
  63. AnalyzeCollision(thisPixelColor, otherPixelColor, collision.gameObject);
  64. lastCollisionTime = Time.time;
  65. break;
  66. }
  67. else
  68. {
  69. Debug.LogWarning($"No valid pixel found for collision between {gameObject.name} and {collision.gameObject.name}");
  70. }
  71. }
  72. }
  73. }
  74.  
  75. private Color GetNearestColor(Texture2D texture, Vector2 localPoint, int maxRadius = 10)
  76. {
  77. if (texture == null)
  78. return Color.clear;
  79.  
  80. // Converti il punto locale in coordinate della texture
  81. int textureX = Mathf.FloorToInt(localPoint.x * texture.width);
  82. int textureY = Mathf.FloorToInt(localPoint.y * texture.height);
  83.  
  84. Debug.Log($"Texture coordinates: ({textureX}, {textureY})");
  85.  
  86. Color detectedColor = Color.clear;
  87.  
  88. // Scansiona per trovare il primo pixel colorato partendo dal punto di contatto verso il centro
  89. for (int radius = 0; radius <= maxRadius; radius++)
  90. {
  91. for (int dx = -radius; dx <= radius; dx++)
  92. {
  93. for (int dy = -radius; dy <= radius; dy++)
  94. {
  95. int x = textureX + dx;
  96. int y = textureY + dy;
  97.  
  98. if (x >= 0 && x < texture.width && y >= 0 && y < texture.height)
  99. {
  100. Color color = texture.GetPixel(x, y);
  101. if (color.a > 0) // Verifica se il pixel non è trasparente
  102. {
  103. Debug.Log($"Detected color: {GetColorName(color)} at ({x}, {y})");
  104. return color;
  105. }
  106. }
  107. }
  108. }
  109. }
  110.  
  111. return Color.clear;
  112. }
  113.  
  114. private string GetColorName(Color color)
  115. {
  116. if (color == Color.red) return "red";
  117. if (color == Color.blue) return "blue";
  118. return "unknown";
  119. }
  120.  
  121. private void AnalyzeCollision(Color thisColor, Color otherColor, GameObject enemy)
  122. {
  123. HealthManager thisHealth = GetComponent<HealthManager>();
  124. HealthManager enemyHealth = enemy.GetComponent<HealthManager>();
  125. EnemyCollision enemyCollision = enemy.GetComponent<EnemyCollision>();
  126.  
  127. if (thisHealth != null && enemyHealth != null && enemyCollision != null)
  128. {
  129. int playerDamage = 0;
  130. int enemyDamage = 0;
  131.  
  132. if (thisColor == Color.red && otherColor == Color.blue)
  133. {
  134. Debug.Log("Player causes damage, Enemy blocks");
  135. enemyDamage = Mathf.Max(redDamageAmount - blueBlockAmount, 0);
  136. enemyCollision.ApplyDamage(enemyHealth, redDamageAmount, blueBlockAmount);
  137. }
  138. else if (thisColor == Color.blue && otherColor == Color.red)
  139. {
  140. Debug.Log("Enemy causes damage, Player blocks");
  141. playerDamage = Mathf.Max(enemyCollision.redDamageAmount - blueBlockAmount, 0);
  142. thisHealth.TakeDamage(enemyCollision.redDamageAmount);
  143. }
  144. else if (thisColor == Color.red && otherColor == Color.red)
  145. {
  146. Debug.Log("Both cause damage");
  147. playerDamage = redDamageAmount;
  148. enemyDamage = redDamageAmount;
  149. enemyCollision.ApplyDamage(enemyHealth, redDamageAmount, 0);
  150. thisHealth.TakeDamage(enemyCollision.redDamageAmount);
  151. }
  152. else if (thisColor == Color.blue && otherColor == Color.blue)
  153. {
  154. Debug.Log("Both block");
  155. }
  156. else
  157. {
  158. Debug.LogWarning($"Unexpected color combination: thisColor: {thisColor}, otherColor: {otherColor}");
  159. }
  160.  
  161. Debug.Log($"Damage dealt - Player: {playerDamage}, Enemy: {enemyDamage}");
  162. Debug.Log($"Player health: {thisHealth.GetCurrentHealth()}, Enemy health: {enemyHealth.GetCurrentHealth()}");
  163.  
  164. if (enemyHealth.GetCurrentHealth() <= 0)
  165. {
  166. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
  167. if (enemies.Length == 1)
  168. {
  169. Debug.Log("Last enemy defeated. Victory!");
  170. }
  171. }
  172. }
  173. }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment