Esempio n. 1
0
        public void SetupUnity(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.SetupUnity(cd, engine);

            engine.Mainer(cd, Dep.On(Scene.Root), () =>
            {
                var root = Scene.Root.Read();
                var item = Item.Read();
                if (!root || item == null)
                {
                    return;
                }
                var map = root.GetComponent <Common.Map>();
                var scd = root.GetComponent <Common.DisposeOnDestroy>().cd;

                Common.Binders.Label(scd, engine,
                                     map.GetComponent <Text>("name"), item.Name
                                     );
                Common.Binders.Image(scd, engine,
                                     map.GetComponent <Image>("image"), item.Image
                                     );
                Common.Binders.Label(scd, engine,
                                     map.GetComponent <Text>("level"), item.Level,
                                     i => string.Format("Level {0}", i)
                                     );
                Common.Binders.ButtonClick(scd, engine,
                                           map.GetComponent <Button>("sell"), state.Inventory.SellItem.Trigger,
                                           () => item
                                           );
            });
        }
Esempio n. 2
0
 public static bool Tabs <T>(CompositeDisposable cd, IEngine engine,
                             Map map, string[] tabs, string[] toggles,
                             IWriteable <T> dst, Func <string, T> dstConverter,
                             IReadable <T> src, Func <T, string> srcConverter)
 {
     engine.Reader(cd, Dep.On(src), () =>
     {
         string selected = srcConverter(src.Read());
         for (int i = 0, n = tabs.Length; i < n; ++i)
         {
             // TODO Fade animation
             map.Get(tabs[i]).SetActive(tabs[i] == selected);
         }
     });
     for (int i = 0, n = toggles.Length; i < n; ++i)
     {
         string tab    = tabs[i];
         string toggle = toggles[i];
         ToggleIsOn(cd, engine,
                    map.GetComponent <Toggle>(toggles[i]), src,
                    t => srcConverter(t) == tab
                    );
         ToggleChange(cd, engine,
                      map.GetComponent <Toggle>(toggles[i]),
                      b => { if (b)
                             {
                                 dst.Write(dstConverter(tab));
                             }
                      }
                      );
     }
     return(true);
 }
Esempio n. 3
0
        public void SetupUnity(CompositeDisposable cd, IEngine engine)
        {
            Dialog.SetupUnity(cd, engine);

            engine.Mainer(cd, Dep.On(Dialog.Root), () =>
            {
                var root = Dialog.Root.Read();
                if (!root)
                {
                    return;
                }
                var map = root.GetComponent <Common.Map>();
                var scd = root.GetComponent <Common.DisposeOnDestroy>().cd;

                map.GetComponent <Text>("message").text = messageFormatter(Current);

                Common.Binders.ButtonClick(scd, engine,
                                           map.GetComponent <Button>("yes"), Yes,
                                           () => Current
                                           );
                Common.Binders.Interactable(scd, engine,
                                            map.GetComponent <Button>("yes"), Status,
                                            b => b
                                            );
                Common.Binders.ButtonClick(scd, engine,
                                           map.GetComponent <Button>("no"), No,
                                           () => Current
                                           );
            });
        }
Esempio n. 4
0
        public void Setup()
        {
            engine = new SinglethreadEngine();
            state  = new State(engine);

            state.Setup(cd, engine);

            Common.Binders.Label(cd, engine,
                                 map.GetComponent <Text>("user_id"), state.UserId,
                                 i => string.Format("User ID: {0}", i.HasValue ? i.Value.ToString() : "<none>")
                                 );
            Common.Binders.Label(cd, engine,
                                 map.GetComponent <Text>("repo_count"), state.RepoCount,
                                 i => string.Format("{0} repositories", i.HasValue ? i.Value.ToString() : "<none>")
                                 );
            Common.Binders.InputField(cd, engine,
                                      map.GetComponent <InputField>("user_name"), state.UserName,
                                      s => s
                                      );
            Common.Binders.Enabled(cd, engine,
                                   map.Get("is_busy"), state.IsBusy
                                   );

            engine.Mainer(cd, Dep.On(state.HttpUserId.Error), () =>
            {
                HttpError error;
                if (state.HttpUserId.Error.TryRead(out error))
                {
                    Debug.Log(error);
                }
            });
        }
Esempio n. 5
0
        public void Setup(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.Setup(cd, engine, state.SceneStack);

            engine.Worker(cd, Dep.On(Scene.Open, state.Inventory.Items), () =>
            {
                Item openItem;
                if (Scene.Open.TryRead(out openItem))
                {
                    Item.Write(openItem);
                }
                else
                {
                    var item  = Item.Read();
                    var items = state.Inventory.Items.Read();
                    if (item != null && !items.Contains(item))
                    {
                        Item.Write(null);
                    }
                }
            });
            engine.Worker(cd, Dep.On(Scene.State, Item), () =>
            {
                if (Scene.State == SceneState.Opened && Item.Read() == null)
                {
                    Scene.Close.Fire(Empty.Instance);
                }
            });
        }
Esempio n. 6
0
        public void SetupUnity(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.SetupUnity(cd, engine);
            UpgradeItem.SetupUnity(cd, engine);
            SellItem.SetupUnity(cd, engine);

            engine.Mainer(cd, Dep.On(Scene.Root), () =>
            {
                var root = Scene.Root.Read();
                if (!root)
                {
                    return;
                }
                var map = root.GetComponent <Common.Map>();
                var scd = root.GetComponent <Common.DisposeOnDestroy>().cd;

                Common.Binders.Label(scd, engine,
                                     map.GetComponent <Text>("gold"), state.Gold,
                                     i => string.Format("Gold: {0}", i)
                                     );
                Common.Binders.Click(scd, engine,
                                     map.GetComponent <Common.Clickable>("upgrade"), UpgradeItem.Trigger,
                                     () => SelectedItem.Read()
                                     );
                Common.Binders.Interactable(scd, engine,
                                            map.GetComponent <Button>("upgrade"), UpgradeItem.Status,
                                            b => b
                                            );
                Common.Binders.List(scd, engine,
                                    map.GetComponent <Transform>("items"),
                                    map.GetComponent <Common.Map>("item"),
                                    Items, (icd, imap, item) =>
                {
                    Common.Binders.Label(icd, engine,
                                         imap.GetComponent <Text>("level"), item.Level,
                                         i => string.Format("Lv.{0}", i)
                                         );
                    Common.Binders.Image(icd, engine,
                                         imap.GetComponent <Image>("image"), item.Image
                                         );
                    Common.Binders.ButtonClick(icd, engine,
                                               imap.GetComponent <Button>("view_details"),
                                               () => ItemDetails.CreateAndShow(engine, state, item)
                                               );
                    Common.Binders.Click(icd, engine,
                                         imap.GetComponent <Common.Clickable>("select"),
                                         () => SelectedItem.Raw.Write(
                                             SelectedItem.Raw == item.Id ? null : item.Id)
                                         );
                    Common.Binders.Enabled(icd, engine,
                                           imap.Get("is_selected"), SelectedItem,
                                           it => it == item
                                           );
                }
                                    );
            });
        }
Esempio n. 7
0
    void Start()
    {
        Deform = G.Engine.Op <Ops.Deform>();

        deformingMesh    = GetComponent <MeshFilter>().mesh;
        originalVertices = deformingMesh.vertices;
        {
            var tmp = new Vector3[originalVertices.Length];
            Array.Copy(originalVertices, tmp, originalVertices.Length);
            displacedVertices = new Ar <Vector3>(G.Engine, tmp);
        }
        vertexVelocities = new Ar <Vector3>(G.Engine, new Vector3[originalVertices.Length]);
        uniformScale     = transform.localScale.x;

        G.Engine.Computer(cd, Dep.On(Deform, G.Tick), () =>
        {
            if (Deform.Count <= 0 && isStill(this.vertexVelocities.Read()))
            {
                return;
            }
            var vertexVelocities  = this.vertexVelocities.AsWrite();
            var displacedVertices = this.displacedVertices.Read();
            float dt = G.Tick.Reduced;

            for (int i = 0, n = Deform.Count; i < n; ++i)
            {
                var d = Deform[i];
                AddDeformingForce(d.Point, d.Force, displacedVertices, vertexVelocities, dt);
            }

            for (int i = 0, n = displacedVertices.Length; i < n; i++)
            {
                UpdateVelocity(i, displacedVertices, vertexVelocities, dt);
            }
        });
        G.Engine.Computer(cd, Dep.On(G.Tick), () =>
        {
            if (isStill(this.vertexVelocities.Read()))
            {
                return;
            }
            var vertexVelocities  = this.vertexVelocities.Read();
            var displacedVertices = this.displacedVertices.AsWrite();
            float dt = G.Tick.Reduced;

            for (int i = 0, n = displacedVertices.Length; i < n; i++)
            {
                UpdateVertex(i, displacedVertices, vertexVelocities, dt);
            }
        });

        G.Engine.Reader(cd, Dep.On(G.Tick), () =>
        {
            deformingMesh.vertices = displacedVertices.Read();
            deformingMesh.RecalculateNormals();
        });
    }
Esempio n. 8
0
 public void Setup(CompositeDisposable cd, IEngine engine, State state)
 {
     engine.OpWorker(cd, Dep.On(state.Inventory.UpgradeItem.Yes), () =>
     {
         if (state.Inventory.UpgradeItem.Yes.Unwrap == this)
         {
             Level.Write(Level + 1);
         }
     });
 }
Esempio n. 9
0
        public void Setup(CompositeDisposable cd, IEngine engine)
        {
            engine.Worker(cd, Dep.On(Register, Unregister), () =>
            {
                var registered = registeredScenes.AsWriteProxy();
                for (int i = 0, n = Register.Count; i < n; ++i)
                {
                    registered.Add(Register[i]);
                }
                for (int i = 0, n = Unregister.Count; i < n; ++i)
                {
                    // TODO Should be RemoveExact
                    registered.Remove(Unregister[i]);
                }
                registered.Commit();
            });

            var watcher = engine.Wa(cd, registeredScenes, scene => scene.State);

            engine.Worker(cd, Dep.On(watcher, Unregister), () =>
            {
                var active = ActiveScenes.AsWriteProxy();
                for (int i = 0, n = Unregister.Count; i < n; ++i)
                {
                    active.Remove(Unregister[i]);
                }
                for (int i = 0, n = registeredScenes.Count; i < n; ++i)
                {
                    var scene = registeredScenes[i];
                    var state = scene.State.Read();
                    if ((state == SceneState.Opened || state == SceneState.Opening) &&
                        !active.Contains(scene))
                    {
                        active.Add(scene);
                    }
                    else if (state == SceneState.Closed && active.Contains(scene))
                    {
                        active.Remove(scene);
                    }
                }
                active.Commit();
            });

            engine.Worker(cd, Dep.On(Back), () =>
            {
                bool isSystem;
                if (!Back.TryRead(out isSystem) || ActiveScenes.Count <= 1)
                {
                    return;
                }
                ActiveScenes[ActiveScenes.Count - 1].Back.Fire(isSystem);
            });
        }
Esempio n. 10
0
        public void Setup(CompositeDisposable cd, IEngine engine, SceneStack sceneStack)
        {
            Dialog.Setup(cd, engine, sceneStack);

            engine.Worker(cd, Dep.On(Status, Trigger), () =>
            {
                if (Trigger && Status)
                {
                    Dialog.Open.Fire(Empty.Instance);
                }
            });
            engine.Worker(cd, Dep.On(Status, Trigger, Yes, No), () =>
            {
                T value;
                if (!Status || Yes || No)
                {
                    Current.Write(default(T));
                }
                else if (Trigger.TryRead(out value))
                {
                    Current.Write(value);
                }
            });
            engine.Worker(cd, Dep.On(Status, Trigger), () =>
            {
                T value;
                if (Trigger.TryRead(out value) && !Status)
                {
                    Rejected.Fire(value);
                }
            });
            engine.Worker(cd, Dep.On(Status, Dialog.State, Yes, No), () =>
            {
                if (!Status && Dialog.State == SceneState.Opened)
                {
                    Dialog.Close.Fire(Empty.Instance);
                }
                else if (Yes || No)
                {
                    Dialog.Close.Fire(Empty.Instance);
                }
            });

            engine.Worker(cd, Dep.On(Status, Yes), () =>
            {
                if (Yes && !Status)
                {
                    throw new InvalidOperationException();
                }
            });
        }
Esempio n. 11
0
        public void SetupUnity(CompositeDisposable cd, IEngine engine)
        {
            engine.Mainer(cd, Dep.On(Back), () =>
            {
                bool isSystem;
                if (!Back.TryRead(out isSystem) || !isSystem || ActiveScenes.Count > 1)
                {
                    return;
                }

#if UNITY_EDITOR
                EditorApplication.isPlaying = false;
#else
                Application.Quit();
#endif
            });

            Common.CoroutineExecutor.Instance.StartCoroutine(cd, CheckBack());
        }
Esempio n. 12
0
        public void Setup(CompositeDisposable cd, IEngine engine)
        {
            HttpUserId.Setup(cd, engine);
            HttpRepoCount.Setup(cd, engine);

            engine.Worker(cd, Dep.On(UserName, HttpUserId.Request, HttpUserId.Error, HttpUserId.Response), () =>
            {
                int resUserId;
                if (string.IsNullOrEmpty(UserName) || HttpUserId.Request || HttpUserId.Error)
                {
                    UserId.Write(null);
                }
                else if (HttpUserId.Response.TryRead(out resUserId))
                {
                    UserId.Write(resUserId);
                }
            });
            engine.Worker(cd, Dep.On(UserName, HttpRepoCount.Request, HttpRepoCount.Error, HttpRepoCount.Response), () =>
            {
                int?resRepoCount;
                if (string.IsNullOrEmpty(UserName) || HttpRepoCount.Request || HttpRepoCount.Error)
                {
                    RepoCount.Write(null);
                }
                else if (HttpRepoCount.Response.TryRead(out resRepoCount))
                {
                    RepoCount.Write(resRepoCount);
                }
            });
            engine.Worker(cd, Dep.On(UserName), () =>
            {
                if (!string.IsNullOrEmpty(UserName))
                {
                    HttpUserId.Request.Fire(UserName);
                    HttpRepoCount.Request.Fire(UserName);
                }
            });
            engine.Worker(cd, Dep.On(HttpUserId.Requesting, HttpRepoCount.Requesting), () =>
            {
                IsBusy.Write(HttpUserId.Requesting > 0 || HttpRepoCount.Requesting > 0);
            });
        }
Esempio n. 13
0
        public void Setup(CompositeDisposable cd, IEngine engine)
        {
            var state = this;

            Home.Setup(cd, engine, state);
            Inventory.Setup(cd, engine, state);
            SimpleLoading.Setup(cd, engine, state.SceneStack);

            SceneStack.Setup(cd, engine);

            engine.Worker(cd, Dep.On(Inventory.UpgradeItem.Yes, Inventory.SellItem.Yes), () =>
            {
                Gold.Write(Gold
                           - 10 * (Inventory.UpgradeItem.Yes ? 1 : 0)
                           + 5 * (Inventory.SellItem.Yes ? 1 : 0)
                           );
            });
            engine.Worker(cd, Dep.On(SimpleLoading.State,
                                     Home.Scene.State, Inventory.Scene.State,
                                     Inventory.UpgradeItem.Dialog.State), () =>
            {
                bool needLoading = Home.Scene.State == SceneState.Opening ||
                                   Inventory.Scene.State == SceneState.Opening ||
                                   Inventory.UpgradeItem.Dialog.State == SceneState.Opening;

                if (SimpleLoading.State == SceneState.Closed && needLoading)
                {
                    SimpleLoading.Open.Fire(Empty.Instance);
                }
                else if (SimpleLoading.State == SceneState.Opened && !needLoading)
                {
                    SimpleLoading.Close.Fire(Empty.Instance);
                }
            });
            engine.Worker(cd, Dep.On(SceneStack.ActiveScenes), () =>
            {
                var active = SceneStack.ActiveScenes.Read();
                ShouldShowBack.Write(active.Count > 1 && active[active.Count - 1].BackAutoClose);
            });
        }
Esempio n. 14
0
 public void Setup(CompositeDisposable cd, IEngine engine)
 {
     engine.Worker(cd, Dep.On(
                       Inventory.SellItem.Yes, Inventory.SellItems.Yes,
                       Inventory.FuseItem.Yes), () =>
     {
         var items = Profile.Items.AsWriteProxy();
         if (Inventory.SellItem.Yes)
         {
             items.RemoveExact(Inventory.ViewingItem);
         }
         if (Inventory.SellItems.Yes)
         {
             items.RemoveExact(Inventory.SellingItems.Read());
         }
         if (Inventory.FuseItem.Yes)
         {
             items.Add(Inventory.FuseItemResult);
         }
         items.Commit();
     });
 }
Esempio n. 15
0
        public void SetupUnity(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.SetupUnity(cd, engine);

            engine.Mainer(cd, Dep.On(Scene.Root), () =>
            {
                var root = Scene.Root.Read();
                if (!root)
                {
                    return;
                }
                var map = root.GetComponent <Common.Map>();
                var scd = root.GetComponent <Common.DisposeOnDestroy>().cd;

                Common.Binders.Label(scd, engine,
                                     map.GetComponent <Text>("gold"), state.Gold,
                                     i => string.Format("Gold: {0}", i)
                                     );
                Common.Binders.ButtonClick(scd, engine,
                                           map.GetComponent <Button>("inventory"), state.Inventory.Scene.Open,
                                           () => Empty.Instance
                                           );
                Common.Binders.Tabs(scd, engine, map,
                                    new string[] { "tab_base", "tab_more" },
                                    new string[] { "toggle_base", "toggle_more" },
                                    SelectedTab.Raw, s => s,
                                    SelectedTab, s => s
                                    );
                Common.Binders.Interactable(scd, engine,
                                            map.GetComponent <Toggle>("toggle_more"), CanOpenMore,
                                            b => b
                                            );
                Common.Binders.NonInteractableClick(scd, engine,
                                                    map.GetComponent <Common.Clickable>("toggle_more"),
                                                    () => UnityEngine.Debug.Log("Open inventory first, to unlock More")
                                                    );
            });
        }
Esempio n. 16
0
        public void Setup(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.Setup(cd, engine, state.SceneStack);

            engine.Worker(cd, Dep.On(SelectedTab.Raw, state.Inventory.Scene.Open), () =>
            {
                if (state.Inventory.Scene.Open && CanOpenMore)
                {
                    SelectedTab.Write("tab_more");
                }
                else
                {
                    SelectedTab.Write(SelectedTab.Raw);
                }
            });
            engine.Worker(cd, Dep.On(state.Inventory.Scene.Open), () =>
            {
                if (state.Inventory.Scene.Open)
                {
                    CanOpenMore.Write(true);
                }
            });
        }
Esempio n. 17
0
        public void Setup()
        {
            engine    = new SinglethreadEngine();
            input     = engine.ElWithRaw(string.Empty);
            randomize = engine.Op <Empty>();

            var sb   = new StringBuilder();
            var rand = new System.Random();

            engine.Worker(cd, Dep.On(input.Raw, randomize), () =>
            {
                sb.Length = 0;
                sb.Append(input.Raw.Read());
                sb.Replace("hello", "HELLO");
                sb.Replace("bye", "");

                if (randomize)
                {
                    for (int i = 0, n = sb.Length; i < n; ++i)
                    {
                        sb[i] += (char)rand.Next(-10, 10);
                    }
                }

                input.Write(sb.ToString());
            });

            Common.Binders.InputFieldTwoWay(cd, engine,
                                            map.GetComponent <InputField>("input"),
                                            input.Raw, s => s,
                                            input, s => s
                                            );
            Common.Binders.ButtonClick(cd, engine,
                                       map.GetComponent <Button>("randomize"), randomize,
                                       () => Empty.Instance
                                       );
        }
Esempio n. 18
0
        public void Setup(CompositeDisposable cd, IEngine engine)
        {
            if (rawResponse != null)
            {
                engine.Worker(cd, Dep.On(rawResponse), () =>
                {
                    string raw;
                    if (rawResponse.TryRead(out raw))
                    {
                        var res = default(TRes);
                        if (responseParser != null)
                        {
                            res = responseParser(raw);
                        }
                        Response.Fire(res);
                    }
                });
            }
            engine.Worker(cd, Dep.On(Request, Response, Error), () =>
            {
                int result = Requesting - (Response ? 1 : 0) - (Error ? 1 : 0);
                switch (pipe)
                {
                case HttpPipe.Multiple:
                    result += Request ? 1 : 0;
                    break;

                case HttpPipe.SingleFirst:
                case HttpPipe.SingleLast:
                    result += Requesting > 0 ? 0 : Math.Min(1, Request ? 1 : 0);
                    break;
                }
                if (result < 0)
                {
                    throw new NotImplementedException();
                }
                else if (pipe.IsSingle() && result > 1)
                {
                    throw new NotImplementedException();
                }
                Requesting.Write(result);
            });

            engine.Mainer(cd, Dep.On(Request), () =>
            {
                TReq req;
                if (Request.TryRead(out req))
                {
                    if (pipe == HttpPipe.SingleFirst && Requesting > 0)
                    {
                        UnityEngine.Debug.LogWarning("Skip a HTTP request, because of single on: " + url);
                        UnityEngine.Debug.LogWarning("Request: " + req);
                    }
                    else
                    {
                        if (pipe == HttpPipe.SingleLast && Requesting > 0)
                        {
                            UnityEngine.Debug.LogWarning("Stop previous HTTP request, because of single on: " + url);
                            UnityEngine.Debug.LogWarning("New request: " + req);
                            HttpMain.Instance.StopCoroutine(lastExec);
                        }
                        lastExec = HttpMain.Instance.StartCoroutine(Exec(req));
                    }
                }
            });
        }
Esempio n. 19
0
        public void Setup(CompositeDisposable cd, IEngine engine, State state)
        {
            Scene.Setup(cd, engine, state.SceneStack);
            ItemFactory.Setup(cd, engine, state);
            UpgradeItem.Setup(cd, engine, state.SceneStack);
            SellItem.Setup(cd, engine, state.SceneStack);

            engine.Worker(cd, Dep.On(state.Gold, SelectedItem), () =>
            {
                UpgradeItem.Status.Write(state.Gold >= 10 && SelectedItem.Read() != null);
            });
            engine.Worker(cd, Dep.On(SellItem.Current, Items), () =>
            {
                var current = SellItem.Current.Read();
                SellItem.Status.Write(current == null || Items.Read().Contains(current));
            });
            int nextId = 1;

            engine.Worker(cd, Dep.On(Scene.Open, SellItem.Yes), () =>
            {
                var items = Items.AsWriteProxy();
                if (Scene.Open)
                {
                    items.Add(ItemFactory.Create(
                                  nextId.ToString(), "Sword " + nextId,
                                  "item_" + ((nextId - 1) % 3 + 1), 1
                                  ));
                    ++nextId;
                }
                Item item;
                if (SellItem.Yes.TryRead(out item))
                {
                    ItemFactory.Dispose(item);
                    // TODO Should be remove exact
                    items.Remove(item);
                }
                items.Commit();
            });
            engine.Worker(cd, Dep.On(SelectedItem.Raw, Items), () =>
            {
                var items  = Items.Read();
                var itemId = SelectedItem.Raw.Read();
                if (SelectedItem.Read() != null && !items.Contains(SelectedItem))
                {
                    SelectedItem.Write(null);
                }
                else if (!string.IsNullOrEmpty(itemId))
                {
                    for (int i = 0, n = items.Count; i < n; ++i)
                    {
                        if (items[i].Id == itemId)
                        {
                            SelectedItem.Write(items[i]);
                            break;
                        }
                    }
                }
            });
            engine.Worker(cd, Dep.On(UpgradeItem.Dialog.Back), () =>
            {
                var back = UpgradeItem.Dialog.Back;
                if (!back || !back.Unwrap)
                {
                    return;
                }
                UpgradeItem.Dialog.Close.Fire(Empty.Instance);
            });

#if DEBUG
            engine.OpWorker(cd, Dep.On(UpgradeItem.Trigger), () =>
            {
                if (!Items.Read().Contains(UpgradeItem.Trigger.Unwrap))
                {
                    throw new NotImplementedException();
                }
            });
            engine.OpWorker(cd, Dep.On(SellItem.Trigger), () =>
            {
                if (!Items.Read().Contains(SellItem.Trigger.Unwrap))
                {
                    throw new NotImplementedException();
                }
            });
#endif
        }