2022-03-08 03:13:53 -05:00
|
|
|
package funkin;
|
2020-10-21 19:33:43 -04:00
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
import funkin.save.Save;
|
2022-03-08 03:13:53 -05:00
|
|
|
import funkin.Controls;
|
2020-10-21 19:33:43 -04:00
|
|
|
import flixel.FlxCamera;
|
2023-06-22 01:41:01 -04:00
|
|
|
import funkin.input.PreciseInputManager;
|
2021-03-22 22:39:35 -04:00
|
|
|
import flixel.input.actions.FlxActionInput;
|
|
|
|
import flixel.input.gamepad.FlxGamepad;
|
2020-10-21 19:33:43 -04:00
|
|
|
import flixel.util.FlxSignal;
|
|
|
|
|
|
|
|
// import ui.DeviceManager;
|
|
|
|
// import props.Player;
|
|
|
|
class PlayerSettings
|
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
public static var numPlayers(default, null) = 0;
|
|
|
|
public static var numAvatars(default, null) = 0;
|
|
|
|
public static var player1(default, null):PlayerSettings;
|
|
|
|
public static var player2(default, null):PlayerSettings;
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
public static var onAvatarAdd(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
|
|
|
|
public static var onAvatarRemove(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
public var id(default, null):Int;
|
|
|
|
|
|
|
|
public var controls(default, null):Controls;
|
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
/**
|
|
|
|
* Return the PlayerSettings for the given player number, or `null` if that player isn't active.
|
|
|
|
*/
|
|
|
|
public static function get(id:Int):Null<PlayerSettings>
|
|
|
|
{
|
|
|
|
return switch (id)
|
|
|
|
{
|
|
|
|
case 1: player1;
|
|
|
|
case 2: player2;
|
|
|
|
default: null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function init():Void
|
|
|
|
{
|
|
|
|
if (player1 == null)
|
|
|
|
{
|
|
|
|
player1 = new PlayerSettings(1);
|
|
|
|
++numPlayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlxG.gamepads.deviceConnected.add(onGamepadAdded);
|
|
|
|
|
|
|
|
var numGamepads = FlxG.gamepads.numActiveGamepads;
|
|
|
|
for (i in 0...numGamepads)
|
|
|
|
{
|
|
|
|
var gamepad = FlxG.gamepads.getByID(i);
|
|
|
|
if (gamepad != null) onGamepadAdded(gamepad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function reset()
|
|
|
|
{
|
|
|
|
player1 = null;
|
|
|
|
player2 = null;
|
|
|
|
numPlayers = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function onGamepadAdded(gamepad:FlxGamepad)
|
|
|
|
{
|
|
|
|
player1.addGamepad(gamepad);
|
|
|
|
}
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
/**
|
|
|
|
* @param id The player number this represents. This was refactored to START AT `1`.
|
|
|
|
*/
|
|
|
|
private function new(id:Int)
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-06-26 20:39:47 -04:00
|
|
|
trace('loading player settings for id: $id');
|
|
|
|
|
2023-01-22 22:25:45 -05:00
|
|
|
this.id = id;
|
|
|
|
this.controls = new Controls('player$id', None);
|
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
addKeyboard();
|
|
|
|
}
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
function addKeyboard():Void
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
var useDefault = true;
|
2023-10-17 00:38:28 -04:00
|
|
|
if (Save.get().hasControls(id, Keys))
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
var keyControlData = Save.get().getControls(id, Keys);
|
|
|
|
trace("keyControlData: " + haxe.Json.stringify(keyControlData));
|
|
|
|
useDefault = false;
|
|
|
|
controls.fromSaveData(keyControlData, Keys);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
useDefault = true;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
2023-06-26 20:39:47 -04:00
|
|
|
if (useDefault)
|
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
trace("Loading default keyboard control scheme");
|
2023-06-26 20:39:47 -04:00
|
|
|
controls.setKeyboardScheme(Solo);
|
|
|
|
}
|
2023-06-22 01:41:01 -04:00
|
|
|
|
|
|
|
PreciseInputManager.instance.initializeKeys(controls);
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
/**
|
|
|
|
* Called after an FlxGamepad has been detected.
|
|
|
|
* @param gamepad The gamepad that was detected.
|
|
|
|
*/
|
2023-01-22 22:25:45 -05:00
|
|
|
function addGamepad(gamepad:FlxGamepad)
|
|
|
|
{
|
|
|
|
var useDefault = true;
|
2023-10-17 00:38:28 -04:00
|
|
|
if (Save.get().hasControls(id, Gamepad(gamepad.id)))
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
var padControlData = Save.get().getControls(id, Gamepad(gamepad.id));
|
|
|
|
trace("padControlData: " + haxe.Json.stringify(padControlData));
|
|
|
|
useDefault = false;
|
|
|
|
controls.addGamepadWithSaveData(gamepad.id, padControlData);
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
2023-10-17 00:38:28 -04:00
|
|
|
else
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
useDefault = true;
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
2023-10-17 00:38:28 -04:00
|
|
|
|
|
|
|
if (useDefault)
|
2023-01-22 22:25:45 -05:00
|
|
|
{
|
2023-10-17 00:38:28 -04:00
|
|
|
trace("Loading gamepad control scheme");
|
|
|
|
controls.addDefaultGamepad(gamepad.id);
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
2023-10-17 00:38:28 -04:00
|
|
|
PreciseInputManager.instance.initializeButtons(controls, gamepad);
|
|
|
|
}
|
2023-01-22 22:25:45 -05:00
|
|
|
|
2023-10-17 00:38:28 -04:00
|
|
|
/**
|
|
|
|
* Save this player's controls to the game's persistent save.
|
|
|
|
*/
|
|
|
|
public function saveControls()
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
var keyData = controls.createSaveData(Keys);
|
|
|
|
if (keyData != null)
|
|
|
|
{
|
|
|
|
trace("saving key data: " + haxe.Json.stringify(keyData));
|
2023-10-17 00:38:28 -04:00
|
|
|
Save.get().setControls(id, Keys, keyData);
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (controls.gamepadsAdded.length > 0)
|
|
|
|
{
|
|
|
|
var padData = controls.createSaveData(Gamepad(controls.gamepadsAdded[0]));
|
|
|
|
if (padData != null)
|
|
|
|
{
|
|
|
|
trace("saving pad data: " + haxe.Json.stringify(padData));
|
2023-10-17 00:38:28 -04:00
|
|
|
Save.get().setControls(id, Gamepad(controls.gamepadsAdded[0]), padData);
|
2023-01-22 22:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 19:33:43 -04:00
|
|
|
}
|