/*******************************************************************************
 * Copyright 2014 Rafael Garcia Moreno.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.bladecoder.engine.actions;

import com.bladecoder.engine.actions.Param.Type;
import com.bladecoder.engine.assets.EngineAssetManager;
import com.bladecoder.engine.model.BaseActor;
import com.bladecoder.engine.model.CharacterActor;
import com.bladecoder.engine.model.Scene;
import com.bladecoder.engine.model.VerbRunner;
import com.bladecoder.engine.model.World;

@ActionDescription("Sets the scene player")
public class SetPlayerAction implements Action {

	@ActionProperty(type = Type.SCENE_CHARACTER_ACTOR, required = true)
	@ActionPropertyDescription("The scene player")	
	private SceneActorRef actor;
	
	@ActionProperty
	@ActionPropertyDescription("The inventory 'id' for the player. If empty, the inventory will not change.")	
	private String inventory;
	
	private World w;
	
	@Override
	public void init(World w) {
		this.w = w;
	}

	@Override
	public boolean run(VerbRunner cb) {		
		Scene s = actor.getScene(w);

		BaseActor a = s.getActor(actor.getActorId(), true);
		
		s.setPlayer((CharacterActor)a);
		
		if(inventory != null) {
			w.getInventory().dispose();
			w.setInventory(inventory);
			w.getInventory().loadAssets();
			EngineAssetManager.getInstance().finishLoading();
			w.getInventory().retrieveAssets();
		}

		return false;
	}

}

@@ -992,24 +992,70 @@
 acterActor;%0A
+import com.bladecoder.engine.model.Inventory;%0A
 import com.b
@@ -1084,16 +1084,16 @@
 .Scene;%0A
-
 import c
@@ -1384,17 +1384,16 @@
 player%22)
-%09
 %0A%09privat
@@ -1415,18 +1415,17 @@
  actor;%0A
-%09
 %0A
+
 %09@Action
@@ -1539,17 +1539,16 @@
 hange.%22)
-%09
 %0A%09privat
@@ -1567,18 +1567,17 @@
 entory;%0A
-%09
 %0A
+
 %09private
@@ -1586,18 +1586,17 @@
 orld w;%0A
-%09
 %0A
+
 %09@Overri
@@ -1692,18 +1692,16 @@
 er cb) %7B
-%09%09
 %0A%09%09Scene
@@ -1779,18 +1779,16 @@
  true);%0A
-%09%09
 %0A%09%09s.set
@@ -1814,19 +1814,19 @@
 tor)
+ 
 a);%0A
-%09%09
 %0A%09%09if
+ 
 (inv
@@ -1843,15 +1843,77 @@
 null
-) %7B%0A%09%09%09
+ && !inventory.equals(w.getCurrentInventory())) %7B%0A%09%09%09Inventory old = 
 w.ge
@@ -1928,20 +1928,11 @@
 ry()
-.dispose()
 ;%0A
+%0A
 %09%09%09w
@@ -2044,16 +2044,16 @@
 ding();%0A
-
 %09%09%09w.get
@@ -2082,16 +2082,36 @@
 sets();%0A
+%0A%09%09%09old.dispose();%0A%0A
 %09%09%7D%0A%0A%09%09r

Improve inventory loading when changing players.