Sign in to follow this  
overeasy

The correct way to avoid typeof?

Recommended Posts

So I have a class called camera which can take a type of "Entity". Entity has the fields "row" and "col". Actor is a descendant of type entity, and has the additional fields "row_offset" and "col_offset". Entity (no offset data) |---> Actor (offset data) |------> NPC (etc...) When I update the camera object, I want to use the row_offset and col_offset data if it is present. This implies that I need to check the type of the Entity to see if it is actually an "Actor". How do I avoid this? I don't want to have to include the additional row and col offset data in the entity type, and I also know that in java at least type casting is incredibly slow. Is there an easy way to do this? I know I could make the integer fields in entity but eventually it will get cluttered with all the additional checks that only apply to actors (drawing health bars, that sort of thing). (I know that this case is trivial, but the method will apply to many other parts of my game's code.)

Share this post


Link to post
Share on other sites
It sounds like updating the camera object should be a function in the Entity class, and in the Actor class you want to override that function so that updating the camera object takes into account the offset data.

Share this post


Link to post
Share on other sites
Quote:
Original post by overeasy
So I have a class called camera which can take a type of "Entity".

Option 1 - Make it take type of Actor.
Option 2 - Move row and col to entity.


Quote:
and I also know that in java at least type casting is incredibly slow.

It should be incredibly fast in Java.

Quote:
Is there an easy way to do this? I know I could make the integer fields in entity but eventually it will get cluttered with all the additional checks that only apply to actors (drawing health bars, that sort of thing).

Can camera manipulate an entity? If so, then it needs all the information it needs from entity.

If it can only manipulate Actors, then it cannot take entity, since entity simply doesn't provide enough information.

Share this post


Link to post
Share on other sites
Quote:
Original post by no such user
It sounds like updating the camera object should be a function in the Entity class, and in the Actor class you want to override that function so that updating the camera object takes into account the offset data.


Cameras can switch target entities. They can target entities AND actors because actor is a descendant of entity. I want it to perform specific additional functions when targeting an actor and not an entity

Share this post


Link to post
Share on other sites
Quote:
Original post by overeasy

Cameras can switch target entities. They can target entities AND actors because actor is a descendant of entity.


In that case all the functionality that Camera needs must be moved into entity.

Quote:
I want it to perform specific additional functions when targeting an actor and not an entity

Then there will not be one camera, but several different ones. It's like having a long-lens camera for far away objects, a macro camera for close-ups and high speed camera for slow motion shots. They all record a movie, but they are three different cameras.

One way to solve it is by letting entities create cameras (virtual Camera * createCamera()), so each can provide a different subclass. The problem then remains how to transform one camera to another. If there is a third-party common element (renderer, screen, ...) then that can be migrated to a newly created instance - in above example, the photographer is same between all cameras, and each camera has same controls (focus, trigger, tripod mount, ...).

Share this post


Link to post
Share on other sites
Would this work for you?

public interface CameraTarget {
int getTargetRow();
}

public class Camera {
public void function(CameraTarget target) {
...do things using getTargetRow()...
}
}

public class Entity implements CamerTarget{
private int row;
public int getRow() { return row; }

public int getTargetRow() { return row; }
}

public class Actor extends Entity {
private int rowOffset;
public int getRowOffset() { return rowOffset; }

public int getTargetRow() { return rowOffset; }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this