parent
							
								
									8fa0dd927f
								
							
						
					
					
						commit
						e4e908ad75
					
				@ -1,32 +1,57 @@ | 
				
			||||
package mightypork.rogue.world.item; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import mightypork.gamecore.util.annot.DefaultImpl; | 
				
			||||
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound; | 
				
			||||
import java.io.IOException; | 
				
			||||
 | 
				
			||||
import mightypork.gamecore.util.ion.IonInput; | 
				
			||||
import mightypork.gamecore.util.ion.IonOutput; | 
				
			||||
 | 
				
			||||
public abstract class ItemModel { | 
				
			||||
	 | 
				
			||||
/** | 
				
			||||
 * Item model (builder) | 
				
			||||
 *  | 
				
			||||
 * @author MightyPork | 
				
			||||
 */ | 
				
			||||
public final class ItemModel { | 
				
			||||
	 | 
				
			||||
	/** Model ID */ | 
				
			||||
	public final int id; | 
				
			||||
	public final Class<? extends Item> itemClass; | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public ItemModel(int id) | 
				
			||||
	public ItemModel(int id, Class<? extends Item> item) | 
				
			||||
	{ | 
				
			||||
		Items.register(id, this); | 
				
			||||
		this.id = id; | 
				
			||||
		this.itemClass = item; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	/** | 
				
			||||
	 * @return new tile with this model | 
				
			||||
	 * @return new item instance of this type | 
				
			||||
	 */ | 
				
			||||
	@DefaultImpl | 
				
			||||
	public Item create() | 
				
			||||
	public <T extends Item> T createItem() | 
				
			||||
	{ | 
				
			||||
		return new Item(this); | 
				
			||||
		try { | 
				
			||||
			return (T) itemClass.getConstructor(ItemModel.class).newInstance(this); | 
				
			||||
		} catch (final Exception e) { | 
				
			||||
			throw new RuntimeException("Could not instantiate an item.", e); | 
				
			||||
		} | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public abstract void render(Item item, RectBound context); | 
				
			||||
	public Item loadItem(IonInput in) throws IOException | 
				
			||||
	{ | 
				
			||||
		final Item t = createItem(); | 
				
			||||
		t.load(in); | 
				
			||||
		return t; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void saveItem(IonOutput out, Item tile) throws IOException | 
				
			||||
	{ | 
				
			||||
		if (itemClass != tile.getClass()) throw new RuntimeException("Item class mismatch."); | 
				
			||||
		 | 
				
			||||
		tile.save(out); | 
				
			||||
	} | 
				
			||||
} | 
				
			||||
 | 
				
			||||
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue