• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Component-Based Entities in Games  RSS feed

 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, I new to the forum, but not Java.

If this topic is in the wrong place, please move it.

I was doing some reading about Component-Based Entities http://gamedevelopment.tutsplus.com/tutorials/create-a-simple-asteroids-game-using-component-based-entities--gamedev-1324 and it suggested that I implement it in my game.

Now, my game is a text adventure game, so methods like draw, render...etc are not there.

Given my code:




and another interface that extends IGameObject :




The main problem might be if I'm extending too many interfaces, now I have the following class called SleepWeapon. How would I redo my code and properly implement and use Component-Based Entities?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Svetlana. Sorry no one has been able to offer you any help here, yet. What little I know about CBEs suggests that extending an interface may not be your best option. However, it's a complex subject. Also, it appears that it was developed more for graphics-oriented games, than for the venerable genre of text-based adventure games. That's not to say you couldn't use it, nor that it wouldn't be a worthwhile exercise to proceed as you are.

Much as I wish this forum got more traffic, Java has yet to become the first choice for a lot of game programmers. The topic you're asking about is pretty much specific to games, so you're in kind of a lonely place, with a very game-specific question, and a not very game-specific programming language. Best I can do for you is suggest you pursue CBEs on your own, but, when some aspect of the Java language gets in your way, remember we're here, and feel free to post any questions we might help with. Don't worry about which forum is best. If we think it belongs elsewhere, we'll move it for you, no charge .

Good luck!
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Welcome to the Ranch, Svetlana. Sorry no one has been able to offer you any help here, yet. What little I know about CBEs suggests that extending an interface may not be your best option. However, it's a complex subject. Also, it appears that it was developed more for graphics-oriented games, than for the venerable genre of text-based adventure games. That's not to say you couldn't use it, nor that it wouldn't be a worthwhile exercise to proceed as you are.

Much as I wish this forum got more traffic, Java has yet to become the first choice for a lot of game programmers. The topic you're asking about is pretty much specific to games, so you're in kind of a lonely place, with a very game-specific question, and a not very game-specific programming language. Best I can do for you is suggest you pursue CBEs on your own, but, when some aspect of the Java language gets in your way, remember we're here, and feel free to post any questions we might help with. Don't worry about which forum is best. If we think it belongs elsewhere, we'll move it for you, no charge .

Good luck!


Thanks for your reply. As you stated it's more for graphical games, but I jumped at the chance to learn a new design pattern. After some thinking and reading, I managed to figure it out, but soon realized that it was unnecessary given the scale of my game. Simple objects and a HashTable was all I needed.

Long story short: I'm an ex-SO user and decided to read/learn rather than have others give me the answers, and what makes matter worse is that some questions don't lead to answers but to users arguing what is the best approach.
 
Stephan van Hulst
Saloon Keeper
Posts: 7727
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's necessarily a bad thing, because discussions about the correct approach come with arguments that can be very educational.

Anyway, the article argues that deep inheritance relationships are bad, and I tend to agree. In this case though, I can only see that an IWeapon extends a IGameObject, and a SleepWeapon is a particular implementation of an IWeapon. I wouldn't call that a deep inheritance hierarchy, unless you made SleepWeapon an interface as well, and you had specific types of weapons that could implement a SleepWeapon.

In such cases, the answer is to use interfaces as traits, and use other patterns such as 'roles'.

For instance, say you have a weapon that has a sleep effect. You can make an interface Effect that fulfills the 'role' pattern: A weapon has an effect, and you can apply the effect to a weapon target:

Now, imagine you have another whole class of objects that can be enchanted, let's say potions. That means that in your design it's worth to have a trait Enchantable, and then provide a default implementation:

A Weapon can then be implemented like this:

Yes, by extending an AbstractEnchantable which extends an AbstractGameObject, you're deepening the inheritance hierarchy, but they are only default implementations to improve code reuse. You could still write a completely custom class that is usable by the rest of the game by implementing the applicable traits:


I don't know if this helps you at all, but I think that composing your entities using components that do more than your entity needs is a solution for complex designs in which it becomes difficult to introduce more traits and roles. I don't think you should design your application like that from the start.

Another unrelated thing: Use established JavaDoc conventions. That way, you can actually generate documentation from your comments:

Welcome to the Ranch!
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svetlana Rosemond wrote:...what makes matter worse is that some questions don't lead to answers but to users arguing what is the best approach.

Oh, well, then you have come to the right place! Arguments like that never happen here at The Ranch.

Except when Winston and I get into it... or Stephan... or Stephan and Junilu... or me, Campbell, Henry, and Jeanne. Sometimes Bear. Paul and Roel, not so much... Let's see, who have I forgotten...?
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan van Hulst

Many thanks for your effort and code. After carefully reading through, it helped me a great deal in understanding CBS.

 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svetlana Rosemond wrote:@Stephan van Hulst

Many thanks for your effort and code. After carefully reading through, it helped me a great deal in understanding CBS.



Indeed, a bravura performance by my fellow Bartender. He gets a cow!
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thanks to you, Svetlana, for introducing me (and, maybe a few others) to a useful paradigm. You get a cow too!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!