Example #1
0
        protected override async Task SynchronizedStartAsync(CancellationToken cancellationToken)
        {
            if (ActivationGroups == null ||
                ActivationGroups.Count == 0)
            {
                throw new InvalidOperationException($"There's no defined activation group names. Please add at least one it into '{nameof(ActivationGroups)}' property.");
            }

            _logger.Debug("Loading types for activation...");

            foreach (var serviceType in GetServiceTypes())
            {
                var activatable = GetActivatable(serviceType);
                if (!ActivationGroups.Contains(activatable.Group))
                {
                    continue;
                }

                _logger.Write(LogEventLevel.Debug, nameof(ServiceActivator), null,
                              "Setting up service '{ServiceType}' for activation...", serviceType.Name);

                try
                {
                    var container = CreateServiceContainer(serviceType, activatable);
                    var service   = container.GetInstance <IService>();

                    _serviceContainer.Add(service, activatable.Tier);
                    ServiceProviders.Add(serviceType, container);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "An error occurred while activating service '{ServiceType}'", serviceType.Name);

                    throw;
                }
            }

            _logger.Debug("Starting service container...");

            await _serviceContainer.StartAsync(cancellationToken);

            _logger.Debug("Service container started");

            await base.SynchronizedStartAsync(cancellationToken);
        }
Example #2
0
    /// <summary>
    /// Load a vehicle
    /// </summary>
    /// <param name="file">File to load</param>
    /// <returns></returns>
    public GameObject LoadVehicle(string file)
    {
        Vehicle v;

        try
        {
            v = Vehicle.Load(file);
        }
        catch
        {
            return(null);
        }
        float mass = 0;

        Destroy(GameObject.Find("Vehicle"));
        GameObject vehicleGO = new GameObject("Vehicle");

        foreach (Vehicle.Part part in v.parts)
        {
            GameObject go;
            GameObject prefab = null;
            if (!assets.TryGetValue(part.type, out prefab))
            {
                prefab = new GameObject();
                Debug.LogErrorFormat("The prefab '{0}' of part '{1}' is null.", part.type, part.id);
            }

            go = Instantiate(
                prefab,
                new Vector3(part.x, part.y),
                Quaternion.Euler(0, 0, part.r)
                ) as GameObject;

            try
            {
                mass += go.GetComponent <Rigidbody2D>().mass;
            }
            catch { }
            go.GetComponent <Rigidbody2D>().simulated = false;
            go.name = part.id.ToString();
            go.transform.SetParent(vehicleGO.transform);
            go.transform.localScale = new Vector3(
                part.scaleX / go.transform.parent.localScale.x * (part.flipX ? -1 : 1),
                part.scaleY / go.transform.parent.localScale.x * (part.flipX ? -1 : 1), 1);
            if (part.type.ToLower().Contains("pod"))      //If this part is the pod
            {
                go.tag = "Player";
                ActivationGroups ag     = go.AddComponent <ActivationGroups>();
                List <string[]>  stages = new List <string[]>();
                foreach (List <Vehicle.Activation> stage in v.stages)
                {
                    List <string> _stage = new List <string>();
                    foreach (Vehicle.Activation act in stage)
                    {
                        _stage.Add(act.id.ToString());
                    }
                    stages.Add(_stage.ToArray());
                }
                ag.steps = stages.ToArray();
                ag.ready = true;
            }
        }
        massText.text = (mass * 500).ToString("N0") + " kg";



        foreach (Vehicle.Connection con in v.connections)        //Set parents and connections
        {
            GameObject parent = GameObject.Find(con.parent.ToString());
            GameObject child  = GameObject.Find(con.child.ToString());
            child.transform.SetParent(parent.transform);
            if (child.CompareTag("Wheel"))
            {
                AnchoredJoint2D joint = child.GetComponent <AnchoredJoint2D>();
                joint.connectedBody   = parent.GetComponent <Rigidbody2D>();
                joint.connectedAnchor = child.transform.localPosition;
                joint.enabled         = true;
            }
            else
            {
                AnchoredJoint2D joint = child.GetComponent <AnchoredJoint2D>();
                joint.connectedBody   = parent.GetComponent <Rigidbody2D>();
                joint.enableCollision = true;
                joint.anchor          = child.transform.worldToLocalMatrix.MultiplyPoint3x4(
                    child.GetComponent <Collider2D>().bounds.ClosestPoint(parent.transform.position));
                joint.enabled = true;
            }
        }
        return(vehicleGO);
    }