✨ Name & Health bar
This commit is contained in:
		
							
								
								
									
										10
									
								
								.idea/.idea.CodingLand/.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										10
									
								
								.idea/.idea.CodingLand/.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							| @@ -5,16 +5,10 @@ | |||||||
|   </component> |   </component> | ||||||
|   <component name="ChangeListManager"> |   <component name="ChangeListManager"> | ||||||
|     <list default="true" id="c7bd02c7-bbb4-431f-81ca-d2f8b7b09b37" name="Changes" comment=""> |     <list default="true" id="c7bd02c7-bbb4-431f-81ca-d2f8b7b09b37" name="Changes" comment=""> | ||||||
|       <change afterPath="$PROJECT_DIR$/Scenes/Bullet.tscn" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/Scripts/Bullet.cs" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/Scripts/Effects/CameraShake.cs" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/Sprites/Projectiles.png" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/Sprites/Projectiles.png.import" afterDir="false" /> |  | ||||||
|       <change beforePath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" afterDir="false" /> |       <change beforePath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.CodingLand/.idea/workspace.xml" afterDir="false" /> | ||||||
|       <change beforePath="$PROJECT_DIR$/Scenes/Player.tscn" beforeDir="false" afterPath="$PROJECT_DIR$/Scenes/Player.tscn" afterDir="false" /> |       <change beforePath="$PROJECT_DIR$/Scenes/Player.tscn" beforeDir="false" afterPath="$PROJECT_DIR$/Scenes/Player.tscn" afterDir="false" /> | ||||||
|       <change beforePath="$PROJECT_DIR$/Scripts/Logic/PlayerInput.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Logic/PlayerInput.cs" afterDir="false" /> |       <change beforePath="$PROJECT_DIR$/Scripts/Effects/CameraShake.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Effects/CameraShake.cs" afterDir="false" /> | ||||||
|       <change beforePath="$PROJECT_DIR$/Scripts/Player.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Player.cs" afterDir="false" /> |       <change beforePath="$PROJECT_DIR$/Scripts/Player.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Scripts/Player.cs" afterDir="false" /> | ||||||
|       <change beforePath="$PROJECT_DIR$/project.godot" beforeDir="false" afterPath="$PROJECT_DIR$/project.godot" afterDir="false" /> |  | ||||||
|     </list> |     </list> | ||||||
|     <option name="SHOW_DIALOG" value="false" /> |     <option name="SHOW_DIALOG" value="false" /> | ||||||
|     <option name="HIGHLIGHT_CONFLICTS" value="true" /> |     <option name="HIGHLIGHT_CONFLICTS" value="true" /> | ||||||
| @@ -116,7 +110,7 @@ | |||||||
|       <workItem from="1722846222698" duration="9239000" /> |       <workItem from="1722846222698" duration="9239000" /> | ||||||
|       <workItem from="1722855558149" duration="11578000" /> |       <workItem from="1722855558149" duration="11578000" /> | ||||||
|       <workItem from="1723040712804" duration="74000" /> |       <workItem from="1723040712804" duration="74000" /> | ||||||
|       <workItem from="1723040934916" duration="6034000" /> |       <workItem from="1723040934916" duration="7673000" /> | ||||||
|     </task> |     </task> | ||||||
|     <task id="LOCAL-00001" summary=":sparkles: Usable multiplayer"> |     <task id="LOCAL-00001" summary=":sparkles: Usable multiplayer"> | ||||||
|       <option name="closed" value="true" /> |       <option name="closed" value="true" /> | ||||||
|   | |||||||
| @@ -33,11 +33,38 @@ PlayerCamera = NodePath("Camera2D") | |||||||
| PlayerInput = NodePath("InputSynchronizer") | PlayerInput = NodePath("InputSynchronizer") | ||||||
| BulletScene = ExtResource("2_nmop0") | BulletScene = ExtResource("2_nmop0") | ||||||
|  |  | ||||||
| [node name="Sprite2D" type="Sprite2D" parent="."] | [node name="HUD" type="VBoxContainer" parent="."] | ||||||
|  | offset_left = -70.0 | ||||||
|  | offset_top = -80.0 | ||||||
|  | offset_right = 70.0 | ||||||
|  | offset_bottom = -20.0 | ||||||
|  | theme_override_constants/separation = 6 | ||||||
|  | alignment = 1 | ||||||
|  |  | ||||||
|  | [node name="NameTag" type="Label" parent="HUD"] | ||||||
|  | layout_mode = 2 | ||||||
|  | text = "Player" | ||||||
|  | horizontal_alignment = 1 | ||||||
|  | vertical_alignment = 1 | ||||||
|  | clip_text = true | ||||||
|  |  | ||||||
|  | [node name="HealthBar" type="ProgressBar" parent="HUD"] | ||||||
|  | layout_mode = 2 | ||||||
|  | rounded = true | ||||||
|  | allow_greater = true | ||||||
|  | allow_lesser = true | ||||||
|  | show_percentage = false | ||||||
|  |  | ||||||
|  | [node name="RotationCentre" type="Node2D" parent="."] | ||||||
|  |  | ||||||
|  | [node name="Sprite2D" type="Sprite2D" parent="RotationCentre"] | ||||||
| position = Vector2(2.08165e-12, 2.08165e-12) | position = Vector2(2.08165e-12, 2.08165e-12) | ||||||
| scale = Vector2(0.04, 0.04) | scale = Vector2(0.04, 0.04) | ||||||
| texture = ExtResource("1_cqpqa") | texture = ExtResource("1_cqpqa") | ||||||
|  |  | ||||||
|  | [node name="Muzzle" type="Marker2D" parent="RotationCentre"] | ||||||
|  | position = Vector2(2.08165e-12, -20) | ||||||
|  |  | ||||||
| [node name="CollisionShape2D" type="CollisionShape2D" parent="."] | [node name="CollisionShape2D" type="CollisionShape2D" parent="."] | ||||||
| shape = SubResource("CircleShape2D_68yf8") | shape = SubResource("CircleShape2D_68yf8") | ||||||
|  |  | ||||||
| @@ -49,9 +76,6 @@ rotation_smoothing_enabled = true | |||||||
| script = ExtResource("4_fwngj") | script = ExtResource("4_fwngj") | ||||||
| Noise = SubResource("FastNoiseLite_cfnx7") | Noise = SubResource("FastNoiseLite_cfnx7") | ||||||
|  |  | ||||||
| [node name="Muzzle" type="Marker2D" parent="."] |  | ||||||
| position = Vector2(2.08165e-12, -20) |  | ||||||
|  |  | ||||||
| [node name="DashCountdown" type="Timer" parent="."] | [node name="DashCountdown" type="Timer" parent="."] | ||||||
| one_shot = true | one_shot = true | ||||||
|  |  | ||||||
|   | |||||||
| @@ -50,9 +50,4 @@ public partial class CameraShake : Camera2D | |||||||
|     { |     { | ||||||
|         _trauma = Mathf.Min(_trauma + amount, 1.0f); |         _trauma = Mathf.Min(_trauma + amount, 1.0f); | ||||||
|     } |     } | ||||||
|      |  | ||||||
|     public void SetTrauma(float amount) |  | ||||||
|     { |  | ||||||
|         _trauma = Mathf.Min(amount, 1.0f); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @@ -6,113 +6,124 @@ namespace AceField.Scripts; | |||||||
|  |  | ||||||
| public partial class Player : CharacterBody2D | public partial class Player : CharacterBody2D | ||||||
| { | { | ||||||
| 	private int _currentPlayerId = 1; |     private int _currentPlayerId = 1; | ||||||
|  |  | ||||||
| 	[Export] public float MaxSpeed = 400f; |     [Export] public float MaxSpeed = 400f; | ||||||
| 	[Export] public float Acceleration = 500f; |     [Export] public float Acceleration = 500f; | ||||||
| 	[Export] public float Deceleration = 500f; |     [Export] public float Deceleration = 500f; | ||||||
| 	[Export] public float RotationSpeed = 5f; |     [Export] public float RotationSpeed = 5f; | ||||||
|  |  | ||||||
| 	[Export] public int Reach = 5; |     [Export] public int Reach = 5; | ||||||
|  |  | ||||||
| 	[Export] public double Health = 100; |     [Export] public double Health = 100; | ||||||
| 	[Export] public double MaxHealth = 100; |     [Export] public double MaxHealth = 100; | ||||||
| 	[Export] public double ActionPoints = 20; |     [Export] public double ActionPoints = 20; | ||||||
| 	[Export] public double MaxActionPoints = 20; |     [Export] public double MaxActionPoints = 20; | ||||||
|  |  | ||||||
| 	[Export] public Camera2D PlayerCamera; |     [Export] public Camera2D PlayerCamera; | ||||||
| 	[Export] public PlayerInput PlayerInput; |     [Export] public PlayerInput PlayerInput; | ||||||
|  |  | ||||||
| 	[Export] public float PlayerDashAcceleration = 2f; |     [Export] public float PlayerDashAcceleration = 2f; | ||||||
|  |  | ||||||
| 	[Export] public PackedScene BulletScene; |     [Export] public PackedScene BulletScene; | ||||||
|  |  | ||||||
| 	[Export] |     [Export] public string PlayerName; | ||||||
| 	public int PlayerId |  | ||||||
| 	{ |  | ||||||
| 		get => _currentPlayerId; |  | ||||||
| 		set |  | ||||||
| 		{ |  | ||||||
| 			_currentPlayerId = value; |  | ||||||
| 			PlayerInput.SetMultiplayerAuthority(value); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	public void SetPlayerId(int id) |     [Export] | ||||||
| 	{ |     public int PlayerId | ||||||
| 		PlayerId = id; |     { | ||||||
| 		PlayerInput.SetMultiplayerAuthority(id); |         get => _currentPlayerId; | ||||||
| 	} |         set | ||||||
| 	 |         { | ||||||
| 	public override void _Ready() |             _currentPlayerId = value; | ||||||
| 	{ |             PlayerInput.SetMultiplayerAuthority(value); | ||||||
| 		Health = MaxHealth; |         } | ||||||
| 		ActionPoints = MaxActionPoints; |     } | ||||||
| 		 |  | ||||||
| 		if (PlayerId == Multiplayer.GetUniqueId()) |  | ||||||
| 			PlayerCamera.Enabled = true; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	public override void _Process(double delta) |     public void SetPlayerId(int id) | ||||||
| 	{ |     { | ||||||
| 		if (PlayerInput.IsShooting) |         PlayerId = id; | ||||||
| 		{ |         PlayerInput.SetMultiplayerAuthority(id); | ||||||
| 			Rpc(nameof(Shoot)); |     } | ||||||
| 			PlayerInput.IsShooting = false; |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	public override void _PhysicsProcess(double delta) |     public override void _Ready() | ||||||
| 	{ |     { | ||||||
| 		var input = PlayerInput.MovementDirection; |         Health = MaxHealth; | ||||||
|  |         ActionPoints = MaxActionPoints; | ||||||
|  |  | ||||||
| 		if (input != Vector2.Zero) |         if (PlayerId == Multiplayer.GetUniqueId()) | ||||||
| 		{ |             PlayerCamera.Enabled = true; | ||||||
| 			input = input.Normalized(); |  | ||||||
| 			Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta); |  | ||||||
|  |  | ||||||
| 			var finalRotation = input.Angle() + Mathf.Pi / 2; |         PlayerName = $"Player#{PlayerId}"; // TODO Remove mock data | ||||||
| 			Rotation = Mathf.LerpAngle(Rotation, finalRotation, RotationSpeed * (float)delta); |         GetNode<Label>("HUD/NameTag").Text = PlayerName; | ||||||
| 		} |  | ||||||
| 		else |  | ||||||
| 		{ |  | ||||||
| 			Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		var dashCountdown = GetNode<Timer>("DashCountdown"); |         GetNode<ProgressBar>("HUD/HealthBar").Value = Health / MaxHealth * 100; | ||||||
| 		if (PlayerInput.IsDashing && dashCountdown.IsStopped()) |     } | ||||||
| 		{ |  | ||||||
| 			PlayerInput.IsDashing = false; |  | ||||||
| 			Velocity *= PlayerDashAcceleration; |  | ||||||
| 			dashCountdown.Start(); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		Position += Velocity * (float)delta; |     public override void _Process(double delta) | ||||||
| 		MoveAndSlide(); |     { | ||||||
| 	} |         if (PlayerInput.IsShooting) | ||||||
|  |         { | ||||||
|  |             Rpc(nameof(Shoot)); | ||||||
|  |             PlayerInput.IsShooting = false; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| 	public void TakeDamage(double damage) |     public override void _PhysicsProcess(double delta) | ||||||
| 	{ |     { | ||||||
| 		Rpc(nameof(GotDamage), damage); |         var input = PlayerInput.MovementDirection; | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] |         if (input != Vector2.Zero) | ||||||
| 	private void GotDamage(double damage) |         { | ||||||
| 	{ |             input = input.Normalized(); | ||||||
| 		Health -= damage; |             Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta); | ||||||
| 		 |  | ||||||
| 		var shakableCamera = GetNode<CameraShake>("Camera2D"); |  | ||||||
| 		shakableCamera.AddTrauma(0.5f); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] |             var centre = GetNode<Node2D>("RotationCentre"); | ||||||
| 	private void Shoot() |             var finalRotation = input.Angle() + Mathf.Pi / 2; | ||||||
| 	{ |             centre.Rotation = Mathf.LerpAngle(centre.Rotation, finalRotation, RotationSpeed * (float)delta); | ||||||
| 		var marker = GetNode<Marker2D>("Muzzle"); |         } | ||||||
| 		var projectile = BulletScene.Instantiate<Bullet>(); |         else | ||||||
| 		projectile.Transform = marker.GlobalTransform; |         { | ||||||
| 		projectile.PlayerId = PlayerId; |             Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta); | ||||||
| 		 |         } | ||||||
| 		GetParent().AddChild(projectile); |  | ||||||
| 	} |         var dashCountdown = GetNode<Timer>("DashCountdown"); | ||||||
| } |         if (PlayerInput.IsDashing && dashCountdown.IsStopped()) | ||||||
|  |         { | ||||||
|  |             PlayerInput.IsDashing = false; | ||||||
|  |             Velocity *= PlayerDashAcceleration; | ||||||
|  |             dashCountdown.Start(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Position += Velocity * (float)delta; | ||||||
|  |         MoveAndSlide(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void TakeDamage(double damage) | ||||||
|  |     { | ||||||
|  |         Rpc(nameof(GotDamage), damage); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] | ||||||
|  |     private void GotDamage(double damage) | ||||||
|  |     { | ||||||
|  |         Health -= damage; | ||||||
|  |  | ||||||
|  |         var bar = GetNode<ProgressBar>("HUD/HealthBar"); | ||||||
|  |         bar.Value = Health / MaxHealth * 100; | ||||||
|  |  | ||||||
|  |         var shakableCamera = GetNode<CameraShake>("Camera2D"); | ||||||
|  |         shakableCamera.AddTrauma(0.5f); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] | ||||||
|  |     private void Shoot() | ||||||
|  |     { | ||||||
|  |         var marker = GetNode<Marker2D>("RotationCentre/Muzzle"); | ||||||
|  |         var projectile = BulletScene.Instantiate<Bullet>(); | ||||||
|  |         projectile.Transform = marker.GlobalTransform; | ||||||
|  |         projectile.PlayerId = PlayerId; | ||||||
|  |  | ||||||
|  |         GetParent().AddChild(projectile); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user