Skip to content

Commit

Permalink
Implemented 'joystick' style ego movement, with a touch pad control.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceewing committed Mar 15, 2024
1 parent 82fda52 commit dfcd0e3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
100 changes: 98 additions & 2 deletions core/src/main/java/com/agifans/agile/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.agifans.agile.ui.KeyboardType;
import com.agifans.agile.ui.ViewportManager;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class GameScreen implements Screen {
private Stage landscapeTouchpadStage;
private Touchpad portraitTouchpad;
private Touchpad landscapeTouchpad;
private int previousDirection;

/**
* Details about the AGI game that was selected to be run.
Expand Down Expand Up @@ -330,11 +332,105 @@ private void draw(float delta) {
joyX = landscapeTouchpad.getKnobPercentX();
joyY = landscapeTouchpad.getKnobPercentY();
}
// TODO: Apply movement
//machine.getJoystick().touchPad(joyX, joyY);
processJoystickInput(joyX, joyY);
}
}

private static final int[] DIRECTION_TO_KEY_MAP = new int[] {
0,
Keys.UP,
Keys.PAGE_UP,
Keys.RIGHT,
Keys.PAGE_DOWN,
Keys.DOWN,
Keys.END,
Keys.LEFT,
Keys.HOME
};

/**
* Processes joystick input, converting the touchpad position into an AGI
* direction and then setting the corresponding direction key.
*
* @param joyX
* @param joyY
*/
private void processJoystickInput(float joyX, float joyY) {
double heading = Math.atan2(-joyY, joyX);
double distance = Math.sqrt((joyX * joyX) + (joyY * joyY));

int direction = 0;

if (distance > 0.3) {
// Convert heading to an AGI direction.
if (heading == 0) {
// Right
direction = 3;
}
else if (heading > 0) {
// Down
if (heading < 0.3926991) {
// Right
direction = 3;
}
else if (heading < 1.178097) {
// Down/Right
direction = 4;
}
else if (heading < 1.9634954) {
// Down
direction = 5;
}
else if (heading < 2.7488936) {
// Down/Left
direction = 6;
}
else {
// Left
direction = 7;
}
}
else {
// Up
if (heading > -0.3926991) {
// Right
direction = 3;
}
else if (heading > -1.178097) {
// Up/Right
direction = 2;
}
else if (heading > -1.9634954) {
// Up
direction = 1;
}
else if (heading > -2.7488936) {
// Up/Left
direction = 8;
}
else {
// Left
direction = 7;
}
}
}

UserInput userInput = getAgileRunner().getUserInput();

if (previousDirection != 0) {
userInput.setKey(DIRECTION_TO_KEY_MAP[previousDirection], false);
}

if ((previousDirection != 0) && (direction == 0)) {
getAgileRunner().getVariableData().setVar(Defines.EGODIR, direction);
}
else if (direction != 0) {
userInput.setKey(DIRECTION_TO_KEY_MAP[direction], true);
}

previousDirection = direction;
}

@Override
public void resize(int width, int height) {
viewport.update(width, height, false);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/agifans/agile/UserInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public int getKey() {

public abstract boolean oldKeys(int keycode);

protected abstract void setKey(int keycode, boolean value);
public abstract void setKey(int keycode, boolean value);

protected abstract void setOldKey(int keycode, boolean value);

Expand Down
2 changes: 1 addition & 1 deletion html/src/main/java/com/agifans/agile/gwt/GwtUserInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean oldKeys(int keycode) {
}

@Override
protected void setKey(int keycode, boolean value) {
public void setKey(int keycode, boolean value) {
keys.set(keycode, value? TRUE : FALSE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean oldKeys(int keycode) {
}

@Override
protected void setKey(int keycode, boolean value) {
public void setKey(int keycode, boolean value) {
keys[keycode] = value;
}

Expand Down

0 comments on commit dfcd0e3

Please sign in to comment.