Skip to content

Commit

Permalink
added input modifier use case sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Mar 21, 2024
1 parent 858fa5c commit bc81316
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions fxgl-samples/src/main/java/sandbox/InputModifierSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package sandbox;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.KeyTrigger;
import com.almasb.fxgl.input.TriggerListener;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import static com.almasb.fxgl.dsl.FXGL.*;

/**
*
*/
public class InputModifierSample extends GameApplication {

private Entity e;
private double speedMult = 1.0;

@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(1280);
settings.setHeight(720);
}

@Override
protected void initInput() {
getInput().addTriggerListener(new TriggerListener() {
@Override
protected void onKeyBegin(KeyTrigger keyTrigger) {
if (keyTrigger.getKey() == KeyCode.SHIFT) {
speedMult = 3.0;
}
}

@Override
protected void onKey(KeyTrigger keyTrigger) {
switch (keyTrigger.getKey()) {
case W -> { e.translateY(-speedMult * 1); }
case S -> { e.translateY(speedMult * 1); }
case A -> { e.translateX(-speedMult * 1); }
case D -> { e.translateX(speedMult * 1); }
}
}

@Override
protected void onKeyEnd(KeyTrigger keyTrigger) {
if (keyTrigger.getKey() == KeyCode.SHIFT) {
speedMult = 1.0;
}
}
});
}

@Override
protected void initGame() {
e = entityBuilder()
.at(150, 150)
.view(new Rectangle(40, 40, Color.BLUE))
.buildAndAttach();
}

public static void main(String[] args) {
launch(args);
}
}

0 comments on commit bc81316

Please sign in to comment.