🐛 Fix buges

This commit is contained in:
2024-08-09 00:42:22 +08:00
parent b5819a249c
commit 3fa89145b4
7 changed files with 214 additions and 163 deletions

29
Scripts/Brick.cs Normal file
View File

@ -0,0 +1,29 @@
using Godot;
namespace AceField.Scripts;
public partial class Brick : StaticBody2D
{
[Export] public int MaxDecayProgress = 50;
[Export] public int DecayProgress;
public override void _Ready()
{
DecayProgress = MaxDecayProgress;
GetNode<Timer>("DecayTimer").Timeout += () =>
{
DecayProgress--;
if (DecayProgress > 0) return;
GetNode<Timer>("DecayTimer").Stop();
QueueFree();
};
}
public override void _Process(double delta)
{
var sprite = GetNode<Sprite2D>("Sprite2D");
sprite.SelfModulate = new Color(sprite.SelfModulate, (float)DecayProgress / MaxDecayProgress);
}
}

View File

@ -17,10 +17,10 @@ public partial class Bullet : Area2D
BodyEntered += body =>
{
if (body is not Player player || player.PlayerId == PlayerId) return;
if (body is Player p)
if (body is Player player)
{
p.TakeDamage(Damage);
if (player.PlayerId == PlayerId) return;
player.TakeDamage(Damage);
}
QueueFree();

View File

@ -46,12 +46,7 @@ public partial class PlayerInput : MultiplayerSynchronizer
Rpc(nameof(Shoot));
if (Input.IsActionJustPressed("weapon_reload"))
Rpc(nameof(Reload));
}
public override void _Input(InputEvent evt)
{
if (!IsCurrentPlayer) return;
if (evt is InputEventMouseButton { Pressed: true })
if (Input.IsActionJustPressed("skill_place_tile"))
Rpc(nameof(Build));
}
}

View File

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