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
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.
public class InputState {
///// Attributes /////
private InputStateHelper keyUP;
private InputStateHelper keyDOWN;// ect...
///// Getter(s) / Setter(s) /////
// More code...
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.
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 :
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…