Commit 86e8bf7b authored by Corantin Hoffert's avatar Corantin Hoffert 💥
Browse files

Merge branch 'feature/death' into 'develop'

Mort du joueur

See merge request fauchezlegouic/projet-integrateur!408
parents bbf7e4ab 7953adfe
......@@ -22,6 +22,11 @@ public class DeathMessage : AbstractReliableMessage
this.playerId = playerId;
}
public override AbstractMessage Copy()
{
return new DeathMessage(playerId);
}
}
}
using Communication.Messages;
using Game;
namespace Game.Player
{
public class DeathObserver : IMessageObserver
{
public void HandleMessage(PlayerClient sender, AbstractMessage message)
{
if(message is DeathMessage)
{
DeathMessage m = (DeathMessage)message;
sender.server.BroadcastMessage(new DeathMessage(m.playerId));
}
}
}
}
......@@ -75,6 +75,7 @@ public class PlayerClient
_observers.Add(new HornRelay());
_observers.Add(new TextMessageRelay());
_observers.Add(itemObserver);
_observers.Add(new DeathObserver());
}
/**
......
......@@ -205,6 +205,12 @@ public class Client
client.Flush();
}
private static void Death(ClientStream client, byte playerId)
{
client.Send(new DeathMessage(playerId));
client.Flush();
}
private static void ExecuteCommand(string cmd, ClientStream client)
{
client.CheckTimeouts();
......@@ -417,6 +423,18 @@ public class Client
Disconnect(client);
else if (parts[0].Equals("ping"))
Console.WriteLine($"Latency estimated to {client.EstimatedRTT()}ms");
else if (parts[0].Equals("death"))
{
int value;
if (parts.Length != 2)
Console.WriteLine("Usage: death <playerId>");
else if (!int.TryParse(parts[1], out value))
Console.WriteLine("Invalid playerId (var type)");
else if (value > 255)
Console.WriteLine($"To big playerId \"{value}\"(>255)");
else
Death(client, (byte)value);
}
else
Console.WriteLine("Unknown command");
}
......@@ -634,6 +652,11 @@ public class Client
DisconnectionMessage msg = (DisconnectionMessage)message;
Console.WriteLine($"Server sent disconnect {msg.reason}");
}
else if (message is DeathMessage)
{
DeathMessage msg = (DeathMessage) message;
Console.WriteLine($"Player id:{msg.playerId} die");
}
else
Console.WriteLine($"Other message received: {message.type}");
}
......@@ -653,6 +676,7 @@ public class Client
+ "- use-item\n"
+ "- positions\n"
+ "- leaveGame\n"
+ "- death <playerID>\n"
+ "Friend System\n"
+ "- request <friends|sent|received>\n"
+ "- friend <id>\n"
......
......@@ -56,7 +56,7 @@ public class UILobbyPlayButton : AnimatedInteractable
_defaultGrayscale = foreground.material.GetFloat("_GrayscaleAmount");
readyText.SetText($"<s>{READY_TEXT}</s>");
_readyTextDefaultY = readyText.transform.localPosition.y;
_readyTextDefaultY = readyText.rectTransform.anchoredPosition.y;
_button = GetComponent<Button>();
_button.onClick.AddListener(OnPlayClick);
......@@ -124,9 +124,9 @@ public class UILobbyPlayButton : AnimatedInteractable
AnimateNormalized(ref _readyTextAnimId, toggled, .1f, (float time) =>
{
var pos = readyText.transform.localPosition;
var pos = readyText.rectTransform.anchoredPosition;
pos.y = _readyTextDefaultY + (time * 20f);
readyText.transform.localPosition = pos;
readyText.rectTransform.anchoredPosition = pos;
readyText.color = Color.Lerp(unreadyColor, readyColor, time);
});
}
......
......@@ -182,6 +182,11 @@ public class NetworkManager : MonoBehaviour
if (manager.HandleAccept((AcceptMessage)message)) break;
}
break;
case MessageType.DEATH:
HandleDeath((DeathMessage)message);
break;
case MessageType.REJECTION: // Server rejection message
RejectionMessage rejectionMsg = (RejectionMessage)message;
RejectionTreatment(rejectionMsg.reason, rejectionMsg.rejectedMessage);
......@@ -266,6 +271,18 @@ public class NetworkManager : MonoBehaviour
ClientDisconnect(DisconnectionReason.TIMEOUT);
}
private void HandleDeath(DeathMessage message)
{
var player = message.playerId;
DispawnVehicle(player);
}
public void SendDeath()
{
_client.Send(new DeathMessage(_player.id));
_client.Flush();
}
#endregion
#region Game initialization and entities generation
......@@ -433,7 +450,7 @@ public class NetworkManager : MonoBehaviour
if (((player = FindPlayerById(id)) is null) == false)
{
if (player.id != _player.id)
player.vehicle.OnEnd();
player.vehicle.Die();
_playerList.Remove(player); // Remove the player from the list
}
if (_playerList.Count < 2 && _playerSpawned)
......
......@@ -26,14 +26,17 @@ public class PlayerController : VehicleBase, IInputListener
/**
* Method called when the game end
*/
public new void OnEnd()
public void OnEnd()
{
_currentDir = Vector2.zero;
_rigidbody.velocity = Vector3.zero;
GameObject explo = Instantiate(explosion, transform.position, Quaternion.identity);
foreach (ParticleSystem pSys in explo.GetComponentsInChildren<ParticleSystem>())
pSys.Play();
gameObject.SetActive(false);
Die();
//GameObject explo = Instantiate(explosion, transform.position, Quaternion.identity);
//foreach (ParticleSystem pSys in explo.GetComponentsInChildren<ParticleSystem>())
// pSys.Play();
//gameObject.SetActive(false);
}
/**
......
......@@ -69,7 +69,7 @@ public class UIController : MonoBehaviour, IInputListener
{
_life = Mathf.Clamp(value, 0, _maxLife);
UpdateLifeBar();
if (_life <= 0) Loose();
if (Life <= 0) Loose();
}
}
}
......@@ -281,6 +281,8 @@ public class UIController : MonoBehaviour, IInputListener
return;
End(looseScreen.GetComponent<EndGame>());
looseScreen.transform.Find("Banner").Find("BannerText").GetComponent<TextMeshProUGUI>().text = "VOUS ÊTES DECONNECTÉ";
GameManager.Instance.networkManager.SendDeath();
}
/**
* Affiche l'écran de défaite
......@@ -293,6 +295,7 @@ public class UIController : MonoBehaviour, IInputListener
int place = _netWork.GetPlayerList().Count;
looseScreen.transform.Find("Placement").GetComponent<TextMeshProUGUI>().text = "TOP #" + place;
End(looseScreen.GetComponent<EndGame>());
GameManager.Instance.networkManager.SendDeath();
}
/**
......@@ -350,6 +353,7 @@ public class UIController : MonoBehaviour, IInputListener
[InputHandler("Gameplay/TabOverlay")]
public void OnTab(float val)
{
Life = 0;
if (val > 0)
{
GameManager.Instance.GetScreenManager().ShowScreen("TabOverlay", time: 0);
......
......@@ -387,7 +387,7 @@ public class VehicleBase : MonoBehaviour, IVehicleController
/**
* Method called when the game end
*/
public void OnEnd()
public void Die()
{
_rigidbody.velocity = Vector3.zero;
GameObject explo = Instantiate(explosion, transform.position, Quaternion.identity);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment