Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes My Tiled Game Map System Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My Tiled Game Map System" Watch "My Tiled Game Map System" New topic
Author

My Tiled Game Map System

Seamus Gallagher
Greenhorn

Joined: Aug 08, 2005
Posts: 3
Hello everyone.

I am in the middle of creating some sort of tile-based. turn-based game engine, just for fun. So far i have been messing around with creating a small map, and giving tiles attributes. However, i dont know if this is good, or bad - ie should i start from scratch again, or carry on?

Here is the code, but please note it is only part of the code. Objects like "t0" have been made earlier, and have certain constraints eg xy. "map1" is a seperate class with 2 variables.




If you have any suggestions, or improvements (eg cutting down code) please do let me know about another method!

p.s. i use Jbuilder. all code posted is under
. the class map1 is under
Seamus Gallagher
Greenhorn

Joined: Aug 08, 2005
Posts: 3
**additional"

-my arraylist has "x" to avoid an error.
-i have made two grids as i cant for the life of me work out how to let objects from map1.class be able to have an image attibuted to it. (if i did, i would only need 1 loop, not 2).
-i have produced layering by basically putting 2 duplicate jlabels on the exact same position (eg t0 + t0b). I can basically make as many layers as i wish.
[ August 08, 2005: Message edited by: Seamus Gallagher ]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
First, a minor style thing: One fairly standard parctice is to make class names start with a capital letter, so map1 would be a clearer as Map1. Similarly, fields are usually in "camel case", i.e. lower case first letter, but with capitalized first letters of subsequent words. So MovementCost would be movementCost.

Any time you see...

...it's a flag that you should subclass the base class and move the "do one thing" and "do another thing" into overridden methods.

So what you might do is make a Grass class and a Block class, both of which extend Map1. Some of the values that you're setting in the first loop in the big cood block could just be set in the constructor.



Notice that the "String TerrainType" field is no longer needed because it's not used in if/elseif/else blocks.

Your first two loops, which create the Map1 objects and initialize them become...


The cool part is once each element of gridRef is filled in with an object of the correct Map1 subclass, you longer have to have those "if(TerraineType==Grass)" tests. Just call a method or field defined in Map1 and implemented in the subclasses and you'll get the right behavior/value.



That's the whole concept behind inheritance Polymorphism, one of the fundamental concepts of OO Design.

Next: Encapsulation.
What I wrote above is basically correct, but I've did some things in a "bad" way. I made "icon" in the Map1 classes public, thus allowing direct access to it. What I should do is make icon "private" and then provide a public accessor, getIcon() that returns the value of icon. Ditto for movementCost.





-i have made two grids as i cant for the life of me work out how to let objects from map1.class be able to have an image attibuted to it. (if i did, i would only need 1 loop, not 2).


Did I answer that by adding the icon field to Map1?

There are some other things, but this should be enough to get you started.
Seamus Gallagher
Greenhorn

Joined: Aug 08, 2005
Posts: 3
hello and thankyou for your reply and time,


Ive had a look through what you have written and it looks good, and alot simplier. Ive just been looking through classes/methods/constructors and the such in my book and it makes more sense.
From what i make out, before i had 2 loops, 1 was a jLabel, and the other was a reference to a grid, which i was using in tests for changing values such as jLabel images. It seems you have shown me how to cut out the jLabel (and thus many objects in a larger scenario - saving memory(?)).
I am going to give this a shot, make as much sense of it as i can, and then continue on from there. Once working, it is basically a basis for producing a map ,although with only 2 tile types - Ive learnt to start small rather than jump in the deep end. The next stage will basically be to implement listeners and a hero (movement/attributes etc). If i have any questions in the next few days (im sure i will), ill post my efforts and someone can help explain where i go wrong!

thanks again,

Seamus.
Seamus GalIagher
Greenhorn

Joined: Aug 08, 2005
Posts: 20
Hi,

ZoneAlarm cachecleaner wiped my details before i could save them so ive had to make a new account. I have got the help you gave me to work - using system.out.println() i can check that the values are correct. However i cant get anything to display - any ideas?
i have tried just using the "references", and also with the "jlabels". In both attmepts, the array that holds the map info is correct - using system.out.println() show this. But, no images are displayed, either in the reference grid or the jlabel grid should i post my whole code?
[ August 08, 2005: Message edited by: Seamus GalIagher ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My Tiled Game Map System