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"]
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/spawn = true
properties/0/replication_mode = 0
properties/1/path = NodePath(".:DecayProgress")
properties/0/replication_mode = 1
properties/1/path = NodePath(".:PlayerId")
properties/1/spawn = true
properties/1/replication_mode = 0
properties/2/path = NodePath(".:MaxDecayProgress")
properties/1/replication_mode = 1
properties/2/path = NodePath(".:DecayProgress")
properties/2/spawn = true
properties/2/replication_mode = 0
properties/2/replication_mode = 1
[node name="Brick" type="StaticBody2D"]
script = ExtResource("1_u0jqj")
@ -33,4 +33,4 @@ wait_time = 0.1
autostart = true
[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
one_shot = true
[node name="WeaponCountdown" type="Timer" parent="."]
wait_time = 0.1
one_shot = true
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_bekoo")

View File

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

View File

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

View File

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

View File

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

View File

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