Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

282 Neutral

About EpicWally

  • Rank
  1. EpicWally

    TDD development / examples

    I asked this same question not too long ago. Rather than ramble off the advice passed to me, I'll link you to the post here so you can read it. There's a project linked there for a TDD Tetris project that I found very useful. (Re-linked here so you don't have to dig for it.)
  2. EpicWally

    JFrame KeyEvent question

    I'm a derp. Forgot addKeyListener(this) in the frame. >.<.
  3. So I'm working on a rogue-like, and right wrong or indifferent, I am using swing. My current setup has a GameWindow class that extends JFrame, and implements KeyListener, a GamePanel class that extends JPanel, a GameState class that holds the game panel of whatever state I'm in, and also holds that state's logic, and a GameController class that knows which state the game is in, and can cycle through states. The way I am passing KeyEvents is the frame has the keylistener, and when a key is pressed it passes that KeyEvent to the GameController, which then on the next cycle through the GameLoop it passes it to the GameState through the state's input function, and the state proccesses accordingly. This sounds good, but for whatever reason, it doesn't seem to be working. I am showing the relevent source code below, but I have a hunch my error is in method rather than syntax? Any help would be greatly appreciated. Thanks! -Wally GameWindow code package main.java.dungeon.gui; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; import main.java.main.GameController; public class GameWindow extends JFrame implements KeyListener{ private static final long serialVersionUID = 1L; private static GameWindow gameWindow; private static JPanel activePanel; private GameWindow(){ super("Dungeon Crawler"); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setBounds(0, 0, 640, 480); setVisible(true); } public static GameWindow create(){ if(gameWindow == null) gameWindow = new GameWindow(); return gameWindow; } public void setActivePanel(JPanel panel){ activePanel = panel; add(activePanel, BorderLayout.CENTER); } @Override public void keyPressed(KeyEvent e) { GameController.keyPressed(e); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { //GameController.keyPressed(e); } } GameController code package main.java.main; import java.awt.event.KeyEvent; import main.java.dungeon.GameState; public abstract class GameController { private static GameState currentState; private static GameState nextState; private static KeyEvent keyPressed; private GameController(){} static void input(){ if(keyPressed != null){ currentState.input(keyPressed); keyPressed = null; } } static void logic(){ currentState.logic(); nextState = currentState.getNextState(); if(nextState != null) currentState = nextState; } static void update(){ currentState.update(); } static void setState(GameState state){ currentState = state; } static GameState getState(){ return currentState; } public static void keyPressed(KeyEvent e){ keyPressed = e; } } Main Menu State code package main.java.dungeon; import java.awt.event.KeyEvent; import main.java.dungeon.gui.MainMenuPanel; public class GameStateMainMenu extends GameState{ private static final String START_GAME = "Start game!"; private static final String OPTIONS = "Options"; private static final String QUIT = "Quit"; private static final String INDENT = " "; private int pointer; private GameState nextState; private boolean pointerUp, pointerDown, changeState, update; private String text; public GameStateMainMenu(){ gamePanel = new MainMenuPanel(); pointer = 1; pointerUp = pointerDown = changeState = false; update = true; } @Override public void input(KeyEvent keyEvent) { if(keyEvent != null){ switch(keyEvent.getKeyCode()){ case KeyEvent.VK_UP: pointerUp = true; update = true; break; case KeyEvent.VK_DOWN: pointerDown = true; update = true; break; case KeyEvent.VK_ENTER: changeState = true; break; } } } @Override public void logic() { if(pointerUp &amp;&amp; pointer != 1) pointer--; if(pointerDown &amp;&amp; pointer != 3) pointer++; if(changeState){ if(pointer == 1) nextState = new GameStatePlaying(); if(pointer == 2) nextState = new GameStateMainMenuOptions(); if(pointer == 3){} //TODO generate quit code } } @Override public void update() { if(update){ text = "\n\n\n\n" + INDENT + (pointer == 1 ? ">" : " ") + START_GAME + "\n\n\n" + INDENT + (pointer == 2 ? ">" : " ") + OPTIONS + "\n\n\n" + INDENT + (pointer == 3 ? ">" : " ") + QUIT; update = false; } ((MainMenuPanel)gamePanel).setText(text); } @Override public GameState getNextState() { return nextState; } } and the game panel's code package main.java.dungeon.gui; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JPanel; import javax.swing.JTextPane; public class MainMenuPanel extends JPanel{ private static final long serialVersionUID = 1L; private JTextPane text; public MainMenuPanel(){ setPreferredSize(new Dimension(640, 480)); setBounds(0, 0, 640, 480); text = new JTextPane(); text.setPreferredSize(new Dimension(640, 480)); text.setEditable(false); add(text, BorderLayout.CENTER); } public void setText(String s){ if(!s.equals(text.getText())) text.setText(s); } } Lots of code to read, I know. Thank you for taking the time to look at it! If there is any other code you need to see, please let me know! (Also, this is very much a work in progress, I know there are a lot of things that will need cleaned up in this as I work through it.) Thanks again.
  4. EpicWally


    I have to agree with Ashaman73. Think small goals. Feel good when you accomplish them. Try to think as little as possible about your finished game as far as mechanics go. Usually this leads me down the path of "Oh, I want this to work like this, which means I'll need this object, which will need to communicate with such, and oh I should make this interface..." etc ad infinitum until you're thinking about 100 different things that need done, and you shutdown from overload. I prefer to think along the lines of "Ok, where am I now? What is one thing I can easily add? or, What is the next logical thing to implement?" You add that thing, test it out, refactor, and boom, you just successfully got a little closer to your end goal, and you feel great about it. Sometimes I just have no idea what to work on, so I go back and look at some existing code and ask "Is this as clean, or as efficient as it could be?" and then sit down to clean up that one piece of code, which also improves your program and your ability to look at it and determine next steps. In a nutshell, think baby steps, and put the blinders on for every step thats past the one that is next to work on. Start from the ground, and build up to the final game, rather than looking at the final game and trying to break it down to it's tiny pieces.
  5. Another user linked me to this a while ago. I think this will help solidify the concept, and show you a state machine in action in a simple game.
  6. EpicWally

    Rogue-like tile system display question

    Thanks again for the tips! I have changed the structure such that now the map contains 2 arrays, one of tiles, and one of entities. I have also replaced the looped string concatenation with a stringBuilder. Doing some reading, java inherently uses a stringbuilder when you need to concatenate strings, but in doing so in a loop it is creating a new stringbuilder for every loop, which is just ridiculous, so I now create one before the loop, and return its tostring after, yielding a significant optimization. I like the idea of the byte array. Saving the finished state of the board construction such that if nothing changes in a game loop, it doesn't have to completely reconstruct the board. Seems like a smart idea, I will have to play around with this.
  7. EpicWally

    Rogue-like tile system display question

    Adrian, Thank you for your response! In answer to your list of questions: 1.) No, no external libraries. I'm seeing what I can do entirely of my own efforts. 2.) I am doing text based. Not wanting to use the console, I am using a JTextPane to display the text of the game. 3.) I haven't even gotten that far yet, truthfully. At the moment, I am simply trying to figure out the best way to draw ____, be it a room, floor, etc. We'll just call it a map for the time-being. Once I get that figured out, I can get the overarching structure set for rooms/floors/dungeons, etc. I agree with the rough structure and pseudo-code you posted, but I guess my big question is how to address that IF section at the front of it, and I guess equally importantly, how the map tracks the entities inside it, be it a list, or map, or what have you. My problem with the approach you outlined is in essence you are doing the following (or at least this is how I foresee implementing it, if you have a better alternative, I guess that is exactly what I'm looking for.) for(i =0; i < map.height; i++{ for(j=0; j< map.width, j++{ **if(collection contains entity at (j,i)** draw(entity) else draw(tile) How do you implement this line without checking every entity on the map, for every tile on the map? If you have a 10x10 map, with 5 entities on it, you'll be making 500 different conditional checks, as the way I'm seeing it now, you'll have to iterate across your whole entity collection for every tile. ...Unless you were to keep entities in an array, rather than a list... That would give 2 arrays, one of tiles, and one of entities... the Entities array being null everywhere that there isn't an entity... then in the loop above you could have something like: if(entities[j] != null draw entities[j] else draw tiles[j] This seems like it avoids basically all the iterative problems I was worried about, though I don't know much on the overhead of an array vs. that of a list. What are your thoughts on this approach, and how do you store entities in the one you are working on? To your later comment, yes, I am worried about performance, not so much because of it's effect on this game that I'm programming now, but mostly to learn the most efficient structures for later in my programming career, when such things might matter more. I'd like to learn best practices from the get-go, rather than possibly get into bad habits. Also, thanks for the tip on using a StringBuilder, I will definitely look into this. Thanks again for your response! -Wally
  8. EpicWally

    Rogue-like tile system display question

    I hate doing this, but bump? Anyone got thoughts on this one?
  9. So I'm working on a roguelike, and I have a question about drawing the map and all the entities and items that reside on it. The way I have it programmed at the moment, is the board is a 2 dimensional array of tiles. Each Tile has its own char to represent it, and contains an Entity member. This way, when the draw method is called, each tile spits back either its own icon, or if it has an Entity, the Icon belonging to the entity. These chars are all concatenated into the string that is eventually thrown to the display. Needless to say, this works, though I'm not certain it's the best way to do it. 1.) From a coupling perspective, there's really no reason a tile should know the entity that is on top of it. and 2.) now for every tile, the if(Entity != null) is being checked to determine what char to display. When 99% of the tiles are empty, this seems like an unnecessary check. I don't imagine this is serious overhead, but it still doesn't feel clean to me. All that said, I've been stewing on alternatives, and none of them sounds particularly appealing either. These are what I've come up with: 1.) Have a graphics manager class with a Board reference, and a list or hashmap of Entities. Then, for every x/y on the display, it checks to see if an entity resides there from the list/map, if so displays it, else displays the tile's icon. This just seems hideous to me, to have to iteratively go through a list for every tile? Though I suppose I could have Entity implement comparable, and make the natural sorting order go left to right, top to bottom, so that only the next entity in the list is being checked vs the coordinates you're drawing at. Still feels heavy... 2.) Same design as above, graphics manager with board and entity references. This time, take the board, and convert it to a char[][] array. Then go through the entity list, replacing the elements of that array with the entities icons. Finally convert the char[][] to a string and display it. This avoids a lot of the iteration problems that are encountered above, but it requires instantiating a temporary array, filling it, and then converting that array back to a string, which all of these steps are iterative in and of themselves, so this doesn't seem like it would end up saving anything. I would love to be able to simply replace char at index in a string, but I haven't found that such a method exists. (Even if it does, are the underlying mechanics similarly iterative, so would it even save anything over the options described above?) Either I'm over-thinking this, and one of the above approaches is fine/the best way to do it, or there's some other method that I am missing. Thoughts and suggestions? Thank you in advance! -Wally
  10. I'm not an expert programmer, but I have been reading a lot of books recently on best practices and agile programming, so feel free to take this advice with a large grain of salt. You cannot plan for everything up front. Think of a city. It starts as a town, with a small town square, and just a couple necessary buildings. Then it expands, adding in a few stores. Eventually some buildings get ripped down and replaced with strip malls. Roads eventually get ripped out and replaced with highways. etc.Coding necessarily has to follow the same progression, as it is a huge task to plan everything up front. Besides, you will always run into roadblocks along the way, and you will need to be adaptable. My opinion is that your best bet, is to cover as much of your project as possible with unit tests. This way, you can fearlessly try refactoring your code into cleaner structures every time your program needs to evolve. You will know you haven't broken anything if all the tests still pass. Most of what's above is shamelessly stolen from "Clean Code - A Handbook of Agile Software Craftsmanship". I simply copied/paraphrased to make the relevant points easily accessible. I highly recommend this book.
  11. First, the public vs. private argument. I'm fairly new to programming myself, but in general, from what I've read, I make everything private unless I'm given a compelling reason to do otherwise. In your example, the "actor" would not need to find the other actors. In fact, it shouldn't really even know they exist. All this should be handled by some other physics engine, which handles all the actors, and calculates colisions. When a colision occurs, it calls the necessary method on the actor. i.e. changing movement speed/direction. The actor really only know where it is, and how to move. The physics engine gives it the specifics. Similarly the actor shouldn't know about keyboard inputs, gravity, or anything like that.
  12. Take a look at chapter 6 in this book. The author does a pretty good job explaining the purpose of getters and setters, and objects vs data structures. Overall I think the other posters have hit the major points, but it's here as a reference. I've found this to be a very interesting read so far, and I would definitely recomend it.
  13. EpicWally

    TDD in game development?

    Frob - Thank you for the link to the Tetris by TDD tutorials! I got through the first section, and I'm enjoying it/learning a lot from it. I already did my own Tetris clone a while ago, so to go back through it in a different way like this is pretty neat. I can't wait to get through the rest of it!
  14. Kind of a random question that might be putting the cart before the horse... I've recently been reading Clean Code - A Handbook of Agile Software Craftsmanship at the suggestion of another user on this site in another post. One thing that is mentioned is unit-testing. Being somewhat new to programing (only having programed Tetris, Breakout, a Maze game, and starting on a Gradius-type clone) I had no idea what this even was. So, I did some reading on the concept, and ultimately learned of Test Driven Design (TDD) and conceptually it seems like a good idea. My two questions: 1.) Is TDD applicable to game development? I'm guessing the answer is yes for large projects/teams, but I don't know about small project games like what I've mentioned I've done/am working on. 2.) If it is, what kinds of tests would one run? This is probably a case of "If you have to ask, you don't know enough to use it yet" but I want to get an idea if I should spend time learning/practicing the concept, or if it's something I can skip-over. Thanks in advance for the feedback!
  15. I stole this from another post, but this article http://lazyfoo.net/articles/article04/index.php (and a number of the others on this site) have given me a pretty good idea on how to structure games.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!