Page 1 of 1

Don't destroy certain sprites connected to MMOItems

Posted: 11 Mar 2014, 19:38
by SizzleWon
Each player has the ability to shoot/ create MMOitems. The MMOitems they create will have a sprite attached that is responsible for moving the MMOItem. Other players track that movement and their corresponding sprites are moved accordingly.

I created a 2nd list of MMOItems to handle the spells which the player created. The thought being I can add extra conditionals to them to avoid them being removed when they leave that players AOI. That works: the sprite continues past the AOI. My problem is removing those sprites when the MMOItem is destroyed.

Code: Select all

		//Handle all spells
		foreach (IMMOItem item in addedItems){

			double pw = (double)item.GetVariable("power").GetDoubleValue();
			int owner = (int)item.GetVariable("owner").GetIntValue();
			int spellID = item.Id;
		
			GameObject spl = Instantiate(
				spellPrefab, new Vector3(item.AOIEntryPoint.FloatX, item.AOIEntryPoint.FloatY, item.AOIEntryPoint.FloatZ), 
				new Quaternion(0, (float) item.GetVariable("rot").GetDoubleValue() , 0, 0)) as GameObject;
			
			spl.GetComponent<spellData>().spellPower = pw;
			spl.GetComponent<spellData>().spellOwner = owner;
			spl.GetComponent<spellData>().iD = spellID;
			
			//if this is a spell from anohter player, SimpleRemoteInterpolation is added to move the sprite
			//based on where the spells owner moves it
			if (spl.GetComponent<spellData>().spellOwner != smartFox.MySelf.Id){
			
			spl.AddComponent<SimpleRemoteInterpolation>();
			remoteSpells.Add( item, spl);
				
			}
			
			if (spl.GetComponent<spellData>().spellOwner == smartFox.MySelf.Id){
				
				spl.AddComponent<SpellController>();
				spl.GetComponent<SpellController>().iD = spellID;
				spl.GetComponent<SpellController>().localPlayer = localPlayer;
				spl.AddComponent<SpellRange>();
				mySpells.Add( item, spl);
					
			}
			
			
		}
			
			
		foreach (IMMOItem item in removedItems){
			
			RemoveItem ((IMMOItem) item);
			
		}
		
	}
I've edited the RemoveItem function to include the 2nd list.

Code: Select all

	private void RemoveItem (IMMOItem item){
		
		if (remoteSpells.ContainsKey(item)){
			
			Destroy(remoteSpells[item]);
			remoteSpells.Remove(item);
			
		}
		
		if (mySpells.ContainsKey(item)){
			
			if (item.GetVariable("power").GetDoubleValue() <= 0 ){
		
			Destroy(mySpells[item]);
			mySpells.Remove(item);
				
			}
			else return;
		}
	}
This doesn't seem to work even though prior to removing the item on the server side I set it's variable "power" to 0;

Server Side

Code: Select all

public class killSpell extends BaseClientRequestHandler{
    
        @Override
        public void handleClientRequest(User user, ISFSObject objIn){
            
            int ID = objIn.getInt("ID");
            
            ISFSMMOApi mmoApi = SmartFoxServer.getInstance().getAPIManager().getMMOApi();

            MMORoom targetRoom = (MMORoom)WarMageExtension.zone.getRoomByName("WMage");
            
            BaseMMOItem spell = (MMOItem)targetRoom.getMMOItemById(ID);
            
            List<IMMOItemVariable> vars = new LinkedList<IMMOItemVariable>();
            
            vars.add(new MMOItemVariable("power",(double) 0));
            
            mmoApi.setMMOItemVariables(spell, vars);
            
            mmoApi.removeMMOItem(spell);
            
        }
    
}