Ejemplo n.º 1
0
    private void CreateView(ExampleNetworkedEntity entity)
    {
        LSLog.LogImportant("print: " + JsonUtility.ToJson(entity));
        ColyseusNetworkedEntityView newView = Instantiate(prefab);

        ExampleManager.Instance.RegisterNetworkedEntityView(entity, newView);
        newView.gameObject.SetActive(true);
    }
    public void EntityRemoved(ExampleNetworkedEntity entity, ColyseusNetworkedEntityView view)
    {
        foreach (Scoreboard board in scoreboards)
        {
            board.RemoveView(entity);
        }

        scoreOnlyBoard?.RemoveView(entity);
    }
    /// <summary>
    ///     The callback for the event when a <see cref="ExampleNetworkedEntity" /> is removed from a room.
    /// </summary>
    /// <param name="entity">The entity that was just removed.</param>
    /// <param name="key">The entity's key</param>
    private void OnEntityRemoved(string key, ExampleNetworkedEntity entity)
    {
        if (_entities.ContainsKey(entity.id))
        {
            _entities.Remove(entity.id);
        }

        ColyseusNetworkedEntityView view = null;

        if (_entityViews.ContainsKey(entity.id))
        {
            view = _entityViews[entity.id];
            _entityViews.Remove(entity.id);
        }

        onRemoveNetworkEntity?.Invoke(entity, view);
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Registers the <see cref="ExampleNetworkedEntityView"/> with the manager for tracking.
    /// <para>Initializes the <see cref="ExampleNetworkedEntityView"/> if it has not yet been initialized.</para>
    /// </summary>
    /// <param name="model"></param>
    /// <param name="view"></param>
    public void RegisterNetworkedEntityView(ExampleNetworkedEntity model, ColyseusNetworkedEntityView view)
    {
        if (string.IsNullOrEmpty(model.id) || view == null || _entities.ContainsKey(model.id) == false)
        {
            LSLog.LogError("Cannot Find Entity in Room");
            return;
        }

        ExampleNetworkedEntityView entityView = (ExampleNetworkedEntityView)view;

        if (entityView && !entityView.HasInit)
        {
            entityView.InitiView(model);
        }

        _entityViews.Add(model.id, (ExampleNetworkedEntityView)view);
        view.SendMessage("OnEntityViewRegistered", SendMessageOptions.DontRequireReceiver);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Creates a new <see cref="ExampleNetworkedEntity"/> attributes and <see cref="ColyseusNetworkedEntityView"/>.
    /// </summary>
    /// <param name="room">The room the entity will be added to</param>
    /// <param name="attributes">Position for the new entity</param>
    /// <param name="viewToAssign">The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity"/></param>
    /// <param name="callback">Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity"/></param>
    public void CreateNetworkedEntity(ColyseusRoom <ExampleRoomState> room, Dictionary <string, object> attributes = null, ColyseusNetworkedEntityView viewToAssign = null, Action <ExampleNetworkedEntity> callback = null)
    {
        try
        {
            string creationId = null;

            if (viewToAssign != null || callback != null)
            {
                creationId = Guid.NewGuid().ToString();
                if (callback != null)
                {
                    if (viewToAssign != null)
                    {
                        _creationCallbacks.Add(creationId, (newEntity) =>
                        {
                            RegisterNetworkedEntityView(newEntity, viewToAssign);
                            callback.Invoke(newEntity);
                        });
                    }
                    else
                    {
                        _creationCallbacks.Add(creationId, callback);
                    }
                }
                else
                {
                    _creationCallbacks.Add(creationId,
                                           (newEntity) => { RegisterNetworkedEntityView(newEntity, viewToAssign); });
                }
            }

            _ = room.Send("createEntity",
                          new EntityCreationMessage()
            {
                creationId = creationId, attributes = attributes
            });
        }
        catch (System.Exception err)
        {
            LSLog.LogError(err.Message + err.StackTrace);
        }
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Creates a new <see cref="ExampleNetworkedEntity"/> with the given prefab, attributes, and <see cref="ColyseusNetworkedEntityView"/>.
    /// </summary>
    /// <param name="room">The room the entity will be added to</param>
    /// <param name="prefab">Prefab you would like to use</param>
    /// <param name="attributes">Position for the new entity</param>
    /// <param name="viewToAssign">The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity"/></param>
    /// <param name="callback">Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity"/></param>
    public void CreateNetworkedEntity(ColyseusRoom <ExampleRoomState> room, string prefab, Dictionary <string, object> attributes = null, ColyseusNetworkedEntityView viewToAssign = null, Action <ExampleNetworkedEntity> callback = null)
    {
        Dictionary <string, object> updatedAttributes = (attributes != null)
            ? new Dictionary <string, object>(attributes)
            : new Dictionary <string, object>();

        updatedAttributes.Add("prefab", prefab);
        CreateNetworkedEntity(room, updatedAttributes, viewToAssign, callback);
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Creates a new <see cref="ExampleNetworkedEntity"/> with the given <see cref="ColyseusNetworkedEntityView"/> and attributes
    /// and places it at the provided position and rotation.
    /// </summary>
    /// <param name="room">The room the entity will be added to</param>
    /// <param name="position">Position for the new entity</param>
    /// <param name="rotation">Position for the new entity</param>
    /// <param name="attributes">Position for the new entity</param>
    /// <param name="viewToAssign">The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity"/></param>
    /// <param name="callback">Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity"/></param>
    public void CreateNetworkedEntityWithTransform(ColyseusRoom <ExampleRoomState> room, Vector3 position, Quaternion rotation,
                                                   Dictionary <string, object> attributes   = null, ColyseusNetworkedEntityView viewToAssign = null,
                                                   Action <ExampleNetworkedEntity> callback = null)
    {
        if (attributes != null)
        {
            attributes.Add("creationPos", new object[3] {
                position.x, position.y, position.z
            });
            attributes.Add("creationRot", new object[4] {
                rotation.x, rotation.y, rotation.z, rotation.w
            });
        }
        else
        {
            attributes = new Dictionary <string, object>()
            {
                ["creationPos"] = new object[3] {
                    position.x, position.y, position.z
                },
                ["creationRot"] = new object[4] {
                    rotation.x, rotation.y, rotation.z, rotation.w
                }
            };
        }

        CreateNetworkedEntity(room, attributes, viewToAssign, callback);
    }
 private void RemoveView(ColyseusNetworkedEntityView view)
 {
     view.SendMessage("OnEntityRemoved", SendMessageOptions.DontRequireReceiver);
 }
 private void OnNetworkRemove(ExampleNetworkedEntity entity, ColyseusNetworkedEntityView view)
 {
     RemoveView(view);
     scoreboardController.EntityRemoved(entity, view);
 }
Ejemplo n.º 10
0
 private void OnNetworkRemove(ExampleNetworkedEntity entity, ColyseusNetworkedEntityView view)
 {
     RemoveView(view);
 }
 /// <summary>
 ///     Registers the <see cref="ColyseusNetworkedEntityView" /> with the manager for tracking.
 ///     <para>Initializes the <see cref="ColyseusNetworkedEntityView" /> if it has not yet been initialized.</para>
 /// </summary>
 /// <param name="model"></param>
 /// <param name="view"></param>
 public void RegisterNetworkedEntityView(ExampleNetworkedEntity model, ColyseusNetworkedEntityView view)
 {
     _networkedEntityFactory.RegisterNetworkedEntityView(model, view);
 }
 /// <summary>
 ///     Creates a new <see cref="ExampleNetworkedEntity" /> attributes and <see cref="ColyseusNetworkedEntityView" />.
 /// </summary>
 /// <param name="attributes">Position for the new entity</param>
 /// <param name="viewToAssign">
 ///     The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity" />
 /// </param>
 /// <param name="callback">
 ///     Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity" />
 /// </param>
 public static void CreateNetworkedEntity(Dictionary <string, object> attributes   = null,
                                          ColyseusNetworkedEntityView viewToAssign = null, Action <ExampleNetworkedEntity> callback = null)
 {
     Instance._networkedEntityFactory.CreateNetworkedEntity(Instance._roomController.Room, attributes, viewToAssign,
                                                            callback);
 }
 /// <summary>
 ///     Creates a new <see cref="ExampleNetworkedEntity" /> with the given <see cref="ColyseusNetworkedEntityView" /> and
 ///     attributes
 ///     and places it at the provided position and rotation.
 /// </summary>
 /// <param name="position">Position for the new entity</param>
 /// <param name="rotation">Position for the new entity</param>
 /// <param name="attributes">Position for the new entity</param>
 /// <param name="viewToAssign">
 ///     The provided view that will be assigned to the new <see cref="ExampleNetworkedEntity" />
 /// </param>
 /// <param name="callback">
 ///     Callback that will be invoked with the newly created <see cref="ExampleNetworkedEntity" />
 /// </param>
 public static void CreateNetworkedEntityWithTransform(Vector3 position, Quaternion rotation,
                                                       Dictionary <string, object> attributes   = null, ColyseusNetworkedEntityView viewToAssign = null,
                                                       Action <ExampleNetworkedEntity> callback = null)
 {
     Instance._networkedEntityFactory.CreateNetworkedEntityWithTransform(Instance._roomController.Room, position,
                                                                         rotation, attributes, viewToAssign, callback);
 }
 /// <summary>
 ///     Send a Remote Function Call
 /// </summary>
 /// <param name="entity">The entity we want to send the RFC</param>
 /// <param name="function">The name of the function to call</param>
 /// <param name="param">The parameters of the function to call</param>
 /// <param name="target">Who should receive this RFC</param>
 public static void RFC(ColyseusNetworkedEntityView entity, string function, object[] param,
                        ExampleRFCTargets target = ExampleRFCTargets.ALL)
 {
     RFC(entity.Id, function, param, target);
 }