Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Add multiplayer nicknames over connected players
Browse files Browse the repository at this point in the history
  • Loading branch information
ForLoveOfCats committed Feb 21, 2019
1 parent 56ec3b1 commit e706c78
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Game : Node

public static int MaxPlayers = 8;
public static bool BindsEnabled = false;
public static System.Collections.Generic.Dictionary<int, Spatial> PlayerList = new System.Collections.Generic.Dictionary<int, Spatial>();
public static Player PossessedPlayer = ((PackedScene)GD.Load("res://Player/Player.tscn")).Instance() as Player;
public static Dictionary<int, Spatial> PlayerList = new Dictionary<int, Spatial>();
public static Player PossessedPlayer = GD.Load<PackedScene>("res://Player/Player.tscn").Instance() as Player;
//Prevent crashes when player movement commands are run when world is not initalized

public static bool WorldOpen = false;
Expand Down
6 changes: 6 additions & 0 deletions Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public void RecieveNick(int Id, string NickArg)
{
Nicknames[Id] = NickArg;

if(Id != GetTree().GetNetworkUniqueId())
{
Game.PossessedPlayer.HUDInstance.AddNickLabel(Id, NickArg);
}

if(GetTree().IsNetworkServer())
{
foreach(KeyValuePair<int, string> Entry in Nicknames)
Expand All @@ -175,6 +180,7 @@ public void _PlayerDisconnected(int Id)
if(Nicknames.ContainsKey(Id))
{
Nicknames.Remove(Id);
Game.PossessedPlayer.HUDInstance.RemoveNickLabel(Id);
}
}

Expand Down
4 changes: 4 additions & 0 deletions Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Player : KinematicBody

public int BuildRotation = 0;

public Camera Cam;

public HUD HUDInstance;
private Ghost GhostInstance;

Expand All @@ -69,6 +71,8 @@ public class Player : KinematicBody

public override void _Ready()
{
Cam = GetNode<Camera>("SteelCamera");

PositionReset();

if(Possessed)
Expand Down
39 changes: 39 additions & 0 deletions UI/HUD.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Godot;
using System.Collections.Generic;


public class HUD : Node
{
private Texture Alpha;
private Texture Triangle;
private PackedScene NickLabelScene;

private Dictionary<int, Label> NickLabels = new Dictionary<int, Label>();

private TextureRect Crosshair;

Expand All @@ -14,6 +18,7 @@ public class HUD : Node

Alpha = GD.Load("res://UI/Textures/Alpha.png") as Texture;
Triangle = GD.Load("res://UI/Textures/Triangle.png") as Texture;
NickLabelScene = GD.Load<PackedScene>("res://UI/NickLabel.tscn");
}


Expand Down Expand Up @@ -91,10 +96,44 @@ public override void _Ready()
}


public void AddNickLabel(int Id, string Nick)
{
Label Instance = NickLabelScene.Instance() as Label;
Instance.Text = Nick;
AddChild(Instance);
NickLabels[Id] = Instance;
}


public void RemoveNickLabel(int Id)
{
if(NickLabels.ContainsKey(Id))
{
NickLabels[Id].QueueFree();
NickLabels.Remove(Id);
}
}


public override void _Process(float Delta)
{
Crosshair.Visible = !Menu.IsOpen;

foreach(KeyValuePair<int, Label> Current in NickLabels)
{
Vector3 PlayerPos = Game.PlayerList[Current.Key].Translation + new Vector3(0,7.5f,0);
if(Game.PossessedPlayer.Cam.IsPositionBehind(PlayerPos))
{
Current.Value.Visible = false;
}
else
{
Current.Value.Visible = true;
Current.Value.MarginLeft = Game.PossessedPlayer.Cam.UnprojectPosition(PlayerPos).x - Current.Value.RectSize.x/2;
Current.Value.MarginTop = Game.PossessedPlayer.Cam.UnprojectPosition(PlayerPos).y - Current.Value.RectSize.y/2;
}
}

//TODO save these to private members in _Ready and clear up this mess
((Label)(GetNode("CLayer").GetNode("FPSLabel"))).SetText(Engine.GetFramesPerSecond().ToString() + " fps");
((Label)(GetNode("CLayer").GetNode("PlayerPosition"))).SetText("Player Position: " + Game.PossessedPlayer.Translation.Round().ToString());
Expand Down
13 changes: 13 additions & 0 deletions UI/NickLabel.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://UI/Fonts/OverpassSemibold.tres" type="DynamicFont" id=1]

[node name="Label" type="Label"]
margin_right = 202.0
margin_bottom = 24.0
size_flags_vertical = 0
custom_fonts/font = ExtResource( 1 )
custom_colors/font_color = Color( 0, 0.670588, 0.121569, 1 )
text = "Default Label Text"
align = 1

0 comments on commit e706c78

Please sign in to comment.