using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
using InputDevice = UnityEngine.InputSystem.InputDevice;
using Random = UnityEngine.Random;
public class CharacterSpawn : MonoBehaviour
{
//All the Characters
public List<GameObject> characters;
public GameObject playerParent;
//All the Stages
public List<GameObject> stages;
private PlayerChoices player1Choice;
private PlayerChoices player2Choice;
private PlayerChoices player3Choice;
private PlayerChoices player4Choice;
private int x;
private PlayerChoices player;
private InputDevice keyboard = Keyboard.current;
private GameObject spawnedPlayer;
private struct PlayerChoices
{
public GameObject playerObject;
public int character;
}
public CinemachineTargetGroup targetGroup;
//Start is called before the first frame update
void Start()
{
if (PlayerPrefs.GetInt("Lives") == 0)
PlayerPrefs.SetInt("Lives", 4);
Destroy(GameObject.FindGameObjectWithTag("Delete"));
player1Choice.character = PlayerPrefs.GetInt("player1char");
player2Choice.character = PlayerPrefs.GetInt("player2char");
player3Choice.character = PlayerPrefs.GetInt("player3char");
player4Choice.character = PlayerPrefs.GetInt("player4char");
Instantiate(stages[PlayerPrefs.GetInt("Stage",0)]);
for (x = 0; x < 4; x++)
{
switch (x)
{
case 0: player = player1Choice; break;
case 1: player = player2Choice; break;
case 2: player = player3Choice; break;
case 3: player = player4Choice; break;
}
player.playerObject = characters[player.character];
bool isBot = (PlayerPrefs.GetInt("Has Bot", 0) == 1 && x == 1);
CreatePlayer(isBot);
Debug.Log("Memory used: " + UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong());
}
}
private void CreatePlayer(bool isBot)
{
/*Checks if the player wants to use the keyboard or not. (Likely will need tweaking once we test the build)
could add something in settings to opt out of keyboard use. Problem for future Johnathan.*/
//DONE
var spawnedPlayer0 = PlayerInput.Instantiate(player.playerObject, controlScheme: "Gameplay", pairWithDevice: InputSystem.GetDeviceById(PlayerPrefs.GetInt("player" + (x+1) + "deviceid")));
spawnedPlayer0.transform.parent.transform.parent = playerParent.transform;
spawnedPlayer = spawnedPlayer0.gameObject;
if (InputSystem.GetDeviceById(PlayerPrefs.GetInt("player" + (x+1) + "deviceid")).name.Contains("keyboard"))
spawnedPlayer.transform.parent.tag = "Keyboard";
else
{
spawnedPlayer.transform.parent.tag = "Controller";
}
spawnedPlayer.GetComponent<PlayerVariables>().playerNum = (x+1);
spawnedPlayer.GetComponent<PlayerVariables>().playerDeviceId = PlayerPrefs.GetInt("_player" + (x+1) + "deviceid");
spawnedPlayer.GetComponent<PlayerVariables>().colorHex = PlayerPrefs.GetString("player" + (x+1) + "color");
if (InputSystem.GetDeviceById(PlayerPrefs.GetInt("player" + (x + 1) + "deviceid")).name.Contains("Keyboard"))
spawnedPlayer.transform.parent.tag = "Keyboard";
else
{
spawnedPlayer.transform.parent.tag = "Controller";
}
spawnedPlayer.transform.position = (GameObject.Find("P" + spawnedPlayer.GetComponent<PlayerVariables>().playerNum + "Spawn").transform.position);
if(isBot)
{
//spawnedPlayer.GetComponent<PlayerInput>().enabled = false;
Destroy(spawnedPlayer.GetComponent<PlayerInput>());
}
spawnedPlayer.name = "Player: " + (x + 1);
if(spawnedPlayer.transform != null)
targetGroup.AddMember(spawnedPlayer.transform, spawnedPlayer.GetComponent<Rigidbody2D>().mass, 4);
}
}