Skip to content

Commit

Permalink
Fixed Speedometer on events and creators + Fixed HUD on creators
Browse files Browse the repository at this point in the history
  • Loading branch information
LDami committed Jul 14, 2022
1 parent 46e3aa9 commit 4a17e58
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 159 deletions.
2 changes: 1 addition & 1 deletion SampSharpGameMode1/Display/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class HUD
{
public bool HasError { get; private set; }

private TextdrawLayer layer;
protected TextdrawLayer layer;
/// <summary>
/// Create a HUD for a player from a JSON file
/// </summary>
Expand Down
61 changes: 38 additions & 23 deletions SampSharpGameMode1/Display/Speedometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SampSharp.GameMode.World;
using SampSharp.GameMode.SAMP;
using SampSharp.GameMode.World;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -7,6 +8,7 @@ namespace SampSharpGameMode1.Display
{
public class Speedometer
{
public bool IsDisplayed { get; private set; }
private HUD hud;
private Player player;
private int health_icon_index = 0; // 0 does not exist: means that the icon is not displayed yet
Expand All @@ -25,14 +27,13 @@ public void Show()
Hide();
if (!hud.HasError)
{
hud.SetText("vehiclename", "Unknown model");
hud.SetText("speed", "0 km/h");
hud.SetText("health", "0 %");
hud.SetText("vehiclename", player.Vehicle?.ModelInfo.Name ?? "Unknown model");
hud.Show();
hud.Hide("health_icon");
IsDisplayed = true;
}
else
Logger.WriteLineAndClose("has error");
}

public void Hide()
Expand All @@ -41,36 +42,50 @@ public void Hide()
{
hud.Hide();
}
IsDisplayed = false;
}

public void Update()
{
double vel = Math.Sqrt(player.Vehicle.Velocity.LengthSquared) * 181.5;
hud.SetText("speed", vel.ToString(@"N0") + " km/h");
hud.SetText("health", (player.Vehicle.Health / 10).ToString(@"N0") + " %");
if (player.Vehicle.Health < 300)
if(IsDisplayed)
{
if(health_icon_index == 0)
string model = player.Vehicle?.ModelInfo.Name ?? "Unknown model";
if (model.Length > 11)
{
health_icon_index = 1;
hud.SetText("health_icon", "LD_DUAL:ex1");
hud.Show("health_icon");
health_icon_lastUpdate = DateTime.Now;
model = model.Insert(11, "\n");
}
if((DateTime.Now - health_icon_lastUpdate).TotalMilliseconds > health_icon_delay)
hud.SetText("vehiclename", model);

double vel = Math.Sqrt(player.Vehicle.Velocity.LengthSquared) * 181.5;
hud.SetText("speed", vel.ToString(@"N0") + " km/h");

hud.SetText("health", (player.Vehicle.Health / 10).ToString(@"N0") + " %");
if (player.Vehicle.Health < 250)
{
health_icon_index++;
if (health_icon_index > 4)
hud.SetColor("health", Color.Red);
if (health_icon_index == 0)
{
health_icon_index = 1;
hud.SetText("health_icon", "LD_DUAL:ex" + health_icon_index);
hud.SetText("health_icon", "LD_DUAL:ex1");
hud.Show("health_icon");
health_icon_lastUpdate = DateTime.Now;
}
if ((DateTime.Now - health_icon_lastUpdate).TotalMilliseconds > health_icon_delay)
{
health_icon_index++;
if (health_icon_index > 4)
health_icon_index = 1;
hud.SetText("health_icon", "LD_DUAL:ex" + health_icon_index);
}
}
}
else
{
if(health_icon_index != 0)
else
{
hud.Hide("health_icon");
health_icon_index = 0;
hud.SetColor("health", Color.White);
if (health_icon_index != 0)
{
hud.Hide("health_icon");
health_icon_index = 0;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SampSharpGameMode1/Display/Textdraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct textdraw
public Color BackColor;
public string Type;
}
class Textdraw : PlayerTextDraw
public class Textdraw : PlayerTextDraw
{
public string name;
public Vector2 position = Vector2.Zero;
Expand Down
15 changes: 1 addition & 14 deletions SampSharpGameMode1/Display/TextdrawLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SampSharpGameMode1.Display
{
class TextdrawLayer
public class TextdrawLayer
{
private Color editingColor = new Color(180, 50, 50);

Expand Down Expand Up @@ -81,35 +81,23 @@ public void Show(string name)
if (!textdrawList.ContainsKey(name))
throw new TextdrawNameNotFoundException(name);
textdrawList[name].Show();
#if DEBUG
Logger.WriteLineAndClose("TextdrawLayer.cs - TextdrawLayer.Show:I: Showing " + name);
#endif
}
public void ShowAll()
{
foreach (KeyValuePair<string, Textdraw> td in textdrawList)
td.Value.Show();
#if DEBUG
Logger.WriteLineAndClose("TextdrawLayer.cs - TextdrawLayer.Show:I: Showing all");
#endif
}
public void Hide(string name)
{
if (!textdrawList.ContainsKey(name))
throw new TextdrawNameNotFoundException(name);
textdrawList[name].Hide();
#if DEBUG
Logger.WriteLineAndClose("TextdrawLayer.cs - TextdrawLayer.Show:I: Hiding " + name);
#endif
}

public void HideAll()
{
foreach (KeyValuePair<string, Textdraw> td in textdrawList)
td.Value.Hide();
#if DEBUG
Logger.WriteLineAndClose("TextdrawLayer.cs - TextdrawLayer.Show:I: Hiding all");
#endif
}
public void Destroy()
{
Expand Down Expand Up @@ -158,7 +146,6 @@ public void Resize(string name, Vector2 offset)
throw new TextdrawNameNotFoundException(name);
textdrawList[name].Width += offset.X;
textdrawList[name].Height += offset.Y;
Console.WriteLine("new height: " + textdrawList[name].Height);
this.UpdateTextdraw(name);
}
public Vector2 GetTextdrawPosition(string name)
Expand Down
7 changes: 5 additions & 2 deletions SampSharpGameMode1/Events/Derbys/Derby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void Prepare(List<EventSlot> slots)
playersLiveInfoHUD[slot.Player] = new HUD(slot.Player, "derbyhud.json");
playersLiveInfoHUD[slot.Player].Hide("iconrockets");
playersLiveInfoHUD[slot.Player].Hide("remainingrockets");
playersLiveInfoHUD[slot.Player].SetText("remainingplayers", "Remaining players: " + players.Count + "/" + players.Count);
playersLiveInfoHUD[slot.Player].SetText("remainingplayers", slots.Count.ToString(@"000"));

slot.Player.VirtualWorld = virtualWorld;

Expand Down Expand Up @@ -382,7 +382,9 @@ public void OnPlayerFinished(Player player, string reason)
}

players.Remove(player);
if(players.Count == 0) // Si c'est le vainqueur
foreach(Player p in players)
playersLiveInfoHUD[p].SetText("remainingplayers", players.Count.ToString(@"000"));
if (players.Count == 0) // Si c'est le vainqueur
{
SampSharp.GameMode.SAMP.Timer ejectionTimer = new SampSharp.GameMode.SAMP.Timer(2000, false);
ejectionTimer.Tick += (object sender, EventArgs e) =>
Expand Down Expand Up @@ -418,6 +420,7 @@ public void OnPlayerFinished(Player player, string reason)
}
public void Eject(Player player)
{
playersLiveInfoHUD[player].Hide();
players.RemoveAll(x => x.Equals(player));
spectatingPlayers.RemoveAll(x => x.Equals(player));

Expand Down
79 changes: 17 additions & 62 deletions SampSharpGameMode1/Events/Derbys/DerbyCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,15 @@ namespace SampSharpGameMode1.Events.Derbys
{
public class DerbyCreator : EventCreator
{
class HUD
class HUD : Display.HUD
{
TextdrawLayer layer;
public HUD(Player player)
public HUD(Player player) : base(player, "derbycreator.json")
{
layer = new TextdrawLayer();
string filename = Directory.GetCurrentDirectory() + "\\scriptfiles\\derbycreator.json";
string jsonData = "";
if (File.Exists(filename))
{
try
{
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
{
byte[] output = new byte[fs.Length];
int idx = 0;
int blockLength = 1;
byte[] tmp = new byte[blockLength];
int readBytes;
while ((readBytes = fs.Read(tmp, 0, blockLength)) > 0)
{
for (int i = 0; i < readBytes; i++)
output[idx + i] = tmp[i];
idx += readBytes;
}
jsonData = new UTF8Encoding(true).GetString(output);
List<textdraw> textdraws = JsonConvert.DeserializeObject<List<textdraw>>(jsonData);
foreach (textdraw textdraw in textdraws)
{
if (textdraw.Type.Equals("box"))
{
layer.CreateTextdraw(player, textdraw.Name, TextdrawLayer.TextdrawType.Box);
layer.SetTextdrawPosition(textdraw.Name, new Vector2(textdraw.PosX, textdraw.PosY));
layer.SetTextdrawSize(textdraw.Name, textdraw.Width, textdraw.Height);
}
if (textdraw.Type.Equals("text"))
{
layer.CreateTextdraw(player, textdraw.Name, TextdrawLayer.TextdrawType.Text);
layer.SetTextdrawPosition(textdraw.Name, new Vector2(textdraw.PosX, textdraw.PosY));
}
}
layer.SetTextdrawText("derbynamelabel", "Derby Name:");
layer.SetTextdrawText("derbyname", "None");
layer.SetTextdrawText("selectedidx", "Spawn nbr:");
layer.SetTextdrawText("editingmode", "Mode: None");
layer.UnselectAllTextdraw();
fs.Close();
}
}
catch (IOException e)
{
Logger.WriteLineAndClose("DerbyCreator.cs - DerbyCreator.HUD._:E: Cannot load Derby Creator HUD:");
Logger.WriteLineAndClose(e.Message);
}
catch(TextdrawNameNotFoundException e)
{
Logger.WriteLineAndClose("DerbyCreator.cs - DerbyCreator.HUD._:E: Cannot load Derby Creator HUD:");
Logger.WriteLineAndClose(e.Message);
}
}
layer.SetTextdrawText("derbynamelabel", "Derby Name:");
layer.SetTextdrawText("derbyname", "None");
layer.SetTextdrawText("selectedidx", "Spawn nbr:");
layer.SetTextdrawText("editingmode", "Mode: None");
layer.UnselectAllTextdraw();
}
public void Destroy()
{
Expand All @@ -93,10 +42,10 @@ public void SetSelectedIdx(int idx)
}
public void SetEditingMode(EditingMode editingMode)
{
layer.SetTextdrawText("editingmode", editingMode.ToString());
layer.SetTextdrawText("editingmode", "Mode: " + editingMode.ToString());
}
}
enum EditingMode { Mapping, SpawnPos }
enum EditingMode { None, SpawnPos }

protected MySQLConnector mySQLConnector = MySQLConnector.Instance();

Expand Down Expand Up @@ -226,7 +175,7 @@ private void SetPlayerInEditor()

if (!player.InAnyVehicle)
{
BaseVehicle veh = BaseVehicle.Create(VehicleModelType.Infernus, pos, rot, 1, 1);
BaseVehicle veh = BaseVehicle.Create(VehicleModelType.Infernus, pos, rot, 243, 243);
veh.VirtualWorld = player.VirtualWorld;
player.DisableRemoteVehicleCollisions(true);
player.PutInVehicle(veh);
Expand All @@ -239,7 +188,7 @@ private void SetPlayerInEditor()

hud = new HUD(player);
hud.SetDerbyName(editingDerby.Name ?? "Untitled");
editingMode = EditingMode.Mapping;
editingMode = EditingMode.None;
hud.SetEditingMode(editingMode);
}

Expand Down Expand Up @@ -518,6 +467,8 @@ private void DerbyDialog_Response(object sender, DialogResponseEventArgs e)
editingDerby.SpawnPoints = spawnerCreator.GetSpawnPoints();
spawnerCreator.Unload();
spawnerCreator = null;
editingMode = EditingMode.None;
hud.SetEditingMode(editingMode);
}
else
{
Expand All @@ -535,7 +486,11 @@ private void DerbyDialog_Response(object sender, DialogResponseEventArgs e)
{
editingDerby.SpawnPoints = e.spawnPoints;
player.Notificate("Spawn points updated");
editingMode = EditingMode.None;
hud.SetEditingMode(editingMode);
};
editingMode = EditingMode.SpawnPos;
hud.SetEditingMode(editingMode);
}
break;
}
Expand Down
Loading

0 comments on commit 4a17e58

Please sign in to comment.