🐛 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="Script" path="res://Scripts/Brick.cs" id="1_u0jqj"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5wmf4"]
size = Vector2(51.2, 51.2)
@ -8,9 +9,16 @@ size = Vector2(51.2, 51.2)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_r3565"]
properties/0/path = NodePath(".:position")
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"]
script = ExtResource("1_u0jqj")
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(2.08165e-12, 2.08165e-12)
@ -21,7 +29,7 @@ texture = ExtResource("1_tqbee")
shape = SubResource("RectangleShape2D_5wmf4")
[node name="DecayTimer" type="Timer" parent="."]
one_shot = true
wait_time = 0.1
autostart = true
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]

View File

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

@ -19,8 +19,8 @@ public partial class Player : CharacterBody2D
[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 int AmmoAmount = 30;
[Export] public int MaxAmmoAmount = 30;
[Export] public Camera2D PlayerCamera;
[Export] public PlayerInput PlayerInput;
@ -78,10 +78,18 @@ public partial class Player : CharacterBody2D
if (PlayerInput.IsShooting)
{
PlayerInput.IsShooting = false;
Shoot();
var name = GD.Randi();
Rpc(nameof(Shoot), name.ToString());
}
if (PlayerInput.IsReloading)
{
if (AmmoAmount == MaxAmmoAmount)
{
PlayerInput.IsReloading = false;
}
else
{
var timer = GetNode<Timer>("ReloadTimer");
if (timer.IsStopped())
@ -90,6 +98,7 @@ public partial class Player : CharacterBody2D
timer.Start();
}
}
}
if (PlayerInput.IsBuilding)
{
@ -100,7 +109,8 @@ public partial class Player : CharacterBody2D
if (distance <= Reach * TileSize)
{
var name = GD.Randi();
Rpc(nameof(AddTile), target, name.ToString());
if (GetParent<World>().GetTileByPosition<Node2D>(target) == null)
Rpc(nameof(AddTile), target, PlayerId, name.ToString());
}
}
}
@ -141,12 +151,14 @@ public partial class Player : CharacterBody2D
Rpc(nameof(GotDamage), damage);
}
private void Shoot()
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void Shoot(string name)
{
if (AmmoAmount <= 0) return;
var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
var projectile = BulletScene.Instantiate<Bullet>();
projectile.Name = $"Bullet@{name}";
projectile.Transform = marker.GlobalTransform;
projectile.PlayerId = PlayerId;
@ -154,6 +166,17 @@ public partial class Player : CharacterBody2D
AmmoAmount--;
}
[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(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void GotDamage(double damage)
@ -174,17 +197,4 @@ public partial class Player : CharacterBody2D
var shakableCamera = GetNode<CameraShake>("Camera2D");
shakableCamera.AddTrauma(0.5f);
}
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void AddTile(Vector2 pos, string name)
{
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/icon="res://icon.svg"
[display]
window/stretch/mode="canvas_items"
[dotnet]
project/assembly_name="AceField"
@ -48,7 +52,7 @@ move_dash={
}
weapon_shoot={
"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={
@ -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)
]
}
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]