so I'm making a 2d Java game with the slick2d and MarteEngine libraries. This is the biggest project since I made checkers for my Java class. I am still pretty new to programming concepts and using optimal ways to get stuff done.

The basic structure of my game is you are a player/hero in a zombie apocalypse and you can gather survivors to help you. There are a many areas that I'm concerned about in my programming. I'm not sure if my methods are a good choice for what I want. This game also does not currently have a grid/tile system.

I've looked at some open source java games and they don't really answer my questions to my methods. So I'm going to make a list here of what I'm uncertain about and I hope you guys can confirm/deny if my methods are appropriate. Sorry if this list gets too long, I'm thinking of the questions as I type.

  1. Targeting/Attacking - Survivors will automatically attack zombies once they get within the gun's target range. To do this, I have every survivor get the distance (using distance formula) to every zombie and find the closest one to attack. I check for this constantly so if a faster zombie gets closer, the survivor will change targets. For zombies, they acquire a target and stick to it (for now). The zombies constantly check if they are within the attack range (around 50 pixels) using the distance formula. If they are within range, stop and attack, otherwise, move towards the target.

  2. 2D Camera - So a camera in a 2D environment moves the world around instead of you. My current method is have my zombies/survivors/any entities on the map stored in array lists. First the background is adjusted, then all the lists are cycled through and every entity's x and y values are modified. This seems to work alright but some stuff you can really notice sliding around on the background. Not really sure how to avoid this.

  3. User Interface - I really have no clue how to work with UI. What I've been doing so far is simply using a background and then generating button objects and manually lining them up. Then, I check if the mouse is over any of the button's areas and if there is a click while moused over the button. I have three different backgrounds and buttons that I switch out with booleans. I'm going to recode that area though, using objects with the background and buttons. Is this the correct way to do UI?

  4. Path Finding - I have no path finding system yet. Do I have to stick to a grid system? I really rather my entities move freely along the terrain and not in a weird square to square motion.

  5. Selecting - I have it so you can select survivors, upgrade them, and other random stuff. My current method for selecting is constantly check where the mouse X and Y is. I get the distance from the mouse to every survivor and check if it is within 30 pixels. Then, I check if there is a click, if so, select the survivor and unselect all others. I'm still trying to figure out how to unselect all survivors if I click on open space. Is there a better way to go about doing this?

  6. Picking stuff up - Same way as said before. I check the distance from the player to every item that can be picked up. If the item is within 30 pixels of the player, it picks it up. It seems to work fine for the moment I suppose. Maybe there really is no other way to do this.

  7. Animations - I understand the how to animate with sprites, but I just want to make sure. So if I have 7 different guns to be shot, do I need to manually make functions that have precise timing on each sprite. Say if I have a shotgun, it needs a recoil, pump forward, brief pause, pump back, and ready again. For a pistol I need just the recoil really. So I'd have to make unique functions for each of these animations?

Sorry to type this long list of questions. I try to gather information on this stuff as much as possible and I haven't been able to find many examples on this stuff. I greatly appreciated any answers, even just a yes or no answer. Thanks in advance!


2 Answers 2

  • Targeting/Attacking - That's fine. There are optimizations here, but don't worry about those yet.

  • 2D Camera - No, that's not the way you do a camera. I'm sure your engine must have a camera class built in. If not, Slick2D has the option to translate. So before drawing your scene, you translate the scene by the 2D camera offset. Then draw your entities at their absolute positions. Don't modify your entity positions!

  • Interface - That works OK, but it's hard to maintain. You can checkout some GUI libraries that utilize Slick2D. Many of them use XML files to define the layout of your UI and are easily modifiable. But only switch methods if this is causing problems for you.

  • Path Finding - You seem to be confusing path finding with motion. They're not the same thing. You find the path, which will likely be "square to square", then you have your entities follow the path. If you want free motion, you can implement steering and have them steer between the nodes of the path you found. Check out A*, as that's a popular path finding algorithm.

  • Selecting - It would probably be best to have an event system for this (and your other GUI commands). But basically you can do what you're doing now, poll the mouse, find out what's under it. You can either do this constantly, or just when a mouse click happens. Either way, you only want to change the selection when the mouse is clicked. If there's nothing under the mouse, deselect all the survivors.

  • Picking stuff up - There are other ways to do this, but this is good enough for a first pass.

  • Animations - No, you make one function that has inputs for fire speed, reload rate, etc. That one function calculates the time required to fire a gun. Don't make multiple "unique" functions.

I can tell already that this answer will likely be followed up by even more questions. This question is already border line a discussion, so don't follow up with more questions. Keep doing research. It sounds like you're new enough that looking at open source projects will be far less useful to you than reading through questions and answers on a site like this. So do more research here. Use the "related" links on the right to browse through related questions and just soak in some information. You have a lot to learn, so take your time. When you have more questions, really think about the problem you're facing, try to solve it yourself first and if you don't solve it post one question at a time here. Good luck!

  • \$\begingroup\$ Thanks for the answer. I guess I misunderstood when I first researched/asked about a 2D camera. I will fix what I have now to not modify the X and Y positions, only render positions. Thanks! \$\endgroup\$
    – Paha
    Jul 12, 2012 at 15:32
  1. Targeting/Attacking - Is ok for prototype, but later there could be a room for optimization as when you will have lots of units, performance could drop if you check everyone vs everyone. It may or may be not a problem. You will need to check and see :)
  2. *2D Camera * - Don't modify entity's position coords.. modify your camera position and while drawing the view, calculate entity space to screen space coords and don't draw if they are outside of your viewport.
  3. Animations - make an object that keeps info about current animation frame, start and end frames, and which can draw frames from a to b (for example 30 to 40) with given speed in milliseconds.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .