⏪ Rollback place brick feature
This commit is contained in:
@ -5,7 +5,7 @@ namespace AceField.Scripts;
|
||||
public partial class Bullet : Area2D
|
||||
{
|
||||
[Export] public int PlayerId = 1;
|
||||
|
||||
|
||||
[Export] public float Speed = 1500;
|
||||
[Export] public double Damage = 8;
|
||||
|
||||
@ -18,7 +18,11 @@ public partial class Bullet : Area2D
|
||||
BodyEntered += body =>
|
||||
{
|
||||
if (body is not Player player || player.PlayerId == PlayerId) return;
|
||||
player.TakeDamage(Damage);
|
||||
if (body is Player p)
|
||||
{
|
||||
p.TakeDamage(Damage);
|
||||
}
|
||||
|
||||
QueueFree();
|
||||
};
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace AceField.Scripts.Logic;
|
||||
public partial class PlayerInput : MultiplayerSynchronizer
|
||||
{
|
||||
[Export] public bool IsDashing;
|
||||
[Export] public bool IsBuilding;
|
||||
[Export] public bool IsShooting;
|
||||
[Export] public bool IsReloading;
|
||||
|
||||
@ -30,6 +31,10 @@ public partial class PlayerInput : MultiplayerSynchronizer
|
||||
[Rpc(CallLocal = true)]
|
||||
private void Reload()
|
||||
=> IsReloading = true;
|
||||
|
||||
[Rpc(CallLocal = true)]
|
||||
private void Build()
|
||||
=> IsBuilding = true;
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
@ -46,5 +51,7 @@ public partial class PlayerInput : MultiplayerSynchronizer
|
||||
public override void _Input(InputEvent evt)
|
||||
{
|
||||
if (!IsCurrentPlayer) return;
|
||||
if (evt is InputEventMouseButton { Pressed: true })
|
||||
Rpc(nameof(Build));
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ public partial class World : Node2D
|
||||
}
|
||||
|
||||
private static string BuildPlayerName(int id)
|
||||
=> $"Player#{id}";
|
||||
=> $"Player@{id}";
|
||||
|
||||
private void AddPlayer_Adaptor(long id)
|
||||
=> AddPlayer((int)id);
|
||||
@ -75,4 +75,15 @@ public partial class World : Node2D
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
|
||||
{
|
||||
foreach (var item in GetChildren())
|
||||
{
|
||||
if (item is T tile && tile.Position == position)
|
||||
return tile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -28,6 +28,9 @@ public partial class Player : CharacterBody2D
|
||||
[Export] public float PlayerDashAcceleration = 2f;
|
||||
|
||||
[Export] public PackedScene BulletScene;
|
||||
[Export] public PackedScene TileScene;
|
||||
|
||||
[Export] public float TileSize;
|
||||
|
||||
[Export] public string PlayerName;
|
||||
|
||||
@ -82,7 +85,23 @@ public partial class Player : CharacterBody2D
|
||||
{
|
||||
var timer = GetNode<Timer>("ReloadTimer");
|
||||
if (timer.IsStopped())
|
||||
{
|
||||
AmmoAmount = 0;
|
||||
timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,6 +140,20 @@ public partial class Player : CharacterBody2D
|
||||
{
|
||||
Rpc(nameof(GotDamage), damage);
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
if (AmmoAmount <= 0) return;
|
||||
|
||||
var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
|
||||
var projectile = BulletScene.Instantiate<Bullet>();
|
||||
projectile.Transform = marker.GlobalTransform;
|
||||
projectile.PlayerId = PlayerId;
|
||||
|
||||
GetParent().AddChild(projectile);
|
||||
AmmoAmount--;
|
||||
}
|
||||
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||
private void GotDamage(double damage)
|
||||
@ -142,16 +175,16 @@ public partial class Player : CharacterBody2D
|
||||
shakableCamera.AddTrauma(0.5f);
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||
public void AddTile(Vector2 pos, string name)
|
||||
{
|
||||
if (AmmoAmount <= 0) return;
|
||||
if (GetParent<World>().GetTileByPosition<Node2D>(pos) != null) return;
|
||||
|
||||
var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
|
||||
var projectile = BulletScene.Instantiate<Bullet>();
|
||||
projectile.Transform = marker.GlobalTransform;
|
||||
projectile.PlayerId = PlayerId;
|
||||
|
||||
GetParent().AddChild(projectile);
|
||||
AmmoAmount--;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user