Closed connerdouglass closed 2 years ago
You could just make the EntityType enum have the constructor for the entity as its value?
@AL1L That would make this situation easier, but would remove the ability to console.log
an entity's type. In my example above ENTITY_TYPE_TO_CLASS
does what you're suggesting, just separate from the enum itself.
I have an idea for how to get the best of both worlds. I'll give it a shot and have you review the PR
If you get an
Entity
from some API call (World.getEntities()
for instance) we are able to check the type withEntity.getType()
but we can't actually cast the entity to the correct instance type. Everything is stuck as the base class.I think we need to update
Entity.fromJava(...)
to construct an instance of the proper entity subclass, based on the entity's specific type. This would enable us to do convenientif (entity instanceof Player)
checks.Here's the
Entity.fromJava
function today:Here's roughly what I'm thinking it should become:
Want to discuss design considerations before implementing this.
I don't like the idea of
Entity
needing to import all of its subclasses, so maybe this should be split off into a utility function elsewhere.