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

@ -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]