Files

148 lines
6.5 KiB
C#
Raw Permalink Normal View History

2022-12-14 18:33:44 +02:00
using Demonics.Manager;
2022-12-14 15:24:10 +02:00
using SharedGame;
2022-12-04 19:19:07 +02:00
using System;
using UnityEngine;
2022-12-22 00:03:51 +02:00
public class GameSimulationView : MonoBehaviour, IGameView
2022-12-04 19:19:07 +02:00
{
2022-12-05 19:21:17 +02:00
[SerializeField] private Player _player = default;
2022-12-20 17:18:37 +02:00
[SerializeField] private TrainingMenu _trainingMenu = default;
2022-12-05 19:21:17 +02:00
private Player[] playerViews = Array.Empty<Player>();
private GameManager gameManager => GameManager.Instance;
2022-12-04 19:19:07 +02:00
2022-12-22 01:39:39 +02:00
private void SetGame(GameSimulation gs)
2022-12-04 19:19:07 +02:00
{
2022-12-22 01:39:39 +02:00
var playersGss = GameSimulation._players;
2022-12-06 12:46:02 +02:00
playerViews = new Player[playersGss.Length];
2022-12-05 19:21:17 +02:00
playerViews[0] = Instantiate(_player);
playerViews[1] = Instantiate(_player);
GameplayManager.Instance.InitializePlayers(playerViews[0].gameObject, playerViews[1].gameObject);
}
2022-12-04 19:19:07 +02:00
2022-12-05 19:21:17 +02:00
public void UpdateGameView(IGameRunner runner)
{
2022-12-22 01:39:39 +02:00
GameSimulation game = (GameSimulation)runner.Game;
2022-12-05 19:21:17 +02:00
GameInfo gameInfo = runner.GameInfo;
2022-12-22 01:39:39 +02:00
var playersGss = GameSimulation._players;
2022-12-08 14:35:27 +02:00
if (playerViews.Length != playersGss.Length)
2022-12-14 18:33:44 +02:00
SetGame(game);
2023-01-01 02:09:37 +02:00
if (GameSimulation.Start)
{
GameplayManager.Instance.SetupGame();
GameSimulation.Start = false;
}
GameplayManager.Instance.SetCountdown(GameSimulation.Timer);
2022-12-14 15:24:10 +02:00
for (int i = 0; i < playersGss.Length; ++i)
{
2022-12-26 03:47:56 +02:00
playerViews[i].PlayerSimulation.Simulate(playersGss[i], gameInfo.players[i]);
2022-12-15 21:13:43 +02:00
UpdateEffects(i, playersGss[i].effects);
2023-01-12 19:12:56 +02:00
UpdateProjectiles(i, playersGss[i].projectiles);
2023-01-13 15:28:18 +02:00
UpdateAssists(i, playersGss[i].shadow);
if (SceneSettings.IsTrainingMode)
{
_trainingMenu.SetState(i, playersGss[i].state);
_trainingMenu.FramedataValue(i, playersGss[i].resultAttack);
if (!playersGss[i].hitstop)
_trainingMenu.FramedataMeterValue(i, playersGss[i].framedataEnum);
}
}
if (SceneSettings.IsTrainingMode)
{
if (!playersGss[0].hitstop && !playersGss[1].hitstop)
_trainingMenu.FramedataMeterRun();
2022-12-14 15:24:10 +02:00
}
}
2022-12-15 21:13:43 +02:00
private void UpdateEffects(int index, EffectNetwork[] effects)
2022-12-14 15:24:10 +02:00
{
2022-12-15 21:13:43 +02:00
for (int i = 0; i < effects.Length; i++)
2022-12-15 13:25:22 +02:00
{
2022-12-15 21:52:59 +02:00
GameObject[] effectObjects = ObjectPoolingManager.Instance.GetPool(index, effects[i].name);
2022-12-15 21:13:43 +02:00
if (effectObjects.Length > 0)
{
for (int j = 0; j < effectObjects.Length; j++)
{
effectObjects[j].SetActive(effects[i].effectGroups[j].active);
2022-12-17 22:51:47 +02:00
if (!effectObjects[j].activeSelf)
2022-12-16 17:36:37 +02:00
{
if (effectObjects[j].TryGetComponent(out PlayerGhost playerGhost))
{
playerGhost.SetSprite(playerViews[index].PlayerAnimator.GetCurrentSprite());
}
}
2022-12-17 22:51:47 +02:00
else
{
2022-12-29 14:40:06 +02:00
effectObjects[j].transform.position = new Vector2((int)effects[i].effectGroups[j].position.x, (int)effects[i].effectGroups[j].position.y);
2022-12-17 22:51:47 +02:00
effectObjects[j].GetComponent<SpriteRenderer>().flipX = effects[i].effectGroups[j].flip;
if (!effectObjects[j].TryGetComponent(out PlayerGhost playerGhost))
{
effectObjects[j].GetComponent<DemonicsAnimator>().SetAnimation("Idle", effects[i].effectGroups[j].animationFrames);
}
}
2022-12-15 21:13:43 +02:00
}
}
2022-12-15 13:25:22 +02:00
}
2022-12-05 19:21:17 +02:00
}
2023-11-09 17:32:01 +02:00
public static void UpdateParticles(int index, EffectNetwork particle, DemonVector2 position, Vector3 flip = default)
{
GameObject[] particlesObjects = ObjectPoolingManager.Instance.GetParticlePool(index, particle.name);
if (particlesObjects.Length > 0)
{
for (int i = 0; i < particlesObjects.Length; i++)
{
if (!particlesObjects[i].activeSelf)
{
particlesObjects[i].SetActive(true);
particlesObjects[i].transform.position = new Vector2((int)position.x, (int)position.y);
2023-07-03 20:04:17 +03:00
if (flip != default)
2023-11-09 17:32:01 +02:00
particlesObjects[i].transform.localRotation = Quaternion.Euler(flip.x, flip.y, flip.z);
return;
}
}
}
}
2023-01-12 19:12:56 +02:00
private void UpdateProjectiles(int index, ProjectileNetwork[] projectiles)
{
for (int i = 0; i < projectiles.Length; i++)
{
GameObject[] projectileObjects = ObjectPoolingManager.Instance.GetProjectilePool(index, projectiles[i].name);
if (projectileObjects.Length > 0)
{
2023-01-14 19:39:35 +02:00
for (int j = 0; j < projectileObjects.Length; j++)
2023-01-12 19:12:56 +02:00
{
2023-01-14 19:39:35 +02:00
projectileObjects[j].SetActive(projectiles[i].active);
if (projectiles[i].active)
{
projectileObjects[j].transform.position = new Vector2((int)projectiles[i].position.x, (int)projectiles[i].position.y);
projectileObjects[j].GetComponent<SpriteRenderer>().flipX = projectiles[i].flip;
projectileObjects[j].GetComponent<DemonicsAnimator>().SetAnimation("Idle", projectiles[i].animationFrames);
projectileObjects[j].transform.GetChild(0).GetComponent<CollisionVisualizer>().ShowBox(projectiles[i].hitbox);
}
2023-01-12 19:12:56 +02:00
}
}
}
}
2023-01-13 15:28:18 +02:00
private void UpdateAssists(int index, ShadowNetwork shadow)
{
GameObject assistObject = ObjectPoolingManager.Instance.GetAssistPool(index, shadow.projectile.name);
if (assistObject != null)
{
assistObject.SetActive(shadow.projectile.active);
if (shadow.projectile.active)
{
assistObject.transform.position = new Vector2((int)shadow.projectile.position.x, (int)shadow.projectile.position.y);
2023-01-23 18:31:58 +02:00
assistObject.transform.right = new Vector2((float)shadow.spawnRotation.x, (float)shadow.spawnRotation.y * shadow.flip);
2023-01-13 15:28:18 +02:00
assistObject.GetComponent<SpriteRenderer>().flipX = shadow.projectile.flip;
assistObject.GetComponent<DemonicsAnimator>().SetAnimation("Idle", shadow.projectile.animationFrames);
assistObject.transform.GetChild(0).GetComponent<CollisionVisualizer>().ShowBox(shadow.projectile.hitbox);
}
}
}
2022-12-05 19:21:17 +02:00
private void Update()
{
if (gameManager.IsRunning)
UpdateGameView(gameManager.Runner);
2022-12-04 19:19:07 +02:00
}
2022-12-05 19:21:17 +02:00
}