Advertisement
powerofsoul

Untitled

Feb 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. namespace Game {
  8.     public class SpriteCollider {
  9.         public List<Vector2> LeftColliderPoints { get; set; }
  10.         public List<Vector2> RightColliderPoints { get; set; }
  11.  
  12.         private Texture2D texture2D;
  13.  
  14.         private float x;
  15.         private float y;
  16.  
  17.         public SpriteCollider(Sprite s, float x, float y) {
  18.             texture2D = RotateImage(TextureFromSprite(s), 90);
  19.             LeftColliderPoints = new List<Vector2>();
  20.             RightColliderPoints = new List<Vector2>();
  21.             this.x = x;
  22.             this.y = y;
  23.             GenerateCollider();
  24.         }
  25.  
  26.  
  27.         public static Texture2D TextureFromSprite(Sprite sprite) {
  28.             if (sprite.rect.width != sprite.texture.width) {
  29.                 Texture2D newText = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
  30.                 Color[] newColors = sprite.texture.GetPixels((int)sprite.textureRect.x,
  31.                                                              (int)sprite.textureRect.y,
  32.                                                              (int)sprite.textureRect.width,
  33.                                                              (int)sprite.textureRect.height);
  34.                 newText.SetPixels(newColors);
  35.                 newText.Apply();
  36.                 return newText;
  37.             } else return sprite.texture;
  38.         }
  39.  
  40.         private void GenerateCollider() {
  41.             for (float i = 0; i <= texture2D.height; i++) {
  42.                 for (float j = 0; j <= texture2D.width; j++)
  43.                     if (texture2D.GetPixel((int)i, (int)j).a != 0) {
  44.                         float x = j - texture2D.width / 2f;
  45.                         float y = -i + texture2D.width / 2f;
  46.  
  47.                         x = x / 300f;
  48.                         y = y / 300f;
  49.                         LeftColliderPoints.Add(new Vector2(this.x + x, this.y + y));
  50.                         break;
  51.                     }
  52.             }
  53.  
  54.             for (float i = 0; i <= texture2D.height; i++) {
  55.                 for (float j = texture2D.width; j >=0; j--)
  56.                     if (texture2D.GetPixel((int)i, (int)j).a != 0) {
  57.                         float x = j - texture2D.width / 2f;
  58.                         float y = -i + texture2D.width / 2f;
  59.                         x = x / 300f;
  60.                         y = y / 300f;
  61.                        
  62.                         RightColliderPoints.Add(new Vector2(this.x + x, this.y + y));
  63.                         break;
  64.                     }
  65.             }
  66.         }
  67.  
  68.         public void DrawCollider() {
  69.             for (int i = 0; i < LeftColliderPoints.Count - 1; i++) {
  70.                 Debug.DrawLine(LeftColliderPoints[i], LeftColliderPoints[i + 1], Color.blue);
  71.             }
  72.  
  73.             for (int i = 0; i < RightColliderPoints.Count - 1; i++) {
  74.                 Debug.DrawLine(RightColliderPoints[i], RightColliderPoints[i + 1], Color.red);
  75.             }
  76.         }
  77.  
  78.         private static Texture2D RotateImage(Texture2D originTexture, int angle) {
  79.             Texture2D result;
  80.             result = new Texture2D(originTexture.width, originTexture.height);
  81.             Color32[] pix1 = result.GetPixels32();
  82.             Color32[] pix2 = originTexture.GetPixels32();
  83.             int W = originTexture.width;
  84.             int H = originTexture.height;
  85.             int x = 0;
  86.             int y = 0;
  87.             Color32[] pix3 = rotateSquare(pix2, (Math.PI / 180 * (double)angle), originTexture);
  88.             for (int j = 0; j < H; j++) {
  89.                 for (var i = 0; i < W; i++) {
  90.                     //pix1[result.width/2 - originTexture.width/2 + x + i + result.width*(result.height/2-originTexture.height/2+j+y)] = pix2[i + j*originTexture.width];
  91.                     pix1[result.width / 2 - W / 2 + x + i + result.width * (result.height / 2 - H / 2 + j + y)] = pix3[i + j * W];
  92.                 }
  93.             }
  94.             result.SetPixels32(pix1);
  95.             result.Apply();
  96.             return result;
  97.         }
  98.  
  99.         private static Color32[] rotateSquare(Color32[] arr, double phi, Texture2D originTexture) {
  100.             int x;
  101.             int y;
  102.             int i;
  103.             int j;
  104.             double sn = Math.Sin(phi);
  105.             double cs = Math.Cos(phi);
  106.             Color32[] arr2 = originTexture.GetPixels32();
  107.             int W = originTexture.width;
  108.             int H = originTexture.height;
  109.             int xc = W / 2;
  110.             int yc = H / 2;
  111.             for (j = 0; j < H; j++) {
  112.                 for (i = 0; i < W; i++) {
  113.                     arr2[j * W + i] = new Color32(0, 0, 0, 0);
  114.                     x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
  115.                     y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
  116.                     if ((x > -1) && (x < W) && (y > -1) && (y < H)) {
  117.                         arr2[j * W + i] = arr[y * W + x];
  118.                     }
  119.                 }
  120.             }
  121.             return arr2;
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement