Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to implement game objects?  (Read 1079 times)
0 Members and 1 Guest are viewing this topic.
Offline Stein102

Junior Member


Medals: 1



« Posted 2013-09-21 21:32:25 »

I've started working on a RPG and have the basics done so far. Basic movement around the screen, drawing a map, collision with the map, etc. What I'm stuck on is a good way to add game objects into the world? I know I want the world to be data driven, so I can just add perhaps a new type of sword into an XML file and it's in the game. I'm not sure on how to go about this and what to do next. Any suggestions at all?
Offline ralphchapin

Junior Member


Medals: 3
Projects: 1



« Reply #1 - Posted 2013-09-21 23:31:53 »

I'd start with a base class or interface called something like GameObject.  Extend it or implement it to get all your game objects.  You need to put a lot of thought into this over time, but you might have Weapon class extending GameObject.  Then Sword extending Weapon.  Try to keep your method definitions as low down the hierarchy as possible.  Ideally you would want to deal with a Weapon as a Weapon (or even GameObject) rather than have to check to see if it's a 22FootPike which extends Pike which extends Weapon.

(I usually start with GameObject as an interface, then find that all implementations have common code so I change it to a class with the common code in it.  Then I come up with a new game object that already extends a class and can't extend GameObject, so I have to use an interface after all--maybe GameItem.)

GameObject should have (abstract) methods to read and write XML to aid in generating objects from an XML file.  (Personally I have a phobia about XML, and would be more inclined to write an editor in Java.  I did that with my tank game.)

Don't get too carried away with subclassing.  With my tanks, I had one Tank class (extending GameObject).  To make various types of tanks, I'd drop different Turret, Gun, Engine, Transmission, etc. objects into it to make all the various types.  Of course, then you've got to worry about all the weapon parts in addition to the weapon itself.  (I think this is called "Dependency Injection", but I've seen the term used in some funny ways, so I tend not to use the term myself.)

Ideally, you'd want to be able to write a lot of code that only has to deal with GameObject instances, and not care what they are.  This is probably impractical, but you might come close with Weapon instances.  This simplifies complex code and is known as "Polymorphism".

Hope this helps.  (You did ask for "Any suggestions at all".)
Offline Stein102

Junior Member


Medals: 1



« Reply #2 - Posted 2013-09-22 05:48:52 »

So what you're saying is that I can create an inheritance tree like GameObject > Consumable > HealthItem, and have it read how much the item is supposed to heal and the item name from some sort of data? Or would I need to make it more like GameObject > Consumable > HealthItem > Potion > LargeHealthPotion, which stores all the information needed for a "Large Health Potion"?

Just out of curiosity, why do you have a phobia about XML? Are there some disadvantages to XML that I'm somehow unaware of? Also, what do you mean by "write and editor in Java"? Do you think you could explain this a little more? I'm interested in learning about his this works. My plan was to have all my data in XML and to build an editor where I can distribute it to a non-programmer and give them the capability to add items into the game.

Thanks for the suggestions you've given me. I really appreciate the time you've taken to help me out.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-09-22 05:55:42 »

Because of the lack of multiple inheritance, making a inheritance hierarchy doesn't make sense for rpgs in Java (for example, a sword may be enchanted and is a melee weapon, but it can't extend both EnchantedItem and Weapon).

I asked the same question: http://www.java-gaming.org/topics/composition-with-an-rpg/29851/msg/274826/view.html
reponses were very helpful.
Offline Stein102

Junior Member


Medals: 1



« Reply #4 - Posted 2013-09-23 00:24:26 »

Because of the lack of multiple inheritance, making a inheritance hierarchy doesn't make sense for rpgs in Java (for example, a sword may be enchanted and is a melee weapon, but it can't extend both EnchantedItem and Weapon).

I asked the same question: http://www.java-gaming.org/topics/composition-with-an-rpg/29851/msg/274826/view.html
reponses were very helpful.

In the end, how did you personally end up implementing it? Do you think I could take a look at some of your source code so I can get some ideas?

Thanks.
Offline ralphchapin

Junior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2013-09-23 00:54:30 »

@Stein102:  I'd go with GameObject>Consumable>HealthItem, then pull a healing value from somewhere.  Keep it simple.  When making it more complicated, take care.  You do have only single inheritance.  And as you subclass, you want to keep the subclass hidden as much as you can.

For instance, here you might have Consumable have a "getEffects" method that returns a class that's just a bunch of numbers (an array would work as well).  One for life points, one for strength points, one for visibility points, etc.  Someone drinks your Consumable, and you see, via getEffects, that they gain life points, become a bit weaker, become slightly invisible, etc.  You don't even know, or care, that the Consumable is a HealthItem.  HealthItem is just where the code is to make this type of Consumable work.  The HealthItem in turn can just be a set of numbers, really.  And you can create each HealthItem instance with it's own set of numbers.  (Presumably it has some code that's not in Consumable or you could ditch HealthItem completely.)

Sometimes numbers won't be enough.  The HealthItem may do different things to different races or classes of characters.  Then you need new methods.  Now you may want to extend HealthItem to get a class that makes characters whose name begins with "A" turn invisible, gives super strength to thieves, and gives extra life to half-elves.  It's created as a new WeirdPotion, but from then on you can treat it as another Consumable and forget its origins.

Alternatively, HealthItem might contain another object, and use that object to get all its values; an Effects class, say.  It's a little bit awkward as all the HI methods have to be set up to call the contained Effects object to get return values and make things happen.  However, you can change the HealthItem's effects completely by swapping out the old Effects object and dropping in a new one.

(And, as I suggested in my previous reply, a HealthItem might contain several such classes.  One for effects.  Maybe one for taste (makes some characters gag).  One for intoxication (makes gnomes drunk).  One for addictive qualities.)

What you're looking for is a HealthItem that, once you get it set up, goes through your game doing it's job as a Consumable without any further bother on your part.  If a character consumes it, it know what to to do to the character and how to do it without any work on your part beyond a method call like:  "character.consumes( consumable );".  Or maybe "consumable.drunkBy( character );"--needs some thought.

(I'm putting out ideas here.  In real life, you want to keep your game as simple and as bare-bones as you can manage.)

XML:  I call my feelings a phobia because they're not really rational, and you probably know more about it than I do.  However, since you asked, I like Java objects.  Once something's a Java object I can work with it.  I serialize them for files and I/O with ObjectOutput, or sometimes my own code, but always in binary format.  It wouldn't really be hard to serialize them to XML or JSON, but I like binary and I'm starting to appreciate that ObjectInput can figure out what a class is without me telling it.  And is XML really human readable?  It's wordy, and if there are a lot of numbers there's a lot of conversions going on--it's slow.  (I know the speed doesn't really matter, especially in this case, but....)  Of course, Java objects are not human readable (by most of us) at all, so you need an editor.  But my thinking is if you have something like a HealthItem class set up for your game, use the same class in your editor.  Serialize it as necessary to save it or move it from editor to game and back again.

Of course, with XML you can skip the editor and write it in XML at first.  When you write an editor you can check what the editor's doing easily.  Every other programmer in the universe will tell you to use XML.  And you need to do what works for your.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-09-23 02:50:23 »

Because of the lack of multiple inheritance, making a inheritance hierarchy doesn't make sense for rpgs in Java (for example, a sword may be enchanted and is a melee weapon, but it can't extend both EnchantedItem and Weapon).

I asked the same question: http://www.java-gaming.org/topics/composition-with-an-rpg/29851/msg/274826/view.html
reponses were very helpful.

In the end, how did you personally end up implementing it? Do you think I could take a look at some of your source code so I can get some ideas?

Thanks.
In short, I used a builder pattern - so I could get something like this
1  
Weapon wep = WeaponBuilder.name("Weapon1").enchantment(Enchantment.FIRE).build();

Offline Jeremy
« Reply #7 - Posted 2013-09-23 06:48:08 »

XML:  I call my feelings a phobia because they're not really rational, and you probably know more about it than I do.  However, since you asked, I like Java objects.  Once something's a Java object I can work with it.  I serialize them for files and I/O with ObjectOutput, or sometimes my own code, but always in binary format.  It wouldn't really be hard to serialize them to XML or JSON, but I like binary and I'm starting to appreciate that ObjectInput can figure out what a class is without me telling it.  And is XML really human readable?  It's wordy, and if there are a lot of numbers there's a lot of conversions going on--it's slow.  (I know the speed doesn't really matter, especially in this case, but....)  Of course, Java objects are not human readable (by most of us) at all, so you need an editor.  But my thinking is if you have something like a HealthItem class set up for your game, use the same class in your editor.  Serialize it as necessary to save it or move it from editor to game and back again.

I think this is a totally justified 'phobia.'

Everyone jumps to XML for data serialization, that makes sense if you want portable data. However, XML has a lot of redundant data and it will kill you in sizes if you're using it to save map files etc (depending on what kind of map files they are ofcourse.)

You can use a custom format, they're easy to parse with regex expressions or a basic tokenizer. Additionally, object serialization is fine as well.

Of-course, using any light compression removes the redundancies anyway.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2013-09-23 12:14:35 »

Of course, with XML you can skip the editor and write it in XML at first.  When you write an editor you can check what the editor's doing easily.  Every other programmer in the universe will tell you to use XML.  And you need to do what works for your.

There's a whole range of possibilities between binary and xml. Json and yaml are the obvious alternatives, both considerably less verbose. And if you go binary too soon you make your data harder to work with and change.

See also The Power Of Plain Text

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Stein102

Junior Member


Medals: 1



« Reply #9 - Posted 2013-09-24 01:58:11 »

I'm honestly really lost about where to take this. I want to be able to have something similar to the below to create objects, but I can't quite figure out how to get it done. Can anyone point me in the direction of a source code example where I can see something like this in use?

[/quote]
In short, I used a builder pattern - so I could get something like this
1  
Weapon wep = WeaponBuilder.name("Weapon1").enchantment(Enchantment.FIRE).build();


[/quote]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #10 - Posted 2013-09-24 18:59:17 »

here you go (code not tested)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
class WeaponBuilder {
    String name;
    Enchantment enchantment;
    WeaponBuilder(String name) {
        this.name = name;
    }
    static WeaponBuilder name(String name) {
        return new WeaponBuilder(name)
    }
    WeaponBuilder enchantment(Enchantment enchantment) {
        this.enchantment = enchantment;
        return this;
    }
    Weapon build() {
        return new Weapon(name, enchantment);
    }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!