Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- using Sirenix.Utilities;
- using UnityEditor;
- using UnityEngine;
- public class PokemonTable : SerializedMonoBehaviour
- {
- public enum TypeMatchUp
- {
- NORMAL = 1,
- STRONG = 2,
- WEAK = 3,
- IMMUNE= 4,
- }
- [OnValueChanged(nameof(BuildWithTypes))]
- public List<string> types = new List<string>();
- [TableMatrix(DrawElementMethod = nameof(DrawMatchUp), SquareCells = true)]
- public MatchUp[,] matchUps = new MatchUp[18,18];
- [Serializable]
- public class MatchUp
- {
- public TypeMatchUp matchUp;
- [ReadOnly] public string type1;
- [ReadOnly] public string type2;
- }
- public void BuildWithTypes(List<string> types)
- {
- this.types = new List<string>(types);
- matchUps = new MatchUp[this.types.Count,this.types.Count];
- for (int x = 0; x < types.Count; x++)
- {
- for (int y = 0; y < types.Count; y++)
- {
- var t0 = types[x];
- var t1 = types[y];
- var matchUpType = TypeMatchUp.NORMAL;
- var matchUp = new MatchUp()
- {
- type1 = t0,
- type2 = t1,
- matchUp = matchUpType
- };
- matchUps[x, y] = matchUp;
- }
- }
- }
- TypeMatchUp GetPrev(TypeMatchUp typeMatchUp)
- {
- int i = (int)typeMatchUp;
- i--;
- if (i < 1)
- i = 4;
- return (TypeMatchUp)i;
- }
- TypeMatchUp GetNext(TypeMatchUp typeMatchUp)
- {
- int i = (int)typeMatchUp;
- i++;
- if (i > 4)
- i = 1;
- return (TypeMatchUp)i;
- }
- private MatchUp DrawMatchUp(Rect rect, MatchUp value)
- {
- if (this.types == null || types.Count == 0) return new MatchUp();
- if (value == null)
- {
- BuildWithTypes(this.types);
- return null;
- }
- if (Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
- {
- if (Event.current.button == 0)
- {
- value.matchUp = GetNext(value.matchUp);
- }
- else if (Event.current.button == 1)
- {
- value.matchUp = GetPrev(value.matchUp);
- }
- GUI.changed = true;
- Event.current.Use();
- }
- var color = ForType(value.matchUp);
- var topLeft = rect.AlignTop(16).AlignLeft(40);
- var topMiddle = rect.AlignTop(16).AlignCenter(20);
- var topRight = rect.AlignTop(16).AlignRight(40);
- UnityEditor.EditorGUI.DrawRect(rect.Padding(1), color);
- GUI.contentColor= new Color(0.1f, 0.1f, 0.1f);
- GUI.Label(topLeft, value.type1);
- GUI.contentColor = new Color(0.3f, 0.3f, 0.3f);
- GUI.Label(topMiddle, "vs");
- GUI.contentColor= new Color(0.1f, 0.1f, 0.1f);
- GUI.Label(topRight, value.type2);
- var center = rect.AlignCenter(45);
- GUI.Label(center, value.matchUp.ToString());
- return value;
- }
- Color ForType(TypeMatchUp matchUp)
- {
- switch (matchUp)
- {
- case TypeMatchUp.NORMAL:
- return Color.gray;
- break;
- case TypeMatchUp.STRONG:
- return Color.green;
- break;
- case TypeMatchUp.WEAK:
- return Color.red;
- break;
- case TypeMatchUp.IMMUNE:
- return Color.black;
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(matchUp), matchUp, null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment