Block placing

This commit is contained in:
LittleSheep 2024-08-05 23:54:22 +08:00
parent 3788e640a9
commit eb29e137a6
14 changed files with 282 additions and 42 deletions

View File

@ -5,7 +5,7 @@
[ext_resource type="Script" path="res://Scripts/Logic/PlayerInput.cs" id="3_tvoua"] [ext_resource type="Script" path="res://Scripts/Logic/PlayerInput.cs" id="3_tvoua"]
[sub_resource type="CircleShape2D" id="CircleShape2D_68yf8"] [sub_resource type="CircleShape2D" id="CircleShape2D_68yf8"]
radius = 26.0768 radius = 20.48
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_bekoo"] [sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_bekoo"]
properties/0/path = NodePath(".:position") properties/0/path = NodePath(".:position")
@ -23,15 +23,14 @@ properties/1/path = NodePath("InputSynchronizer:MovementDirection")
properties/1/spawn = true properties/1/spawn = true
properties/1/replication_mode = 1 properties/1/replication_mode = 1
[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("PlayerCamera", "PlayerInput", "PlayerDashCountdown")] [node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("PlayerCamera", "PlayerInput")]
script = ExtResource("1_0btyt") script = ExtResource("1_0btyt")
PlayerCamera = NodePath("Camera2D") PlayerCamera = NodePath("Camera2D")
PlayerInput = NodePath("InputSynchronizer") PlayerInput = NodePath("InputSynchronizer")
PlayerDashCountdown = NodePath("DashCountdown")
[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)
scale = Vector2(0.05, 0.05) scale = Vector2(0.04, 0.04)
texture = ExtResource("1_cqpqa") texture = ExtResource("1_cqpqa")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]

View File

@ -1,23 +1,31 @@
[gd_scene load_steps=5 format=3 uid="uid://bjhmjrldq4lkt"] [gd_scene load_steps=6 format=3 uid="uid://bjhmjrldq4lkt"]
[ext_resource type="Script" path="res://Scripts/Multiplayer.cs" id="1_fym13"]
[ext_resource type="PackedScene" uid="uid://b3gx0bl43lku3" path="res://Scenes/Player.tscn" id="1_vby0g"] [ext_resource type="PackedScene" uid="uid://b3gx0bl43lku3" path="res://Scenes/Player.tscn" id="1_vby0g"]
[ext_resource type="PackedScene" uid="uid://bvll23f5ibd4v" path="res://Scenes/UI/MultiplayerUI.tscn" id="2_7o53i"] [ext_resource type="PackedScene" uid="uid://bvll23f5ibd4v" path="res://Scenes/UI/StartScreen.tscn" id="2_7o53i"]
[ext_resource type="Script" path="res://Scripts/Launcher.cs" id="2_u5cms"]
[ext_resource type="Script" path="res://Scripts/Logic/World.cs" id="3_xwguj"] [ext_resource type="Script" path="res://Scripts/Logic/World.cs" id="3_xwguj"]
[ext_resource type="Script" path="res://Scripts/TilesManager.cs" id="5_5wdx5"]
[node name="Node" type="Node"] [node name="Node" type="Node"]
[node name="MultiplayerNode" type="Node" parent="." node_paths=PackedStringArray("World")] [node name="StartScreen" parent="." node_paths=PackedStringArray("Launcher") instance=ExtResource("2_7o53i")]
script = ExtResource("1_fym13") Launcher = NodePath("../LauncherNode")
World = NodePath("../World")
[node name="MultiplayerUi" parent="." node_paths=PackedStringArray("MultiplayerController") instance=ExtResource("2_7o53i")] [node name="LauncherNode" type="Node" parent="." node_paths=PackedStringArray("World")]
MultiplayerController = NodePath("../MultiplayerNode") script = ExtResource("2_u5cms")
World = NodePath("../World")
[node name="World" type="Node2D" parent="."] [node name="World" type="Node2D" parent="."]
script = ExtResource("3_xwguj") script = ExtResource("3_xwguj")
PlayerScene = ExtResource("1_vby0g") PlayerScene = ExtResource("1_vby0g")
[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="."] [node name="TilesSpawner" type="MultiplayerSpawner" parent="World"]
_spawnable_scenes = PackedStringArray("res://Scenes/Tiles/Brick.tscn")
spawn_path = NodePath("../Tiles")
[node name="Tiles" type="Node2D" parent="World"]
script = ExtResource("5_5wdx5")
[node name="PlayerSpawner" type="MultiplayerSpawner" parent="."]
_spawnable_scenes = PackedStringArray("res://Scenes/Player.tscn") _spawnable_scenes = PackedStringArray("res://Scenes/Player.tscn")
spawn_path = NodePath("../World") spawn_path = NodePath("../World")

24
Scenes/Tiles/Brick.tscn Normal file
View File

@ -0,0 +1,24 @@
[gd_scene load_steps=4 format=3 uid="uid://nj0k4l4mgq6"]
[ext_resource type="Texture2D" uid="uid://c2qpm7mcrvq57" path="res://Sprites/Brick.png" id="1_fms8g"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_saayk"]
size = Vector2(51.2, 51.2)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_fbc2m"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/replication_mode = 1
[node name="Brick" type="StaticBody2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(2.08165e-12, 2.08165e-12)
scale = Vector2(0.05, 0.05)
texture = ExtResource("1_fms8g")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_saayk")
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_fbc2m")

View File

@ -1,8 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://bvll23f5ibd4v"] [gd_scene load_steps=2 format=3 uid="uid://bvll23f5ibd4v"]
[ext_resource type="Script" path="res://Scripts/UI/MultiplayerUi.cs" id="1_fm6j5"] [ext_resource type="Script" path="res://Scripts/UI/StartScreen.cs" id="1_f8by8"]
[node name="MultiplayerUi" type="Control" node_paths=PackedStringArray("ServerPortInput", "ServerAddrInput", "StartAsServerButton", "StartAsClientButton")] [node name="StartScreen" type="Control" node_paths=PackedStringArray("ServerPortInput", "ServerAddrInput", "StartAsSingleButton", "StartAsServerButton", "StartAsClientButton")]
process_mode = 3 process_mode = 3
layout_mode = 3 layout_mode = 3
anchors_preset = 15 anchors_preset = 15
@ -10,11 +10,12 @@ anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_fm6j5") script = ExtResource("1_f8by8")
ServerPortInput = NodePath("CenterContainer/VBoxContainer/PortEdit") ServerPortInput = NodePath("CenterContainer/VBoxContainer/PortEdit")
ServerAddrInput = NodePath("CenterContainer/VBoxContainer/AddrEdit") ServerAddrInput = NodePath("CenterContainer/VBoxContainer/AddrEdit")
StartAsServerButton = NodePath("CenterContainer/VBoxContainer/HBoxContainer/HostButton") StartAsSingleButton = NodePath("CenterContainer/VBoxContainer/PlayAloneButton")
StartAsClientButton = NodePath("CenterContainer/VBoxContainer/HBoxContainer/ConnectButton") StartAsServerButton = NodePath("CenterContainer/VBoxContainer/MultiplayerActions/HostButton")
StartAsClientButton = NodePath("CenterContainer/VBoxContainer/MultiplayerActions/ConnectButton")
[node name="CenterContainer" type="CenterContainer" parent="."] [node name="CenterContainer" type="CenterContainer" parent="."]
layout_mode = 0 layout_mode = 0
@ -24,7 +25,16 @@ offset_bottom = 648.0
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"] [node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer"]
layout_mode = 2 layout_mode = 2
[node name="Label" type="Label" parent="CenterContainer/VBoxContainer"] [node name="SinglePlayerTitle" type="Label" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Singleplayer"
horizontal_alignment = 1
[node name="PlayAloneButton" type="Button" parent="CenterContainer/VBoxContainer"]
layout_mode = 2
text = "Play Alone"
[node name="MultiplayerTitle" type="Label" parent="CenterContainer/VBoxContainer"]
layout_mode = 2 layout_mode = 2
text = "Mutiplayer" text = "Mutiplayer"
horizontal_alignment = 1 horizontal_alignment = 1
@ -37,13 +47,13 @@ placeholder_text = "Server port"
layout_mode = 2 layout_mode = 2
placeholder_text = "Server address" placeholder_text = "Server address"
[node name="HBoxContainer" type="HBoxContainer" parent="CenterContainer/VBoxContainer"] [node name="MultiplayerActions" type="HBoxContainer" parent="CenterContainer/VBoxContainer"]
layout_mode = 2 layout_mode = 2
[node name="HostButton" type="Button" parent="CenterContainer/VBoxContainer/HBoxContainer"] [node name="HostButton" type="Button" parent="CenterContainer/VBoxContainer/MultiplayerActions"]
layout_mode = 2 layout_mode = 2
text = "Host a Game" text = "Host a Game"
[node name="ConnectButton" type="Button" parent="CenterContainer/VBoxContainer/HBoxContainer"] [node name="ConnectButton" type="Button" parent="CenterContainer/VBoxContainer/MultiplayerActions"]
layout_mode = 2 layout_mode = 2
text = "Connect a Game" text = "Connect a Game"

View File

@ -3,7 +3,7 @@ using Godot;
namespace CodingLand.Scripts; namespace CodingLand.Scripts;
public partial class Multiplayer : Node public partial class Launcher : Node
{ {
[Export] public World World; [Export] public World World;
@ -30,6 +30,12 @@ public partial class Multiplayer : Node
} }
} }
public void StartAsSingle()
{
GameUnfreeze();
World.StartGame();
}
public bool StartAsServer(int port) public bool StartAsServer(int port)
{ {
var peer = new ENetMultiplayerPeer(); var peer = new ENetMultiplayerPeer();

View File

@ -6,22 +6,26 @@ public partial class PlayerInput : MultiplayerSynchronizer
{ {
[Export] public bool IsDashing; [Export] public bool IsDashing;
[Export] public Vector2 BuildingAt;
[Export] public Vector2 MovementDirection; [Export] public Vector2 MovementDirection;
private bool IsCurrentPlayer => GetMultiplayerAuthority() == Multiplayer.GetUniqueId();
public override void _Ready() public override void _Ready()
{ {
if (GetMultiplayerAuthority() != Multiplayer.GetUniqueId()) if (IsCurrentPlayer) return;
{ SetProcess(false);
SetProcess(false); SetPhysicsProcess(false);
SetPhysicsProcess(false);
}
} }
[Rpc(CallLocal = true)] [Rpc(CallLocal = true)]
private void Dash() private void Dash()
{ => IsDashing = true;
IsDashing = true;
} [Rpc(CallLocal = true)]
private void Build(Vector2 pos)
=> BuildingAt = pos;
public override void _Process(double delta) public override void _Process(double delta)
{ {
@ -30,4 +34,11 @@ public partial class PlayerInput : MultiplayerSynchronizer
if (Input.IsActionJustPressed("move_dash")) if (Input.IsActionJustPressed("move_dash"))
Rpc(nameof(Dash)); Rpc(nameof(Dash));
} }
public override void _Input(InputEvent evt)
{
if (!IsCurrentPlayer) return;
if (evt is InputEventMouseButton { Pressed: true })
Rpc(nameof(Build), GetViewport().GetMousePosition());
}
} }

View File

@ -1,4 +1,3 @@
using System.Numerics;
using Godot; using Godot;
using Vector2 = Godot.Vector2; using Vector2 = Godot.Vector2;

View File

@ -12,14 +12,16 @@ public partial class Player : CharacterBody2D
[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 Camera2D PlayerCamera; [Export] public Camera2D PlayerCamera;
[Export] public PlayerInput PlayerInput; [Export] public PlayerInput PlayerInput;
[Export] public Timer PlayerDashCountdown;
[Export] public float PlayerDashAcceleration = 2f; [Export] public float PlayerDashAcceleration = 2f;
[Export] public int PlayerId [Export]
public int PlayerId
{ {
get => _currentPlayerId; get => _currentPlayerId;
set set
@ -35,17 +37,33 @@ public partial class Player : CharacterBody2D
PlayerInput.SetMultiplayerAuthority(id); PlayerInput.SetMultiplayerAuthority(id);
} }
private TilesManager _tilesMgr;
public override void _Ready() public override void _Ready()
{ {
if (PlayerId == Multiplayer.GetUniqueId()) if (PlayerId == Multiplayer.GetUniqueId())
PlayerCamera.Enabled = true; PlayerCamera.Enabled = true;
_tilesMgr = GetNode<TilesManager>("../Tiles");
}
public override void _Process(double delta)
{
var vec = GetGlobalMousePosition();
if (PlayerInput.BuildingAt == Vector2.Zero) return;
var distance = Position.DistanceTo(vec);
if (distance <= Reach * _tilesMgr.TileSize)
{
// Able to build
Rpc(nameof(AddTile), vec);
}
PlayerInput.BuildingAt = Vector2.Zero;
} }
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
var input = PlayerInput.MovementDirection; var input = PlayerInput.MovementDirection;
if (input != Vector2.Zero) if (input != Vector2.Zero)
{ {
input = input.Normalized(); input = input.Normalized();
@ -59,14 +77,27 @@ public partial class Player : CharacterBody2D
Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta); Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
} }
if (PlayerInput.IsDashing && PlayerDashCountdown.IsStopped()) var dashCountdown = GetNode<Timer>("DashCountdown");
if (PlayerInput.IsDashing && dashCountdown.IsStopped())
{ {
PlayerInput.IsDashing = false; PlayerInput.IsDashing = false;
Velocity *= PlayerDashAcceleration; Velocity *= PlayerDashAcceleration;
PlayerDashCountdown.Start(); dashCountdown.Start();
} }
Position += Velocity * (float)delta; Position += Velocity * (float)delta;
MoveAndSlide(); MoveAndSlide();
} }
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void AddTile(Vector2 pos)
{
if (_tilesMgr.GetTileByPosition<Node2D>(pos) != null) return;
var tileVec = new Vector2(_tilesMgr.TileSize, _tilesMgr.TileSize);
var blueprint = GD.Load<PackedScene>("res://Scenes/Tiles/Brick.tscn");
var instance = blueprint.Instantiate<Node2D>();
instance.Position = pos.Snapped(tileVec);
_tilesMgr.AddChild(instance);
}
} }

35
Scripts/TilesManager.cs Normal file
View File

@ -0,0 +1,35 @@
using Godot;
namespace CodingLand.Scripts;
public partial class TilesManager : Node2D
{
[Export] public float TileSize = 51.2f;
[Rpc(CallLocal = true)]
public void AddTile(Vector2 pos)
{
if (GetTileByPosition<Node2D>(pos) == null)
{
var tileVec = new Vector2(TileSize, TileSize);
// TODO Replace the brick to player selection
var blueprint = GD.Load<PackedScene>("res://Scenes/Tiles/Brick.tscn");
var instance = blueprint.Instantiate<Node2D>();
instance.Position = pos.Snapped(tileVec);
AddChild(instance);
}
}
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;
}
}

View File

@ -2,15 +2,16 @@ using Godot;
namespace CodingLand.Scripts.UI; namespace CodingLand.Scripts.UI;
public partial class MultiplayerUi : Control public partial class StartScreen : Control
{ {
[Export] public int DefaultServerPort = 4343; [Export] public int DefaultServerPort = 4343;
[Export] public string DefaultServerAddr = "127.0.0.1"; [Export] public string DefaultServerAddr = "127.0.0.1";
[Export] public Multiplayer MultiplayerController; [Export] public Launcher Launcher;
[Export] public LineEdit ServerPortInput; [Export] public LineEdit ServerPortInput;
[Export] public LineEdit ServerAddrInput; [Export] public LineEdit ServerAddrInput;
[Export] public Button StartAsSingleButton;
[Export] public Button StartAsServerButton; [Export] public Button StartAsServerButton;
[Export] public Button StartAsClientButton; [Export] public Button StartAsClientButton;
@ -41,6 +42,11 @@ public partial class MultiplayerUi : Control
ServerPortInput.Text = DefaultServerPort.ToString(); ServerPortInput.Text = DefaultServerPort.ToString();
ServerAddrInput.Text = DefaultServerAddr; ServerAddrInput.Text = DefaultServerAddr;
StartAsSingleButton.Pressed += () =>
{
Launcher.StartAsSingle();
Hide();
};
StartAsServerButton.Pressed += () => StartAsServerButton.Pressed += () =>
{ {
if (!DoValidation()) if (!DoValidation())
@ -50,7 +56,7 @@ public partial class MultiplayerUi : Control
} }
var port = ServerPortInput.Text; var port = ServerPortInput.Text;
var result = MultiplayerController.StartAsServer(int.Parse(port)); var result = Launcher.StartAsServer(int.Parse(port));
if (result) if (result)
Hide(); Hide();
@ -65,7 +71,7 @@ public partial class MultiplayerUi : Control
var addr = ServerAddrInput.Text; var addr = ServerAddrInput.Text;
var port = ServerPortInput.Text; var port = ServerPortInput.Text;
var result = MultiplayerController.StartAsClient(addr, int.Parse(port)); var result = Launcher.StartAsClient(addr, int.Parse(port));
if (result) if (result)
Hide(); Hide();

BIN
Sprites/Brick.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

34
Sprites/Brick.png.import Normal file
View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://c2qpm7mcrvq57"
path="res://.godot/imported/Brick.png-618f5af6fe8b4d7875a5284d9ba4a8f0.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Sprites/Brick.png"
dest_files=["res://.godot/imported/Brick.png-618f5af6fe8b4d7875a5284d9ba4a8f0.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

76
export_presets.cfg Normal file
View File

@ -0,0 +1,76 @@
[preset.0]
name="iOS"
platform="iOS"
runnable=true
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=""
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false
[preset.0.options]
custom_template/debug=""
custom_template/release=""
architectures/arm64=true
application/app_store_team_id=""
application/code_sign_identity_debug=""
application/export_method_debug=1
application/code_sign_identity_release=""
application/export_method_release=0
application/targeted_device_family=2
application/bundle_identifier=""
application/signature=""
application/short_version=""
application/version=""
application/icon_interpolation=4
application/launch_screens_interpolation=4
application/export_project_only=false
capabilities/access_wifi=false
capabilities/push_notifications=false
user_data/accessible_from_files_app=false
user_data/accessible_from_itunes_sharing=false
privacy/camera_usage_description=""
privacy/camera_usage_description_localized={}
privacy/microphone_usage_description=""
privacy/microphone_usage_description_localized={}
privacy/photolibrary_usage_description=""
privacy/photolibrary_usage_description_localized={}
icons/iphone_120x120=""
icons/iphone_180x180=""
icons/ipad_76x76=""
icons/ipad_152x152=""
icons/ipad_167x167=""
icons/app_store_1024x1024=""
icons/spotlight_40x40=""
icons/spotlight_80x80=""
icons/settings_58x58=""
icons/settings_87x87=""
icons/notification_40x40=""
icons/notification_60x60=""
storyboard/use_launch_screen_storyboard=true
storyboard/image_scale_mode=0
storyboard/custom_image@2x=""
storyboard/custom_image@3x=""
storyboard/use_custom_bg_color=false
storyboard/custom_bg_color=Color(0, 0, 0, 1)
landscape_launch_screens/iphone_2436x1125=""
landscape_launch_screens/iphone_2208x1242=""
landscape_launch_screens/ipad_1024x768=""
landscape_launch_screens/ipad_2048x1536=""
portrait_launch_screens/iphone_640x960=""
portrait_launch_screens/iphone_640x1136=""
portrait_launch_screens/iphone_750x1334=""
portrait_launch_screens/iphone_1125x2436=""
portrait_launch_screens/ipad_768x1024=""
portrait_launch_screens/ipad_1536x2048=""
portrait_launch_screens/iphone_1242x2208=""
dotnet/include_scripts_content=false
dotnet/include_debug_symbols=true
dotnet/embed_build_outputs=false

View File

@ -50,3 +50,4 @@ move_dash={
[rendering] [rendering]
renderer/rendering_method="mobile" renderer/rendering_method="mobile"
textures/vram_compression/import_etc2_astc=true