Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Need Some Advice  (Read 1006 times)

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Need Some Advice
« on: March 06, 2011, 11:31:31 pm »

This is my first Roguelike. It's my first serious program, really. Up until now, I've only made "Hello World!" type stuff. I know that this is probably not the best thing to tackle first off, but I couldn't think of anything else. It's programmed in java. It's also not done yet. All I have implemented so far its some basic tracking of the world, a very simple start on having creatures, and the renderer and display. The reason I'm posting on the board is because my code is getting a little sloppy and I'd like some advice on how to clean it up before I implement anything more. The big thing thats bothering me is the Renderer's render function. It does what it's supposed to, but its become a giant block of code. Is there a way to slim it down, or at least make it more readable?

You may notice that the world tracking system is a little weird. The way it works is that the WorldManager tracks the information that is relevant across the setting, while the Areas represent buildings or compounds. The LocalAreas represent individual rooms, and are where the characters physical presence is tracked. Instead of having a giant array or something, the LocalAreas each remember what other LocalAreas border them. This allows me to do interesting things, like have a gentle slope that causes you to walk through a tunnel and come out at a higher point than you went in. Or have a terrible desert where you always seem to walk in circles, no matter how certain you are that you took a straight path. anyway, here are the files. I'm also open to any other criticism or advice you may have.

Spoiler: Area (click to show/hide)

Spoiler: Character (click to show/hide)

Spoiler: Display (click to show/hide)

Spoiler: Entity (click to show/hide)

Spoiler: LocalArea (click to show/hide)

Spoiler: Main (click to show/hide)

Spoiler: Player (click to show/hide)

Spoiler: Renderer (click to show/hide)

Spoiler: WorldManager (click to show/hide)

Edit - Whoah didn't mean to post yet. hold please.

Edit2 - I really need to be more careful about pressing the right ctl-key. I just reset the thing and lost my whole huge rant. aargh!!

Edit3 - Finally Wote everything. I think.
« Last Edit: March 07, 2011, 05:40:25 pm by Angle »
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Need Some Advice
« Reply #1 on: March 07, 2011, 02:55:13 am »

Well give me a second to go through your code, but singleton is an easy one for starters. Basically it makes sure you only ever get one instance of a class, without the messy sticky static. Well it still uses static, but not in the same way. I'll write up some code to show you how it works, but if any of it isn't java, please tell me, as I can't always remember what is and ian;t in that language.

*Note: This is not thread safe currently.
Code: [Select]
public class thingy
{
private static thingy _instance;

public static thingy getInstance()
{
if (_instance == null)
{
_instance = new thingy();
}
return thingy;
}

private thingy() { }
}

Ok, so that was done in notepad in a few seconds, but you get the idea. You can see that rather then calling a constructor, you call a static method to get an instance, so everybody has access to that single instance. Think you can use that?

Now onto reading through your code.

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Need Some Advice
« Reply #2 on: March 07, 2011, 08:34:45 am »

so to rewrite the worldmanager to properly be a singleton would look something like this?

Spoiler: WorldManager (click to show/hide)

with the rest of the code updated so it all works properly, of course.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Need Some Advice
« Reply #3 on: March 07, 2011, 12:06:03 pm »

I think you can omit the if(... == null) by using private static _instance = new thingy(); as the declaration

There is probably some tradeoff or other, but either way should work.
Logged
Eh?
Eh!

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Need Some Advice
« Reply #4 on: March 07, 2011, 05:18:02 pm »

I've updated the project so that those objects implement the singleton code better. Also, with regards to the forum software, are you allowed to nest spoilers? I tried it with the OP but it didn't work right.
« Last Edit: March 07, 2011, 10:52:01 pm by Angle »
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Need Some Advice
« Reply #5 on: March 08, 2011, 12:51:19 am »

Making a game is, IMO, an excellent way to learn how to program. You'll learn a lot, but the trick is to not get discouraged when you realize how high you've set your bar.

And if you're trying this for a 7DRL contest, know that you will probably not have even a half-finished project by the end of the week (unless you do nothing but code the whole time ;)).

Try not to get too bogged down with The Right WayTM, and don't be afraid of re-inventing the wheel. Try to make the most of what you already know. Remember that learning about the most efficient methods and all the complex libraries can wait. Designing your own hash-table implementation can be fun and educational, even if the result is horribly inefficient! (A bit of experience here :) )

Good luck and remember to have fun!

I think you can omit the if(... == null) by using private static _instance = new thingy(); as the declaration

There is probably some tradeoff or other, but either way should work.

Whether that'll work depends on what's done in the constructor. The main thing that comes to mind is exceptions: there's no way to catch them doing it the way you describe. This may or may not be an issue. On the other hand, if you say
Code: [Select]
public static final instance = new Foo()you can omit the getInstance() method entirely (might make it harder to thread-proof though... I'm not too familiar with that area)
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Angle

  • Bay Watcher
  • 39 Indigo Spear Questions the Poor
    • View Profile
    • Agora Forum Demo!
Re: Need Some Advice
« Reply #6 on: March 08, 2011, 08:17:53 am »

I'm not going to make his multi-threaded, and I'm certainly not going to enter it in the 7DRL. I just don't work that fast.
Logged

Agora: open-source platform to facilitate complicated discussions between large numbers of people. Now with test site!

The Temple of the Elements: Quirky Dungeon Crawler