Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
rogue-savage-rats/src/mightypork/rogue/world/entity/EntityModel.java

75 lines
1.4 KiB

package mightypork.rogue.world.entity;
import java.io.IOException;
import mightypork.rogue.world.World;
import mightypork.utils.ion.IonDataBundle;
import mightypork.utils.ion.IonInput;
import mightypork.utils.ion.IonOutput;
/**
* Entity model - builder
*
* @author Ondřej Hruška (MightyPork)
*/
public final class EntityModel {
/** Model ID */
public final int id;
public final Class<? extends Entity> tileClass;
public EntityModel(int id, Class<? extends Entity> entity)
{
Entities.register(id, this);
this.id = id;
this.tileClass = entity;
}
public Entity createEntity(World world)
{
return createEntity(world.getNewEID());
}
public Entity createEntity(int eid)
{
try {
return tileClass.getConstructor(EntityModel.class, int.class).newInstance(this, eid);
} catch (final Exception e) {
throw new RuntimeException("Could not instantiate a tile.", e);
}
}
/**
* Create entitiy without EID. EID will be assigned when the entity is added
* to a level.
*
* @return entity.
*/
public Entity createEntity()
{
return createEntity(-1);
}
public Entity loadEntity(IonInput in) throws IOException
{
final IonDataBundle bundle = in.readBundle();
final Entity ent = createEntity(-1);
ent.load(bundle);
return ent;
}
public void saveEntity(IonOutput out, Entity entity) throws IOException
{
final IonDataBundle bundle = new IonDataBundle();
entity.save(bundle);
out.writeBundle(bundle);
}
}