TyrannicGoat

Pokemon Table using Table Matrix

Jul 10th, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Sirenix.OdinInspector;
  4. using Sirenix.Utilities;
  5. using UnityEditor;
  6. using UnityEngine;
  7.  
  8.     public class PokemonTable : SerializedMonoBehaviour
  9.     {
  10.        
  11.         public enum TypeMatchUp
  12.         {
  13.             NORMAL = 1,
  14.             STRONG = 2,
  15.             WEAK = 3,
  16.             IMMUNE= 4,
  17.         }
  18.  
  19.  
  20.         [OnValueChanged(nameof(BuildWithTypes))]
  21.         public List<string> types = new List<string>();
  22.        
  23.         [TableMatrix(DrawElementMethod = nameof(DrawMatchUp), SquareCells = true)]
  24.         public MatchUp[,] matchUps = new MatchUp[18,18];
  25.  
  26.         [Serializable]
  27.         public class MatchUp
  28.         {
  29.             public TypeMatchUp matchUp;
  30.             [ReadOnly] public string type1;
  31.             [ReadOnly] public string type2;
  32.         }
  33.         public void BuildWithTypes(List<string>  types)
  34.         {
  35.             this.types = new List<string>(types);
  36.             matchUps = new MatchUp[this.types.Count,this.types.Count];
  37.             for (int x = 0; x < types.Count; x++)
  38.             {
  39.                 for (int y = 0; y < types.Count; y++)
  40.                 {
  41.                     var t0 = types[x];
  42.                     var t1 = types[y];
  43.                     var matchUpType = TypeMatchUp.NORMAL;
  44.                     var matchUp = new MatchUp()
  45.                     {
  46.                         type1 = t0,
  47.                         type2 = t1,
  48.                         matchUp = matchUpType
  49.                     };
  50.                     matchUps[x, y] = matchUp;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         TypeMatchUp GetPrev(TypeMatchUp typeMatchUp)
  56.         {
  57.             int i = (int)typeMatchUp;
  58.             i--;
  59.             if (i < 1)
  60.                 i = 4;
  61.             return (TypeMatchUp)i;
  62.         }
  63.         TypeMatchUp GetNext(TypeMatchUp typeMatchUp)
  64.         {
  65.             int i = (int)typeMatchUp;
  66.             i++;
  67.             if (i > 4)
  68.                 i = 1;
  69.             return (TypeMatchUp)i;
  70.         }
  71.         private MatchUp DrawMatchUp(Rect rect, MatchUp value)
  72.         {
  73.             if (this.types == null || types.Count == 0) return new MatchUp();
  74.             if (value == null)
  75.             {
  76.                 BuildWithTypes(this.types);
  77.                 return null;
  78.             }
  79.             if (Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
  80.             {
  81.                 if (Event.current.button == 0)
  82.                 {
  83.                     value.matchUp = GetNext(value.matchUp);
  84.                 }
  85.                 else if (Event.current.button == 1)
  86.                 {
  87.                     value.matchUp = GetPrev(value.matchUp);
  88.                 }
  89.                 GUI.changed = true;
  90.                 Event.current.Use();
  91.             }
  92.  
  93.             var color = ForType(value.matchUp);
  94.             var topLeft = rect.AlignTop(16).AlignLeft(40);
  95.             var topMiddle = rect.AlignTop(16).AlignCenter(20);
  96.             var topRight = rect.AlignTop(16).AlignRight(40);
  97.            
  98.            
  99.             UnityEditor.EditorGUI.DrawRect(rect.Padding(1), color);
  100.             GUI.contentColor= new Color(0.1f, 0.1f, 0.1f);
  101.             GUI.Label(topLeft, value.type1);
  102.             GUI.contentColor = new Color(0.3f, 0.3f, 0.3f);
  103.             GUI.Label(topMiddle, "vs");
  104.             GUI.contentColor= new Color(0.1f, 0.1f, 0.1f);
  105.             GUI.Label(topRight, value.type2);
  106.             var center = rect.AlignCenter(45);
  107.             GUI.Label(center, value.matchUp.ToString());
  108.  
  109.             return value;
  110.         }
  111.  
  112.         Color ForType(TypeMatchUp matchUp)
  113.         {
  114.             switch (matchUp)
  115.             {
  116.                 case TypeMatchUp.NORMAL:
  117.                     return Color.gray;
  118.                     break;
  119.                 case TypeMatchUp.STRONG:
  120.                     return Color.green;
  121.                     break;
  122.                 case TypeMatchUp.WEAK:
  123.                     return Color.red;
  124.                     break;
  125.                 case TypeMatchUp.IMMUNE:
  126.                     return Color.black;
  127.                     break;
  128.                 default:
  129.                     throw new ArgumentOutOfRangeException(nameof(matchUp), matchUp, null);
  130.             }
  131.         }
  132.     }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment