1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-19 21:33:06 +00:00
Funkin/source/funkin/PlayerSettings.hx

179 lines
4.5 KiB
Haxe
Raw Normal View History

package funkin;
2020-10-21 23:33:43 +00:00
import funkin.save.Save;
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;
/**
* 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.
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;
public static var onAvatarAdd(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
public static var onAvatarRemove(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
2024-03-17 02:20:22 +00:00
/**
* The player number associated with this settings object.
*/
public var id(default, null):Int;
2024-03-17 02:20:22 +00:00
/**
* The controls handler for this player.
*/
public var controls(default, null):Controls;
/**
* 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.
*/
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.
*/
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
{
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
{
2024-03-17 02:20:22 +00:00
// TODO: Make this detect and handle multiple players
player1.addGamepad(gamepad);
}
/**
* @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)
{
trace('loading player settings for id: $id');
this.id = id;
this.controls = new Controls('player$id', None);
addKeyboard();
}
function addKeyboard():Void
{
2024-03-17 02:20:22 +00:00
var useDefault:Bool = true;
if (Save.instance.hasControls(id, Keys))
{
var keyControlData = Save.instance.getControls(id, Keys);
2024-03-17 02:20:22 +00:00
trace('Loading keyboard control scheme from user save');
useDefault = false;
controls.fromSaveData(keyControlData, Keys);
}
else
{
useDefault = true;
}
if (useDefault)
{
2024-03-17 02:20:22 +00:00
trace('Loading default keyboard control scheme');
controls.setKeyboardScheme(Solo);
}
2023-06-22 05:41:01 +00:00
PreciseInputManager.instance.initializeKeys(controls);
}
/**
* 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
{
var useDefault = true;
if (Save.instance.hasControls(id, Gamepad(gamepad.id)))
{
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');
useDefault = false;
controls.addGamepadWithSaveData(gamepad.id, padControlData);
}
else
{
useDefault = true;
}
if (useDefault)
{
2024-03-17 02:20:22 +00:00
trace('Loading default gamepad control scheme');
controls.addDefaultGamepad(gamepad.id);
}
PreciseInputManager.instance.initializeButtons(controls, gamepad);
}
/**
* Save this player's controls to the game's persistent save.
*/
2024-03-17 02:20:22 +00:00
public function saveControls():Void
{
var keyData = controls.createSaveData(Keys);
if (keyData != null)
{
2024-03-17 02:20:22 +00:00
trace('Saving keyboard control scheme to user save');
Save.instance.setControls(id, Keys, keyData);
}
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');
Save.instance.setControls(id, Gamepad(controls.gamepadsAdded[0]), padData);
}
}
}
2020-10-21 23:33:43 +00:00
}