🐛 Fix buges

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

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=4 format=3 uid="uid://nu34biv4xo5k"] [gd_scene load_steps=5 format=3 uid="uid://nu34biv4xo5k"]
[ext_resource type="Texture2D" uid="uid://c2qpm7mcrvq57" path="res://Sprites/Brick.png" id="1_tqbee"] [ext_resource type="Texture2D" uid="uid://c2qpm7mcrvq57" path="res://Sprites/Brick.png" id="1_tqbee"]
[ext_resource type="Script" path="res://Scripts/Brick.cs" id="1_u0jqj"]
[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)
@ -8,9 +9,16 @@ size = Vector2(51.2, 51.2)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_r3565"] [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_r3565"]
properties/0/path = NodePath(".:position") properties/0/path = NodePath(".:position")
properties/0/spawn = true properties/0/spawn = true
properties/0/replication_mode = 1 properties/0/replication_mode = 0
properties/1/path = NodePath(".:DecayProgress")
properties/1/spawn = true
properties/1/replication_mode = 0
properties/2/path = NodePath(".:MaxDecayProgress")
properties/2/spawn = true
properties/2/replication_mode = 0
[node name="Brick" type="StaticBody2D"] [node name="Brick" type="StaticBody2D"]
script = ExtResource("1_u0jqj")
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(2.08165e-12, 2.08165e-12) position = Vector2(2.08165e-12, 2.08165e-12)
@ -21,7 +29,7 @@ texture = ExtResource("1_tqbee")
shape = SubResource("RectangleShape2D_5wmf4") shape = SubResource("RectangleShape2D_5wmf4")
[node name="DecayTimer" type="Timer" parent="."] [node name="DecayTimer" type="Timer" parent="."]
one_shot = true wait_time = 0.1
autostart = true autostart = true
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."] [node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]

View File

@ -25,6 +25,6 @@ World = NodePath("../../World")
script = ExtResource("3_xwguj") script = ExtResource("3_xwguj")
PlayerScene = ExtResource("1_vby0g") PlayerScene = ExtResource("1_vby0g")
[node name="WorldSpawner" type="MultiplayerSpawner" parent="."] [node name="PlayerSpawner" type="MultiplayerSpawner" parent="."]
_spawnable_scenes = PackedStringArray("res://Scenes/Player.tscn", "res://Scenes/Brick.tscn") _spawnable_scenes = PackedStringArray("res://Scenes/Player.tscn")
spawn_path = NodePath("../World") spawn_path = NodePath("../World")

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 => BodyEntered += body =>
{ {
if (body is not Player player || player.PlayerId == PlayerId) return; if (body is Player player)
if (body is Player p)
{ {
p.TakeDamage(Damage); if (player.PlayerId == PlayerId) return;
player.TakeDamage(Damage);
} }
QueueFree(); QueueFree();

View File

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

View File

@ -6,185 +6,195 @@ 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 double AmmoAmount = 30; [Export] public int AmmoAmount = 30;
[Export] public double 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;
Shoot();
}
if (PlayerInput.IsReloading) var name = GD.Randi();
{ Rpc(nameof(Shoot), name.ToString());
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();
Rpc(nameof(AddTile), target, 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) Position += Velocity * (float)delta;
{ MoveAndSlide();
Rpc(nameof(GotDamage), damage); }
}
private void Shoot() public void TakeDamage(double damage)
{ {
if (AmmoAmount <= 0) return; Rpc(nameof(GotDamage), damage);
}
var marker = GetNode<Marker2D>("RotationCentre/Muzzle"); [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
var projectile = BulletScene.Instantiate<Bullet>(); private void Shoot(string name)
projectile.Transform = marker.GlobalTransform; {
projectile.PlayerId = PlayerId; if (AmmoAmount <= 0) return;
GetParent().AddChild(projectile); var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
AmmoAmount--; var projectile = BulletScene.Instantiate<Bullet>();
} projectile.Name = $"Bullet@{name}";
projectile.Transform = marker.GlobalTransform;
projectile.PlayerId = PlayerId;
GetParent().AddChild(projectile);
AmmoAmount--;
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void GotDamage(double damage) public void AddTile(Vector2 pos, int playerId, string name)
{ {
var protection = GetNode<Timer>("ProtectionCountdown"); 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);
}
if (protection.IsStopped()) [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
{ private void GotDamage(double damage)
Health -= damage; {
var protection = GetNode<Timer>("ProtectionCountdown");
var tween = CreateTween(); if (protection.IsStopped())
var bar = GetNode<ProgressBar>("Overlay/HealthBar"); {
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3); Health -= damage;
protection.Start(); var tween = CreateTween();
} var bar = GetNode<ProgressBar>("Overlay/HealthBar");
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
var shakableCamera = GetNode<CameraShake>("Camera2D"); protection.Start();
shakableCamera.AddTrauma(0.5f); }
}
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)] var shakableCamera = GetNode<CameraShake>("Camera2D");
public void AddTile(Vector2 pos, string name) shakableCamera.AddTrauma(0.5f);
{ }
if (GetParent<World>().GetTileByPosition<Node2D>(pos) != null) return;
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);
}
} }

View File

@ -15,6 +15,10 @@ run/main_scene="res://Scenes/Root.tscn"
config/features=PackedStringArray("4.2", "C#", "Mobile") config/features=PackedStringArray("4.2", "C#", "Mobile")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[display]
window/stretch/mode="canvas_items"
[dotnet] [dotnet]
project/assembly_name="AceField" project/assembly_name="AceField"
@ -48,7 +52,7 @@ move_dash={
} }
weapon_shoot={ weapon_shoot={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":102,"echo":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(166, 20),"global_position":Vector2(174, 100),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }
weapon_reload={ weapon_reload={
@ -56,6 +60,11 @@ weapon_reload={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null)
] ]
} }
skill_place_tile={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(174, 42),"global_position":Vector2(182, 122),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
[rendering] [rendering]