diff --git a/Game.cs b/Game.cs index 4b60e95..91d576e 100644 --- a/Game.cs +++ b/Game.cs @@ -4,9 +4,13 @@ public partial class Game : Node { public static Game Instance { get; private set; } public static new MultiplayerManager Multiplayer { get; private set; } + public static Players Players { get; private set; } public static Player LocalPlayer { get; private set; } + public static Workshops Workshops { get; private set; } + public static Workshop LocalWorkshop { get; private set; } + public override void _EnterTree() { // Set invariant culture so formatting is consistent. @@ -14,7 +18,11 @@ public partial class Game : Node Instance = this; Multiplayer = this.GetNodeOrThrow(nameof(MultiplayerManager)); + Players = this.GetNodeOrThrow(nameof(Players)); LocalPlayer = Players.Single(); + + Workshops = this.GetNodeOrThrow(nameof(Workshops)); + LocalWorkshop = Workshops.Single(); } } diff --git a/game.tscn b/game.tscn index ea5c7ad..7de7f9b 100644 --- a/game.tscn +++ b/game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=3 uid="uid://cqitgdxo33amx"] +[gd_scene load_steps=12 format=3 uid="uid://cqitgdxo33amx"] [ext_resource type="Script" path="res://scripts/MultiplayerManager.cs" id="1_7shyh"] [ext_resource type="Script" path="res://Game.cs" id="1_uywdd"] @@ -7,6 +7,7 @@ [ext_resource type="Script" path="res://scripts/Players.cs" id="4_l8q75"] [ext_resource type="Material" uid="uid://c0q35rri3vb07" path="res://assets/shaders/outline_material.tres" id="5_a3fxj"] [ext_resource type="Script" path="res://scripts/OutlineCamera.cs" id="5_qpc14"] +[ext_resource type="Script" path="res://scripts/Workshops.cs" id="5_rns2j"] [ext_resource type="PackedScene" uid="uid://c5ooi36ibspfo" path="res://ui/menu.tscn" id="6_ol0j5"] [ext_resource type="Texture2D" uid="uid://lxxfestfg2dt" path="res://assets/crosshair.png" id="7_0l5tv"] [ext_resource type="Script" path="res://scripts/Crosshair.cs" id="8_mfhgr"] @@ -24,6 +25,7 @@ script = ExtResource("4_l8q75") [node name="1" parent="Players" instance=ExtResource("2_iv2f7")] [node name="Workshops" type="Node" parent="."] +script = ExtResource("5_rns2j") [node name="1" parent="Workshops" instance=ExtResource("3_4u5ql")] diff --git a/player/Player.cs b/player/Player.cs index 0d3475d..07f65cd 100644 --- a/player/Player.cs +++ b/player/Player.cs @@ -1,8 +1,15 @@ public partial class Player : CharacterBody3D { - public int PeerId => GetMultiplayerAuthority(); + /// Returns whether is the local player. public bool IsLocal => this.IsAuthority(); + /// Gets the peer ID of this player. + public int PeerId => GetMultiplayerAuthority(); + + /// Gets the workshop owned by this player. + public Workshop Workshop => Game.Workshops.ByPeerId(PeerId); + + public MovementController Movement { get; private set; } public CameraController Camera { get; private set; } public AnimationController Animation { get; private set; } diff --git a/scenes/Workshop.cs b/scenes/Workshop.cs new file mode 100644 index 0000000..6b73de9 --- /dev/null +++ b/scenes/Workshop.cs @@ -0,0 +1,11 @@ +public partial class Workshop : Area3D +{ + /// Returns whether this workshop is owned by the local player. + public bool IsLocal => this.IsAuthority(); + + /// Gets the peer ID of the player owning this workshop. + public int PeerId => GetMultiplayerAuthority(); + + /// Gets the player that owns this workshop. + public Player Player => Game.Players.ByPeerId(PeerId); +} diff --git a/scenes/workshop.tscn b/scenes/workshop.tscn index 08db2fc..9e6dbe3 100644 --- a/scenes/workshop.tscn +++ b/scenes/workshop.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=14 format=3 uid="uid://bwfuet1irfi17"] +[gd_scene load_steps=16 format=3 uid="uid://bwfuet1irfi17"] +[ext_resource type="Script" path="res://scenes/Workshop.cs" id="1_i8qyc"] [ext_resource type="Script" path="res://scenes/ItemManager.cs" id="1_l6hw6"] [ext_resource type="PackedScene" uid="uid://yvy5vvaqgxy8" path="res://objects/crate.tscn" id="2_j6a20"] [ext_resource type="Texture2D" uid="uid://dts3g3ivc4stn" path="res://assets/palettes/metal.png" id="3_kvstu"] @@ -9,6 +10,9 @@ [ext_resource type="PackedScene" uid="uid://bjgfm5x7a0dab" path="res://objects/bolt.tscn" id="5_r6ljd"] [ext_resource type="PackedScene" uid="uid://54575e3ygpxl" path="res://objects/grid.tscn" id="6_okibm"] +[sub_resource type="BoxShape3D" id="BoxShape3D_q481w"] +size = Vector3(15, 5, 15) + [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_5erfn"] [sub_resource type="PlaneMesh" id="PlaneMesh_tg4vq"] @@ -23,11 +27,18 @@ size = Vector3(2, 0.1, 1) [sub_resource type="BoxMesh" id="BoxMesh_efbik"] size = Vector3(0.1, 0.9, 0.1) -[node name="Workshop" type="Node3D"] +[node name="Workshop" type="Area3D"] +collision_layer = 0 +collision_mask = 0 +script = ExtResource("1_i8qyc") [node name="ItemManager" type="Node" parent="."] script = ExtResource("1_l6hw6") +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0) +shape = SubResource("BoxShape3D_q481w") + [node name="Sun" type="DirectionalLight3D" parent="."] transform = Transform3D(0.866025, 0, -0.5, 0.25, 0.866025, 0.433013, 0.433013, -0.5, 0.75, 0, 5, 0) diff --git a/scripts/Players.cs b/scripts/Players.cs index 8e7ebee..88b7e73 100644 --- a/scripts/Players.cs +++ b/scripts/Players.cs @@ -1,12 +1,13 @@ public partial class Players : Node , IReadOnlyCollection { - public int Count - => GetChildCount(); - public Player ByPeerId(int peerId) => this.GetNodeOrThrow(peerId.ToString()); + // IReadOnlyCollection implementation + + public int Count + => GetChildCount(); public IEnumerator GetEnumerator() => GetChildren().Cast().GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() diff --git a/scripts/Workshops.cs b/scripts/Workshops.cs new file mode 100644 index 0000000..e6c4c2a --- /dev/null +++ b/scripts/Workshops.cs @@ -0,0 +1,15 @@ +public partial class Workshops : Node + , IReadOnlyCollection +{ + public Workshop ByPeerId(int peerId) + => this.GetNodeOrThrow(peerId.ToString()); + + // IReadOnlyCollection implementation + + public int Count + => GetChildCount(); + public IEnumerator GetEnumerator() + => GetChildren().Cast().GetEnumerator(); + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + => GetEnumerator(); +}