mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2026-04-27 13:11:42 +00:00
Add basic gamepad support
This commit is contained in:
parent
aa93731f04
commit
db435f4f05
7 changed files with 158 additions and 18 deletions
|
|
@ -26,6 +26,7 @@ import h3d.Camera;
|
||||||
import h3d.Vector;
|
import h3d.Vector;
|
||||||
import hxsl.Types.Matrix;
|
import hxsl.Types.Matrix;
|
||||||
import h3d.scene.Scene;
|
import h3d.scene.Scene;
|
||||||
|
import src.Gamepad;
|
||||||
|
|
||||||
enum CameraMode {
|
enum CameraMode {
|
||||||
FreeOrbit;
|
FreeOrbit;
|
||||||
|
|
@ -160,18 +161,14 @@ class CameraController extends Object {
|
||||||
|
|
||||||
var lerpt = hxd.Math.min(1, 1 - Math.pow(0.6, dt * 600));
|
var lerpt = hxd.Math.min(1, 1 - Math.pow(0.6, dt * 600));
|
||||||
|
|
||||||
if (Key.isDown(Settings.controlsSettings.camForward)) {
|
var cameraPitchDelta = (Key.isDown(Settings.controlsSettings.camBackward) ? 1 : 0)
|
||||||
nextCameraPitch += 0.75 * 5 * dt;
|
- (Key.isDown(Settings.controlsSettings.camForward) ? 1 : 0)
|
||||||
}
|
- Gamepad.getAxis(Settings.gamepadSettings.cameraYAxis);
|
||||||
if (Key.isDown(Settings.controlsSettings.camBackward)) {
|
nextCameraPitch += 0.75 * 5 * cameraPitchDelta * dt;
|
||||||
nextCameraPitch -= 0.75 * 5 * dt;
|
var cameraYawDelta = (Key.isDown(Settings.controlsSettings.camRight) ? 1 : 0)
|
||||||
}
|
- (Key.isDown(Settings.controlsSettings.camLeft) ? 1 : 0)
|
||||||
if (Key.isDown(Settings.controlsSettings.camLeft)) {
|
+ Gamepad.getAxis(Settings.gamepadSettings.cameraXAxis);
|
||||||
nextCameraYaw -= 0.75 * 5 * dt;
|
nextCameraYaw += 0.75 * 5 * cameraYawDelta * dt;
|
||||||
}
|
|
||||||
if (Key.isDown(Settings.controlsSettings.camRight)) {
|
|
||||||
nextCameraYaw += 0.75 * 5 * dt;
|
|
||||||
}
|
|
||||||
|
|
||||||
nextCameraPitch = Math.max(-Math.PI / 2 + Math.PI / 4, Math.min(Math.PI / 2 - 0.0001, nextCameraPitch));
|
nextCameraPitch = Math.max(-Math.PI / 2 + Math.PI / 4, Math.min(Math.PI / 2 - 0.0001, nextCameraPitch));
|
||||||
|
|
||||||
|
|
|
||||||
92
src/Gamepad.hx
Normal file
92
src/Gamepad.hx
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
package src;
|
||||||
|
|
||||||
|
import hxd.Pad;
|
||||||
|
|
||||||
|
class Gamepad {
|
||||||
|
public static var gamepad:Pad = Pad.createDummy();
|
||||||
|
|
||||||
|
public static function init() {
|
||||||
|
Pad.wait(onPad);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function onPad(pad:Pad) {
|
||||||
|
gamepad = pad;
|
||||||
|
pad.onDisconnect = function() {
|
||||||
|
gamepad = Pad.createDummy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getId(name:String) {
|
||||||
|
switch (name) {
|
||||||
|
case "start":
|
||||||
|
return gamepad.config.start;
|
||||||
|
case "ranalogY":
|
||||||
|
return gamepad.config.ranalogY;
|
||||||
|
case "ranalogX":
|
||||||
|
return gamepad.config.ranalogX;
|
||||||
|
case "ranalogClick":
|
||||||
|
return gamepad.config.ranalogClick;
|
||||||
|
case "dpadUp":
|
||||||
|
return gamepad.config.dpadUp;
|
||||||
|
case "dpadRight":
|
||||||
|
return gamepad.config.dpadRight;
|
||||||
|
case "dpadLeft":
|
||||||
|
return gamepad.config.dpadLeft;
|
||||||
|
case "dpadDown":
|
||||||
|
return gamepad.config.dpadDown;
|
||||||
|
case "back":
|
||||||
|
return gamepad.config.back;
|
||||||
|
case "analogY":
|
||||||
|
return gamepad.config.analogY;
|
||||||
|
case "analogX":
|
||||||
|
return gamepad.config.analogX;
|
||||||
|
case "analogClick":
|
||||||
|
return gamepad.config.analogClick;
|
||||||
|
case "Y":
|
||||||
|
return gamepad.config.Y;
|
||||||
|
case "X":
|
||||||
|
return gamepad.config.X;
|
||||||
|
case "RT":
|
||||||
|
return gamepad.config.RT;
|
||||||
|
case "RB":
|
||||||
|
return gamepad.config.RB;
|
||||||
|
case "LT":
|
||||||
|
return gamepad.config.LT;
|
||||||
|
case "LB":
|
||||||
|
return gamepad.config.LB;
|
||||||
|
case "B":
|
||||||
|
return gamepad.config.B;
|
||||||
|
case "A":
|
||||||
|
return gamepad.config.A;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isDown(buttons:Array<String>) {
|
||||||
|
for (button in buttons) {
|
||||||
|
if (gamepad.isDown(getId(button)))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isPressed(buttons:Array<String>) {
|
||||||
|
for (button in buttons) {
|
||||||
|
if (gamepad.isPressed(getId(button)))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isReleased(buttons:Array<String>) {
|
||||||
|
for (button in buttons) {
|
||||||
|
if (gamepad.isReleased(getId(button)))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAxis(axis:String) {
|
||||||
|
return gamepad.values[getId(axis)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ import hxd.res.DefaultFont;
|
||||||
import h2d.Text;
|
import h2d.Text;
|
||||||
import h3d.Vector;
|
import h3d.Vector;
|
||||||
import src.ProfilerUI;
|
import src.ProfilerUI;
|
||||||
|
import src.Gamepad;
|
||||||
|
|
||||||
class Main extends hxd.App {
|
class Main extends hxd.App {
|
||||||
var marbleGame:MarbleGame;
|
var marbleGame:MarbleGame;
|
||||||
|
|
@ -67,6 +68,7 @@ class Main extends hxd.App {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Settings.init();
|
Settings.init();
|
||||||
|
Gamepad.init();
|
||||||
ResourceLoader.init(s2d, () -> {
|
ResourceLoader.init(s2d, () -> {
|
||||||
AudioManager.init();
|
AudioManager.init();
|
||||||
AudioManager.playShell();
|
AudioManager.playShell();
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ import collision.CCDCollision.TraceInfo;
|
||||||
import src.ResourceLoaderWorker;
|
import src.ResourceLoaderWorker;
|
||||||
import src.InteriorObject;
|
import src.InteriorObject;
|
||||||
import src.Console;
|
import src.Console;
|
||||||
|
import src.Gamepad;
|
||||||
|
|
||||||
class Move {
|
class Move {
|
||||||
public var d:Vector;
|
public var d:Vector;
|
||||||
|
|
@ -1686,6 +1687,8 @@ class Marble extends GameObject {
|
||||||
var move = new Move();
|
var move = new Move();
|
||||||
move.d = new Vector();
|
move.d = new Vector();
|
||||||
if (this.controllable && this.mode != Finish && !MarbleGame.instance.paused && !this.level.isWatching) {
|
if (this.controllable && this.mode != Finish && !MarbleGame.instance.paused && !this.level.isWatching) {
|
||||||
|
move.d.x = -Gamepad.getAxis(Settings.gamepadSettings.moveYAxis);
|
||||||
|
move.d.y = -Gamepad.getAxis(Settings.gamepadSettings.moveXAxis);
|
||||||
if (Key.isDown(Settings.controlsSettings.forward)) {
|
if (Key.isDown(Settings.controlsSettings.forward)) {
|
||||||
move.d.x -= 1;
|
move.d.x -= 1;
|
||||||
}
|
}
|
||||||
|
|
@ -1698,10 +1701,14 @@ class Marble extends GameObject {
|
||||||
if (Key.isDown(Settings.controlsSettings.right)) {
|
if (Key.isDown(Settings.controlsSettings.right)) {
|
||||||
move.d.y -= 1;
|
move.d.y -= 1;
|
||||||
}
|
}
|
||||||
if (Key.isDown(Settings.controlsSettings.jump) || MarbleGame.instance.touchInput.jumpButton.pressed) {
|
if (Key.isDown(Settings.controlsSettings.jump)
|
||||||
|
|| MarbleGame.instance.touchInput.jumpButton.pressed
|
||||||
|
|| Gamepad.isDown(Settings.gamepadSettings.jump)) {
|
||||||
move.jump = true;
|
move.jump = true;
|
||||||
}
|
}
|
||||||
if (Util.isTouchDevice() ? MarbleGame.instance.touchInput.powerupButton.pressed : Key.isDown(Settings.controlsSettings.powerup)) {
|
if (Key.isDown(Settings.controlsSettings.powerup)
|
||||||
|
|| (Util.isTouchDevice() && MarbleGame.instance.touchInput.powerupButton.pressed)
|
||||||
|
|| Gamepad.isDown(Settings.gamepadSettings.powerup)) {
|
||||||
move.powerup = true;
|
move.powerup = true;
|
||||||
}
|
}
|
||||||
if (MarbleGame.instance.touchInput.movementInput.pressed) {
|
if (MarbleGame.instance.touchInput.movementInput.pressed) {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import src.ProfilerUI;
|
||||||
import src.Settings;
|
import src.Settings;
|
||||||
import src.Console;
|
import src.Console;
|
||||||
import src.Debug;
|
import src.Debug;
|
||||||
|
import src.Gamepad;
|
||||||
|
|
||||||
@:publicFields
|
@:publicFields
|
||||||
class MarbleGame {
|
class MarbleGame {
|
||||||
|
|
@ -182,7 +183,8 @@ class MarbleGame {
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
world.update(dt * Debug.timeScale);
|
world.update(dt * Debug.timeScale);
|
||||||
}
|
}
|
||||||
if (Key.isPressed(Key.ESCAPE) && world.finishTime == null && world._ready) {
|
if ((Key.isPressed(Key.ESCAPE) || Gamepad.isPressed(["start"]))
|
||||||
|
&& world.finishTime == null && world._ready) {
|
||||||
#if hl
|
#if hl
|
||||||
paused = !paused;
|
paused = !paused;
|
||||||
handlePauseGame();
|
handlePauseGame();
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,7 @@ import src.ProfilerUI;
|
||||||
import src.ResourceLoaderWorker;
|
import src.ResourceLoaderWorker;
|
||||||
import haxe.io.Path;
|
import haxe.io.Path;
|
||||||
import src.Console;
|
import src.Console;
|
||||||
|
import src.Gamepad;
|
||||||
|
|
||||||
class MarbleWorld extends Scheduler {
|
class MarbleWorld extends Scheduler {
|
||||||
public var collisionWorld:CollisionWorld;
|
public var collisionWorld:CollisionWorld;
|
||||||
|
|
@ -1183,12 +1184,15 @@ class MarbleWorld extends Scheduler {
|
||||||
ProfilerUI.measure("updateTimer");
|
ProfilerUI.measure("updateTimer");
|
||||||
this.updateTimer(dt);
|
this.updateTimer(dt);
|
||||||
|
|
||||||
if ((Key.isPressed(Settings.controlsSettings.respawn)) && this.finishTime == null) {
|
if ((Key.isPressed(Settings.controlsSettings.respawn) || Gamepad.isPressed(Settings.gamepadSettings.respawn))
|
||||||
|
&& this.finishTime == null) {
|
||||||
performRestart();
|
performRestart();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((Key.isDown(Settings.controlsSettings.respawn) || MarbleGame.instance.touchInput.restartButton.pressed)
|
if ((Key.isDown(Settings.controlsSettings.respawn)
|
||||||
|
|| MarbleGame.instance.touchInput.restartButton.pressed
|
||||||
|
|| Gamepad.isDown(Settings.gamepadSettings.respawn))
|
||||||
&& !this.isWatching
|
&& !this.isWatching
|
||||||
&& this.finishTime == null) {
|
&& this.finishTime == null) {
|
||||||
if (timeState.timeSinceLoad - this.respawnPressedTime > 1.5) {
|
if (timeState.timeSinceLoad - this.respawnPressedTime > 1.5) {
|
||||||
|
|
@ -1202,6 +1206,7 @@ class MarbleWorld extends Scheduler {
|
||||||
|
|
||||||
if (Key.isPressed(Settings.controlsSettings.blast)
|
if (Key.isPressed(Settings.controlsSettings.blast)
|
||||||
|| (MarbleGame.instance.touchInput.blastbutton.pressed)
|
|| (MarbleGame.instance.touchInput.blastbutton.pressed)
|
||||||
|
|| Gamepad.isPressed(Settings.gamepadSettings.blast)
|
||||||
&& !this.isWatching
|
&& !this.isWatching
|
||||||
&& this.game == "ultra") {
|
&& this.game == "ultra") {
|
||||||
this.marble.useBlast();
|
this.marble.useBlast();
|
||||||
|
|
@ -1245,7 +1250,10 @@ class MarbleWorld extends Scheduler {
|
||||||
ProfilerUI.measure("updateAudio");
|
ProfilerUI.measure("updateAudio");
|
||||||
AudioManager.update(this.scene);
|
AudioManager.update(this.scene);
|
||||||
|
|
||||||
if (this.outOfBounds && this.finishTime == null && Key.isDown(Settings.controlsSettings.powerup) && !this.isWatching) {
|
if (this.outOfBounds
|
||||||
|
&& this.finishTime == null
|
||||||
|
&& (Key.isDown(Settings.controlsSettings.powerup) || Gamepad.isDown(Settings.gamepadSettings.powerup))
|
||||||
|
&& !this.isWatching) {
|
||||||
this.restart();
|
this.restart();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,20 @@ typedef TouchSettings = {
|
||||||
var buttonJoystickMultiplier:Float;
|
var buttonJoystickMultiplier:Float;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef GamepadSettings = {
|
||||||
|
var moveXAxis:String;
|
||||||
|
var moveYAxis:String;
|
||||||
|
var cameraXAxis:String;
|
||||||
|
var cameraYAxis:String;
|
||||||
|
var jump:Array<String>;
|
||||||
|
var powerup:Array<String>;
|
||||||
|
var cameraSensitivity:Float;
|
||||||
|
var invertXAxis:Bool;
|
||||||
|
var invertYAxis:Bool;
|
||||||
|
var respawn:Array<String>;
|
||||||
|
var blast:Array<String>;
|
||||||
|
}
|
||||||
|
|
||||||
typedef PlayStatistics = {
|
typedef PlayStatistics = {
|
||||||
var oobs:Int;
|
var oobs:Int;
|
||||||
var respawns:Int;
|
var respawns:Int;
|
||||||
|
|
@ -141,6 +155,20 @@ class Settings {
|
||||||
buttonJoystickMultiplier: 2.5
|
buttonJoystickMultiplier: 2.5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static var gamepadSettings:GamepadSettings = {
|
||||||
|
moveXAxis: "analogX",
|
||||||
|
moveYAxis: "analogY",
|
||||||
|
cameraXAxis: "ranalogX",
|
||||||
|
cameraYAxis: "ranalogY",
|
||||||
|
jump: ["A", "LT"],
|
||||||
|
powerup: ["B", "RT"],
|
||||||
|
cameraSensitivity: 0.6,
|
||||||
|
invertXAxis: false,
|
||||||
|
invertYAxis: false,
|
||||||
|
respawn: ["back"],
|
||||||
|
blast: ["X", "LB", "RB"]
|
||||||
|
}
|
||||||
|
|
||||||
public static var playStatistics:PlayStatistics = {
|
public static var playStatistics:PlayStatistics = {
|
||||||
oobs: 0,
|
oobs: 0,
|
||||||
respawns: 0,
|
respawns: 0,
|
||||||
|
|
@ -200,6 +228,7 @@ class Settings {
|
||||||
options: optionsSettings,
|
options: optionsSettings,
|
||||||
controls: controlsSettings,
|
controls: controlsSettings,
|
||||||
touch: touchSettings,
|
touch: touchSettings,
|
||||||
|
gamepad: gamepadSettings,
|
||||||
stats: playStatistics,
|
stats: playStatistics,
|
||||||
highscoreName: highscoreName,
|
highscoreName: highscoreName,
|
||||||
marbleIndex: optionsSettings.marbleIndex,
|
marbleIndex: optionsSettings.marbleIndex,
|
||||||
|
|
@ -310,6 +339,9 @@ class Settings {
|
||||||
touchSettings.blastButtonPos = [300, 240];
|
touchSettings.blastButtonPos = [300, 240];
|
||||||
touchSettings.blastButtonSize = 60;
|
touchSettings.blastButtonSize = 60;
|
||||||
}
|
}
|
||||||
|
if (json.gamepad != null) {
|
||||||
|
gamepadSettings = json.gamepad;
|
||||||
|
}
|
||||||
if (json.stats != null) {
|
if (json.stats != null) {
|
||||||
playStatistics = json.stats;
|
playStatistics = json.stats;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue