2022-03-08 08:13:53 +00:00
|
|
|
package funkin;
|
2020-10-21 23:33:43 +00:00
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
import funkin.save.Save;
|
2023-10-31 19:30:47 +00:00
|
|
|
import funkin.input.Controls;
|
2020-10-21 23:33:43 +00:00
|
|
|
import flixel.FlxCamera;
|
2023-06-22 05:41:01 +00:00
|
|
|
import funkin.input.PreciseInputManager;
|
2021-03-23 02:39:35 +00:00
|
|
|
import flixel.input.actions.FlxActionInput;
|
|
|
|
import flixel.input.gamepad.FlxGamepad;
|
2020-10-21 23:33:43 +00:00
|
|
|
import flixel.util.FlxSignal;
|
|
|
|
|
2023-11-07 09:04:22 +00:00
|
|
|
/**
|
|
|
|
* A core class which represents the current player(s) and their controls and other configuration.
|
|
|
|
*/
|
2020-10-21 23:33:43 +00:00
|
|
|
class PlayerSettings
|
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
// TODO: Finish implementation of second player.
|
2023-10-17 04:38:28 +00: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-23 03:25:45 +00:00
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
public static var onAvatarAdd(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
|
|
|
|
public static var onAvatarRemove(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* The player number associated with this settings object.
|
|
|
|
*/
|
2023-01-23 03:25:45 +00:00
|
|
|
public var id(default, null):Int;
|
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* The controls handler for this player.
|
|
|
|
*/
|
2023-01-23 03:25:45 +00:00
|
|
|
public var controls(default, null):Controls;
|
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
/**
|
|
|
|
* Return the PlayerSettings for the given player number, or `null` if that player isn't active.
|
2024-03-17 02:20:22 +00:00
|
|
|
*
|
|
|
|
* @param id The player number this represents.
|
|
|
|
* @return The PlayerSettings for the given player number, or `null` if that player isn't active.
|
2023-10-17 04:38:28 +00:00
|
|
|
*/
|
|
|
|
public static function get(id:Int):Null<PlayerSettings>
|
|
|
|
{
|
|
|
|
return switch (id)
|
|
|
|
{
|
|
|
|
case 1: player1;
|
|
|
|
case 2: player2;
|
|
|
|
default: null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* Initialize the PlayerSettings singletons for each player.
|
|
|
|
*/
|
2023-10-17 04:38:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* Forcibly destroy the PlayerSettings singletons for each player.
|
|
|
|
*/
|
|
|
|
public static function reset():Void
|
2023-10-17 04:38:28 +00:00
|
|
|
{
|
|
|
|
player1 = null;
|
|
|
|
player2 = null;
|
|
|
|
numPlayers = 0;
|
|
|
|
}
|
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* Callback invoked when a gamepad is added.
|
|
|
|
* @param gamepad The gamepad that was added.
|
|
|
|
*/
|
|
|
|
static function onGamepadAdded(gamepad:FlxGamepad):Void
|
2023-10-17 04:38:28 +00:00
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
// TODO: Make this detect and handle multiple players
|
2023-10-17 04:38:28 +00:00
|
|
|
player1.addGamepad(gamepad);
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
/**
|
|
|
|
* @param id The player number this represents. This was refactored to START AT `1`.
|
|
|
|
*/
|
2024-03-17 02:20:22 +00:00
|
|
|
function new(id:Int)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-06-27 00:39:47 +00:00
|
|
|
trace('loading player settings for id: $id');
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
this.id = id;
|
|
|
|
this.controls = new Controls('player$id', None);
|
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
addKeyboard();
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
function addKeyboard():Void
|
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
var useDefault:Bool = true;
|
2024-03-04 21:37:42 +00:00
|
|
|
if (Save.instance.hasControls(id, Keys))
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-03-04 21:37:42 +00:00
|
|
|
var keyControlData = Save.instance.getControls(id, Keys);
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Loading keyboard control scheme from user save');
|
2023-10-17 04:38:28 +00:00
|
|
|
useDefault = false;
|
|
|
|
controls.fromSaveData(keyControlData, Keys);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
useDefault = true;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 00:39:47 +00:00
|
|
|
if (useDefault)
|
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Loading default keyboard control scheme');
|
2023-06-27 00:39:47 +00:00
|
|
|
controls.setKeyboardScheme(Solo);
|
|
|
|
}
|
2023-06-22 05:41:01 +00:00
|
|
|
|
|
|
|
PreciseInputManager.instance.initializeKeys(controls);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
/**
|
|
|
|
* Called after an FlxGamepad has been detected.
|
|
|
|
* @param gamepad The gamepad that was detected.
|
|
|
|
*/
|
2024-03-17 02:20:22 +00:00
|
|
|
function addGamepad(gamepad:FlxGamepad):Void
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
var useDefault = true;
|
2024-03-04 21:37:42 +00:00
|
|
|
if (Save.instance.hasControls(id, Gamepad(gamepad.id)))
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-03-04 21:37:42 +00:00
|
|
|
var padControlData = Save.instance.getControls(id, Gamepad(gamepad.id));
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Loading gamepad control scheme from user save');
|
2023-10-17 04:38:28 +00:00
|
|
|
useDefault = false;
|
|
|
|
controls.addGamepadWithSaveData(gamepad.id, padControlData);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2023-10-17 04:38:28 +00:00
|
|
|
else
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2023-10-17 04:38:28 +00:00
|
|
|
useDefault = true;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2023-10-17 04:38:28 +00:00
|
|
|
|
|
|
|
if (useDefault)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Loading default gamepad control scheme');
|
2023-10-17 04:38:28 +00:00
|
|
|
controls.addDefaultGamepad(gamepad.id);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2023-10-17 04:38:28 +00:00
|
|
|
PreciseInputManager.instance.initializeButtons(controls, gamepad);
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
|
2023-10-17 04:38:28 +00:00
|
|
|
/**
|
|
|
|
* Save this player's controls to the game's persistent save.
|
|
|
|
*/
|
2024-03-17 02:20:22 +00:00
|
|
|
public function saveControls():Void
|
2023-10-17 04:38:28 +00:00
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
var keyData = controls.createSaveData(Keys);
|
|
|
|
if (keyData != null)
|
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Saving keyboard control scheme to user save');
|
2024-03-04 21:37:42 +00:00
|
|
|
Save.instance.setControls(id, Keys, keyData);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (controls.gamepadsAdded.length > 0)
|
|
|
|
{
|
|
|
|
var padData = controls.createSaveData(Gamepad(controls.gamepadsAdded[0]));
|
|
|
|
if (padData != null)
|
|
|
|
{
|
2024-03-17 02:20:22 +00:00
|
|
|
trace('Saving gamepad control scheme to user save');
|
2024-03-04 21:37:42 +00:00
|
|
|
Save.instance.setControls(id, Gamepad(controls.gamepadsAdded[0]), padData);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 23:33:43 +00:00
|
|
|
}
|