Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerCollision : MonoBehaviour
- {
- private Texture2D texture;
- public int redDamageAmount = 10;
- public int blueBlockAmount = 5;
- public float cooldown = 2.0f;
- private float lastCollisionTime = -1f;
- public ContactPointVisualizer playerVisualizer;
- public ContactPointVisualizer enemyVisualizer;
- void Start()
- {
- SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
- if (spriteRenderer != null && spriteRenderer.sprite != null)
- {
- texture = spriteRenderer.sprite.texture;
- }
- if (playerVisualizer == null)
- {
- playerVisualizer = gameObject.AddComponent<ContactPointVisualizer>();
- playerVisualizer.gizmoColor = Color.black;
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.CompareTag("Enemy"))
- {
- if (enemyVisualizer == null)
- {
- enemyVisualizer = collision.gameObject.AddComponent<ContactPointVisualizer>();
- enemyVisualizer.gizmoColor = Color.black;
- }
- if (Time.time - lastCollisionTime < cooldown && lastCollisionTime >= 0) return;
- foreach (ContactPoint2D contact in collision.contacts)
- {
- Vector2 contactPoint = contact.point;
- Vector2 localPointThis = transform.InverseTransformPoint(contactPoint);
- Vector2 localPointOther = collision.transform.InverseTransformPoint(contactPoint);
- Debug.Log($"Contact point: {contactPoint}, Local Point (Player): {localPointThis}, Local Point (Enemy): {localPointOther}");
- // Rileva il colore del primo pixel non trasparente partendo dal punto di contatto verso l'interno della sprite
- Color thisPixelColor = GetNearestColor(texture, localPointThis);
- Color otherPixelColor = GetNearestColor(collision.gameObject.GetComponent<SpriteRenderer>().sprite.texture, localPointOther);
- string thisPixelColorName = GetColorName(thisPixelColor);
- string otherPixelColorName = GetColorName(otherPixelColor);
- Debug.Log($"Player contact color: {thisPixelColorName}, Enemy contact color: {otherPixelColorName}");
- // Aggiorna i punti di contatto per i gizmo
- playerVisualizer.contactPoint = localPointThis;
- enemyVisualizer.contactPoint = localPointOther;
- if (thisPixelColor.a > 0 && otherPixelColor.a > 0)
- {
- AnalyzeCollision(thisPixelColor, otherPixelColor, collision.gameObject);
- lastCollisionTime = Time.time;
- break;
- }
- else
- {
- Debug.LogWarning($"No valid pixel found for collision between {gameObject.name} and {collision.gameObject.name}");
- }
- }
- }
- }
- private Color GetNearestColor(Texture2D texture, Vector2 localPoint, int maxRadius = 10)
- {
- if (texture == null)
- return Color.clear;
- // Converti il punto locale in coordinate della texture
- int textureX = Mathf.FloorToInt(localPoint.x * texture.width);
- int textureY = Mathf.FloorToInt(localPoint.y * texture.height);
- Debug.Log($"Texture coordinates: ({textureX}, {textureY})");
- Color detectedColor = Color.clear;
- // Scansiona per trovare il primo pixel colorato partendo dal punto di contatto verso il centro
- for (int radius = 0; radius <= maxRadius; radius++)
- {
- for (int dx = -radius; dx <= radius; dx++)
- {
- for (int dy = -radius; dy <= radius; dy++)
- {
- int x = textureX + dx;
- int y = textureY + dy;
- if (x >= 0 && x < texture.width && y >= 0 && y < texture.height)
- {
- Color color = texture.GetPixel(x, y);
- if (color.a > 0) // Verifica se il pixel non è trasparente
- {
- Debug.Log($"Detected color: {GetColorName(color)} at ({x}, {y})");
- return color;
- }
- }
- }
- }
- }
- return Color.clear;
- }
- private string GetColorName(Color color)
- {
- if (color == Color.red) return "red";
- if (color == Color.blue) return "blue";
- return "unknown";
- }
- private void AnalyzeCollision(Color thisColor, Color otherColor, GameObject enemy)
- {
- HealthManager thisHealth = GetComponent<HealthManager>();
- HealthManager enemyHealth = enemy.GetComponent<HealthManager>();
- EnemyCollision enemyCollision = enemy.GetComponent<EnemyCollision>();
- if (thisHealth != null && enemyHealth != null && enemyCollision != null)
- {
- int playerDamage = 0;
- int enemyDamage = 0;
- if (thisColor == Color.red && otherColor == Color.blue)
- {
- Debug.Log("Player causes damage, Enemy blocks");
- enemyDamage = Mathf.Max(redDamageAmount - blueBlockAmount, 0);
- enemyCollision.ApplyDamage(enemyHealth, redDamageAmount, blueBlockAmount);
- }
- else if (thisColor == Color.blue && otherColor == Color.red)
- {
- Debug.Log("Enemy causes damage, Player blocks");
- playerDamage = Mathf.Max(enemyCollision.redDamageAmount - blueBlockAmount, 0);
- thisHealth.TakeDamage(enemyCollision.redDamageAmount);
- }
- else if (thisColor == Color.red && otherColor == Color.red)
- {
- Debug.Log("Both cause damage");
- playerDamage = redDamageAmount;
- enemyDamage = redDamageAmount;
- enemyCollision.ApplyDamage(enemyHealth, redDamageAmount, 0);
- thisHealth.TakeDamage(enemyCollision.redDamageAmount);
- }
- else if (thisColor == Color.blue && otherColor == Color.blue)
- {
- Debug.Log("Both block");
- }
- else
- {
- Debug.LogWarning($"Unexpected color combination: thisColor: {thisColor}, otherColor: {otherColor}");
- }
- Debug.Log($"Damage dealt - Player: {playerDamage}, Enemy: {enemyDamage}");
- Debug.Log($"Player health: {thisHealth.GetCurrentHealth()}, Enemy health: {enemyHealth.GetCurrentHealth()}");
- if (enemyHealth.GetCurrentHealth() <= 0)
- {
- GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
- if (enemies.Length == 1)
- {
- Debug.Log("Last enemy defeated. Victory!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment