-
-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added input modifier use case sample
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
fxgl-samples/src/main/java/sandbox/InputModifierSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |