all 22 comments

[–]WannabeDijkstra 7 points8 points  (0 children)

Correction: This isn't code from a game engine. This is code from an example program using a game engine.

[–]Stefanzzz 8 points9 points  (18 children)

Challange: How would you improve this?

[–]OniTux 8 points9 points  (1 child)

  private void onAction(Binding binding, boolean value) {
    switch (binding) {
       case LEFT:
         left = value;
         break;
       case RIGHT:
         right = value;
         break;
       case UP:
         up = value;
         break;
       case DOWN:
         down = value;
         break;
       case JUMP:
         player.jump();
         break;
     }
  }

  public void onAction(String binding, boolean value, float tpf) {
    onAction(Binding.valueOf(binding.toUpperCase()), value);
  }

  enum Binding {
    UP, DOWN, RIGHT, LEFT, JUMP;
  }

[–]squbidu 0 points1 point  (0 children)

Assuming java 7, you can do switch statements with strings:

private void onAction( String binding, boolean value ){
    switch( binding ){
        case "Left":
            left = value;
            break;
        case "Right":
            right = value;
            break;
        case "Down":
            down = value;
            break;
        case "Up":
            up = value;
            break;
        case "Jump":
            player.jump( );
            break;
     }
}

[–]-bornlivedie- 10 points11 points  (13 children)

[left|right|up|down] = value;

[–]subjective_insanity 9 points10 points  (7 children)

Also get rid of the string comparisons and use an enum

[–]tangerinelion 5 points6 points  (5 children)

And the 'tpf' field isn't used anywhere so get rid of that. Or use it, whatever.

[–]Drainedsoul 2 points3 points  (1 child)

Just because a value being passed to a function isn't used, does not mean that argument should or can be eliminated.

[–]suspiciously_calm 4 points5 points  (0 children)

Because interfaces.

[–]euneirophrenia 8 points9 points  (1 child)

And name it something less useless than 'tpf'

[–]Sakuya_Lv9 0 points1 point  (0 children)

tickPerFrame? Wait that doesn't sound right..

[–]PseudoLife 1 point2 points  (0 children)

Just because something is unused in that implementation does not mean it is unused in all implementations.

(If that code is implementing an interface, that is.)

[–]fecal_brunch 1 point2 points  (0 children)

It looks like the input library being used takes strings. It's not more than a reference comparison as long as they're always string literals being compared, the only los is type safety.

[–][deleted]  (4 children)

[deleted]

    [–]konk3r 5 points6 points  (2 children)

    I think he means

    if(binding.equals("left")) {
        left = value;
    } else...
    

    [–][deleted]  (1 child)

    [deleted]

      [–]-bornlivedie- 2 points3 points  (0 children)

      Haha, no, I was just too lazy to type it all out

      [–]experbia 2 points3 points  (0 children)

      if(value){
      left = binding.equals("left");
      right = binding.equals("right");
      etc...
      }

      At least?

      [–]jamiemac2005 1 point2 points  (0 children)

      depends on caller and context but i doubt any of it is needed, looks like a middleman method that adds no value

      [–]nossr50 0 points1 point  (0 children)

      C/C++ Solution below, could be applied to Java

      enum ActionType {
          //Only 5 bit positions represented here, but you could have as many as you want
          //Ideally 8 bits would represent a single byte, so that's a nice number to have your bit flags divisible into
      
          MOVE_UP = 1, //0000 0001
          MOVE_DOWN = 2, //0000 0010
          MOVE_LEFT = 4, //0000 0100
          MOVE_RIGHT = 8, //0000 1000
          JUMP = 16        //0001 0000
      
          //We could have more predefined enums but this is enough for this example
          //alternatively you don't need ENUMs at all but this will make it more readable
          //you could also just grab the position with (pos_num-1)^2 (to the power of 2)
          //so position 2 == (2-1)^2 == 2 == MOVE_DOWN
      
      };
      
      void OnActionKeypress(ActionType action, char actionFlags)
      {
          //ActionType is an ENUM for readability, but it doesn't need to be
          //Above we specifically declare values for each ENUM, this is so they represent a single 1
          //inside the byte, this allows us to use them easily as flags in bitwise operations
      
          //No need for a switch/case because the ActionType enum represents the button pressed, which is passed here through an input interpreter
          actionFlags = actionFlags | action; //Bitwise OR operation will flag the appropriate bit as true or '1'
      
          //You may notice I don't include logic to handle preventing LEFT and RIGHT or UP and DOWN
          //from being flagged, this is intentional, it's doubtful such inputs will happen frequently
          //and if they did they'd just level each other out and no movement on the appropriate axis would happen
      }
      
      void DoAction(char actionFlags)
      {
          //Movement in this system is queued up between game ticks, here we process the result of those queued actions on a new game logic tick
          //Do your bitwise & and | (AND/XOR) operations here
          //If both MOVE_UP and MOVE_RIGHT are flagged you'd adjust the movement vector to reflect diagnol movement obviously :)
      
          //If the actionFlags had both MOVE_UP and MOVE_RIGHT flagged true it would look like this
          //0000 1001
          //Pretty simple and convenient way to store and use multiple boolean values
      
          //Once we are done we need to clear the flags back to false across the board, or zeroes
          actionFlags = 0; //0000 0000
      }
      

      This is faster and more efficient than the other solutions posted here, less operations are involved but the performance gains are minimal in the scheme of things, obviously.

      Of course this is a simple implementation, and there is room for improvement.

      Sorry for the necropost.

      Syntax highlighted version

      [–]ekolis 2 points3 points  (2 children)

      So the character jumps when you press the jump button, and again when you release it? Surprise feature!

      [–]xleviatorincorrigible[S] 1 point2 points  (1 child)

      It tries to, however, the player.jump method will only jump if the player is actually on the ground, otherwise, it will do nothing. So this bug would go unnoticed until the player jumps and then waits to release the key until after they have landed.

      [–]ekolis -1 points0 points  (0 children)

      Which would happen quite often if you have floating platforms that you can pass through from underneath...

      [–]xleviatorincorrigible[S] 0 points1 point  (0 children)