Example #1
0
    private void Startup()
    {
        if (ObjectState.ObjectStates.Dead != StatefulObject.GetState(gameObject).State)
        {
            _isActive = true;

            BoxCollider2D collider = GetComponent <BoxCollider2D>();
            collider.isTrigger = true;
            foreach (var obj in Physics2D.OverlapBoxAll(transform.position, collider.size, 0f))
            {
                if (obj.CompareTag(_targetTag))
                {
                    _isActive = false;
                    if (_canTriggerOnStart)
                    {
                        triggered.Invoke();
                    }
                }
            }
        }
        else
        {
            gameObject.SetActive(false);
        }
    }
Example #2
0
        private void Use(Ray ray)
        {
            if (heldObject.Value != null)
            {
                Plane plane = new Plane(Vector3.up, Vector3.zero);

                float d;
                plane.Raycast(ray, out d);

                if (d > 0)
                {
                    Vector3 target = ray.origin + ray.direction * d;
                    Vector3 to     = target - transform.position;

                    heldObject.Value.GetComponent <Rigidbody>().velocity = to * throwVelocity;
                    heldObject.Value = null;
                }
            }
            else
            {
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    StatefulObject stateful = hit.collider.GetComponentInParent <StatefulObject>();

                    if (stateful != null && stateful != GetComponent <StatefulObject>() && stateful.Ownable.TryTakeOwnership(GetComponent <OwnableObject>().Owner.Value))
                    {
                        heldObject.Value = stateful;
                    }
                }
            }
        }
Example #3
0
        public void Receive_WhenPassedNull_ThrowsArgumentNullException()
        {
            var contract = StatefulObject.Create <FavoriteNumberContract>(new Dictionary <string, object>());
            var receiver = contract.RegisterWithRegistry(new InMemoryIntegration.FakeContractContext(this.addressFactory.CreateAddress()));

            Assert.Throws <ArgumentNullException>(() => receiver.Invoke(null));
        }
        public static StatefulObject DeserializeStatefulObject(byte[] serialized)
        {
            var dictionary = StateSerialization.DeserializeState(serialized);

            var type  = Type.GetType(dictionary.Get <string>("Type"));
            var state = dictionary.Get <IDictionary <string, object> >("State");

            return(StatefulObject.Create(type, state, true));
        }
Example #5
0
    public static void UpdateState(GameObject obj, ObjectState objState)
    {
        StatefulObject state = obj.GetComponent <StatefulObject>();

        if (null != state)
        {
            state.State = objState;
        }
    }
        public static byte[] SerializeStatefulObject(StatefulObject statefulObject)
        {
            var dictionary = new Dictionary <string, object>()
            {
                { "Type", statefulObject.GetType().AssemblyQualifiedName },
                { "State", statefulObject.GetState() },
            };

            return(StateSerialization.SerializeState(dictionary));
        }
Example #7
0
    private void Spawn(StatefulObject obj)
    {
        if (obj.obj != null)
        {
            return;
        }

        obj.obj     = Instantiate(objectsToSpawn[obj.index]);
        obj.spawned = true;
        obj.obj.transform.position = objectsToSpawn[obj.index].transform.position;
        obj.obj.SetActive(true);
    }
Example #8
0
        private void HeldObject_OnChanged(StatefulObject current, StatefulObject previous)
        {
            if (current != null)
            {
                current.GetComponent <Rigidbody>().isKinematic = true;
            }

            if (previous != null)
            {
                previous.GetComponent <Rigidbody>().isKinematic = false;
                previous.Ownable.RelinquishOwnership();
            }
        }
            public ContractRegistry LoadContractRegistry()
            {
                var data = this.LoadData(this.integration.contractRegistryAddress);

                this.integration.logger.LogTrace("Received registry data: " + data.Length + " bytes");

                if (data.Length == 0)
                {
                    return(new ContractRegistry());
                }

                return(StatefulObject.Create <ContractRegistry>(
                           StateSerialization.DeserializeState(data)));
            }
Example #10
0
 public void OnTriggerEnter2D(Collider2D collision)
 {
     if ((_isActive) && collision.CompareTag(_targetTag))
     {
         triggered.Invoke();
         _isActive = false;
         if (_oneshot)
         {
             BoxCollider2D collider = GetComponent <BoxCollider2D>();
             collider.enabled = false;
             StatefulObject.GetState(gameObject).State = ObjectState.ObjectStates.Dead;
         }
     }
 }
Example #11
0
    public static ObjectState GetState(GameObject obj)
    {
        StatefulObject state    = obj.GetComponent <StatefulObject>();
        ObjectState    objState = null;

        if (null == state)
        {
            objState = new ObjectState();
        }
        else
        {
            objState = state.State;
        }

        return(objState);
    }
Example #12
0
    private void Start()
    {
        triggers = GetComponentsInChildren <TriggerNotify>();
        foreach (var trigger in triggers)
        {
            trigger.OnTriggerEnter += SpawnAll;
        }

        objects = new StatefulObject[objectsToSpawn.Count];

        for (int i = 0; i < objectsToSpawn.Count; i++)
        {
            objects[i] = new StatefulObject {
                index = i
            }
        }
        ;
    }
Example #13
0
        public void Receive_WhenReceivedSupportedAction_ReturnsTrue()
        {
            var contract = StatefulObject.Create <FavoriteNumberContract>(new Dictionary <string, object>()
            {
                { "User", null }
            });
            var receiver = contract.RegisterWithRegistry(new InMemoryIntegration.FakeContractContext(this.addressFactory.CreateAddress()));

            receiver.Invoke(new Message(
                                contract.Address,
                                contract.Address,
                                contract.Address,
                                SetFavoriteNumberAction.Type,
                                new Dictionary <string, object>()
            {
                { SetFavoriteNumberAction.Number, 0 },
            }));
        }
        public void Serializes_And_Deserializes_Contracts()
        {
            var address      = new Address(new byte[] { 10, 20, 127, 54, 51 });
            var adminAddress = new Address(new byte[] { 10, 20, 4 });
            var contract     = StatefulObject.Create <FavoriteNumberContract>(new Dictionary <string, object> {
                { "Admin", adminAddress.ToString() }, { "Number", 15 }
            });

            contract.RegisterWithRegistry(new InMemoryIntegration.FakeContractContext(address));

            var serializedContract = StrongForceSerialization.SerializeStatefulObject(contract);

            var deserializedContract = (FavoriteNumberContract)StrongForceSerialization.DeserializeStatefulObject(serializedContract);

            deserializedContract.RegisterWithRegistry(new InMemoryIntegration.FakeContractContext(address));

            Assert.Equal(contract.GetState(), deserializedContract.GetState());
            Assert.Equal(contract.Address, deserializedContract.Address);
            Assert.Equal(contract.Number, deserializedContract.Number);
        }
Example #15
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            var initialKit = (KitContract)StatefulObject.Create(
                this.settings.InitialKitType,
                this.settings.InitialKitPayload);

            initialKit.RegisterWithRegistry(
                new InMemoryIntegration.FakeContractContext(
                    KitContract.DefaultAddress));

            var facade = new CosmosIntegration(
                this.logger,
                StrongForceSerialization.SerializeStatefulObject(initialKit));

            this.server = new Server
            {
                Services = { Strongforce.StrongForce.BindService(facade) },
                Ports    = { new ServerPort(this.settings.Hostname, this.settings.Port, ServerCredentials.Insecure) },
            };
            this.server.Start();
            return(Task.CompletedTask);
        }
Example #16
0
    // Start is called before the first frame update
    void Start()
    {
        pantheroffset = Random.Range(-0.99f, 0.99f);
        StartPosition = this.transform.position;
        thisAnim      = this.GetComponent <Animator>();
        Player        = GameObject.Find("character");
        BloodSplatter = GameObject.Find("splat");

        _state = StatefulObject.GetState(gameObject);
        if (null == _state)
        {
            Debug.Log("What " + GetComponent <StatefulObject>().UniqueID);
        }

        if (ObjectState.ObjectStates.Dead == _state.State)
        {
            GameObject splatter = GameObject.Instantiate(BloodSplatter);
            transform.position                         = _state.Position;
            splatter.transform.position                = this.transform.position;
            GetComponent <SpriteRenderer>().enabled    = false;
            GetComponent <PolygonCollider2D>().enabled = false;
        }
    }
        public void Receive_WhenPassedSetFavoriteNumberAction_SetsNumberCorrectly()
        {
            const int expectedNumber = 32;

            var contract = StatefulObject.Create <FavoriteNumberContract>(new Dictionary <string, object>()
            {
                { "User", null }
            });
            var receiver = contract.RegisterWithRegistry(new InMemoryIntegration.FakeContractContext(this.addressFactory.CreateAddress()));

            var action = new Message(
                contract.Address,
                contract.Address,
                contract.Address,
                SetFavoriteNumberAction.Type,
                new Dictionary <string, object>()
            {
                { SetFavoriteNumberAction.Number, expectedNumber },
            });

            receiver.Invoke(action);

            Assert.Equal(expectedNumber, ((FavoriteNumberContract)contract).Number);
        }
Example #18
0
 private void AddDebugView(StatefulObject obj)
 {
     Instantiate(statefulObjectDebugViewPrefab).Initialize(obj);
 }
Example #19
0
    void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.gameObject.name == "Explosion" || coll.gameObject.name == "Slash")
        {
            if (lastHurt < Time.realtimeSinceStartup)
            {
                if (isHurt || enemyType == "panther")
                {
                    if (enemyType == "panther")
                    {
                        AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Jungle_Cat_See_You_Around);
                    }
                    else
                    {
                        if (thisAnim.name == "Enemy2")
                        {
                            AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Enemy_Die_Female);
                        }
                        else
                        {
                            AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Enemy_Die_Male);
                        }
                    }
                    GameObject splatter = GameObject.Instantiate(BloodSplatter);
                    splatter.transform.position = this.transform.position;
                    _state.State    = ObjectState.ObjectStates.Dead;
                    _state.Position = transform.position;
                    GameStateManager.Get().AddKill();
                    StatefulObject.UpdateState(gameObject, _state);
                    GetComponent <SpriteRenderer>().enabled    = false;
                    GetComponent <PolygonCollider2D>().enabled = false;
                }
                else
                {
                    switch (Random.Range(0, 3))
                    {
                    case 0:
                        AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Enemy_Hit_1);

                        break;

                    case 1:
                        AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Enemy_Hit_2);

                        break;

                    case 2:
                        AudioManager.Get().PlaySfxOnce(AudioManager.SFX.Enemy_Hit_3);

                        break;
                    }
                }

                setFalse(thisAnim);
                thisAnim.SetBool("IsHurt", true);
                isHurt   = true;
                lastHurt = Time.realtimeSinceStartup + 0.5f;
                if (ObjectState.ObjectStates.Dead != _state.State)
                {
                    this.gameObject.GetComponent <Rigidbody2D>().AddForce((this.transform.position - coll.transform.position) * 15, ForceMode2D.Impulse);
                }
            }
        }
    }
Example #20
0
        public override void Run(object[] args)
        {
            // Create an instance of StatefulObject.

            StatefulObject obj = new StatefulObject();

            // Output its state.

            Output.WriteLine(obj.State);
            // Output: ( 2 4 6 8 )

            // Attempt to modify the state through the State property.
            // It is not possible. The state is protected against
            // modifications from the outside world. It is only possible
            // to add an item through the member function Add.

            //obj.State.Add(10); // This is not possible.
            //obj.State.Remove(8); // This is also not possible.
            obj.Add(10); // This is the only way to modify the state.
            Output.WriteLine(obj.State);
            // Output: ( 2 4 6 8 10 )

            // Create a "snapshot" of the state. You will get a copy
            // that you can then modify. Naturally, the modifications to
            // the copy do not affect the original state.

            ArrayList <int> state = obj.State.GetWritableCopy();

            state.Add(12);
            Output.WriteLine(state);
            // Output: ( 2 4 6 8 10 12 )
            Output.WriteLine(obj.State);
            // Output: ( 2 4 6 8 10 )

            // Invoke GetMax by passing the state as the parameter.
            // Note that it is also possible to pass normal, writable
            // instances as parameters to such functions. This is
            // demonstrated below.

            Output.WriteLine(GetMax(obj.State));
            // Output: 10
            Output.WriteLine(GetMax(state)); // This is also possible.
            // Output: 12

            // By setting the PUBLIC_INNER conditional compilation
            // symbol, we are able to modify a read-only instance
            // through its Inner property. This, however, can cause the
            // instance to become incoherent and is thus to be avoided.

            obj.State.Inner.Add(12);
            Output.WriteLine(obj.State);
            // Output: ( 2 4 6 8 10 12 )

            // Create an ArrayList of sets of numbers and populate it.

            ArrayList <Set <int> > array = new ArrayList <Set <int> >(
                new Set <int>[] {
                new Set <int>(new int[] { 1, 3, 5 }),
                new Set <int>(new int[] { 2, 4, 6 }),
                new Set <int>(new int[] { 1, 2, 3 })
            });

            Output.WriteLine(array);
            // Output: ( { 1 3 5 } { 2 4 6 } { 1 2 3 } )

            // Create a read-only adapter for the created array of sets.

            // The easiest way to do it:
            ArrayList <Set <int> > .ReadOnly readOnlyArray = array;
            // This happens behind the curtains:
            //ArrayList<Set<int>>.ReadOnly readOnlyArray
            //  = new ArrayList<Set<int>>.ReadOnly(array);

            Output.WriteLine(readOnlyArray);
            // Output: ( { 1 3 5 } { 2 4 6 } { 1 2 3 } )

            // Try to add an empty set through the read-only adapter. It
            // is not possible. It is possible, however, to change each
            // of the three contained sets. This means that read-only
            // adapters are shallow.

            //readOnlyArray.Add(new Set<int>()); // Not possible.
            readOnlyArray[0].Add(7); // Still possible.
            Output.WriteLine(readOnlyArray);
            // Output: ( { 1 3 5 7 } { 2 4 6 } { 1 2 3 } )

            // To create a "deeply" read-only instance, you need to make
            // contained instances read-only as well.

            ArrayList <Set <int> .ReadOnly> .ReadOnly readOnlyArray2
                = new ArrayList <Set <int> .ReadOnly>(
                      new Set <int> .ReadOnly[] {
                new Set <int>(new int[] { 1, 3, 5 }),
                new Set <int>(new int[] { 2, 4, 6 }),
                new Set <int>(new int[] { 1, 2, 3 })
            });
            Output.WriteLine(readOnlyArray2);
            // Output: ( { 1 3 5 } { 2 4 6 } { 1 2 3 } )
            //readOnlyArray2.Add(new Set<int>()); // Not possible.
            //readOnlyArray2[0].Add(7); // Also not possible.
        }
Example #21
0
 public void Invoke(StatefulObject invoker, T arg)
 {
     StartInvoke(invoker, arg);
 }
Example #22
0
 public void Initialize(StatefulObject statefulObject)
 {
     this.statefulObject = statefulObject;
 }
Example #23
0
 private void OnAdd(StatefulObject obj)
 {
     AddDebugView(obj);
 }