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

Add damage indicators #69

Merged
merged 11 commits into from
Aug 4, 2019
2 changes: 1 addition & 1 deletion Items/Logic/Hitscan/Hitscan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void Fire(float VerticalAngle, float HorizontalAngle, float Range,
if(HitPlr.Health - Damage <= 0)
Game.PossessedPlayer.SfxManager.FpKillsound();

HitPlr.RpcId(HitPlr.Id, nameof(Player.ApplyDamage), Damage);
HitPlr.RpcId(HitPlr.Id, nameof(Player.ApplyDamage), Damage, Game.PossessedPlayer.Id);
}
}
else
Expand Down
8 changes: 7 additions & 1 deletion Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,15 @@ public void Respawn()


[Remote]
public void ApplyDamage(float Damage)
public void ApplyDamage(float Damage, int AttackerId)
{
Health = Clamp(Health - Damage, 0, MaxHealth);
// Calculate DamageIndicator angles
Player Attacker = Net.Players[AttackerId];
// Convert the attacker's coordinates to 2D to make this easier
// aka remove height
Vector2 AttackerPoint = new Vector2(Attacker.Translation.x, Attacker.Translation.z);
HUDInstance.ShowDamageIndicator(AttackerPoint); // Convert to degrees
}


Expand Down
1 change: 1 addition & 0 deletions SkyOfSteel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Player\PlayerSfxManager.cs" />
<Compile Include="Scripting\Gamemode.cs" />
<Compile Include="Scripting\GmConfig.cs" />
<Compile Include="UI\DamageIndicator.cs" />
<Compile Include="UI\Menu\HostMenu\SlotButton.cs" />
<Compile Include="UI\Menu\LicensesMenu\LicensesMenu.cs" />
<Compile Include="UI\Menu\Pieces\ButtonPiece.cs" />
Expand Down
44 changes: 44 additions & 0 deletions UI/DamageIndicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Godot;
using System;

public class DamageIndicator : TextureRect
{
// Angle the bullet came from
Vector2 ShotFirePosition;
float OriginalAngle;
public override void _Ready()
{
// Start transparency Tween in _Ready so we know the Tween has been created
Tween TransparencyTween = GetNode<Tween>("Tween");
// Tween the "modulate" property
TransparencyTween.InterpolateProperty(this,"modulate", new Color(255,255,255,1), new Color(255,255,255,0), 3, Tween.TransitionType.Linear, Tween.EaseType.InOut);
TransparencyTween.Start();
}


public void SetTargetAngle(Vector2 ShotFirePosition)
{
// The rotation we're looking at is the Y rotation
this.ShotFirePosition = ShotFirePosition;
// Original angle
OriginalAngle = Game.PossessedPlayer.Rotation.y;
}


// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
// Calculate rotation
// Calculate our position on a 2D plane
Vector2 OurPoint = new Vector2(Game.PossessedPlayer.Translation.x, Game.PossessedPlayer.Translation.z);
// Get our rotation by getting the angle from where we we're shot from, and adding where we we're looking before
RectRotation = Mathf.Rad2Deg(OurPoint.AngleToPoint(ShotFirePosition)+OriginalAngle);
}


public void TweenComplete()
{
// We're now totally transparent, so time to
QueueFree();
}
}
16 changes: 16 additions & 0 deletions UI/DamageIndicator.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://UI/Textures/DamageIndicator.png" type="Texture" id=1]
[ext_resource path="res://UI/DamageIndicator.cs" type="Script" id=2]

[node name="DamageIndicator" type="TextureRect"]
margin_left = 312.0
margin_top = 100.0
margin_right = 712.0
margin_bottom = 500.0
rect_pivot_offset = Vector2( 200, 200 )
texture = ExtResource( 1 )
script = ExtResource( 2 )

[node name="Tween" type="Tween" parent="."]
[connection signal="tween_all_completed" from="Tween" to="." method="TweenComplete"]
13 changes: 13 additions & 0 deletions UI/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class HUD : Node
private Texture Triangle;
private PackedScene ItemCountLabelScene;
private PackedScene NickLabelScene;
private PackedScene DamageIndicatorScene;

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

Expand All @@ -17,6 +18,7 @@ public class HUD : Node
private Label ChunkInfoLabel;
private Label PlayerPositionLabel;
private Label FPSLabel;
private CenterContainer DamageIndicatorContainer;
public CanvasLayer NickLabelLayer;

public bool Visible = true;
Expand All @@ -29,6 +31,7 @@ public class HUD : Node
Triangle = GD.Load("res://UI/Textures/Triangle.png") as Texture;
ItemCountLabelScene = GD.Load<PackedScene>("res://UI/ItemCountLabel.tscn");
NickLabelScene = GD.Load<PackedScene>("res://UI/NickLabel.tscn");
DamageIndicatorScene = GD.Load<PackedScene>("res://UI/DamageIndicator.tscn");
}


Expand All @@ -40,6 +43,7 @@ public override void _Ready()
ChunkInfoLabel = GetNode<Label>("CLayer/ChunkInfo");
PlayerPositionLabel = GetNode<Label>("CLayer/PlayerPosition");
FPSLabel = GetNode<Label>("CLayer/FPSLabel");
DamageIndicatorContainer = GetNode<CenterContainer>("CLayer/DamageIndicatorCenter");
NickLabelLayer = GetNode<CanvasLayer>("NickLabelLayer");

GetNode<Label>("CLayer/VersionLabel").Text = $"Version: {Game.Version}";
Expand Down Expand Up @@ -157,6 +161,15 @@ public void RemoveNickLabel(int Id)
}


public void ShowDamageIndicator(Vector2 ShotFirePosition) {
// Angle must be in degrees for the DamageIndicator
DamageIndicator Indicator = DamageIndicatorScene.Instance() as DamageIndicator;
Indicator.SetTargetAngle(ShotFirePosition);
// Add it to its container
DamageIndicatorContainer.AddChild(Indicator);
}


public override void _Process(float Delta)
{
Crosshair.Visible = !Menu.IsOpen;
Expand Down
4 changes: 4 additions & 0 deletions UI/HUD.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ custom_styles/bg = SubResource( 2 )
value = 50.0
percent_visible = false

[node name="DamageIndicatorCenter" type="CenterContainer" parent="CLayer"]
anchor_right = 1.0
anchor_bottom = 1.0

[node name="HealthVBox" type="VBoxContainer" parent="CLayer"]
anchor_right = 1.0
anchor_bottom = 1.0
Expand Down
Binary file added UI/Textures/DamageIndicator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions UI/Textures/DamageIndicator.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/DamageIndicator.png-cbb494ba511e14f2e40bf5115c6800de.stex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://UI/Textures/DamageIndicator.png"
dest_files=[ "res://.import/DamageIndicator.png-cbb494ba511e14f2e40bf5115c6800de.stex" ]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0