Jump to content

  • Log In with Google      Sign In   
  • Create Account

TheChubu

Member Since 13 Sep 2012
Offline Last Active Yesterday, 03:55 PM

Posts I've Made

In Topic: defend sacred animals quest

27 August 2016 - 02:07 PM

Suggestions? Well, let the player join the hunters and kill the animals, getting something in return. Or dont join the hunters and let the player kill the animals for themselves (sell the pelts or something). Or let the player kill everything and everyone for no reason. Those should all be properly handled and possible quest outcomes. 

 

Nothing screams RPG to me more than getting a "Bandits are attacking our village, help us!" quest, then going to the bandits and joining them in the raid.


In Topic: Enums don't have their own variables?

26 August 2016 - 06:45 PM

 It would never occur to me to do something like this in C#

Because you can't. In C#, enums are a thin typesafe shim over integer types. In Java they're full blown objects. Not only can you put fields and methods in enums, but you can also have different overrides in each value of a particular enum. They cant extend classes because they already implicitly extend Enum, but they can implement interfaces.


In Topic: Enums don't have their own variables?

26 August 2016 - 05:58 PM

Yeah dont store stuff in enums. They're basically singletons. Language guaranteed singletons.

 

Putting logic in enums is fair, but it becomes messy if you use the same enum for different things. Although it does opens up to further refactoring (you put too much login in an enum, then it probably means you must make a separate class for that).

 

Furthermore, enums are restrictive. switch over enums is fast, but limited. It can only handle the cases it explicitly states. No more, no less. Thus when you introduce a new value for a given enum, all of the switch cases in which you use said enum become outdated since you have to add the new case.

 

So, often, its better to extract that functionality out of the enum into a different class, and reverse the responsibility. ie, no longer having object X do Y or Z depending on an enum value, but rather have a new class do whatever it knows how to do (Y or Z), at X's request.


In Topic: Libgdx Input System Help

21 August 2016 - 06:22 PM

Of course you could further data drive the whole thing (all those key press conditions, which key could be a field, or an array of keys, and an array of states in which they should be in, etc).

 

You could have it like:

public final class DirectionCondition
{
  public final Direction direction;
  public final Animation animation;
  // Private since arrays are mutable.
  private final boolean[] keyStates;
  private final int[] keyCodes;

  public DirectionCondition ( Direction direction, Animation animation, boolean[] keyStates, int[] keyCodes)
  {
    if(keyStates.length != keyCodes.length)
    {
      throw new Exception("oops!");
    }
    
    this.direction = direction;
    this.animation = animation;
    this.keyStates = keyStates;
    this.keyCodes = keyCodes;
  }

  public boolean tryApply ( Player player )
  {
    for(int i = 0; i < keyStates.length; ++i)
    {
      final boolean expectedState = keyStates[i];
      final int code = keyCodes[i];
    	
      if(Gdx.input.isKeyPressed(code) != expectedState)
      {
        return false;
      }
    }
    player.setCurrentDirection( direction );
    player.setCurrentAnimation( animation );
    return true;
  }
}

Or something like that. Although you hit the point of diminishing returns for this specific case (initializing all those conditions like that becomes tedious).


In Topic: Libgdx Input System Help

21 August 2016 - 06:11 PM

Thats... a lot of repeated code. Why dont you have something like this:

public final class DirectionCondition
{
  public final Direction direction;
  public final Animation animation;
  public final boolean upState;
  public final boolean downState;
  public final boolean leftState;
  public final boolean rightState;

  public DirectionCondition ( Direction direction, Animation animation, boolean upState, boolean downState,
			boolean leftState, boolean rightState )
  {
    super();
    this.direction = direction;
    this.animation = animation;
    this.upState = upState;
    this.downState = downState;
    this.leftState = leftState;
    this.rightState = rightState;
  }

  public boolean tryApply ( Player player )
  {
    if ( testInput( upState, downState, leftState, rightState ) )
    {
      player.setCurrentDirection( direction );
      player.setCurrentAnimation( animation );
      return true;
    }
    return false;
  }

  private static final boolean testInput ( boolean up, boolean down, boolean left, boolean right )
  {
    // Ugh, libGDX singletons.
    return (Gdx.input.isKeyPressed( Input.Keys.UP ) == up &&
       Gdx.input.isKeyPressed( Input.Keys.DOWN ) == down &&
       Gdx.input.isKeyPressed( Input.Keys.LEFT ) == left &&
       Gdx.input.isKeyPressed( Input.Keys.RIGHT ) == right);
  }
}

And you'd use them like this:

// Utility method for creating direction conditions.
public static DirectionCondition condition( Direction direction, Animation animation, boolean upState, boolean downState,
boolean leftState, boolean rightState )
{
  return new DirectionCondition(direction, animation, upState, downState, leftState, rightState);
}
final DirectionCondition[] noDirectionConditions = new DirectionCondition[]
{
  condition( NORTH, walkingUp, true, false, false, false),
  condition( SOUTH, walkingDown, false, true, false, false),
  condition( WEST, walkingLeft, false, false, true, false),
  condition( EAST, walkingRight, false, false, false, true),
  // Omitting the rest for the sake of brevity.
};

// You'd also have northConditions, eastConditions, etc.

// Casting to player, not ideal, you should revise that update(Entity) method.
final Player player = (Player)entity;


// Now check them all.
for(DirectionCondition condition : noDirectionConditions)
{
  if(condition.tryApply(player))
  {
    // One condition already applied, no sense in trying the rest.
    break;
  }
}

 

Moreover, all those directions could be an enum, with different overrides for 'update' if you want.

 

EDIT: There are no words to describe the hate I have for this damn editor.


PARTNERS