Search code examples
javastructureorganization

Organizing Source Code for a Java based game


Even though I am not an expert on java, I decided to make a small game. Problem is, I have no idea how to organize my code into folders, and where to put specific classes. Up until now, I have only done very small programs which consisted of me throwing a couple of .class files in a folder and calling it a day.

I've organized some of my code so far, but I know it is not done to convention, and I would like to make it so.

This is what I have at the moment, but I haven't gotten far into the programming yet, so I can change everything as of now.

-project

  • src
    • element
      • entity
        • animal
        • item
        • mob
        • npc
        • player
      • environment
        • Rock.class
        • Tree.class
    • main
      • Game.class
      • Board.class

I would like all of the entities (animal,item,mob,npc, and player) to be able to reference a class called "entity", and all of the elements (anything under entity or environemnt) to be able to reference a class called "element".

So after all of that information, here are my questions:

  1. How should I organize this? Even after searching for a while, I haven't found anything on conventions, and even then, I still don't know where to put some stuff. (any links to tutorials or information on conventional organization would be great)

  2. Would it be best to store sprites and other files such as sounds in a package with each element, or in a separate "resource" folder?

  3. Where would I place the Element.class , and Entity.class so that every element and entity can access them?

ps: Sorry for if I went overboard with extra information, last time I posted on here about something, it got deleted for not providing enough information


Solution

  • You're on the right track. Take a look at the Maven conventions, which would have your source under src/main/java, and other non-source resources that need to be on classpath in src/main/resources. Namespace all your code under a package root that you "own", something like com.mydomain.

    Your ideas about packages for entities and environment objects and such are valid; organize your classes however makes sense for your application. You don't have to necessarily have everything in the same package for access unless you're trying to use package-private (no modifier) for some reason.