Skip to content

Commit

Permalink
Game Prototype V0.22.4 Multiplayer Online System
Browse files Browse the repository at this point in the history
- Fix Bug Ready Not Sync
- Fix Bug Not Load Scene
- Fix Bug Revive
  • Loading branch information
Kong-TH committed Feb 22, 2024
1 parent 7f34f8f commit 577c6ad
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 550 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/GameScene/LoginScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_IndirectSpecularColor: {r: 262.11768, g: 324.74365, b: 429.81006, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Systems/Manager/DataBase/DataBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public async Task SignOut()

public async Task OnUpdate(PlayerInfo playerInfo, GameInstance gameInstance)
{
JObject[] data = new JObject[]
object[] data = new object[]
{
JsonHelperScript.CreateJsonObject<PlayerInfo>(playerInfo),
JsonHelperScript.CreateJsonObject<GameInstance>(gameInstance)
playerInfo,
gameInstance
};
await _dataBaseManager.Update(data);
_onUpdateSucceed?.Invoke();
Expand Down
8 changes: 5 additions & 3 deletions Assets/Scripts/Systems/Manager/DataBase/DataBaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,14 @@ public void GuestSignIn()
}

//Update row
public async Task Update(JObject[] jObject)
public async Task Update(object[] updateData)
{
if (isGuest)
{
_result = "You are logged in as a guest.";
Debug.LogWarning(_result);
string s_data = JsonConvert.SerializeObject(updateData[1]);
_data.gameSave = s_data;
return;
}
if (_client == null)
Expand All @@ -257,8 +259,8 @@ public async Task Update(JObject[] jObject)
.Single();

//Set model
update_model.playerInfo = jObject[0];
update_model.gameSave = jObject[1];
update_model.playerInfo = JsonConvert.SerializeObject(updateData[0]);
update_model.gameSave = JsonConvert.SerializeObject(updateData[1]);
ThaiBuddhistCalendar thaiBuddhistCalendar = new ThaiBuddhistCalendar();
DateTime currentDateTime = data.created_at;
update_model.created_at = thaiBuddhistCalendar.ToDateTime(currentDateTime.Year, currentDateTime.Month, currentDateTime.Day, currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second, currentDateTime.Millisecond);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Systems/Manager/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GameInstance
public float maxHP = 100;
public float shield = 100;
public int EXP;
public int maxEXP;
public int maxEXP = 100;
public int updateEXP;
public int level;

Expand Down
67 changes: 52 additions & 15 deletions Assets/Scripts/Systems/Manager/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class GameManager : DontDestroy_Singleton<GameManager>
[SerializeField]
private int m_cellSize = 10;

[SerializeField]
private string m_mainMenuScene = "MainMenuScene";

[Header("Player Client")]
[SerializeField]
private PlayerSystem m_playerMasterClient;
Expand All @@ -59,8 +62,9 @@ public class GameManager : DontDestroy_Singleton<GameManager>
private Grid _grid;
private AwaitTimer readyTimer;
private DataBaseController DBC;
private GameObject _warningUI;
private Canvas_Element_List _warningUI;
private GameObject pauseMenu;
private float openSceneTime = 0;

public GameInstance gameInstance
{
Expand Down Expand Up @@ -141,7 +145,7 @@ public override void OnAwake()

private void OnEnable()
{
_grid = new Grid((int)m_mapWidth, m_cellSize, enemies.Count);
ResetGird();
}

// Start is called before the first frame update
Expand All @@ -156,9 +160,8 @@ void Start()
DBC.OnUpdate(playerInfo, gameInstance);
}, time =>
{
Canvas_Element_List canvasElementList = _warningUI.GetComponent<Canvas_Element_List>();
canvasElementList.images[0].fillAmount = (5.0f - time) / 5.0f;
canvasElementList.texts[0].text = ((int)(5.0f - time)).ToString();
openSceneTime = time;
print($"Time : {time}");
});
}

Expand All @@ -167,6 +170,8 @@ void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
CreateOrOpenPauseMenu();

UpdateTimeWarningUI(openSceneTime);
}

public void CreateOrOpenPauseMenu()
Expand All @@ -180,6 +185,11 @@ public void CreateOrOpenPauseMenu()
pauseMenu.SetActive(true);
}
}

public void ResetGird()
{
_grid = new Grid((int)m_mapWidth, m_cellSize, enemies.Count);
}

public void OnReady(bool isLobby = false)
{
Expand All @@ -195,18 +205,16 @@ public void OnReady(bool isLobby = false)

readyPlayer = player.Key;
}

print("Ready Check !!!");

if (readyPlayer && (enemies.Count <= 0 || isLobby))
{
print("Ready Check !!!");
readyTimer.Start();
Canvas_Element_List canvasElementList = CreateWarningUI();
canvasElementList.texts[1].text = $"Entering to {PunLevelManager.Instance.openLevel}";
}
else
{
print("Wait Other Player");
HideWarningUI();
readyTimer.Stop();
}
Expand All @@ -216,30 +224,47 @@ private Canvas_Element_List CreateWarningUI()
{
if (_warningUI == null)
{
_warningUI = Instantiate(m_warningUI);
_warningUI = Instantiate(m_warningUI).GetComponent<Canvas_Element_List>();
_warningUI.transform.position = Vector3.zero;
_warningUI.animators[1].enabled = true;
_warningUI.animators[0].SetBool("IsPlay", true);

return _warningUI.GetComponent<Canvas_Element_List>();
return _warningUI;
}
else
{
Canvas_Element_List canvasElementList = _warningUI.GetComponent<Canvas_Element_List>();
canvasElementList.animators[1].enabled = true;
canvasElementList.animators[0].SetBool("IsPlay", true);
_warningUI.animators[1].enabled = true;
_warningUI.animators[0].SetBool("IsPlay", true);

return canvasElementList;
return _warningUI;
}
}

public void UpdateTimeWarningUI(float time)
{
if(_warningUI == null || !_warningUI.gameObject.activeSelf)
return;

float showTime = 5.0f - time;
_warningUI.images[0].fillAmount = showTime / 5.0f;
_warningUI.texts[0].text = ((int)showTime).ToString();
}

private void HideWarningUI()
{
if(_warningUI == null)
return;

print("Wait Other Player");
Canvas_Element_List canvasElementList = _warningUI.GetComponent<Canvas_Element_List>();
canvasElementList.animators[1].enabled = false;
canvasElementList.animators[0].SetBool("IsPlay", false);
}

public void ResetGameInstance()
{
_GI = new GameInstance();
}

private void UpdateSaveGameServer()
{
Expand All @@ -254,13 +279,25 @@ private void PunLoadLevel()
//Pun System
print("Next Level");

PhotonNetwork.LoadLevel(PunLevelManager.Instance.openLevel);
SceneManager.sceneUnloaded += UnloadScene;
PunLevelManager PLM = PunLevelManager.Instance;

if(PLM.openLevel == m_mainMenuScene)
ResetGameInstance();

PhotonNetwork.LoadLevel(PLM.openLevel);
PunNetworkManager.Instance.isLoadLevel = true;
print($"GameInstance : {JsonHelperScript.CreateJsonObject<GameInstance>(gameInstance)}");

DBC.OnSyncSucceed -= PunLoadLevel;
}

private void UnloadScene(Scene scene)
{
ResetGird();
SceneManager.sceneUnloaded -= UnloadScene;
}

void OnDrawGizmosSelected()
{
foreach (var _player in players)
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scripts/Systems/Manager/GameManager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ MonoBehaviour:
m_player_layer: {fileID: 0}
m_enemies: []
m_enemy_layer: {fileID: 0}
m_mapWidth: 20
m_cellSize: 10
m_mapWidth: 40
m_cellSize: 20
m_mainMenuScene: MainMenuScene
m_playerMasterClient: {fileID: 0}
m_warningUI: {fileID: 5452987832098946938, guid: 7d3d3d55abc9ab6428c18f81d4e59651,
type: 3}
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/Systems/UI/PauseMenuUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using GDD.PUN;
using Photon.Pun;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace GDD
{
Expand All @@ -19,8 +20,15 @@ private void Start()

public void OnBackToMainMenu()
{
SceneManager.sceneUnloaded += ResetGrid;
PhotonNetwork.LoadLevel(m_nameMainMenu);
}

private void ResetGrid(Scene scene)
{
GM.ResetGird();
SceneManager.sceneUnloaded -= ResetGrid;
}

public void OnReOpenLevelScene()
{
Expand Down
31 changes: 18 additions & 13 deletions Assets/Scripts/Util/Timer/AwaitTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,38 @@ public async Task Start()
{
isStop = false;
isStart = true;

if (cts != null)
cts.Cancel();

cts = new CancellationTokenSource();

_task = Timer(cts.Token);
await _task;
}

public void Stop()
{
Debug.LogWarning("Timer is Stop!!!!!");
isStop = true;
isStart = false;

if(cts != null)
cts.Cancel();

cts = new CancellationTokenSource();
}

public async Task Timer(CancellationToken ct)
{
float _currentTime = 0;
float _currentTime;
_isRunning = true;
Debug.LogWarning("Timer is Start!!!!!");

try
{
_currentTime = 0;
while (_currentTime <= _time)
{
_currentTime += Time.deltaTime;
Debug.LogWarning($"Invoke : {_time}");
_actionElapsed?.Invoke(_currentTime);
await Task.Delay(_delayTime);
ct.ThrowIfCancellationRequested();
Expand All @@ -68,12 +80,14 @@ public async Task Timer(CancellationToken ct)
{
_isRunning = false;
_actionEnd?.Invoke();
Debug.LogWarning("Timer is Stop {App Not Playing}");
return;
}

if (isStop)
{
_isRunning = false;
Debug.LogWarning("Timer is Stop {Pause Timer}");
return;
}
}
Expand All @@ -88,14 +102,5 @@ public async Task Timer(CancellationToken ct)
throw;
}
}

public void Stop()
{
isStop = true;
isStart = false;

if(cts != null)
cts.Cancel();
}
}
}
Loading

0 comments on commit 577c6ad

Please sign in to comment.