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

Commit

Permalink
Merge pull request #69 from FinnTheTurkey/master
Browse files Browse the repository at this point in the history
Add damage indicators
  • Loading branch information
ForLoveOfCats authored Aug 4, 2019
2 parents f952b97 + de3ee6b commit 79d3333
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 6 deletions.
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, Origin);
}
}
else
Expand Down
9 changes: 4 additions & 5 deletions Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public override void _Ready()
ProjectileEmitterHinge = GetNode<Spatial>("ProjectileEmitterHinge");
ProjectileEmitter = GetNode<Spatial>("ProjectileEmitterHinge/ProjectileEmitter");

Respawn();

if(Possessed)
{
Cam.MakeCurrent();
Expand Down Expand Up @@ -155,10 +153,9 @@ public override void _Ready()
return;
}

Respawn();
if(GetTree().IsNetworkServer())
{
SetFreeze(false);
}

ItemGive(new Items.Instance(Items.ID.PLATFORM));
ItemGive(new Items.Instance(Items.ID.WALL));
Expand Down Expand Up @@ -217,15 +214,17 @@ public void MovementReset()

public void Respawn()
{
HUDInstance.ClearDamageIndicators();
MovementReset();
Health = MaxHealth;
}


[Remote]
public void ApplyDamage(float Damage)
public void ApplyDamage(float Damage, Vector3 Origin)
{
Health = Clamp(Health - Damage, 0, MaxHealth);
HUDInstance.AddDamageIndicator(Origin, Damage);
}


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
56 changes: 56 additions & 0 deletions UI/DamageIndicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Godot;


public class DamageIndicator : Sprite
{
public float MaxLife;
public float RemainingLife;

public Vector2 ShotFirePos2D;

public ShaderMaterial Mat;

public static Shader TransparencyShader;

static DamageIndicator()
{
TransparencyShader = GD.Load<Shader>("res://UI/DamageIndicator.shader");
}


public void Setup(Vector3 Origin, float MaxLifeArg)
{
MaxLife = MaxLifeArg;
RemainingLife = MaxLife;

ShotFirePos2D = new Vector2(Origin.x, Origin.z);

Mat = new ShaderMaterial();
Mat.Shader = TransparencyShader;
Mat.SetShaderParam("alpha", 1);
Material = Mat;

CenterSprite();
}


private void CenterSprite()
{
GlobalPosition = OS.GetWindowSize() / new Vector2(2, 2);
}


public override void _Process(float Delta)
{
Vector2 PlayerPosition2D = new Vector2(Game.PossessedPlayer.Translation.x, Game.PossessedPlayer.Translation.z);
SetRotation(PlayerPosition2D.AngleToPoint(ShotFirePos2D) + Game.PossessedPlayer.Rotation.y);

RemainingLife -= Delta;
if(RemainingLife <= 0)
QueueFree();

Mat.SetShaderParam("alpha", RemainingLife/MaxLife);

CenterSprite();
}
}
9 changes: 9 additions & 0 deletions UI/DamageIndicator.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
shader_type canvas_item;
uniform float alpha = 1;


void fragment()
{
COLOR = texture(TEXTURE, UV);
COLOR.a = COLOR.a * alpha;
}
15 changes: 15 additions & 0 deletions UI/DamageIndicator.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[gd_scene load_steps=5 format=2]

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

[sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 1 )
shader_param/alpha = 0.5

[node name="DamageIndicator" type="Sprite"]
material = SubResource( 1 )
position = Vector2( 202.18, 203.938 )
texture = ExtResource( 2 )
script = ExtResource( 3 )
21 changes: 21 additions & 0 deletions UI/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

public class HUD : Node
{
public static float DamageIndicatorLifeMultiplyer = 0.1f; //Multipled by damage to calc max life

private Texture Alpha;
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 +20,7 @@ public class HUD : Node
private Label ChunkInfoLabel;
private Label PlayerPositionLabel;
private Label FPSLabel;
private Node2D DamageIndicatorRoot;
public CanvasLayer NickLabelLayer;

public bool Visible = true;
Expand All @@ -29,6 +33,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 +45,7 @@ public override void _Ready()
ChunkInfoLabel = GetNode<Label>("CLayer/ChunkInfo");
PlayerPositionLabel = GetNode<Label>("CLayer/PlayerPosition");
FPSLabel = GetNode<Label>("CLayer/FPSLabel");
DamageIndicatorRoot = GetNode<Node2D>("CLayer/DamageIndicatorRoot");
NickLabelLayer = GetNode<CanvasLayer>("NickLabelLayer");

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


public void AddDamageIndicator(Vector3 ShotFirePosition, float Damage)
{
DamageIndicator Indicator = DamageIndicatorScene.Instance() as DamageIndicator;
Indicator.Setup(ShotFirePosition, Damage*DamageIndicatorLifeMultiplyer);
DamageIndicatorRoot.AddChild(Indicator);
}


public void ClearDamageIndicators()
{
foreach(Node Child in DamageIndicatorRoot.GetChildren())
Child.QueueFree();
}


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

[node name="DamageIndicatorRoot" type="Node2D" parent="CLayer"]

[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

0 comments on commit 79d3333

Please sign in to comment.