1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-14 02:42:55 +00:00
Funkin/source/PlayerSettings.hx

250 lines
5.6 KiB
Haxe
Raw Normal View History

2020-10-21 23:33:43 +00:00
package;
import Controls;
import flixel.FlxCamera;
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;
// import ui.DeviceManager;
// import props.Player;
class PlayerSettings
{
static public var numPlayers(default, null) = 0;
static public var numAvatars(default, null) = 0;
static public var player1(default, null):PlayerSettings;
static public var player2(default, null):PlayerSettings;
2021-03-22 13:40:32 +00:00
static public var onAvatarAdd(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
static public var onAvatarRemove(default, null) = new FlxTypedSignal<PlayerSettings->Void>();
2020-10-21 23:33:43 +00:00
public var id(default, null):Int;
2021-03-22 13:40:32 +00:00
public var controls(default, null):Controls;
2020-10-21 23:33:43 +00:00
// public var avatar:Player;
// public var camera(get, never):PlayCamera;
2021-03-23 02:39:35 +00:00
function new(id)
2020-10-21 23:33:43 +00:00
{
this.id = id;
2021-03-23 02:39:35 +00:00
this.controls = new Controls('player$id', None);
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
#if CLEAR_INPUT_SAVE
FlxG.save.data.controls = null;
FlxG.save.flush();
#end
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
var useDefault = true;
var controlData = FlxG.save.data.controls;
if (controlData != null)
{
var keyData:Dynamic = null;
if (id == 0 && controlData.p1 != null && controlData.p1.keys != null)
keyData = controlData.p1.keys;
else if (id == 1 && controlData.p2 != null && controlData.p2.keys != null)
keyData = controlData.p2.keys;
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
if (keyData != null)
{
useDefault = false;
trace("loaded key data: " + haxe.Json.stringify(keyData));
controls.fromSaveData(keyData, Keys);
}
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
if (useDefault)
controls.setKeyboardScheme(Solo);
2020-10-21 23:33:43 +00:00
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
function addGamepad(gamepad:FlxGamepad)
2020-10-21 23:33:43 +00:00
{
2021-03-23 02:39:35 +00:00
var useDefault = true;
var controlData = FlxG.save.data.controls;
if (controlData != null)
2020-10-21 23:33:43 +00:00
{
2021-03-23 02:39:35 +00:00
var padData:Dynamic = null;
if (id == 0 && controlData.p1 != null && controlData.p1.pad != null)
padData = controlData.p1.pad;
else if (id == 1 && controlData.p2 != null && controlData.p2.pad != null)
padData = controlData.p2.pad;
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
if (padData != null)
2020-10-21 23:33:43 +00:00
{
2021-03-23 02:39:35 +00:00
useDefault = false;
trace("loaded pad data: " + haxe.Json.stringify(padData));
controls.addGamepadWithSaveData(gamepad.id, padData);
2020-10-21 23:33:43 +00:00
}
2021-03-23 02:39:35 +00:00
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
if (useDefault)
controls.addDefaultGamepad(gamepad.id);
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
public function saveControls()
{
if (FlxG.save.data.controls == null)
FlxG.save.data.controls = {};
2021-06-23 08:15:44 +00:00
var playerData:{?keys:Dynamic, ?pad:Dynamic}
2021-03-23 02:39:35 +00:00
if (id == 0)
{
if (FlxG.save.data.controls.p1 == null)
FlxG.save.data.controls.p1 = {};
playerData = FlxG.save.data.controls.p1;
}
else
{
if (FlxG.save.data.controls.p2 == null)
FlxG.save.data.controls.p2 = {};
playerData = FlxG.save.data.controls.p2;
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
var keyData = controls.createSaveData(Keys);
if (keyData != null)
{
playerData.keys = keyData;
trace("saving key data: " + haxe.Json.stringify(keyData));
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
if (controls.gamepadsAdded.length > 0)
{
var padData = controls.createSaveData(Gamepad(controls.gamepadsAdded[0]));
if (padData != null)
2020-10-21 23:33:43 +00:00
{
2021-03-23 02:39:35 +00:00
trace("saving pad data: " + haxe.Json.stringify(padData));
playerData.pad = padData;
2020-10-21 23:33:43 +00:00
}
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
FlxG.save.flush();
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
static public function init():Void
{
if (player1 == null)
{
player1 = new PlayerSettings(0);
++numPlayers;
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
FlxG.gamepads.deviceConnected.add(onGamepadAdded);
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
var numGamepads = FlxG.gamepads.numActiveGamepads;
for (i in 0...numGamepads)
2020-10-21 23:33:43 +00:00
{
2021-03-23 02:39:35 +00:00
var gamepad = FlxG.gamepads.getByID(i);
if (gamepad != null)
onGamepadAdded(gamepad);
}
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
// player1.controls.addDefaultGamepad(0);
// }
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
// if (numGamepads > 1)
// {
// if (player2 == null)
// {
// player2 = new PlayerSettings(1, None);
// ++numPlayers;
// }
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
// var gamepad = FlxG.gamepads.getByID(1);
// if (gamepad == null)
// throw 'Unexpected null gamepad. id:0';
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
// player2.controls.addDefaultGamepad(1);
// }
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
// DeviceManager.init();
}
2021-06-23 08:15:44 +00:00
2021-03-23 02:39:35 +00:00
static function onGamepadAdded(gamepad:FlxGamepad)
{
player1.addGamepad(gamepad);
}
2020-10-21 23:33:43 +00:00
2021-03-23 02:39:35 +00:00
/*
2021-06-23 08:15:44 +00:00
public function setKeyboardScheme(scheme)
2020-10-21 23:33:43 +00:00
{
2021-06-23 08:15:44 +00:00
controls.setKeyboardScheme(scheme);
2020-10-21 23:33:43 +00:00
}
2021-06-23 08:15:44 +00:00
static public function addAvatar(avatar:Player):PlayerSettings
2020-10-21 23:33:43 +00:00
{
2021-06-23 08:15:44 +00:00
var settings:PlayerSettings;
if (player1 == null)
2020-10-21 23:33:43 +00:00
{
2021-06-23 08:15:44 +00:00
player1 = new PlayerSettings(0, Solo);
2020-10-21 23:33:43 +00:00
++numPlayers;
}
2021-06-23 08:15:44 +00:00
if (player1.avatar == null)
settings = player1;
2021-03-23 02:39:35 +00:00
else
2021-06-23 08:15:44 +00:00
{
if (player2 == null)
{
if (player1.controls.keyboardScheme.match(Duo(true)))
player2 = new PlayerSettings(1, Duo(false));
else
player2 = new PlayerSettings(1, None);
++numPlayers;
}
if (player2.avatar == null)
settings = player2;
else
throw throw 'Invalid number of players: ${numPlayers + 1}';
}
++numAvatars;
settings.avatar = avatar;
avatar.settings = settings;
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
splitCameras();
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
onAvatarAdd.dispatch(settings);
2020-10-21 23:33:43 +00:00
2021-06-23 08:15:44 +00:00
return settings;
2020-10-21 23:33:43 +00:00
}
2021-06-23 08:15:44 +00:00
static public function removeAvatar(avatar:Player):Void
2021-03-23 02:39:35 +00:00
{
2021-06-23 08:15:44 +00:00
var settings:PlayerSettings;
if (player1 != null && player1.avatar == avatar)
settings = player1;
else if (player2 != null && player2.avatar == avatar)
{
settings = player2;
if (player1.controls.keyboardScheme.match(Duo(_)))
player1.setKeyboardScheme(Solo);
}
else
throw "Cannot remove avatar that is not for a player";
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
settings.avatar = null;
while (settings.controls.gamepadsAdded.length > 0)
{
final id = settings.controls.gamepadsAdded.shift();
settings.controls.removeGamepad(id);
DeviceManager.releaseGamepad(FlxG.gamepads.getByID(id));
}
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
--numAvatars;
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
splitCameras();
2020-10-21 23:33:43 +00:00
2021-06-23 08:15:44 +00:00
onAvatarRemove.dispatch(avatar.settings);
}
2021-03-23 02:39:35 +00:00
2021-06-23 08:15:44 +00:00
*/
2020-10-21 23:33:43 +00:00
static public function reset()
{
player1 = null;
player2 = null;
numPlayers = 0;
}
}