MiniGame System Test (TCP only)

Nach jeder Runde (jeder spieler hat einmal gewürfelt) wird ein zufälliges minigame geladen. Ist das minigame beendet (stand jz nach 1 sekunde fix) wird die Map in Ihrem vorherigen zustand neu geladen. Die Kommunikation zwischen Client und Server im Minigame erfolgt unabhängig der Kommunikation in der Map
This commit is contained in:
brausjonas
2023-04-15 03:52:15 +02:00
parent 7c0d3ac7ae
commit 412490ea85
30 changed files with 1055 additions and 229 deletions
@@ -4,12 +4,12 @@ using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
//[InitializeOnLoad]
public class AutoSetAndroidPassword
{
static AutoSetAndroidPassword()
{
PlayerSettings.keystorePass = "ACD1576BbBACD";
PlayerSettings.keyaliasPass = "ACD1576BbBACD";
}
// static AutoSetAndroidPassword()
// {
// PlayerSettings.keystorePass = "ACD1576BbBACD";
// PlayerSettings.keyaliasPass = "ACD1576BbBACD";
// }
}
+133 -37
View File
@@ -41,21 +41,57 @@ public class Client : MonoBehaviour
[SerializeField] private Image[] playerItemInfoImages;
private Dice diceScript;
public static int playerID = -1;
//Icon
[SerializeField] private Image[] playerIconInfoImages;
[SerializeField] private Sprite[] playerIconInfoImagesPrefabs;
private static Client thisClient;
//players in game text blub
[SerializeField] private TMP_Text playersInGameText;
//Scene System
public static Scene sceneMap;
//Minigames
private string[] miniGamesMapping = new string[] { "TestGame" };
private int currentMiniGameId = -1;
public static Client GetCurrentInstance()
{
return thisClient;
}
public void LoadMinigame(int id)
{
currentMiniGameId = id;
//Hide the current Scene (not the client!)
GameObject[] objectsToUnload = sceneMap.GetRootGameObjects();
foreach (GameObject g in objectsToUnload)
{
if (!g.name.Equals("EventSystem"))
{
g.SetActive(false);
}
}
SceneManager.LoadScene(miniGamesMapping[id], LoadSceneMode.Additive);
}
public void UnloadMinigame()
{
GameObject[] objectsToUnload = sceneMap.GetRootGameObjects();
foreach (GameObject g in objectsToUnload)
{
g.SetActive(true);
}
SceneManager.UnloadSceneAsync(miniGamesMapping[currentMiniGameId]);
}
private void Awake()
{
Time.fixedDeltaTime = 0.01f;
@@ -63,10 +99,12 @@ public class Client : MonoBehaviour
private void Start()
{
sceneMap = SceneManager.GetActiveScene();
diceScript = dice.GetComponent<Dice>();
Application.targetFrameRate = 60;
thisClient = this;
for (int i = 0; i < 4; i++)
@@ -85,7 +123,7 @@ public class Client : MonoBehaviour
string name = PlayerPrefs.GetString("name");
stream.Write(Encoding.ASCII.GetBytes(name), 0, 10);
int characterID = PlayerPrefs.GetInt("characterID");
stream.Write(new byte[] { (byte)characterID, 0, 0, 0, 0, 0, 0, 0, 0, 0 },0, 10);
stream.Write(new byte[] { (byte)characterID, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0, 10);
for (int i = 0; i < playerInfo.Length; i++)
{
@@ -95,6 +133,46 @@ public class Client : MonoBehaviour
new Thread(Read).Start();
}
/*
*
*
*
* Logic to load another Scene with Seperate Connection, while the current connection is holding
*/
// private bool bub = false;
// private void Update()
// {
// if (Input.GetKeyDown(KeyCode.Space) && !bub)
// {
//
// bub = true;
// GameObject[] gameObjectsToHide = sceneMap.GetRootGameObjects();
// foreach (GameObject g in gameObjectsToHide)
// {
// if (!g.name.Equals("EventSystem"))
// {
// g.SetActive(true);
// }
// }
//
// SceneManager.LoadScene("Test", LoadSceneMode.Additive);
// }
//
// else if (Input.GetKeyDown(KeyCode.Space) && bub)
// {
//
// bub = false;
// GameObject[] gameObjectsToHide = sceneMap.GetRootGameObjects();
// foreach (GameObject g in gameObjectsToHide)
// {
// g.SetActive(true);
// }
//
// SceneManager.UnloadSceneAsync("Test");
// }
// }
public void CalculatePlacement()
{
Player[] temp = new Player[playerAmount];
@@ -105,7 +183,7 @@ public class Client : MonoBehaviour
temp[j] = players[j];
samePlacement[j] = false;
}
for (int i = 0; i < temp.Length - 1; i++)
{
for (int f = 0; f < temp.Length - 1; f++)
@@ -116,9 +194,9 @@ public class Client : MonoBehaviour
}
else if (temp[f].stars == temp[f + 1].stars && temp[f].coins < temp[f + 1].coins)
{
(temp[f], temp[f + 1]) = (temp[f + 1], temp[f]);
(temp[f], temp[f + 1]) = (temp[f + 1], temp[f]);
}
else if(temp[f].stars == temp[f + 1].stars && temp[f].coins == temp[f + 1].coins)
else if (temp[f].stars == temp[f + 1].stars && temp[f].coins == temp[f + 1].coins)
{
samePlacement[f] = true;
}
@@ -147,7 +225,7 @@ public class Client : MonoBehaviour
private void FixedUpdate()
{
fpsText.text = "" + Time.frameCount / Time.time + "";
if (jobs.Count > 0)
{
Job job = jobs.Dequeue();
@@ -157,14 +235,15 @@ public class Client : MonoBehaviour
//-----Create A new player-----
case 1:
playerAmount++;
//player is playable
if (job.data[1] == 1)
{
GameObject player = Instantiate(playerPrefab[job.data[9]]);
playerIconInfoImages[job.data[5]].sprite = playerIconInfoImagesPrefabs[job.data[9]];
player.transform.position = new Vector3(job.data[2], job.data[3] + playerPrefabsHeightInfo[job.data[9]].heightOffset, job.data[4]);
player.transform.position = new Vector3(job.data[2],
job.data[3] + playerPrefabsHeightInfo[job.data[9]].heightOffset, job.data[4]);
PlayablePlayer script = player.AddComponent<PlayablePlayer>();
players[job.data[5]] = script;
@@ -176,7 +255,7 @@ public class Client : MonoBehaviour
//ITEM INFO IMAGES
Image[] temp = new Image[3];
int itemInfoImagesStartIndex = job.data[5] * 3;
int idx = 0;
for (int i = itemInfoImagesStartIndex; i < itemInfoImagesStartIndex + 3; i++)
@@ -197,7 +276,8 @@ public class Client : MonoBehaviour
GameObject player = Instantiate(playerPrefab[job.data[9]]);
playerIconInfoImages[job.data[5]].sprite = playerIconInfoImagesPrefabs[job.data[9]];
player.transform.position = new Vector3(job.data[2], job.data[3] + playerPrefabsHeightInfo[job.data[9]].heightOffset, job.data[4]);
player.transform.position = new Vector3(job.data[2],
job.data[3] + playerPrefabsHeightInfo[job.data[9]].heightOffset, job.data[4]);
NoPlayablePlayer script = player.AddComponent<NoPlayablePlayer>();
players[job.data[5]] = script;
@@ -209,7 +289,7 @@ public class Client : MonoBehaviour
//ITEM INFO IMAGES
Image[] temp = new Image[3];
int itemInfoImagesStartIndex = job.data[5] * 3;
int idx = 0;
for (int i = itemInfoImagesStartIndex; i < itemInfoImagesStartIndex + 3; i++)
@@ -221,7 +301,7 @@ public class Client : MonoBehaviour
script.itemInfoImages = temp;
//END
itemButtons[job.data[5]].interactable = false;
}
@@ -295,6 +375,7 @@ public class Client : MonoBehaviour
{
amount *= -1;
}
players[job.data[3]].AddCoins(amount);
break;
case 14:
@@ -322,10 +403,10 @@ public class Client : MonoBehaviour
break;
}
}
break;
}
uiHandler.ActivateRotator();
rotator.StartRandom();
break;
@@ -334,9 +415,19 @@ public class Client : MonoBehaviour
rotator2.Stop(job.data[1]);
Invoke("ActivateLayout1", 3.5f);
break;
case 100:
playerID = job.data[1];
LoadMinigame(job.data[2]);
break;
case 101:
Debug.Log("test");
UnloadMinigame();
break;
case 126:
stream.Write(new byte[]{126, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 10);
stream.Write(new byte[] { 126, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0, 10);
break;
case 127:
client.Close();
@@ -350,7 +441,7 @@ public class Client : MonoBehaviour
{
uiHandler.ActivateLayout1();
}
private void Activate(int player)
{
for (int i = 0; i < players.Length; i++)
@@ -389,36 +480,36 @@ public class Client : MonoBehaviour
//1 x, x = zahl
public void SendWurfeln(int zahl)
{
stream.Write(new byte[]{3, (byte)zahl, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 3, (byte)zahl, 0, 0, 0, 0, 0, 0, 0, 0 });
}
//0 4 arrow selected
//1 x, x = arrow idx
public void SendArrowSelected(int idx)
{
stream.Write(new byte[]{4, (byte)idx, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 4, (byte)idx, 0, 0, 0, 0, 0, 0, 0, 0 });
}
//0 5
public void SendFinished()
{
stream.Write(new byte[]{5, 0, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendCoinFieldAction(int action)
{
stream.Write(new byte[]{6, (byte)action, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 6, (byte)action, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendEventStopFinished()
{
stream.Write(new byte[]{7, 0, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 7, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendBuyStar()
{
stream.Write(new byte[]{8, 0, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendBuyItem(Item item)
@@ -428,22 +519,22 @@ public class Client : MonoBehaviour
public void SendActiveItem(byte index)
{
stream.Write(new byte[]{10, index, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 10, index, 0, 0, 0, 0, 0, 0, 0, 0 });
}
private void OnApplicationQuit()
{
stream.Write(new byte[]{126, 0, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 126, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
}
private void OnApplicationPause(bool pauseStatus)
{
stream.Write(new byte[]{126, 0, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 126, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendLostItem(byte index)
{
stream.Write(new byte[]{12, index, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 12, index, 0, 0, 0, 0, 0, 0, 0, 0 });
}
public void SendCoins(int amount)
@@ -454,9 +545,9 @@ public class Client : MonoBehaviour
isPositive = 0;
amount *= -1;
}
stream.Write(new byte[]{13, isPositive, (byte)amount, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 13, isPositive, (byte)amount, 0, 0, 0, 0, 0, 0, 0 });
}
/*
@@ -472,13 +563,18 @@ public class Client : MonoBehaviour
* TYPE:
* 0 = Pink Field
*/
public void SendStartRotator(int opt1, int opt2, int opt3, int opt4, int val1, int val2, int val3, int val4, int type)
public void SendStartRotator(int opt1, int opt2, int opt3, int opt4, int val1, int val2, int val3, int val4,
int type)
{
stream.Write(new byte[]{14, (byte)opt1, (byte)opt2, (byte)opt3, (byte)opt4, (byte)val1, (byte)val2, (byte)val3, (byte)val4, (byte)type});
stream.Write(new byte[]
{
14, (byte)opt1, (byte)opt2, (byte)opt3, (byte)opt4, (byte)val1, (byte)val2, (byte)val3, (byte)val4,
(byte)type
});
}
public void SendStopRotator(int stopIndex)
{
stream.Write(new byte[]{15, (byte)stopIndex, 0, 0, 0, 0, 0, 0, 0, 0});
stream.Write(new byte[] { 15, (byte)stopIndex, 0, 0, 0, 0, 0, 0, 0, 0 });
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 85174208f996d96459f4f8b2dc24eec7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,58 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class MinigamesClient : MonoBehaviour
{
private TcpClient client;
private Stream stream;
private Queue<Job> jobs = new Queue<Job>();
private void Start()
{
client = new TcpClient("185.245.96.48", 8051);
stream = client.GetStream();
stream.Write(new byte[]{5, (byte)Client.playerID, 0, 0, 0, 0, 0, 0, 0, 0});
byte[] originalRoomCode = Encoding.ASCII.GetBytes(PlayerPrefs.GetString("roomcode"));
stream.Write(originalRoomCode);
new Thread(Read).Start();
}
private void Update()
{
if (jobs.Count > 0)
{
Job job = jobs.Dequeue();
switch (job.data[0])
{
//TEST
case 1:
stream.Write(new byte[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0});
break;
}
}
}
private void Read()
{
while (true)
{
byte[] read = new byte[10];
stream.Read(read, 0, 10);
jobs.Enqueue(new Job(read));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ddf3c4ab225c68d4d8849a8a6a231bc0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: