Jump to content

  • Log In with Google      Sign In   
  • Create Account

fourvector

Member Since 16 May 2012
Offline Last Active Jul 13 2012 02:27 PM

Topics I've Started

Looking for a pattern, objects registering to a manager

15 June 2012 - 05:18 PM

I'm finding myself running into the same kind of problem over and over again, and I'm wondering if there's a pattern I should be using for it.

It started with my rendering system. I'm using a component based architecture, and naturally my GameObjects would possess renderable components, such as a sprite. In order to render these components in an appropriate order, I had made a renderer object which would sort the renderable components and call render() on them in order. So when I construct a given GameObject, and it will have a renderable component, I pass a renderer to the constructor which the renderable is then added to.

The rub is that I'd like to be able to clone my GameObject, so all my components have to be cloneable. I'd like any renderables that I clone to end up registered to the same renderer as the renderable they were cloned from. To do this, my renderables keep track of which renderers they've been added to, and when they clone themselves, they can add the cloned renderable to the appropriate renderers.

It seemed like a good way of doing it at first, but now I'm beginning to wonder. I've now got an Actor component that needs to be registered to a Time object that manages which Actor takes their turn next (it's a turn based game). By the same reasoning as used for the renderable, the Actor will have to keep track of which Time object it's been added to. But now I'm adding more code to the actor, just to allow it to keep track of which Time object it was added to, than I've got describing the functionality of the Actor.

I'm being tempted by singletons for these object managers, but they make me feel dirty. Has anyone encountered similar issues?

Am I over engineering? A generalized Game Action framework

16 May 2012 - 03:03 PM

I'm writing a roguelike in Java, using an entity-component architecture I put together. I want to develop a framework for defining game actions that entities can take, and can be taken on, for instance "pick up", "attack", "eat" etc.

My design followed from the premise that the definition of every action can be thought of as a triplet consisting of a subject, verb, and object. So for instance, Hand - pick up - item. So I defined three interfaces:
Action
,
CanAct<type extends Action>
,
CanBeActedUpon<type extends Action>
.
Then what I would do would be to define a new action class, such as
Class PickUp implements Action{
	 PickUp(CanPickUp, CanBePickedUp){..}
}
, with two interfaces
CanPickUp extends CanAct<PickUp>
and
CanBePickedUp extends CanBeActedUpon<PickUp>

The idea was that with this framework I could assign different components different CanAct interfaces, and then if an entity lost the component, for instance your hands are cut off, they would lose the ability to take actions, unless of course they had other components which also performed those actions. In addition, I could create menus of possible actions on the fly, since all I'd have to do was take the main entity, compile a list of all it's possible actions (for component in entity, if component instanceof CanAct, component.getType()), then look at it's surroundings for other entities with components that have CanBeActedUpon of that type. Meanwhile the action would provide it's own name, so the menu code could be completely oblivious.

The problem is that Java won't allow an object to inherit two interfaces from the same base interface. So for instance, if I add Hands, I want them to be able to PickUp, Equip, Attack, and Open. CanPickUp extends CanAct<PickUp>,CanEquip extends CanAct<Equip>. My Hands object can't implement both.

Additionally, it gets very messy for some reason. Because everything that handles these actions has to be type agnostic, I end up doing a lot of strange typecasting that I don't fully understand. For example:

public static CanAct<?> canActor(GameObject g, Class<? extends Action> actionType){
  for(Part p : g.allParts){
   if(p instanceof CanAct<?>){
	CanAct<?> CA=(CanAct<?>)p;
	if(CA.actionType()==actionType){
	 return CA;
	}
   }
  }
  return null;
}

Where for this code I should point out that both the CanAct and CanBeActedUpon interface have the function actionType() which returns a Class<? extends Action>, and then CanPickUp for instance would return PickUp.class . I haven't been able to figure out how to do this in a more elegant way... Not to mention having to iterate through all the parts in the Entity.


I suppose my question is, does anyone have any suggestions for me? Am I over engineering my problem? Is there an easier approach? Is there a pattern I've not tried here? Or am I on the right track, and if so, what is my work around for implementing two of the same interfaces with different types?

PARTNERS