package gameEngine.factory.magicFactory;

import gameEngine.constant.GameEngineConstant;
import gameEngine.model.magic.IMagicable;
import java.util.HashMap;


/*
 * wenxin shi
 */
public class MagicsFactory {

    private static MagicsFactory myMagicFactory;
    private HashMap<Integer, IMagicFactory> myFactoryMap = new HashMap<Integer, IMagicFactory>();
    private HashMap<String, Integer> myTranlateMap=new HashMap<String,Integer>();
    
    private MagicsFactory () {
        myFactoryMap.put(GameEngineConstant.FROZEMAGIC_ID, new EFrozeFactory());
        myFactoryMap.put(GameEngineConstant.BOOSTMAGIC_ID, new TBoostFactory());
        myFactoryMap.put(GameEngineConstant.SPEEDUPMAGIC_ID, new ESpeedUpFactory());
        
        
        
        myTranlateMap.put("FrozeMagic", GameEngineConstant.FROZEMAGIC_ID);
        myTranlateMap.put("BoostMagic", GameEngineConstant.FROZEMAGIC_ID);
        myTranlateMap.put("FrozeMagic", GameEngineConstant.FROZEMAGIC_ID);
    }

    public static MagicsFactory getInstance () {
        if (myMagicFactory == null)
            myMagicFactory = new MagicsFactory();
        return myMagicFactory;
    }

    /**
     * This method is for the same magic can't overlap
     * 
     * @param target
     * @param magicIdToCreate
     * @param currentMagicIds,if the magic can overlap then make the currentMagicIds be Constant.OVERLAPMAGIC_ID
     */
    public void createMagics (IMagicable target, IMagicable sender,int magicIdToCreate, int currMagicIds) {
        if(currMagicIds!=GameEngineConstant.OVERLAPMAGIC_ID){
            magicIdToCreate = (~currMagicIds) & magicIdToCreate;
        }
        
        int temp=magicIdToCreate;
        int mask=1;
        while(magicIdToCreate > 0) {
            IMagicFactory factory = myFactoryMap.get(temp&mask);
            if (factory != null) {
                factory.createMagicInstance(target,sender);
            }
            magicIdToCreate = magicIdToCreate >> 1;
            mask=mask<<1;
        }
    }
}

@@ -150,16 +150,39 @@
 ashMap;%0A
+import java.util.List;%0A
 %0A%0A/*%0A * 
@@ -934,37 +934,37 @@
 ranlateMap.put(%22
-Froze
+Speed
 Magic%22, GameEngi
@@ -966,37 +966,39 @@
 eEngineConstant.
-FROZE
+SPEEDUP
 MAGIC_ID);%0A    %7D
@@ -2012,16 +2012,16 @@
 ask%3C%3C1;%0A
-
         
@@ -2028,10 +2028,517 @@
 %7D%0A    %7D%0A
+    %0A    public void creatMagics(IMagicable target, IMagicable sender,List%3CString%3E magicIdToCreate,List%3CString%3E currMagicIds)%7B%0A        int newMagicIds=0;%0A        int currentMagicIds=0;%0A        %0A        for(String str:magicIdToCreate)%7B%0A            newMagicIds+= myTranlateMap.get(str);%0A        %7D%0A        %0A        for(String str:currMagicIds)%7B%0A            currentMagicIds+= myTranlateMap.get(str);%0A        %7D%0A        %0A        createMagics (target, sender,newMagicIds, currentMagicIds);%0A        %0A        %0A    %7D%0A
 %7D%0A

add translate map for magic