Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
85
Assets/Scripts/Gameplay/Spawner.cs
Normal file
85
Assets/Scripts/Gameplay/Spawner.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>Server-side spawning of everything dynamic: crew state, the starter kit, cargo, builds and machines.</summary>
|
||||
public class Spawner : MonoBehaviour
|
||||
{
|
||||
public static Spawner Instance { get; private set; }
|
||||
|
||||
public NetworkObject teamStatePrefab;
|
||||
public NetworkObject[] cargoPrefabs = new NetworkObject[3]; // by CargoKind
|
||||
public NetworkObject[] vehiclePrefabs = new NetworkObject[(int)VehicleKind.Count];
|
||||
public NetworkObject[] piecePrefabs; // by PieceId
|
||||
public Transform dock; // DockZone: vehicles spawn along it
|
||||
|
||||
NetworkManager Nm => NetworkManager.Singleton;
|
||||
TeamState Ts => TeamState.Instance;
|
||||
|
||||
void Awake() { Instance = this; }
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (NetSession.Instance) NetSession.Instance.Started += OnSessionStarted;
|
||||
}
|
||||
|
||||
void OnSessionStarted()
|
||||
{
|
||||
if (!Nm.IsServer) return;
|
||||
var ts = Instantiate(teamStatePrefab);
|
||||
ts.Spawn(true);
|
||||
}
|
||||
|
||||
// ───────────────────────── starter kit ─────────────────────────
|
||||
|
||||
/// <summary>A fresh base: the grass depot is all you get. Company property, can't be removed.</summary>
|
||||
public void ServerSpawnStarter()
|
||||
{
|
||||
var ts = Ts;
|
||||
if (ts) ts.ServerSpawnPiece(PieceId.Depot, new Vector3(-2f, 0f, -1f), Quaternion.identity, Vector3.zero, -1, true);
|
||||
}
|
||||
|
||||
// ───────────────────────── cargo ─────────────────────────
|
||||
|
||||
public Cargo SpawnCargo(CargoKind kind, Vector3 pos, Quaternion rot, Vector3 velocity, float kg = GameDefs.BundleKg)
|
||||
{
|
||||
var prefab = cargoPrefabs[(int)kind];
|
||||
if (!prefab || !Nm.IsServer) return null;
|
||||
var no = Instantiate(prefab, pos, rot);
|
||||
var c = no.GetComponent<Cargo>();
|
||||
c.Kg.Value = kg;
|
||||
no.Spawn(true);
|
||||
var rb = no.GetComponent<Rigidbody>();
|
||||
if (rb && !rb.isKinematic) rb.linearVelocity = velocity;
|
||||
return c;
|
||||
}
|
||||
|
||||
// ───────────────────────── builds ─────────────────────────
|
||||
|
||||
public BuildPiece SpawnPiece(PieceId id, Vector3 pos, Quaternion rot, Vector3 to, bool free)
|
||||
{
|
||||
var prefab = piecePrefabs != null && (int)id < piecePrefabs.Length ? piecePrefabs[(int)id] : null;
|
||||
if (!prefab) { Debug.LogWarning("[Spawner] no prefab for " + id); return null; }
|
||||
var no = Instantiate(prefab, pos, rot);
|
||||
var p = no.GetComponent<BuildPiece>();
|
||||
p.To.Value = to; p.Free.Value = free;
|
||||
no.Spawn(true);
|
||||
return p;
|
||||
}
|
||||
|
||||
// ───────────────────────── fleet ─────────────────────────
|
||||
|
||||
public Vehicle SpawnVehicle(VehicleKind kind, string crew, Vector3 pos, Quaternion rot)
|
||||
{
|
||||
var prefab = vehiclePrefabs[(int)kind];
|
||||
if (!prefab || !Nm.IsServer) return null;
|
||||
var no = Instantiate(prefab, pos, rot);
|
||||
var v = no.GetComponent<Vehicle>();
|
||||
v.Crew.Value = crew;
|
||||
no.Spawn(true);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user