Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  (Yet another) code design questions.  (Read 1320 times)
0 Members and 1 Guest are viewing this topic.
Offline Kryel

Senior Newbie





« Posted 2012-09-15 16:20:11 »

Alright. I've been thinking way too much on this recently so my guess is that I'm doing it wrong. Allow me to explain :

For now, i've been working on some sort of "Input" package, because I wanted to have more info when retrieving a key, for example if it is pressed and for how long, and I plan to use the same logic in several games. Basically there are 4 classes :

- InputConfig.java :::
for storing int values (I'm using slick) to allow ingame player pad/keyboard configuration functions
1  
2  
3  
4  
5  
6  
public class InputConfig {
///// Attributes /////
  private int keyUP;
   private int keyDOWN;// ect...
///// Getter(s) / Setter(s) /////
       // more code for that


- InputState.java ::: InputStateHelper.java :::
Used that way because I want to be able to retrieve more information that a true/false on a key, like the duration as I stated before.
1  
2  
3  
4  
5  
6  
public class InputState {
///// Attributes /////
  private InputStateHelper keyUP;
   private InputStateHelper keyDOWN;// ect...
///// Getter(s) / Setter(s) /////
  // More code...

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class InputStateHelper {
///// Attributes /////
  private int code;// Stored int in the configuration object. Want to keep the configuration external
  private boolean pressed;
   private boolean typed;
   private boolean buffered;
   private int timer;
///// Getter(s) / Setter(s) /////
  // More code...


- InputContainer.java ::: (what will actually be used in the game logic)
Let us say that it acts as an entry point. A player, or enemy, will be assigned an InputContainer object, which contains a configuration and a state.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class InputContainer {
///// Attributes /////
  private InputConfig bindings;
   private InputState states;

///// Methods /////
  public void update(Input input) {
      if (bindings.isActive() && !bindings.isControllerType()) {// If configuration exists and controller is active, then :
        if (bindings.getKeyUP() != 0) this.stateUpdate(states.getKeyUP(), input);
         if (bindings.getKeyDOWN() != 0) this.stateUpdate(states.getKeyDOWN(), input);
         // More code...
     }
   }

   public void stateUpdate(InputStateHelper currentKey, Input input) {
      // Update logic
  }


---
It may sound farfetched I admit, but I made it that way because I want to be able to access the information later in my main class using something like Entity.getInput.getKeyUP.isPressed, and beside it will help me to manage combo entry (quarter circle and so on). That being said, I want to be able to create and dispose of such objects easily in my game, like a factory, AND link them to several independant objects but I have an hard time figuring how to do that.

For example, I have something like this :
1  
2  
3  
4  
5  
6  
7  
8  
public class GameElements {
///// Attributes /////
  private ArrayList<GameEntity> gameEntities;
   // More things that should be accessed in a "whole" when updating or rendering.
  // For example, for the update all I will have to do is a for each loop and something like : currentEntity.update();

   private ArrayList<InputContainer> gameInputs;
   // More "common" elements that could/should be accessed from anywhere in the game logic


I used an ArrayList but is it really a good choice? I want to be able to access "specific" InputContainer object in time, I also want to be able to add and remove them, meaning that any reference to one (like if one is actually at position [3]) HAVE to be updated whenever there's a change in the ArrayList.
I thought about using &reference like in C but I wonder if there's an equivalent in Java? As far as I saw if I continue with my current code the objects will be "copied" in the ArrayList, which is a problem for external use.

Sorry I may be a bit confusing, besides my english is not perfect...
Offline ReBirth
« Reply #1 - Posted 2012-09-16 02:40:56 »

Hello. I don't know what you want to achieve but your way of handling input seems to be overkill. If I were you I'll let my state/screen to handle them and let entities to poll it.

Offline Kryel

Senior Newbie





« Reply #2 - Posted 2012-09-16 09:32:21 »

Hello. I don't know what you want to achieve but your way of handling input seems to be overkill. If I were you I'll let my state/screen to handle them and let entities to poll it.

Hmm... Well I'm using the native Input methods of Slick since he's doing the job fine. But let's say that I want to have additionnal informations when checking a key, like for how much time the player has been holding it and such. So I thought about having my own InputContainer which would contain informations requiered for the game.

Since there's indeed a pool for the keys, the idea was to create an object that will manage and convert native inputs to game inputs. Usually you will have to :
1) Check if a keyboard/gamepad input is pressed or not. (no conversion, raw data)
2) Compare if the key is linked to the configuration made by the player ingame (example : the 'S' keyboard meaning the 'A' attack button)
3) Resolve the result (in the previous example, it will then set the 'A' boolean value either to true or false --> this is what I call a 'conversion' here)
4*) If the previous key is down for quite some time, a timer will keep track of that.
There, I thought about coding everything in a sort of black box, so later I'll only have to use an InputContainer object since it will manage/update everything on its own.

Even though it sounds overkill, as for now my brain doesn't seem able to think of anything much lighter without using insane amount of if/then/else blocs, and I know that repeating code is a bad programming habit. This is why I wonder if I'm just not overthinking that one too?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-09-17 01:58:14 »

Actually this is what to be called indivudual taste of design. Each person has different approach. No one completely wrong or absolutely right and no one should think so. My "overkill"'s just opinion, you dont have to change or feel what. Smiley

Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #4 - Posted 2012-09-17 08:08:23 »

That being said, I want to be able to create and dispose of such objects easily in my game, like a factory, AND link them to several independant objects but I have an hard time figuring how to do that.

For example, I have something like this :
1  
2  
3  
4  
5  
6  
7  
8  
public class GameElements {
///// Attributes /////
  private ArrayList<GameEntity> gameEntities;
   // More things that should be accessed in a "whole" when updating or rendering.
  // For example, for the update all I will have to do is a for each loop and something like : currentEntity.update();

   private ArrayList<InputContainer> gameInputs;
   // More "common" elements that could/should be accessed from anywhere in the game logic


I used an ArrayList but is it really a good choice? I want to be able to access "specific" InputContainer object in time, I also want to be able to add and remove them, meaning that any reference to one (like if one is actually at position [3]) HAVE to be updated whenever there's a change in the ArrayList.
I thought about using &reference like in C but I wonder if there's an equivalent in Java? As far as I saw if I continue with my current code the objects will be "copied" in the ArrayList, which is a problem for external use.

I don't see a problem with using ArrayLists for this purpose. Would it not be sufficient to just refer to the InputContainer object directly where necessary? For example, if you want a specific game entity to receive input from one of the InputContainers you can just add a reference to that specific InputContainer from the entity, for example using a component (if your entity system uses components or something similar) for this purpose. E.g.
1  
2  
3  
4  
5  
6  
7  
8  
9  
public class ControllerComponent {
   InputContainer input;

   public void process(Entity e){
     if (input.getKeyUP.isPressed) {
         // do something interesting with the entity
    }
   }
}


Then the input that is directed at the entity the component is attached to will be handled automagically when the entity gets updated. Did I understand you correctly?

Now my English isn't great either, but I hardly think you need to apologize for yours Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!