protected override void OnUpdate() { if (mAlreadyCreated || ServerGameLoop.Instance == null || !ServerGameLoop.Instance.IsLevelLoaded()) { return; } for (int i = 0; i < 5; ++i) { try { var em = World.EntityManager; Entity e = ReplicatedPrefabMgr.CreateEntity("assets__newnetwork_prefab_barrel_scifi_a_new", World); em.AddComponent(e, typeof(RepBarrelTagComponentData)); em.AddComponent(e, typeof(Translation)); em.AddComponent(e, typeof(GhostComponent)); Transform tr = em.GetComponentObject <Transform>(e); tr.rotation = Quaternion.Euler(-90.0f, 0.0f, 0.0f); Translation translation = new Translation { Value = new float3(-40.0f, 6.5f, -20.0f + i * 3.0f) }; em.SetComponentData(e, translation); tr.position = translation.Value; } catch { } } mAlreadyCreated = true; }
public static Entity CreatePlayer(Entity networkConnectionEnt, int networkId) { var world = ClientServerSystemManager.serverWorld; var em = world.EntityManager; Entity ent = ReplicatedPrefabMgr.CreateEntity("assets_prefabs_cube", world, "CubePlayer"); em.AddComponent(ent, typeof(RepCubeComponentData)); em.AddBuffer <PlayerCommandData>(ent); em.AddComponent(ent, typeof(GhostComponent)); var ctc = em.GetComponentData <CommandTargetComponent>(networkConnectionEnt); ctc.targetEntity = ent; em.SetComponentData(networkConnectionEnt, ctc); var cubeData = new RepCubeComponentData { networkId = networkId, position = new Unity.Mathematics.float3( Random.Range(-5.0f, 5.0f), 0f, Random.Range(-5.0f, 5.0f)), color = new Unity.Mathematics.float3( Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)) }; em.SetComponentData(ent, cubeData); var transform = em.GetComponentObject <Transform>(ent); transform.GetComponent <Renderer>().material.color = new Color(cubeData.color.x, cubeData.color.y, cubeData.color.z); return(ent); }
protected override void OnUpdate() { var cubeEntities = createCubeGoQuery.ToEntityArray(Allocator.TempJob); for (int i = 0; i < cubeEntities.Length; ++i) { var cubeEnt = cubeEntities[i]; ReplicatedPrefabMgr.LoadPrefabIntoEntity("assets_prefabs_cube", World, cubeEnt); EntityManager.AddComponentData(cubeEnt, default(RepCubeGoCreated)); // TODO: LZ: // the value of the first snapshot seems to be buggy, so the following lines don't work well // we need to fix this! #if false var renderer = EntityManager.GetComponentObject <MeshRenderer>(cubeEnt); var cubeData = EntityManager.GetComponentData <RepCubeComponentData>(cubeEnt); renderer.material.color = new Color(cubeData.color.x, cubeData.color.y, cubeData.color.z); if (cubeData.networkId == ServerConnection.sNetworkId) { Console.SetColor(renderer.material.color); } Console.WriteLine(string.Format("Player(NetworkId={0}) joined.", cubeData.networkId)); #endif } cubeEntities.Dispose(); var goEntities = updateCubeGoQuery.ToEntityArray(Allocator.TempJob); for (int i = 0; i < goEntities.Length; ++i) { var goEnt = goEntities[i]; var transform = EntityManager.GetComponentObject <Transform>(goEnt); var cubeData = EntityManager.GetComponentData <RepCubeComponentData>(goEnt); transform.position = cubeData.position; var renderer = EntityManager.GetComponentObject <MeshRenderer>(goEnt); renderer.material.color = new Color(cubeData.color.x, cubeData.color.y, cubeData.color.z); if (cubeData.networkId == ServerConnection.sNetworkId) { SimpleConsole.SetColor(renderer.material.color); } } goEntities.Dispose(); }
// Update is called once per frame void OnGUI() { if (GUI.Button(new Rect(50, 50, 100, 50), "play1")) { if (vfx1 == null) { var vfxGo1 = new UnityEngine.GameObject("VfxGo1"); vfx1 = vfxGo1.AddComponent <UnityEngine.Experimental.VFX.VisualEffect>(); var grenadeClient = GetComponent <GrenadeClient>(); vfx1.visualEffectAsset = grenadeClient.explodeEffect.effect; vfxEventAttribute1 = vfx1.CreateVFXEventAttribute(); vfxEventAttribute1.SetVector3(positionID, new Vector3(20.0f, 0.0f, 0.0f)); } vfx1.Play(vfxEventAttribute1); } #if false if (vfx1 != null && Time.frameCount % 30 == 0) { vfx1.Play(vfxEventAttribute1); } #endif if (GUI.Button(new Rect(50, 150, 100, 50), "play2")) { if (vfx2 == null) { var vfxGo2 = new UnityEngine.GameObject("VfxGo2"); vfx2 = vfxGo2.AddComponent <UnityEngine.Experimental.VFX.VisualEffect>(); var grenadePrefab = ReplicatedPrefabMgr.GetPrefab("assets__newnetwork_prefab_robot_grenade"); var grenadeClient = grenadePrefab.GetComponent <GrenadeClient>(); vfx2.visualEffectAsset = grenadeClient.explodeEffect.effect; vfxEventAttribute2 = vfx2.CreateVFXEventAttribute(); vfxEventAttribute2.SetVector3(positionID, new Vector3(20.0f, 0.0f, 0.0f)); } vfx2.Play(vfxEventAttribute2); } }
protected override void OnUpdate() { if (ClientGameLoop.Instance == null || !ClientGameLoop.Instance.IsLevelLoaded()) { return; } if (!ReplicatedPrefabMgr.IsInitialized()) { return; } // barrel entities var barrelEntities = barrelQuery.GetEntityArraySt(); for (int i = 0; i < barrelEntities.Length; ++i) { var barrelEnt = barrelEntities[i]; if (!EntityManager.HasComponent <RepBarrelGoCreatedTag>(barrelEnt)) { ReplicatedPrefabMgr.LoadPrefabIntoEntity("assets__newnetwork_prefab_barrel_scifi_a_new", World, barrelEnt); EntityManager.AddComponentData(barrelEnt, default(RepBarrelGoCreatedTag)); // TODO: LZ: // rotate it with correct value for now var transform = EntityManager.GetComponentObject <Transform>(barrelEnt); transform.rotation = Quaternion.Euler(-90.0f, 0.0f, 0.0f); } } // barrel GameObjects var barrelGoEntities = barrelGoQuery.GetEntityArraySt(); for (int i = 0; i < barrelGoEntities.Length; ++i) { var barrelGoEnt = barrelGoEntities[i]; var transform = EntityManager.GetComponentObject <Transform>(barrelGoEnt); var translation = EntityManager.GetComponentData <Translation>(barrelGoEnt); transform.position = translation.Value; } }
protected override void OnUpdate() { if (ClientGameLoop.Instance == null || !ClientGameLoop.Instance.IsLevelLoaded()) { return; } if (!ReplicatedPrefabMgr.IsInitialized()) { return; } // grenade entities var grenadeEntities = grenadeQuery.GetEntityArraySt(); for (int i = 0; i < grenadeEntities.Length; ++i) { var grenadeEnt = grenadeEntities[i]; if (!EntityManager.HasComponent <RepGrenadeGoCreatedTag>(grenadeEnt)) { ReplicatedPrefabMgr.LoadPrefabIntoEntity("assets__newnetwork_prefab_robot_grenade", World, grenadeEnt); EntityManager.AddComponentData(grenadeEnt, default(RepGrenadeGoCreatedTag)); UnityEngine.Debug.Log(string.Format("LZ: new grenade entity: {0}", grenadeEnt)); } } // grenade GameObjects var grenadeGoEntities = grenadeGoQuery.GetEntityArraySt(); for (int i = 0; i < grenadeGoEntities.Length; ++i) { var grenadeGoEnt = grenadeGoEntities[i]; var transform = EntityManager.GetComponentObject <Transform>(grenadeGoEnt); var translation = EntityManager.GetComponentData <Translation>(grenadeGoEnt); transform.position = translation.Value; } }
public bool Init(string[] args) { m_StateMachine = new StateMachine <ClientState>(); m_StateMachine.Add(ClientState.Browsing, EnterBrowsingState, UpdateBrowsingState, LeaveBrowsingState); m_StateMachine.Add(ClientState.Connecting, EnterConnectingState, UpdateConnectingState, null); m_StateMachine.Add(ClientState.Loading, EnterLoadingState, UpdateLoadingState, null); m_StateMachine.Add(ClientState.Playing, EnterPlayingState, UpdatePlayingState, LeavePlayingState); #if UNITY_EDITOR Game.game.levelManager.UnloadLevel(); #endif m_GameWorld = new GameWorld("ClientWorld"); m_NetworkTransport = new SocketTransport(); m_NetworkClient = new NetworkClient(m_NetworkTransport); if (Application.isEditor || Game.game.buildId == "AutoBuild") { NetworkClient.clientVerifyProtocol.Value = "0"; } m_NetworkClient.UpdateClientConfig(); m_NetworkStatistics = new NetworkStatisticsClient(m_NetworkClient); m_ChatSystem = new ChatSystemClient(m_NetworkClient); GameDebug.Log("Network client initialized"); m_requestedPlayerSettings.playerName = clientPlayerName.Value; m_requestedPlayerSettings.teamId = -1; Console.AddCommand("disconnect", CmdDisconnect, "Disconnect from server if connected", this.GetHashCode()); Console.AddCommand("prediction", CmdTogglePrediction, "Toggle prediction", this.GetHashCode()); Console.AddCommand("runatserver", CmdRunAtServer, "Run command at server", this.GetHashCode()); Console.AddCommand("respawn", CmdRespawn, "Force a respawn", this.GetHashCode()); Console.AddCommand("nextchar", CmdNextChar, "Select next character", this.GetHashCode()); Console.AddCommand("nextteam", CmdNextTeam, "Select next character", this.GetHashCode()); Console.AddCommand("spectator", CmdSpectator, "Select spectator cam", this.GetHashCode()); Console.AddCommand("matchmake", CmdMatchmake, "matchmake <hostname[:port]/{projectid}>: Find and join a server", this.GetHashCode()); if (args.Length > 0) { targetServer = args[0]; m_StateMachine.SwitchTo(ClientState.Connecting); } else { m_StateMachine.SwitchTo(ClientState.Browsing); } ReplicatedPrefabMgr.Initialize(); ClientServerSystemManager.InitClientSystems(); World.Active.GetExistingSystem <TickClientSimulationSystem>().Enabled = true; World.Active.GetExistingSystem <TickClientPresentationSystem>().Enabled = true; Unity.Networking.Transport.NetworkEndPoint ep = Unity.Networking.Transport.NetworkEndPoint.Parse(targetServer, (ushort)NetworkConfig.netcodeServerPort); World clientWorld = ClientServerSystemManager.clientWorld; EntityManager em = clientWorld.EntityManager; Entity ent = clientWorld.GetExistingSystem <NetworkStreamReceiveSystem>().Connect(ep); em.AddComponentData(ent, new NetworkStreamInGame()); GameDebug.Log("Client initialized"); return(true); }
// TODO: LZ: // the input parameter should be the player // we can spawn the grenade with correct settings according to the input player public static void CreateGrenade(Transform headTr) { // TODO: LZ: // We may also need a nice server time system in NetCode. // Right now we only have NetCode.NetworkTimeSystem. This is client only. if (ServerGameLoop.Instance == null) { return; } var time = ServerGameLoop.Instance.GameWorld.worldTime; var pitchRot = quaternion.AxisAngle(headTr.right, -math.radians(10.0f)); var velocityDir = math.mul(pitchRot, headTr.forward); var world = ClientServerSystemManager.serverWorld; var em = world.EntityManager; var grenadeStartPos = headTr.position + headTr.forward * 2.0f; // TODO: LZ: // we only need to use a simple sphere collider on the server side Entity e = ReplicatedPrefabMgr.CreateEntity("assets__newnetwork_prefab_robot_grenade", world); em.AddComponent(e, typeof(RepGrenadeTagComponentData)); em.AddComponent(e, typeof(Translation)); em.AddComponent(e, typeof(GhostComponent)); em.AddComponent(e, typeof(GrenadeSimSettings)); em.AddComponent(e, typeof(GrenadeInternalState)); Translation translation = new Translation { Value = grenadeStartPos }; em.SetComponentData(e, translation); GrenadeSimSettings settings = new GrenadeSimSettings { maxLifetime = 4.0f, proximityTriggerDist = 1.0f, gravity = 18.0f, bounciness = 0.4f, collisionRadius = 0.2f }; em.SetComponentData(e, settings); GrenadeInternalState internalState = new GrenadeInternalState { active = 1, rayQueryId = -1, position = grenadeStartPos, velocity = velocityDir * 40.0f, owner = e, teamId = 0, // TODO: LZ: startTick = time.tick, explodeTick = 0 // TODO: LZ: }; em.SetComponentData(e, internalState); Transform tr = em.GetComponentObject <Transform>(e); tr.position = translation.Value; Debug.Log(string.Format("LZ: Create grenade {0}!", e.ToString())); }
// Start is called before the first frame update void Start() { ReplicatedPrefabMgr.Initialize(); }
public bool Init(string[] args) { ReplicatedPrefabMgr.Initialize(); ClientServerSystemManager.InitServerSystems(); World.Active.GetExistingSystem <TickServerSimulationSystem>().Enabled = true; Unity.Networking.Transport.NetworkEndPoint ep = Unity.Networking.Transport.NetworkEndPoint.AnyIpv4; ep.Port = (ushort)NetworkConfig.netcodeServerPort; World serverWorld = ClientServerSystemManager.serverWorld; serverWorld.GetExistingSystem <NetworkStreamReceiveSystem>().Listen(ep); // Set up statemachine for ServerGame m_StateMachine = new StateMachine <ServerState>(); m_StateMachine.Add(ServerState.Idle, null, UpdateIdleState, null); m_StateMachine.Add(ServerState.Loading, null, UpdateLoadingState, null); m_StateMachine.Add(ServerState.Active, EnterActiveState, UpdateActiveState, LeaveActiveState); m_StateMachine.SwitchTo(ServerState.Idle); m_NetworkTransport = new SocketTransport(NetworkConfig.serverPort.IntValue, serverMaxClients.IntValue); var listenAddresses = NetworkUtils.GetLocalInterfaceAddresses(); if (listenAddresses.Count > 0) { Console.SetPrompt(listenAddresses[0] + ":" + NetworkConfig.serverPort.Value + "> "); } GameDebug.Log("Listening on " + string.Join(", ", NetworkUtils.GetLocalInterfaceAddresses()) + " on port " + NetworkConfig.serverPort.IntValue); m_NetworkServer = new NetworkServer(m_NetworkTransport); if (Game.game.clientFrontend != null) { var serverPanel = Game.game.clientFrontend.serverPanel; serverPanel.SetPanelActive(true); serverPanel.serverInfo.text += "Listening on:\n"; foreach (var a in NetworkUtils.GetLocalInterfaceAddresses()) { serverPanel.serverInfo.text += a + ":" + NetworkConfig.serverPort.IntValue + "\n"; } } m_NetworkServer.UpdateClientInfo(); m_NetworkServer.serverInfo.compressionModel = m_Model; if (serverServerName.Value == "") { serverServerName.Value = MakeServername(); } #if false m_ServerQueryProtocolServer = new SQP.SQPServer(NetworkConfig.serverSQPPort.IntValue > 0? NetworkConfig.serverSQPPort.IntValue : NetworkConfig.serverPort.IntValue + NetworkConfig.sqpPortOffset); #endif #if UNITY_EDITOR Game.game.levelManager.UnloadLevel(); #endif m_GameWorld = new GameWorld("ServerWorld"); m_NetworkStatistics = new NetworkStatisticsServer(m_NetworkServer); m_ChatSystem = new ChatSystemServer(m_Clients, m_NetworkServer); GameDebug.Log("Network server initialized"); Console.AddCommand("load", CmdLoad, "Load a named scene", this.GetHashCode()); Console.AddCommand("unload", CmdUnload, "Unload current scene", this.GetHashCode()); Console.AddCommand("respawn", CmdRespawn, "Respawn character (usage : respawn playername|playerId)", this.GetHashCode()); Console.AddCommand("servername", CmdSetServerName, "Set name of the server", this.GetHashCode()); Console.AddCommand("beginnetworkprofile", CmdBeginNetworkProfile, "begins a network profile", this.GetHashCode()); Console.AddCommand("endnetworkprofile", CmdEndNetworkProfile, "Ends a network profile and analyzes. [optional] filepath for model data", this.GetHashCode()); Console.AddCommand("loadcompressionmodel", CmdLoadNetworkCompressionModel, "Loads a network compression model from a filepath", this.GetHashCode()); Console.AddCommand("list", CmdList, "List clients", this.GetHashCode()); CmdLoad(args); Game.SetMousePointerLock(false); m_ServerStartTime = Time.time; GameDebug.Log("Server initialized"); Console.SetOpen(false); return(true); }