2020-10-21 23:33:43 +00:00
|
|
|
package;
|
|
|
|
|
|
|
|
import Controls;
|
2021-03-23 02:39:35 +00:00
|
|
|
|
2020-10-21 23:33:43 +00:00
|
|
|
import flixel.FlxCamera;
|
|
|
|
import flixel.FlxG;
|
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);
|
|
|
|
|
|
|
|
#if CLEAR_INPUT_SAVE
|
|
|
|
FlxG.save.data.controls = null;
|
|
|
|
FlxG.save.flush();
|
|
|
|
#end
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (keyData != null)
|
|
|
|
{
|
|
|
|
useDefault = false;
|
|
|
|
trace("loaded key data: " + haxe.Json.stringify(keyData));
|
|
|
|
controls.fromSaveData(keyData, Keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useDefault)
|
|
|
|
controls.setKeyboardScheme(Solo);
|
2020-10-21 23:33:43 +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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (useDefault)
|
|
|
|
controls.addDefaultGamepad(gamepad.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveControls()
|
|
|
|
{
|
|
|
|
if (FlxG.save.data.controls == null)
|
|
|
|
FlxG.save.data.controls = {};
|
|
|
|
|
|
|
|
var playerData:{ ?keys:Dynamic, ?pad:Dynamic }
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyData = controls.createSaveData(Keys);
|
|
|
|
if (keyData != null)
|
|
|
|
{
|
|
|
|
playerData.keys = keyData;
|
|
|
|
trace("saving key data: " + haxe.Json.stringify(keyData));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-23 02:39:35 +00:00
|
|
|
|
|
|
|
FlxG.save.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function init():Void
|
|
|
|
{
|
|
|
|
if (player1 == null)
|
|
|
|
{
|
|
|
|
player1 = new PlayerSettings(0);
|
|
|
|
++numPlayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function onGamepadAdded(gamepad:FlxGamepad)
|
|
|
|
{
|
|
|
|
player1.addGamepad(gamepad);
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:33:43 +00:00
|
|
|
|
2021-03-23 02:39:35 +00:00
|
|
|
/*
|
|
|
|
public function setKeyboardScheme(scheme)
|
|
|
|
{
|
|
|
|
controls.setKeyboardScheme(scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function addAvatar(avatar:Player):PlayerSettings
|
2020-10-21 23:33:43 +00:00
|
|
|
{
|
2021-03-23 02:39:35 +00:00
|
|
|
var settings:PlayerSettings;
|
|
|
|
|
2020-10-21 23:33:43 +00:00
|
|
|
if (player1 == null)
|
|
|
|
{
|
|
|
|
player1 = new PlayerSettings(0, Solo);
|
|
|
|
++numPlayers;
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:39:35 +00:00
|
|
|
if (player1.avatar == null)
|
|
|
|
settings = player1;
|
|
|
|
else
|
2020-10-21 23:33:43 +00:00
|
|
|
{
|
|
|
|
if (player2 == null)
|
|
|
|
{
|
2021-03-23 02:39:35 +00:00
|
|
|
if (player1.controls.keyboardScheme.match(Duo(true)))
|
|
|
|
player2 = new PlayerSettings(1, Duo(false));
|
|
|
|
else
|
|
|
|
player2 = new PlayerSettings(1, None);
|
2020-10-21 23:33:43 +00:00
|
|
|
++numPlayers;
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:39:35 +00:00
|
|
|
if (player2.avatar == null)
|
|
|
|
settings = player2;
|
|
|
|
else
|
|
|
|
throw throw 'Invalid number of players: ${numPlayers + 1}';
|
|
|
|
}
|
|
|
|
++numAvatars;
|
|
|
|
settings.avatar = avatar;
|
|
|
|
avatar.settings = settings;
|
|
|
|
|
|
|
|
splitCameras();
|
|
|
|
|
|
|
|
onAvatarAdd.dispatch(settings);
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function removeAvatar(avatar:Player):Void
|
|
|
|
{
|
|
|
|
var settings:PlayerSettings;
|
2020-10-21 23:33:43 +00:00
|
|
|
|
2021-03-23 02:39:35 +00:00
|
|
|
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);
|
2020-10-21 23:33:43 +00:00
|
|
|
}
|
2021-03-23 02:39:35 +00:00
|
|
|
else
|
|
|
|
throw "Cannot remove avatar that is not for a player";
|
2020-10-21 23:33:43 +00:00
|
|
|
|
2021-03-23 02:39:35 +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));
|
|
|
|
}
|
|
|
|
|
|
|
|
--numAvatars;
|
|
|
|
|
|
|
|
splitCameras();
|
|
|
|
|
|
|
|
onAvatarRemove.dispatch(avatar.settings);
|
2020-10-21 23:33:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 02:39:35 +00:00
|
|
|
*/
|
|
|
|
|
2020-10-21 23:33:43 +00:00
|
|
|
static public function reset()
|
|
|
|
{
|
|
|
|
player1 = null;
|
|
|
|
player2 = null;
|
|
|
|
numPlayers = 0;
|
|
|
|
}
|
|
|
|
}
|