public static JSONClass SerializeComponent(this object component) { var node = new JSONClass(); foreach (var property in component.GetType().GetProperties()) { if (property.CanRead && property.CanWrite) { if (property.PropertyType == typeof(int)) { node.Add(property.Name, new JSONData((int)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(IntReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as IntReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new IntReactiveProperty(); } node.Add(property.Name, new JSONData((int)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(float)) { node.Add(property.Name, new JSONData((float)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(FloatReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as FloatReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new FloatReactiveProperty(); } node.Add(property.Name, new JSONData((float)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(bool)) { node.Add(property.Name, new JSONData((bool)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(BoolReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as BoolReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new BoolReactiveProperty(); } node.Add(property.Name, new JSONData((bool)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(string)) { node.Add(property.Name, new JSONData((string)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(StringReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as StringReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new StringReactiveProperty(); } node.Add(property.Name, new JSONData((string)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(Vector2)) { node.Add(property.Name, new JSONData((Vector2)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(Vector2ReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as Vector2ReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new Vector2ReactiveProperty(); } node.Add(property.Name, new JSONData((Vector2)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(Vector3)) { node.Add(property.Name, new JSONData((Vector3)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(Vector3ReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as Vector3ReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new Vector3ReactiveProperty(); } node.Add(property.Name, new JSONData((Vector3)reactiveProperty.Value)); continue; } } } return(node); }
private static void ApplyValue(object component, JSONNode node, PropertyInfo property) { if (property.CanRead && property.CanWrite) { var propertyData = node[property.Name]; if (propertyData == null) { return; } if (property.PropertyType == typeof(int)) { property.SetValue(component, propertyData.AsInt, null); return; } if (property.PropertyType == typeof(IntReactiveProperty)) { var reactiveProperty = new IntReactiveProperty(propertyData.AsInt); property.SetValue(component, reactiveProperty, null); return; } if (property.PropertyType == typeof(float)) { property.SetValue(component, propertyData.AsFloat, null); return; } if (property.PropertyType == typeof(FloatReactiveProperty)) { var reactiveProperty = new FloatReactiveProperty(propertyData.AsFloat); property.SetValue(component, reactiveProperty, null); return; } if (property.PropertyType == typeof(bool)) { property.SetValue(component, propertyData.AsBool, null); return; } if (property.PropertyType == typeof(BoolReactiveProperty)) { var reactiveProperty = new BoolReactiveProperty(propertyData.AsBool); property.SetValue(component, reactiveProperty, null); return; } if (property.PropertyType == typeof(string)) { property.SetValue(component, propertyData.Value, null); return; } if (property.PropertyType == typeof(StringReactiveProperty)) { var reactiveProperty = new StringReactiveProperty(propertyData.Value); property.SetValue(component, reactiveProperty, null); return; } if (property.PropertyType == typeof(Vector2)) { property.SetValue(component, propertyData.AsVector2, null); return; } if (property.PropertyType == typeof(Vector2ReactiveProperty)) { var reactiveProperty = new Vector2ReactiveProperty(propertyData.AsVector2); property.SetValue(component, reactiveProperty, null); return; } if (property.PropertyType == typeof(Vector3)) { property.SetValue(component, propertyData.AsVector3, null); return; } if (property.PropertyType == typeof(Vector3ReactiveProperty)) { var reactiveProperty = new Vector3ReactiveProperty(propertyData.AsVector3); property.SetValue(component, reactiveProperty, null); return; } } }
public MovementComponent() { Movement = new Vector2ReactiveProperty(); StopMovement = false; }
public static JSONObject Serialize(this object component) { var node = new JSONObject(); foreach (var property in component.GetType().GetProperties()) { if (property.CanRead && property.CanWrite) { if (property.PropertyType == typeof(int) || property.PropertyType.IsEnum) { node.Add(property.Name, new JSONNumber((int)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(IntReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as IntReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new IntReactiveProperty(); } node.Add(property.Name, new JSONNumber((int)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(float)) { node.Add(property.Name, new JSONNumber((float)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(FloatReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as FloatReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new FloatReactiveProperty(); } node.Add(property.Name, new JSONNumber((float)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(bool)) { node.Add(property.Name, new JSONBool((bool)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(BoolReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as BoolReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new BoolReactiveProperty(); } node.Add(property.Name, new JSONBool((bool)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(string)) { node.Add(property.Name, new JSONString((string)property.GetValue(component, null))); continue; } if (property.PropertyType == typeof(StringReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as StringReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new StringReactiveProperty(); } node.Add(property.Name, new JSONString((string)reactiveProperty.Value)); continue; } if (property.PropertyType == typeof(Vector2)) { var jsonObject = ((Vector2)property.GetValue(component, null)).AsJSONObject(); node.Add(property.Name, jsonObject); continue; } if (property.PropertyType == typeof(Vector2ReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as Vector2ReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new Vector2ReactiveProperty(); } var jsonObject = reactiveProperty.Value.AsJSONObject(); node.Add(property.Name, jsonObject); continue; } if (property.PropertyType == typeof(Vector3)) { var jsonObject = ((Vector3)property.GetValue(component, null)).AsJSONObject(); node.Add(property.Name, jsonObject); continue; } if (property.PropertyType == typeof(Vector3ReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as Vector3ReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new Vector3ReactiveProperty(); } var jsonObject = reactiveProperty.Value.AsJSONObject(); node.Add(property.Name, jsonObject); continue; } if (property.PropertyType == typeof(Color)) { var jsonObject = ((Color)property.GetValue(component, null)).AsJSONObject(); node.Add(property.Name, jsonObject); continue; } if (property.PropertyType == typeof(ColorReactiveProperty)) { var reactiveProperty = property.GetValue(component, null) as ColorReactiveProperty; if (reactiveProperty == null) { reactiveProperty = new ColorReactiveProperty(); } var jsonObject = reactiveProperty.Value.AsJSONObject(); node.Add(property.Name, jsonObject); continue; } } } return(node); }
void initUniRxPrograming() { RX_LastMoveFrom = new Vector2ReactiveProperty(CurrentPoint); RX_LastMoveTo = new Vector2ReactiveProperty(CurrentPoint); RX_currentPoint = new Vector2ReactiveProperty(CurrentPoint); RX_LastClickCell.Subscribe(_ => { RX_targetEntity.Value = null; }); //如何过滤过快的点击切换路径? //假若RX_LastClickCell的输入频率是 0.3s 内来了 10个数据(玩家0.3s内点击了10个可以移动的地方) //则以最后一个数据为准作为通知 RX_LastClickCell.Throttle(TimeSpan.FromSeconds(0.2f)).Subscribe(_ => { RX_PathChanged.Value = true; }); RX_currentPoint.Subscribe(point => { mapController.SetStartPoint(Vector2Int.CeilToInt(point)); }); RX_LastMoveTo.Subscribe((to) => { m_Transform.LookAt(HexCoords.GetHexVisualCoords(Vector2Int.CeilToInt(to))); RX_moveFromA2BPer.Value = 0; RX_moveSpeed.Value = 1; }); RX_LastMoveTo.Buffer(RX_LastMoveTo.Where(point => point == RX_currentPoint.Value).Throttle(TimeSpan.FromSeconds(C_TimeToReachOnePoint))).Subscribe(_ => { RX_moveSpeed.Value = 0; }); #region 控制移动 RX_moveFromA2BPer.Skip(1).Subscribe(per => { entityVisual.Run2(); Vector3 fromVisualPos = Coords.PointToVisualPosition(Vector2Int.CeilToInt(RX_LastMoveFrom.Value)); Vector3 toVisualPos = Coords.PointToVisualPosition(Vector2Int.CeilToInt(RX_LastMoveTo.Value)); Vector3 v = Vector3.Lerp(fromVisualPos, toVisualPos, RX_moveFromA2BPer.Value); m_Transform.position = v; //float near_start_or_near_dst = -0.5f + per; if (per >= 0.5f) { RX_currentPoint.Value = RX_LastMoveTo.Value; } else { RX_currentPoint.Value = RX_LastMoveFrom.Value; } }); var idle = RX_moveSpeed.Where(speed => speed == 0); var confused = idle.Where(_ => { return(false); }); idle.Subscribe(_ => { entityVisual.Idle2(); }); #endregion //0 0.5 0.9 1 1 1 0 0.6 1 -> 0 0.5 0.9 1 0 0.6 1 RX_reachFragment = RX_moveFromA2BPer.DistinctUntilChanged(); RX_reachFragment.Subscribe(_ => { //Debug.Log(_); }); RX_reachFragment.Where(per => per == 1).Subscribe(_ => { RXEnterCellPoint(Vector2Int.CeilToInt(RX_currentPoint.Value)); } ); RX_moveFromA2BPer.Buffer(RX_moveFromA2BPer.Where(per => per == 1).Throttle(TimeSpan.FromSeconds(0.3f))) .Where(buffer => GameCore.GetGameStatus() == GameStatus.Run && buffer.Count >= 2).Subscribe(_ => { if (GameEntityMgr.GetSelectedEntity() == this) { ShowEyeSight(); } }); RX_PathChanged.Where(changed => changed).Subscribe(_ => { //allowMove = false; RX_PathChanged.Value = false; Disposable_movementTimeLine?.Dispose(); IList <ICell> path = mapController.CalculatePath(); //Disposable_movementTimeLine = Observable.FromCoroutine(MoveM).Subscribe(unit => Disposable_movementTimeLine = Observable.FromCoroutine((tok) => MoveMS(path)).SelectMany(aftermove).Subscribe(); //RX_moveAlongPath(path, tellmeHowToMove(UseForClickCellMove)); //Disposable_movementTimeLine = Observable.EveryUpdate().CombineLatest(RX_reachFragment, (frame, per) => //{ // return per; //}).Where(per => per >= 1).Where(per => //{ // if (!controllRemote.PTiliMove(1)) // { // Disposable_movementTimeLine.Dispose(); // return false; // } // return true; //}).StartWith(1).Subscribe(h => //{ // Debug.Log("reach and show next fragment"); // if (path.Count > 1) // { // RX_LastMoveFrom.Value = path[0].Point; // RX_LastMoveTo.Value = path[1].Point; // path.RemoveAt(0); // } // else // { // Disposable_movementTimeLine.Dispose(); // } //}); //if (path.Count > 0) //{ // //转变为 (1-2)-(2-3)-(3-4)-(4-5)队列 // var rawPath = path.ToObservable<ICell>(); // var skipheader = rawPath.Skip(1); // var from_to_pathset = rawPath.Zip(skipheader, (raw, skip) => // { // return new FromAndTo(raw.Point, skip.Point); // }); // //要求路线按每隔 XXs 发出1个,其中第一段希望立即发出 这个时间没有基于gamecore状态 // var timeLine = Observable.EveryUpdate().CombineLatest(RX_reachFragment, (frame, per) => // { // return per; // }).Where(per => per == 1); // Disposable_movementTimeLine = timeLine.Subscribe(async (__) => // { // var s = await from_to_pathset.ToYieldInstruction(); // Debug.Log(__ + s.from.ToString() + " " + s.to); // RX_LastMoveFrom.Value = s.from; // RX_LastMoveTo.Value = s.to; // }); // Disposable_movementTimeLine = timeLine.StartWith(1).Zip(from_to_pathset, (time, from_to) => // { // return new { from_to = from_to, per = time }; // }).Where(zip => zip.per == 1).Do(zip => // { // Debug.Log("change next "); // var from_to = zip.from_to; // RX_LastMoveFrom.Value = from_to.from; // RX_LastMoveTo.Value = from_to.to; // }, // () => // { // }).Subscribe(); //} }); RX_alive.Value = BeAlive(); var onDeath = RX_alive.Where(alive => !alive); onDeath.Subscribe(_ => { Disposable_moveFromA2BPer.Dispose(); }, () => { }); //检测当前选中的玩家和第一个被点击的非玩家对象 var getFrameStream = Observable.EveryUpdate(); var selectEntityStream = getFrameStream.Select(_ => GameEntityMgr.GetSelectedEntity()); // 1 0 2 0 1 var onSelectEntityChangedStream = selectEntityStream.DistinctUntilChanged().Where(newEntity => newEntity != null); // 1 0 2 0 1 => 1 2 1 var selectEntityChooseNPCStream = RX_targetEntity.DistinctUntilChanged(); //1 0 2 onSelectEntityChangedStream.Subscribe(_ => { //Debug.Log(_.entityID); }); selectEntityChooseNPCStream.Subscribe(_ => { }); //选中玩家后,第一次选中不同的npc var rx_selectPlayer_then_npc = onSelectEntityChangedStream.CombineLatest(selectEntityChooseNPCStream, (frameDuringSelected, choosenpc) => { //int error = -1; //if (choosenpc == null) // return error; //if (choosenpc.BeAlive()) //{ // return choosenpc.entityID; //} //return error; return(choosenpc); }) /*.DistinctUntilChanged()*/.Where(combineResults => /*combineResults != -1*/ combineResults != null); rx_selectPlayer_then_npc.Subscribe(npc => { Debug.Log(npc.entityID); //move to entity then attack IList <ICell> path2reachEntity = mapController.GetPathFinder().FindPathOnMap(mapController.GetMap().GetCell(CurrentPoint), mapController.GetMap().GetCell(npc.CurrentPoint), mapController.GetMap()); ForgetMovement(); Disposable_movementTimeLine = Observable.FromCoroutine((tok) => MoveMS(path2reachEntity)).SelectMany(aftermove).Subscribe(); //RX_moveAlongPath(path2reachEntity, tellmeHowToMove(UseForBattleMove)); //Observable.FromCoroutine(playerAction2Entity).Subscribe(); }); var rx_selectPlayer_then_mapcell = getFrameStream.CombineLatest(RX_LastClickCell.DistinctUntilChanged(), (frameDuringSelected, selectPoint) => { return(selectPoint); }).DistinctUntilChanged(); rx_selectPlayer_then_mapcell.Subscribe(Cell => { //Debug.Log(Cell); }); }