Better bullet

This commit is contained in:
LittleSheep 2024-08-09 02:20:56 +08:00
parent 3fa89145b4
commit cf6b4b0273
8 changed files with 197 additions and 172 deletions

View File

@ -6,16 +6,16 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5wmf4"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_5wmf4"]
size = Vector2(51.2, 51.2) size = Vector2(51.2, 51.2)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_r3565"] [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_ccaja"]
properties/0/path = NodePath(".:position") properties/0/path = NodePath(".:position")
properties/0/spawn = true properties/0/spawn = true
properties/0/replication_mode = 0 properties/0/replication_mode = 1
properties/1/path = NodePath(".:DecayProgress") properties/1/path = NodePath(".:PlayerId")
properties/1/spawn = true properties/1/spawn = true
properties/1/replication_mode = 0 properties/1/replication_mode = 1
properties/2/path = NodePath(".:MaxDecayProgress") properties/2/path = NodePath(".:DecayProgress")
properties/2/spawn = true properties/2/spawn = true
properties/2/replication_mode = 0 properties/2/replication_mode = 1
[node name="Brick" type="StaticBody2D"] [node name="Brick" type="StaticBody2D"]
script = ExtResource("1_u0jqj") script = ExtResource("1_u0jqj")
@ -33,4 +33,4 @@ wait_time = 0.1
autostart = true autostart = true
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_r3565") replication_config = SubResource("SceneReplicationConfig_ccaja")

View File

@ -115,6 +115,10 @@ one_shot = true
wait_time = 0.05 wait_time = 0.05
one_shot = true one_shot = true
[node name="WeaponCountdown" type="Timer" parent="."]
wait_time = 0.1
one_shot = true
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_bekoo") replication_config = SubResource("SceneReplicationConfig_bekoo")

View File

@ -38,6 +38,7 @@ text = "Health 100/100"
custom_minimum_size = Vector2(2.08165e-12, 16) custom_minimum_size = Vector2(2.08165e-12, 16)
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
max_value = 1.0
rounded = true rounded = true
allow_greater = true allow_greater = true
allow_lesser = true allow_lesser = true
@ -56,6 +57,7 @@ horizontal_alignment = 1
custom_minimum_size = Vector2(2.08165e-12, 16) custom_minimum_size = Vector2(2.08165e-12, 16)
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
max_value = 1.0
rounded = true rounded = true
allow_greater = true allow_greater = true
allow_lesser = true allow_lesser = true

View File

@ -7,6 +7,8 @@ public partial class Brick : StaticBody2D
[Export] public int MaxDecayProgress = 50; [Export] public int MaxDecayProgress = 50;
[Export] public int DecayProgress; [Export] public int DecayProgress;
[Export] public int PlayerId;
public override void _Ready() public override void _Ready()
{ {
DecayProgress = MaxDecayProgress; DecayProgress = MaxDecayProgress;
@ -15,14 +17,14 @@ public partial class Brick : StaticBody2D
{ {
DecayProgress--; DecayProgress--;
if (DecayProgress > 0) return; if (DecayProgress > 0) return;
GetNode<Timer>("DecayTimer").Stop(); GetNode<Timer>("DecayTimer").Stop();
QueueFree();
}; };
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
if(DecayProgress <= 0) QueueFree();
var sprite = GetNode<Sprite2D>("Sprite2D"); var sprite = GetNode<Sprite2D>("Sprite2D");
sprite.SelfModulate = new Color(sprite.SelfModulate, (float)DecayProgress / MaxDecayProgress); sprite.SelfModulate = new Color(sprite.SelfModulate, (float)DecayProgress / MaxDecayProgress);
} }

View File

@ -23,6 +23,12 @@ public partial class Bullet : Area2D
player.TakeDamage(Damage); player.TakeDamage(Damage);
} }
if (body is Brick brick)
{
if (brick.PlayerId == PlayerId) return;
brick.DecayProgress--;
}
QueueFree(); QueueFree();
}; };
} }

View File

@ -42,7 +42,7 @@ public partial class PlayerInput : MultiplayerSynchronizer
if (Input.IsActionJustPressed("move_dash")) if (Input.IsActionJustPressed("move_dash"))
Rpc(nameof(Dash)); Rpc(nameof(Dash));
if (Input.IsActionJustPressed("weapon_shoot")) if (Input.IsActionPressed("weapon_shoot"))
Rpc(nameof(Shoot)); Rpc(nameof(Shoot));
if (Input.IsActionJustPressed("weapon_reload")) if (Input.IsActionJustPressed("weapon_reload"))
Rpc(nameof(Reload)); Rpc(nameof(Reload));

View File

@ -6,195 +6,205 @@ 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 int AmmoAmount = 30; [Export] public int AmmoAmount = 30;
[Export] public int MaxAmmoAmount = 30; [Export] public int MaxAmmoAmount = 30;
[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] public PackedScene TileScene; [Export] public PackedScene TileScene;
[Export] public float TileSize; [Export] public float TileSize;
[Export] public string PlayerName; [Export] public string PlayerName;
[Export] [Export]
public int PlayerId public int PlayerId
{ {
get => _currentPlayerId; get => _currentPlayerId;
set set
{ {
_currentPlayerId = value; _currentPlayerId = value;
PlayerInput.SetMultiplayerAuthority(value); PlayerInput.SetMultiplayerAuthority(value);
} }
} }
public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId(); public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId();
public bool IsReloading public bool IsReloading
=> !GetNode<Timer>("ReloadTimer").IsStopped(); => !GetNode<Timer>("ReloadTimer").IsStopped();
public double TimeRemainingOfReload public double TimeRemainingOfReload
=> GetNode<Timer>("ReloadTimer").TimeLeft; => GetNode<Timer>("ReloadTimer").TimeLeft;
public override void _Ready() public override void _Ready()
{ {
Health = MaxHealth; Health = MaxHealth;
ActionPoints = MaxActionPoints; ActionPoints = MaxActionPoints;
if (PlayerId == Multiplayer.GetUniqueId()) if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true; PlayerCamera.Enabled = true;
PlayerName ??= $"Player#{PlayerId}"; PlayerName ??= $"Player#{PlayerId}";
GetNode<Label>("Overlay/NameTag").Text = PlayerName; GetNode<Label>("Overlay/NameTag").Text = PlayerName;
GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100; GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100;
GetNode<Timer>("ReloadTimer").Timeout += () => GetNode<Timer>("ReloadTimer").Timeout += () =>
{ {
AmmoAmount = MaxAmmoAmount; AmmoAmount = MaxAmmoAmount;
PlayerInput.IsReloading = false; PlayerInput.IsReloading = false;
}; };
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
if (PlayerInput.IsShooting) if (PlayerInput.IsShooting)
{ {
PlayerInput.IsShooting = false; PlayerInput.IsShooting = false;
var name = GD.Randi(); var timer = GetNode<Timer>("WeaponCountdown");
Rpc(nameof(Shoot), name.ToString());
}
if (PlayerInput.IsReloading) if (timer.IsStopped())
{ {
if (AmmoAmount == MaxAmmoAmount) var name = GD.Randi();
{ Shoot(name.ToString());
PlayerInput.IsReloading = false; timer.Start();
} }
else }
{
var timer = GetNode<Timer>("ReloadTimer");
if (timer.IsStopped())
{
AmmoAmount = 0;
timer.Start();
}
}
}
if (PlayerInput.IsBuilding) if (PlayerInput.IsReloading)
{ {
PlayerInput.IsBuilding = false; if (AmmoAmount == MaxAmmoAmount)
{
PlayerInput.IsReloading = false;
}
else
{
var timer = GetNode<Timer>("ReloadTimer");
if (timer.IsStopped())
{
AmmoAmount = 0;
timer.Start();
}
}
}
var target = GetGlobalMousePosition(); if (PlayerInput.IsBuilding)
var distance = Position.DistanceTo(target); {
if (distance <= Reach * TileSize) PlayerInput.IsBuilding = false;
{
var name = GD.Randi();
if (GetParent<World>().GetTileByPosition<Node2D>(target) == null)
Rpc(nameof(AddTile), target, PlayerId, name.ToString());
}
}
}
public override void _PhysicsProcess(double delta) var target = GetGlobalMousePosition();
{ var distance = Position.DistanceTo(target);
var input = PlayerInput.MovementDirection; if (distance <= Reach * TileSize)
{
var name = GD.Randi();
if (GetParent<World>().GetTileByPosition<Node2D>(target) == null)
Rpc(nameof(AddTile), target, PlayerId, name.ToString());
}
}
}
if (input != Vector2.Zero) public override void _PhysicsProcess(double delta)
{ {
input = input.Normalized(); var input = PlayerInput.MovementDirection;
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
var centre = GetNode<Node2D>("RotationCentre"); if (input != Vector2.Zero)
var finalRotation = input.Angle() + Mathf.Pi / 2; {
centre.Rotation = Mathf.LerpAngle(centre.Rotation, finalRotation, RotationSpeed * (float)delta); input = input.Normalized();
} Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
else
{
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
var dashCountdown = GetNode<Timer>("DashCountdown"); var centre = GetNode<Node2D>("RotationCentre");
if (PlayerInput.IsDashing && dashCountdown.IsStopped() && ActionPoints > 0) var finalRotation = input.Angle() + Mathf.Pi / 2;
{ centre.Rotation = Mathf.LerpAngle(centre.Rotation, finalRotation, RotationSpeed * (float)delta);
PlayerInput.IsDashing = false; }
Velocity *= PlayerDashAcceleration; else
ActionPoints--; {
dashCountdown.Start(); Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
} }
Position += Velocity * (float)delta; var dashCountdown = GetNode<Timer>("DashCountdown");
MoveAndSlide(); if (PlayerInput.IsDashing && dashCountdown.IsStopped() && ActionPoints > 0)
} {
PlayerInput.IsDashing = false;
Velocity *= PlayerDashAcceleration;
ActionPoints--;
dashCountdown.Start();
}
public void TakeDamage(double damage) if (IsReloading)
{ {
Rpc(nameof(GotDamage), damage); Velocity *= 0.8f;
} }
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] Position += Velocity * (float)delta;
private void Shoot(string name) MoveAndSlide();
{ }
if (AmmoAmount <= 0) return;
var marker = GetNode<Marker2D>("RotationCentre/Muzzle"); public void TakeDamage(double damage)
var projectile = BulletScene.Instantiate<Bullet>(); {
projectile.Name = $"Bullet@{name}"; Rpc(nameof(GotDamage), damage);
projectile.Transform = marker.GlobalTransform; }
projectile.PlayerId = PlayerId;
GetParent().AddChild(projectile); private void Shoot(string name)
AmmoAmount--; {
} if (AmmoAmount <= 0) return;
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
public void AddTile(Vector2 pos, int playerId, string name) var projectile = BulletScene.Instantiate<Bullet>();
{ projectile.Name = $"Bullet@{name}";
var tiles = GetParent<World>(); projectile.Transform = marker.GlobalTransform;
var tileVec = new Vector2(50, 50); projectile.PlayerId = PlayerId;
var instance = TileScene.Instantiate<Node2D>();
instance.SetMultiplayerAuthority(playerId);
instance.Name = $"Brick@{name}";
instance.Position = pos.Snapped(tileVec);
tiles.AddChild(instance);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] GetParent().AddChild(projectile);
private void GotDamage(double damage) AmmoAmount--;
{ }
var protection = GetNode<Timer>("ProtectionCountdown");
if (protection.IsStopped()) [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
{ public void AddTile(Vector2 pos, int playerId, string name)
Health -= damage; {
var tiles = GetParent<World>();
var tileVec = new Vector2(50, 50);
var instance = TileScene.Instantiate<Brick>();
instance.Name = $"Brick@{name}";
instance.Position = pos.Snapped(tileVec);
instance.PlayerId = playerId;
tiles.AddChild(instance);
}
var tween = CreateTween(); [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
var bar = GetNode<ProgressBar>("Overlay/HealthBar"); private void GotDamage(double damage)
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3); {
var protection = GetNode<Timer>("ProtectionCountdown");
protection.Start(); if (protection.IsStopped())
} {
Health -= damage;
var shakableCamera = GetNode<CameraShake>("Camera2D"); var tween = CreateTween();
shakableCamera.AddTrauma(0.5f); var bar = GetNode<ProgressBar>("Overlay/HealthBar");
} tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
}
protection.Start();
}
var shakableCamera = GetNode<CameraShake>("Camera2D");
shakableCamera.AddTrauma(0.5f);
}
}

View File

@ -13,28 +13,29 @@ public partial class HUD : Control
Position = new Vector2(0, 0); Position = new Vector2(0, 0);
Size = screenSize; Size = screenSize;
GetNode<HBoxContainer>("TopBox").Size = new Vector2(screenSize.X-16*2, 16); GetNode<HBoxContainer>("TopBox").Size = new Vector2(screenSize.X - 16 * 2, 16);
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
var player = World.GetCurrentPlayer(); var player = World.GetCurrentPlayer();
if (player == null) return; if (player == null) return;
var healthBar = GetNode<ProgressBar>("TopBox/HealthBox/Bar"); var healthBar = GetNode<ProgressBar>("TopBox/HealthBox/Bar");
var healthLabel = GetNode<Label>("TopBox/HealthBox/Label"); var healthLabel = GetNode<Label>("TopBox/HealthBox/Label");
healthBar.Value = player.Health / player.MaxHealth * 100; healthBar.Value = player.Health / player.MaxHealth * 100;
healthLabel.Text = $"Health {player.Health}/{player.MaxHealth}"; healthLabel.Text = $"Health {player.Health}/{player.MaxHealth}";
var actionBar = GetNode<ProgressBar>("TopBox/ActionPointBox/Bar"); var actionBar = GetNode<ProgressBar>("TopBox/ActionPointBox/Bar");
var actionLabel = GetNode<Label>("TopBox/ActionPointBox/Label"); var actionLabel = GetNode<Label>("TopBox/ActionPointBox/Label");
actionBar.Value = player.ActionPoints / player.MaxActionPoints * 100; actionBar.Value = player.ActionPoints / player.MaxActionPoints * 100;
actionLabel.Text = $"AP {player.ActionPoints}/{player.MaxActionPoints}"; actionLabel.Text = $"AP {player.ActionPoints}/{player.MaxActionPoints}";
var ammoBar = GetNode<ProgressBar>("TopBox/AmmoBox/Bar"); var ammoBar = GetNode<ProgressBar>("TopBox/AmmoBox/Bar");
var ammoLabel = GetNode<Label>("TopBox/AmmoBox/Label"); var ammoLabel = GetNode<Label>("TopBox/AmmoBox/Label");
ammoBar.Value = player.AmmoAmount / player.MaxAmmoAmount * 100; ammoBar.Value = (float)player.AmmoAmount / player.MaxAmmoAmount * 100;
if (player.IsReloading) ammoLabel.Text = $"Reloading... {player.TimeRemainingOfReload:F2}"; ammoLabel.Text = player.IsReloading
else ammoLabel.Text = $"Ammo {player.AmmoAmount}/{player.MaxAmmoAmount}"; ? $"Reloading... {player.TimeRemainingOfReload:F2}"
: $"Ammo {player.AmmoAmount}/{player.MaxAmmoAmount}";
} }
} }