The correct way to avoid typeof?

Started by
7 comments, last by overeasy 13 years, 11 months ago
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.)
Advertisement
Use virtual methods to get and set those values.
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.
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.
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
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, ...).
Or, you could just check the type.
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; }}
Quote:Original post by Deyja
Or, you could just check the type.


is this acceptable coding?

This topic is closed to new replies.

Advertisement