Beispiel #1
0
        private void OnSuccess()
        {
            _networkedPhysics = NetworkedPhyiscs.Create();
            _networkedPhysics.gameObject.AddComponent <ClientNetworkingHandler>();
            if (NetworkUtils.IsServer)
            {
                _networkedPhysics.gameObject.AddComponent <ServerNetworkingHandler>();
                _networkedPhysics.gameObject.AddComponent <NetworkedBotController>();
            }

            NetworkClient.DisconnectHandler = () => { };
            if (NetworkUtils.IsServer)
            {
                NetworkServer.ConnectHandler    = ServerOnClientConnected;
                NetworkServer.DisconnectHandler = ServerOnClientDisconnected;
            }
            else
            {
                NetworkClient.SetTcpHandler(TcpPacketType.Server_State_Joined, buffer => {
                    while (buffer.TotalBitsLeft >= 8)
                    {
                        CreateStructure(buffer.ReadByte());
                    }
                });
                NetworkClient.SetTcpHandler(TcpPacketType.Server_State_Left,
                                            buffer => Destroy(BotCache.Get(buffer.ReadByte()).gameObject));
            }

            CompleteStructure structure = CreateStructure(NetworkUtils.LocalId);

            InitializeLocalStructure(structure);
        }
Beispiel #2
0
 public LaserWeapon(CompleteStructure structure, RealLiveBlock block, WeaponConstants constants)
     : base(structure, block, constants)
 {
     _particles = Turret.GetComponent <ParticleSystem>();
     ParticleSystem.ShapeModule shape = _particles.shape;
     shape.position = Constants.TurretOffset;
 }
Beispiel #3
0
        private static CompleteStructure CreateStructure(byte playerId)
        {
            CompleteStructure structure = CompleteStructure.Create(BuildingController.ExampleStructure, playerId);

            Assert.IsNotNull(structure, "The example structure creation must be successful.");
            structure.transform.position = new Vector3(0, 10, 0);
            return(structure);
        }
Beispiel #4
0
        /// <summary>
        /// Recreates the specified player's structure.
        /// </summary>
        public static void RespawnPlayerStructure(byte playerId)
        {
            CompleteStructure structure = CreateStructure(playerId);

            if (NetworkUtils.LocalId == playerId)
            {
                InitializeLocalStructure(structure);
            }
        }
Beispiel #5
0
        private static CompleteStructure CreateStructure(byte playerId)
        {
            MutableBitBuffer buffer = new MutableBitBuffer();

            buffer.SetContents(PlayerStructures[playerId]);
            CompleteStructure structure = CompleteStructure.Create(buffer, playerId);

            Assert.IsNotNull(structure, "The example structure creation must be successful.");
            structure.transform.position = new Vector3(0, 10, 0);
            return(structure);
        }
Beispiel #6
0
        protected WeaponSystem(CompleteStructure structure, RealLiveBlock block, WeaponConstants constants)
            : base(structure, block)
        {
            Constants = constants;
            Turret    = block.transform.Find("Turret");

            if (!NetworkUtils.IsServer && !NetworkUtils.IsLocal(Structure.Id))
            {
                _turretRotationMultiplier *= 1.25f;
            }
        }
Beispiel #7
0
        /// <summary>
        /// Create a new system instance from the block if the block comes with a system.
        /// </summary>
        public static bool Create(CompleteStructure structure, RealLiveBlock block, out BotSystem system)
        {
            if (!Constructors.TryGetValue(block.Info.Type, out SystemConstructor constructor))
            {
                system = null;
                return(false);
            }

            system = constructor(structure, block);
            return(true);
        }
Beispiel #8
0
        /// <summary>
        /// Remove the structure with the specified player/bot id and returns it.
        /// Fails if the structure isn't registered.
        /// Should only be called by the CompleteStructure class.
        /// </summary>
        public static void Remove(byte id)
        {
            CompleteStructure structure = Cache[id];

            Assert.IsNotNull(structure, "No structure with that id is registered.");
            Cache[id] = null;
            Count--;
            for (int i = 0; i < ExtraCount; i++)
            {
                ExtraData[id, i] = null;
            }
        }
Beispiel #9
0
        private void ClientOnlyPacketReceived(CompleteStructure localStructure, out int toSimulate, out long lastMillis)
        {
            long currentMillis = DoubleProtocol.TimeMillis;

            lastMillis = _sharedBuffer.ReadTimestamp();
            toSimulate = (int)(currentMillis - lastMillis);
            while (_sharedBuffer.TotalBitsLeft >= BotState.SerializedBitsSize)
            {
                _tempBotState.Update(_sharedBuffer);
                BotCache.Get(_tempBotState.Id).UpdateWholeState(_tempBotState);
            }

            if (toSimulate <= 0)
            {
                toSimulate = 0;
                _silentSkipFastForwardUntil = 0;
            }
            else if (toSimulate >= 500)
            {
                if (currentMillis < _silentSkipFastForwardUntil)
                {
                    Debug.LogWarning($"Skipping {toSimulate}ms of networking fast-forward simulation to avoid delays." +
                                     "Hiding this error for at most 100ms.");
                    _silentSkipFastForwardUntil = currentMillis + 100;
                }
                toSimulate = 0;
            }
            else
            {
                _silentSkipFastForwardUntil = 0;
            }

            while (_guessedInputs.Count > 0)
            {
                KeyValuePair <long, GuessedInput> guessed = _guessedInputs.First();
                if (guessed.Value.MovementInput.Equals(localStructure.MovementInput))
                {
                    _guessedInputs.Remove(guessed.Key);
                }
                else
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Initializes this camera controller with the structure it should follow.
        /// </summary>
        public void Initialize(CompleteStructure structure)
        {
            _structure             = structure.transform;
            _lastStructurePosition = _structure.position;

            Bounds bounds = new Bounds(_structure.position, Vector3.zero);

            foreach (Transform child in _structure)
            {
                Renderer childRenderer = child.GetComponent <Renderer>();
                if (childRenderer != null)
                {
                    bounds.Encapsulate(childRenderer.bounds);
                }
            }             //TODO something is not right - fix this
            _offset = new Vector3(0, VerticalOffsetOffset + bounds.extents.y * 2, 0);

            transform.position = _structure.position + _offset;
            transform.rotation = Quaternion.identity;

            transform.position -= transform.forward * DefaultZoom;
            transform.RotateAround(_structure.position + _offset, Vector3.up, DefaultYaw);
            transform.RotateAround(_structure.position, transform.right, DefaultPitch);
        }
Beispiel #11
0
 public SystemManager(CompleteStructure structure)
 {
     _structure = structure;
 }
Beispiel #12
0
 private static void InitializeLocalStructure(CompleteStructure structure)
 {
     structure.gameObject.AddComponent <LocalBotController>().Initialize(_networkedPhysics);
     Camera.main.gameObject.AddComponent <PlayingCameraController>().Initialize(structure);
 }
Beispiel #13
0
 public ThrusterSystem(CompleteStructure structure, RealLiveBlock block, ThrusterConstants constants)
     : base(structure, block)
 {
     Constants = constants;
     _facing   = Rotation.RotateSides(constants.Facing, block.Rotation);
 }
Beispiel #14
0
 protected ProjectileWeapon(CompleteStructure structure, RealLiveBlock block, WeaponConstants constants)
     : base(structure, block, constants)
 {
 }
Beispiel #15
0
 private void Awake()
 {
     _camera    = Camera.main;
     _structure = GetComponent <CompleteStructure>();
 }
Beispiel #16
0
 /// <summary>
 /// Returns whether the 'out' parameter is the structure with the specified player/bot id
 /// or null if no structure was registered with the id.
 /// </summary>
 // ReSharper disable once AnnotateCanBeNullParameter
 public static bool TryGet(byte id, out CompleteStructure structure)
 {
     structure = Cache[id];
     return(structure != null);
 }
Beispiel #17
0
 /// <summary>
 /// Registers the specified structure in this cache.
 /// Should only be called by the CompleteStructure class.
 /// </summary>
 public static void Add(CompleteStructure structure)
 {
     Assert.IsNull(Cache[structure.Id], "A structure with that id is already registered.");
     Cache[structure.Id] = structure;
     Count++;
 }
Beispiel #18
0
 private void Awake()
 {
     _camera          = Camera.main;
     _structure       = GetComponent <CompleteStructure>();
     Cursor.lockState = CursorLockMode.Locked;
 }
Beispiel #19
0
 protected ActiveSystem(CompleteStructure structure, RealLiveBlock block) : base(structure, block)
 {
 }
 public UnrealAcceleratorSystem(CompleteStructure structure, RealLiveBlock block) : base(structure, block)
 {
 }
Beispiel #21
0
        private void Update()
        {
            if (Input.GetButtonDown("Fire1"))
            {
                Place();
            }
            else if (Input.GetButtonDown("Fire2"))
            {
                Delete();
            }

            Rotate(Input.GetAxisRaw("MouseScroll"));
            if (Input.GetButtonDown("Fire3"))
            {
                Switch();
            }

            // ReSharper disable once UnusedVariable
            if (GetSelectedBlock(out GameObject block, out BlockPosition position, out byte rotation))
            {
                if (!position.Equals(_previousPreviewPosition))
                {
                    ShowPreview(position, rotation);
                }
            }
            else
            {
                Destroy(_previewObject);
                _previousPreviewPosition = null;
            }

            if (Input.GetButtonDown("Ability"))
            {
                new Action(() => {
                    EditableStructure.Errors errors = _structure.GetStructureErrors();
                    if (errors != EditableStructure.Errors.None)
                    {
                        Debug.Log("Structure error: " + errors);
                        return;
                    }

                    IDictionary <BlockPosition, IPlacedBlock> notConnected = _structure.GetNotConnectedBlocks();
                    Assert.IsNotNull(notConnected, "The lack of the presence of the Mainframe was not shown among the errors.");
                    if (notConnected.Count != 0)
                    {
                        Debug.Log("Structure error: not connected blocks");
                        return;
                    }

                    BitBuffer someBuffer = new MutableBitBuffer((RealPlacedBlock.SerializedBitsSize
                                                                 * _structure.RealBlockCount + 7) / 8);
                    _structure.Serialize(someBuffer);
                    Debug.Log("Structure: " + string.Join(", ", someBuffer.Array));
                    CompleteStructure complete = CompleteStructure.Create(someBuffer, 1);
                    Assert.IsNotNull(complete, "Own CompleteStructure creation mustn't fail.");

                    complete.transform.position = new Vector3(0, 10, 0);
                    complete.gameObject.AddComponent <LocalBotController>();
                    _camera.gameObject.AddComponent <PlayingCameraController>().Initialize(complete);

                    Destroy(_camera.gameObject.GetComponent <BuildingCameraController>());
                    Destroy(gameObject);

                    CompleteStructure otherStructure = CompleteStructure.Create(ExampleStructure, 2);
                    Assert.IsNotNull(otherStructure, "Other CompleteStructure creation mustn't fail.");
                    otherStructure.transform.position = new Vector3(150, 5, 150);
                }).Invoke();
            }
        }
Beispiel #22
0
 public PlasmaWeapon(CompleteStructure structure, RealLiveBlock block, WeaponConstants constants)
     : base(structure, block, constants)
 {
 }
Beispiel #23
0
 public FullStopSystem(CompleteStructure structure, RealLiveBlock block) : base(structure, block)
 {
 }
Beispiel #24
0
        private void ClientOnlyFixedUpdate()
        {
            CompleteStructure localStructure = BotCache.Get(NetworkUtils.LocalId);
            int  toSimulate;
            long lastMillis;

            if (_lastClientUdpPacket != null)
            {
                _sharedBuffer.SetContents(_lastClientUdpPacket);
                _lastClientUdpPacket = null;
                ClientOnlyPacketReceived(localStructure, out toSimulate, out lastMillis);
                if (toSimulate == 0)
                {
                    return;
                }
            }
            else if (_silentSkipFastForwardUntil != 0)
            {
                return;                 //don't simulate normal steps if skipped the last state update simulation
            }
            else
            {
                toSimulate = TimestepMillis;
                lastMillis = DoubleProtocol.TimeMillis - TimestepMillis;
            }

            while (_guessedInputs.Count > 0)
            {
                KeyValuePair <long, GuessedInput> guessed = _guessedInputs.First();
                if (guessed.Key >= lastMillis)
                {
                    break;
                }

                _guessedInputs.Remove(guessed.Key);
                guessed.Value.RemainingDelay -= (int)(lastMillis - guessed.Key);
                if (guessed.Value.RemainingDelay > 0)
                {
                    _guessedInputs.Remove(lastMillis);
                    _guessedInputs.Add(lastMillis, guessed.Value);
                }
            }

            foreach (KeyValuePair <long, GuessedInput> guessed in _guessedInputs)
            {
                int delta = (int)(guessed.Key - lastMillis);
                if (delta > toSimulate)
                {
                    break;
                }
                else if (delta == 0)
                {
                    localStructure.UpdateInputOnly(guessed.Value.MovementInput, null);
                }
                else
                {
                    Simulate(delta);
                    localStructure.UpdateInputOnly(guessed.Value.MovementInput, null);
                    toSimulate -= delta;
                    lastMillis  = guessed.Key;
                }
            }

            if (toSimulate != 0)
            {
                Simulate(toSimulate);
            }
        }
Beispiel #25
0
 protected BotSystem(CompleteStructure structure, RealLiveBlock block)
 {
     Structure = structure;
     Block     = block;
 }
Beispiel #26
0
 protected PropulsionSystem(CompleteStructure structure, RealLiveBlock block) : base(structure, block)
 {
 }