Ejemplo n.º 1
0
        public void CreatePlayer(byte actorId, int type)
        {
            var bornPos   = _constStateService.playerBornPoss[actorId % _constStateService.playerBornPoss.Count];
            var createPos = bornPos + GameConfig.TankBornOffset;

            _resourceService.ShowBornEffect(createPos);
            _audioService.PlayClipBorn();
            EDir dir = EDir.Up;

            DelayCall(GameConfig.TankBornDelay, () => {
                GameObject obj = null;
                var entity     = CreateUnit(createPos, _config.playerPrefabs, type, dir, transParentPlayer, out obj);
                var actor      = _actorContext.GetEntityWithId(actorId);
                if (actorId == _constStateService.localActorId && Main.Instance.cineCamera != null)
                {
                    Main.Instance.cineCamera.Follow = obj.transform;
                }

                if (actor != null)
                {
                    actor.ReplaceGameLocalId(entity.localId.value);
                    entity.ReplaceActorId(actorId);
                }
                else
                {
                    Debug.LogError(
                        $"can not find a actor after create a game player actorId:{actorId} localId{entity.localId.value}");
                }
            });
        }
Ejemplo n.º 2
0
        public void CreatePlayer(byte actorId, ushort type)
        {
            var bornPos   = _gameConstStateService.PlayerBornPoss[actorId % _gameConstStateService.PlayerBornPoss.Count];
            var createPos = bornPos + _gameConfigService.TankBornOffset;

            _gameEffectService.ShowBornEffect(createPos);
            _gameAudioService.PlayClipBorn();
            EDir dir = EDir.Up;

            DelayCall(_gameConfigService.TankBornDelay, () => {
                var entity       = CreateUnit(createPos, EntityUtil.CreateEntityPlayer(_gameContext, type), dir);
                entity.unit.camp = ECampType.Player;
                var actor        = _actorContext.GetEntityWithActorId(actorId);
                if (actor != null)
                {
                    actor.ReplaceGameEntityId(entity.entityId.value);
                    entity.ReplaceActorId(actorId);
                }
                else
                {
                    Debug.LogError(
                        $"can not find a actor after create a game player actorId:{actorId} entityId{entity.entityId.value}");
                }
            });
        }
        public static ColliderPrefab CreateColliderPrefab(GameObject fab, ColliderData data)
        {
            Debug.Trace("CreateColliderPrefab " + fab.name);
            CBaseShape collider = null;

            if (data == null)
            {
                Debug.LogError(fab.name + " Miss ColliderDataMono ");
                return(null);
            }

            if (data.radius > 0)
            {
                //circle
                collider = new CCircle(data.radius);
            }
            else
            {
                //obb
                collider = new COBB(data.size, data.deg);
            }
            Debug.Trace($"{fab.name} !!!CreateCollider  deg: {data.deg} up:{data.size} radius:{data.radius}");
            var colFab = new ColliderPrefab();

            colFab.parts.Add(new ColliderPart()
            {
                transform = new CTransform2D(LVector2.zero),
                collider  = collider
            });
            return(colFab);
        }
Ejemplo n.º 4
0
        protected void OnRecvMsg(Deserializer reader)
        {
            var msgType = reader.ReadInt16();

            if (msgType >= _maxMsgId)
            {
                Debug.LogError($" send a Error msgType out of range {msgType}");
                return;
            }

            try {
                var _func   = _allMsgDealFuncs[msgType];
                var _parser = _allMsgParsers[msgType];
                if (_func != null && _parser != null)
                {
                    _func(_parser(reader));
                }
                else
                {
                    Debug.LogError($" ErrorMsg type :no msg handler or parser {msgType}");
                }
            }
            catch (Exception e) {
                Debug.LogError($" Deal Msg Error :{(EMsgSC) (msgType)}  " + e);
            }
        }
Ejemplo n.º 5
0
        public static void RemoveListener(EEvent type, GlobalEventHandler listener)
        {
            if (IsTriggingEvent)
            {
                allPendingListeners.Enqueue(new ListenerInfo(false, type, listener));
                return;
            }

            var itype = (int)type;

            if (allListeners.TryGetValue(itype, out var tmplst))
            {
                if (tmplst.Remove(listener))
                {
                    if (tmplst.Count == 0)
                    {
                        allListeners.Remove(itype);
                    }

                    return;
                }
            }

            Debug.LogError("Try remove a not exist listner " + type);
        }
        private void RollbackTo(int tick, int maxContinueServerTick, bool isNeedClear = true)
        {
            _world.RollbackTo(tick, maxContinueServerTick, isNeedClear);
            var hash    = _commonStateService.Hash;
            var curHash = _hashHelper.CalcHash();

            if (hash != curHash)
            {
                Debug.LogError($"Rollback error: Hash isDiff oldHash ={hash}  curHash{curHash}");
            }
        }
Ejemplo n.º 7
0
        //net status
        public void OnReconnect(Player player)
        {
            player.localId = _playerId2LocalId[player.PlayerId];
            _localId2Player[player.localId] = player;
            if (_allPlayers.Contains(player))
            {
                Debug.LogError("Duplicate add players" + player.PlayerId);
                return;
            }

            _allPlayers.Add(player);
        }
Ejemplo n.º 8
0
        public static ColliderPrefab CreateColliderPrefab(GameObject fab, ColliderData data)
        {
            Debug.Trace("CreateColliderPrefab " + fab.name);

            if (data == null)
            {
                Debug.LogError(fab.name + " Miss ColliderDataMono ");
                return(null);
            }

            Debug.Trace($"{fab.name} !!!CreateCollider  deg: {data.deg} up:{data.size} radius:{data.radius}");
            return(CreateColliderPrefab(data));
        }
Ejemplo n.º 9
0
        public void RollbackTo(int tick, int maxContinueServerTick, bool isNeedClear = true)
        {
            if (tick < 0)
            {
                Debug.LogError("Target Tick invalid!" + tick);
                return;
            }

            Debug.Log($" Rollback diff:{Tick - tick} From{Tick}->{tick}  maxContinueServerTick:{maxContinueServerTick} {isNeedClear}");
            _timeMachineService.RollbackTo(tick);
            _commonStateService.SetTick(tick);
            Tick = tick;
        }
Ejemplo n.º 10
0
        private bool RollbackTo(int tick, int maxContinueServerTick, bool isNeedClear = true)
        {
            _world.RollbackTo(tick, maxContinueServerTick, isNeedClear);
            var hash    = _commonStateService.Hash;
            var curHash = _hashHelper.CalcHash();

            if (hash != curHash)
            {
                Debug.LogError($"tick:{tick} Rollback error: Hash isDiff oldHash ={hash}  curHash{curHash}");
#if UNITY_EDITOR
                _dumpHelper.DumpToFile(true);
                return(false);
#endif
            }
            return(true);
        }
Ejemplo n.º 11
0
        T ParseData <T>(Deserializer reader) where T : BaseFormater, new()
        {
            T data = null;

            try {
                data = reader.Parse <T>();
                if (!reader.IsEnd)
                {
                    data = null;
                }
            }
            catch (Exception e) {
                Debug.LogError("Parse Msg Error:" + e);
                data = null;
            }

            return(data);
        }
 public void CreatePlayer(byte actorId, int type){
     var bornPos = _gameConstStateService.playerBornPoss[actorId % _gameConstStateService.playerBornPoss.Count];
     var createPos = bornPos + base._gameConfigService.TankBornOffset;
     _gameEffectService.ShowBornEffect(createPos);
     _gameAudioService.PlayClipBorn();
     EDir dir = EDir.Up;
     DelayCall(base._gameConfigService.TankBornDelay, () => {
         var entity = CreateUnit(createPos, _gameConfigService.playerPrefabs, type, dir);
         var actor = _actorContext.GetEntityWithId(actorId);
         if (actor != null) {
             actor.ReplaceGameLocalId(entity.localId.value);
             entity.ReplaceActorId(actorId);
         }
         else {
             Debug.LogError(
                 $"can not find a actor after create a game player actorId:{actorId} localId{entity.localId.value}");
         }
     });
 }
Ejemplo n.º 13
0
        public void OnPlayerEnter(Player player)
        {
            if (State == ERoomState.Idle)
            {
                State = ERoomState.WaitingToPlay;
            }
            if (State != ERoomState.WaitingToPlay)
            {
                return;
            }
            if (_allPlayers.Contains(player))
            {
                Debug.LogError("Player already exist" + player.PlayerId);
                return;
            }

            Debug.Log($"Player{player.PlayerId} Enter room {RoomId}");
            player.room = this;
            _allPlayers.Add(player);
        }
Ejemplo n.º 14
0
        public void RollbackTo(int tick, int missFrameTick, bool isNeedClear = true)
        {
            if (tick < 0)
            {
                Debug.LogError("Target Tick invalid!" + tick);
                return;
            }

            Debug.Log($" curTick {Tick} RevertTo {tick} {missFrameTick} {isNeedClear}");
            _timeMachineService.RollbackTo(tick);
            _commonStateService.SetTick(tick);
            Tick = tick;
            var hash    = _commonStateService.Hash;
            var curHash = GetHash();

            if (hash != curHash)
            {
                Debug.LogError($"Rollback error: Hash isDiff oldHash ={hash}  curHash{curHash}");
            }
        }
Ejemplo n.º 15
0
        public override void OnEntityCreated(Context f, Lockstep.UnsafeECS.Entity *pEntity)
        {
            if (pEntity == null)
            {
                int i = 0;
                Debug.LogError("OnEntityCreated null");
                return;
            }

            if (pEntity->TypeId != EntityIds.Boid)
            {
                //Debug.LogError("OnEntityCreated not a Enemy" + pEntity->EnumType());
                return;
            }

            var pBoid = (Boid *)pEntity;

            if (_entityPrefabs == null)
            {
                //TODO read config to setup Entity Prefabs
                var _spawners = GameObject.FindObjectsOfType <SSSamples.Boids.Authoring.SpawnRandomInSphere>();
                _entityManager = Unity.Entities.World.Active.EntityManager;
                _entityPrefabs = new Unity.Entities.Entity[_spawners.Length];
                for (int i = 0; i < _spawners.Length; i++)
                {
                    _entityPrefabs[i] = _spawners[i].PrefabEntity;
                }
            }

            var uEntity = _entityManager.Instantiate(_entityPrefabs[pBoid->AssetData.AssetId]);

            _id2GameObject[pEntity->LocalId] = uEntity;
            _entityManager.SetComponentData(uEntity, new Unity.Transforms.LocalToWorld {
                Value = float4x4.TRS(
                    pBoid->LocalToWorld.Position.ToVector3(),
                    //quaternion.identity,
                    quaternion.LookRotationSafe(pBoid->LocalToWorld.Forward.ToVector3(), Unity.Mathematics.math.up()),
                    new float3(1.0f, 1.0f, 1.0f))
            });
            _entityManager.AddComponentData(uEntity, pEntity->_ref);
        }
Ejemplo n.º 16
0
        public void OnPlayerEnter(Player player)
        {
            if (State == ERoomState.Idle)
            {
                State = ERoomState.WaitingToPlay;
            }
            if (_allPlayers.Contains(player))
            {
                Debug.LogError("Player already exist" + player.PlayerId);
                return;
            }

            Debug.Log($"Player{player.PlayerId} Enter room {RoomId}");
            player.room = this;
            var localId = curPlayerLocalId++;

            player.localId = localId;
            netId2LocalId[player.netID] = localId;
            _localId2Player[localId]    = player;
            _allPlayers.Add(player);
        }
Ejemplo n.º 17
0
        public override void DoStart()
        {
            if (_instance != this)
            {
                Debug.LogError("Duplicate CollisionSystemAdapt!");
                return;
            }

            var collisionSystem = new CollisionSystem()
            {
                worldSize    = worldSize,
                pos          = pos,
                minNodeSize  = minNodeSize,
                loosenessval = loosenessval
            };

            Debug.Trace($"worldSize:{worldSize} pos:{pos} minNodeSize:{minNodeSize} loosenessval:{loosenessval}");
            this.collisionSystem = collisionSystem;
            collisionSystem.DoStart(collisionMatrix, allTypes);
            collisionSystem.funcGlobalOnTriggerEvent += GlobalOnTriggerEvent;
        }
Ejemplo n.º 18
0
        public static void AddListener(EEvent type, GlobalEventHandler listener)
        {
#if _DEBUG_EVENT_TRIGGER
            if (IsTriggingEvent)
            {
                Debug.LogError("Error!!! can not modify allListeners when was Trigger Event");
            }
#endif

            var itype = (int)type;
            if (allListeners.TryGetValue(itype, out var tmplst))
            {
                tmplst.Add(listener);
            }
            else
            {
                var lst = new List <GlobalEventHandler>();
                lst.Add(listener);
                allListeners.Add(itype, lst);
            }
        }
Ejemplo n.º 19
0
        private void OnNetMsg(byte[] rawData)
        {
            var reader    = new Deserializer(Compressor.Decompress(rawData));
            var msgTypeId = reader.GetInt16();

            if (msgTypeId >= AllClientMsgDealFuncs.Length)
            {
                Debug.LogError("Recv error msg type" + msgTypeId);
                return;
            }

            var func = AllClientMsgDealFuncs[msgTypeId];

            if (func != null)
            {
                func(reader);
            }
            else
            {
                Debug.LogError("ErrorMsg type :no msgHandler" + (TMsgType)(object)msgTypeId);
            }
        }
Ejemplo n.º 20
0
        public static void RemoveListener(EEvent type, GlobalEventHandler listener)
        {
#if _DEBUG_EVENT_TRIGGER
            if (IsTriggingEvent)
            {
                Debug.LogError("Error!!! can not modify allListeners when was Trigger Event");
            }
#endif
            var itype = (int)type;
            if (allListeners.TryGetValue(itype, out var tmplst))
            {
                if (tmplst.Remove(listener))
                {
                    if (tmplst.Count == 0)
                    {
                        allListeners.Remove(itype);
                    }
                    return;
                }
            }
            Debug.LogError("Try remove a not exist listner " + type);
        }
        public void StartSimulate()
        {
            if (IsRunning)
            {
                Debug.LogError("Already started!");
                return;
            }

            IsRunning = true;
            if (_constStateService.IsClientMode)
            {
                _gameStartTimestampMs = LTime.realtimeSinceStartupMS;
            }

            _world.StartGame(_gameStartInfo, LocalActorId);
            Debug.Log($"Game Start");
            EventHelper.Trigger(EEvent.SimulationStart, null);

            while (inputTick < PreSendInputCount)
            {
                SendInputs(inputTick++);
            }
        }
Ejemplo n.º 22
0
        private void OnNetMsg(byte[] rawData)
        {
            var data      = Compressor.Decompress(rawData);
            var reader    = new Deserializer(data);
            var msgTypeID = reader.GetByte();

            if (msgTypeID >= _allMsgDealFuncs.Length)
            {
                Debug.LogError("Recv error msg type" + msgTypeID);
                return;
            }

            var func = _allMsgDealFuncs[msgTypeID];

            if (func != null)
            {
                func(reader);
            }
            else
            {
                Debug.LogError("ErrorMsg type :no msgHandler" + msgTypeID);
            }
        }
 private void DumpFrame(int hash)
 {
     if (_constStateService.IsClientMode)
     {
         if (_hashHelper.TryGetValue(_world.Tick, out var val))
         {
             _dumpHelper.DumpFrame(false);
             if (hash != val)
             {
                 Debug.LogError($"Tick : CurHash {hash} is different from oldHash {val}");
                 _dumpHelper.DumpToFile();
                 _commonStateService.IsPause = true;
             }
         }
         else
         {
             _dumpHelper.DumpFrame(true);
         }
     }
     else
     {
         _dumpHelper.DumpFrame(true);
     }
 }
Ejemplo n.º 24
0
 void DealMsgHandlerError(Player player, string msg)
 {
     Debug.LogError(msg);
     TickOut(player, 0);
 }
Ejemplo n.º 25
0
        public static ColliderPrefab CreateColliderPrefab(GameObject fab)
        {
            Debug.Trace("CreateColliderPrefab " + fab.name);
#if false
            Collider unityCollider = null;
            var      colliders     = fab.GetComponents <Collider>();
            foreach (var col in colliders)
            {
                if (col.isTrigger)
                {
                    unityCollider = col;
                    break;
                }
            }

            if (unityCollider == null)
            {
                foreach (var col in colliders)
                {
                    unityCollider = col;
                    break;
                }
            }

            if (unityCollider == null)
            {
                return(null);
            }
            CBaseShape collider = null;
            if (unityCollider is BoxCollider boxCol)
            {
                collider = new COBB(boxCol.size.ToLVector2XZ(), LFloat.zero);
            }

            if (unityCollider is SphereCollider cirCol)
            {
                collider = new CCircle(cirCol.radius.ToLFloat());
            }

            if (unityCollider is CapsuleCollider capCol)
            {
                collider = new CCircle(capCol.radius.ToLFloat());
            }

            if (collider is COBB tObb)
            {
                Debug.LogTrace($"{fab.name} CreateCollider OBB deg: {tObb.deg} up:{tObb.up} radius:{tObb.radius}");
            }
            if (collider is CCircle tCircle)
            {
                Debug.LogTrace($"{fab.name} CreateCollider Circle deg: radius:{tCircle.radius}");
            }
#else
            CBaseShape collider = null;
            var        data     = fab.GetComponent <ColliderDataMono>()?.colliderData;
            if (data == null)
            {
                Debug.LogError(fab.name + " Miss ColliderDataMono ");
                return(null);
            }

            if (data.radius > 0)
            {
                //circle
                collider = new CCircle(data.radius);
            }
            else
            {
                //obb
                collider = new COBB(data.size, data.deg);
            }
            Debug.Trace($"{fab.name} !!!CreateCollider  deg: {data.deg} up:{data.size} radius:{data.radius}");
#endif
            var colFab = new ColliderPrefab();
            colFab.parts.Add(new ColliderPart()
            {
                transform = new CTransform2D(LVector2.zero),
                collider  = collider
            });
            return(colFab);
        }
Ejemplo n.º 26
0
        private void Step()
        {
            if (_commonStateService.IsPause)
            {
                return;
            }
            _commonStateService.SetTick(Tick);

            var hash = GetHash();

            if (_constStateService.IsClientMode)
            {
                if (_tick2Hash.TryGetValue(Tick, out var val))
                {
                    _tick2ResumeDumpString[Tick] = DumpStr();
                    if (hash != val)
                    {
                        Debug.LogError($"Tick : CurHash {hash} is different from oldHash {val}");
#if UNITY_EDITOR
                        var path = dumpPath + "/cur.txt";
                        var dir  = Path.GetDirectoryName(path);
                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        var           minTick  = _tick2ResumeDumpString.Keys.Min();
                        StringBuilder sbResume = new StringBuilder();
                        StringBuilder sbRaw    = new StringBuilder();
                        for (int i = minTick; i <= Tick; i++)
                        {
                            sbRaw.AppendLine(_tick2RawDumpString[i].ToString());
                            sbResume.AppendLine(_tick2ResumeDumpString[i].ToString());
                        }

                        File.WriteAllText(dumpPath + "/resume.txt", sbResume.ToString());
                        File.WriteAllText(dumpPath + "/raw.txt", sbRaw.ToString());
                        _commonStateService.IsPause = true;
                        UnityEngine.Debug.Break();
#endif
                    }
                }
                else
                {
                    _tick2RawDumpString[Tick] = DumpStr();
                }
            }

            _commonStateService.Hash = hash;
            _tick2Hash[Tick]         = hash;
            _timeMachineService.Backup(Tick);
            var deltaTime = new LFloat(true, 30);
            foreach (var system in _systems)
            {
                if (system.enable)
                {
                    system.DoUpdate(deltaTime);
                }
            }

            Tick++;
        }