Author Topic: bitwise operation with 2 buttons and enums  (Read 9754 times)

spil778

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
bitwise operation with 2 buttons and enums
« on: March 07, 2015, 05:41:53 AM »
So I made code that basicly take 2 elements and make it into a single effect.
my NGUI set up looks like this:


The two scrollview is element 1 and 2 how can I get these and put it into my YingYangMixing?
So that the first fire button returns: Element.fire to the first space of the bitwise operation and the second button give that elements into the other part?

So far I set the buttons up like this:


  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class addmagic : MonoBehaviour
  5. {
  6.  
  7.         public Element fire{
  8.                 get{return Element.Fire;}
  9.         }
  10.         public Element water{
  11.                 get{return Element.Fire;}
  12.         }
  13.         public Element earth{
  14.                 get{return Element.Earth;}
  15.         }
  16.         public Element wind{
  17.                 get{return Element.Wind;}
  18.         }
  19.         /// <summary>
  20.         /// Different elements to use
  21.         /// </summary>
  22.         public enum Element
  23.         {
  24.                 None = 0,
  25.                 Fire = 1,
  26.                 Water = 2,
  27.                 Earth = 4,
  28.                 Wind = 8,
  29.                 Scorch = 16,
  30.                 Steam = 32,
  31.                 Lava = 64,
  32.                 Gas = 128,
  33.                 Fluid = 256,
  34.                 Tsunami = 512,
  35.                 Ice = 1024,
  36.                 Magma = 2048,
  37.                 Metal = 4096,
  38.                 Wood = 8192,
  39.                 Sand = 16384,
  40.                 Lightning = 32768,
  41.                 Stream = 65536,
  42.                 Tornado = 131072,
  43.                 Poison = 262144,
  44.         }
  45.        
  46.         /// <summary>
  47.         /// Effects
  48.         /// </summary>
  49.         public enum Effect
  50.         {
  51.                 None,
  52.                 Fire,
  53.                 Water,
  54.                 Earth,
  55.                 Wind,
  56.                 Scorch,
  57.                 Steam,
  58.                 Lava,
  59.                 Gas,
  60.                 Fluid,
  61.                 Tsunami,
  62.                 Ice,
  63.                 Magma,
  64.                 Metal,
  65.                 Wood,
  66.                 Sand,
  67.                 Lightning,
  68.                 Tornado,
  69.                 Stream,
  70.                 Poison,
  71.         }
  72.        
  73.        
  74.         /// <summary>
  75.         /// Elemet to Effect lookup table
  76.         /// </summary>
  77.         private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()
  78.         {
  79.                 { (int)( Element.Fire | Element.Fire), Effect.Magma },
  80.                 { (int)(Element.Fire | Element.Water), Effect.Steam },
  81.                 { (int)(Element.Fire | Element.Earth), Effect.Lava },
  82.                 { (int)(Element.Fire | Element.Wind), Effect.Gas },
  83.                 { (int)(Element.Water | Element.Water), Effect.Fluid},
  84.                 { (int)(Element.Water | Element.Earth), Effect.Tsunami },
  85.                 { (int)(Element.Water | Element.Wind), Effect.Ice },
  86.                 { (int)(Element.Wind | Element.Earth), Effect.Sand },
  87.                 { (int)(Element.Wind | Element.Wind), Effect.Tornado },
  88.                 { (int)(Element.Earth | Element.Earth), Effect.Wood },
  89.  
  90.         };
  91.        
  92.         public void YingYangMixing( Element element )
  93.         {
  94.                 Effect ret = Effect.None;
  95.                 if (!_lookUpTable.TryGetValue ((int)element, out ret)) {Debug.LogError( "Element does not exist in Lookup table!" );}
  96.                 else{ Debug.Log(ret);}
  97.  
  98.         }
  99.  
  100.  
  101.        
  102. }

spil778

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: bitwise operation with 2 buttons and enums
« Reply #1 on: March 07, 2015, 06:16:26 AM »
My friend randomly came over (thought he was on vaction) Well he helped me. For people in the futhere here is the solution we came up with:
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class addmagic : MonoBehaviour
  5. {
  6.         public GameObject frstLabel;
  7.         public GameObject scdLabel;
  8.         public string ele1;
  9.         public string ele2;
  10.  
  11.         public UILabel label1;
  12.         public UILabel label2;
  13.  
  14.         public Element fire{
  15.                 get{return Element.Fire;}
  16.         }
  17.         public Element water{
  18.                 get{return Element.Fire;}
  19.         }
  20.         public Element earth{
  21.                 get{return Element.Earth;}
  22.         }
  23.         public Element wind{
  24.                 get{return Element.Wind;}
  25.         }
  26.         /// <summary>
  27.         /// Different elements to use
  28.         /// </summary>
  29.         public enum Element
  30.         {
  31.                 None = 1<<0,
  32.                 Fire = 1<<1,
  33.                 Water = 1<<2,
  34.                 Earth = 1<<3,
  35.                 Wind = 1<<4,
  36.                 Scorch = 1<<5,
  37.                 Steam = 1<<6,
  38.                 Lava = 1<<7,
  39.                 Gas = 1<<8,
  40.                 Fluid = 1<<9,
  41.                 Tsunami = 1<<10,
  42.                 Ice = 1<<11,
  43.                 Magma = 1<<12,
  44.                 Metal = 1<<13,
  45.                 Wood = 1<<14,
  46.                 Sand = 1<<15,
  47.                 Lightning = 1<<16,
  48.                 Stream = 1<<17,
  49.                 Tornado = 1<<18,
  50.                 Poison = 1<<19,
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Effects
  55.         /// </summary>
  56.         public enum Effect
  57.         {
  58.                 None,
  59.                 Fire,
  60.                 Water,
  61.                 Earth,
  62.                 Wind,
  63.                 Scorch,
  64.                 Steam,
  65.                 Lava,
  66.                 Gas,
  67.                 Fluid,
  68.                 Tsunami,
  69.                 Ice,
  70.                 Magma,
  71.                 Metal,
  72.                 Wood,
  73.                 Sand,
  74.                 Lightning,
  75.                 Tornado,
  76.                 Stream,
  77.                 Poison,
  78.         }
  79.        
  80.        
  81.         /// <summary>
  82.         /// Elemet to Effect lookup table
  83.         /// </summary>
  84.         private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()
  85.         {
  86.                 { (int)( Element.Fire | Element.Fire), Effect.Magma },
  87.                 { (int)(Element.Fire | Element.Water), Effect.Steam },
  88.                 { (int)(Element.Fire | Element.Earth), Effect.Lava },
  89.                 { (int)(Element.Fire | Element.Wind), Effect.Gas },
  90.                 { (int)(Element.Water | Element.Water), Effect.Fluid},
  91.                 { (int)(Element.Water | Element.Earth), Effect.Tsunami },
  92.                 { (int)(Element.Water | Element.Wind), Effect.Ice },
  93.                 { (int)(Element.Wind | Element.Earth), Effect.Sand },
  94.                 { (int)(Element.Wind | Element.Wind), Effect.Tornado },
  95.                 { (int)(Element.Earth | Element.Earth), Effect.Wood },
  96.  
  97.         };
  98.        
  99.         public void YingYangMixing()
  100.         {
  101.                 frstLabel = GameObject.FindWithTag ("FrstEleLabel");
  102.                 scdLabel = GameObject.FindWithTag ("ScdEleLabel");
  103.  
  104.                 label1 = frstLabel.GetComponent<UILabel> ();
  105.                 label2 = scdLabel.GetComponent<UILabel> ();
  106.  
  107.                
  108.                 Element element = (Element)System.Enum.Parse( typeof( Element ), label1.text);
  109.                 Element element2 = (Element)System.Enum.Parse( typeof( Element ), label2.text);
  110.  
  111.                 Effect ret = Effect.None;
  112.                 if (!_lookUpTable.TryGetValue ((int)element | (int)element2, out ret)) {Debug.LogError( "Element does not exist in Lookup table!" );}
  113.                 else{ Debug.Log(ret);}
  114.  
  115.         }
  116.  
  117.  
  118.        
  119. }
  120.  

What we basicly did is we took the label and searched for them in the enum and then just did the bitwise directly.