private void ApplyMeshColliderToMeshNode(Mission mission, string meshName)
        {
            var objectToBeColisioned = BaseGenerator.FetchReference(mission, meshName);

            if (objectToBeColisioned)
            {
                var objectFilter = objectToBeColisioned.GetComponent <MeshFilter>();
                if (objectFilter)
                {
                    var newCollider = objectToBeColisioned.AddComponent <MeshCollider>();
                    newCollider.sharedMesh = objectFilter.sharedMesh;
                }
                else
                {
                    for (var i = 0; i < objectToBeColisioned.transform.childCount; i++)
                    {
                        var objectChild       = objectToBeColisioned.transform.GetChild(i);
                        var objectChildFilter = objectChild.GetComponent <MeshFilter>();

                        if (objectChildFilter)
                        {
                            var newCollider = objectChild.gameObject.AddComponent <MeshCollider>();
                            newCollider.sharedMesh = objectChildFilter.sharedMesh;
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void Init()
        {
            switch (data.type)
            {
            case MafiaFormats.Scene2BINLoader.ObjectType.Model:
            {
            }
            break;

            case MafiaFormats.Scene2BINLoader.ObjectType.Sector:
            {
            }
            break;

            case MafiaFormats.Scene2BINLoader.ObjectType.Light:
            {
                //NOTE(zaklaus): Re-parent the light
                var parent = BaseGenerator.FetchCacheReference(GameAPI.instance.missionManager.mission, data.lightSectors == null ? "" : data.lightSectors)?.gameObject;

                if (parent != null)
                {
                    transform.parent = parent.transform;

                    transform.localPosition = data.pos;
                    transform.localRotation = data.rot;
                    transform.localScale    = data.scale;
                }

                if (data.lightType != MafiaFormats.Scene2BINLoader.LightType.Spot && data.lightType != MafiaFormats.Scene2BINLoader.LightType.Point &&
                    data.lightType != MafiaFormats.Scene2BINLoader.LightType.Directional &&
                    data.lightType != MafiaFormats.Scene2BINLoader.LightType.Ambient &&
                    data.lightType != MafiaFormats.Scene2BINLoader.LightType.Fog)
                {
                    break;
                }

                var light = gameObject.AddComponent <Light>();

                light.type    = LightType.Point;
                light.shadows = LightShadows.Soft;

                if (data.lightType == MafiaFormats.Scene2BINLoader.LightType.Spot)
                {
                    light.type      = LightType.Spot;
                    light.spotAngle = Mathf.Rad2Deg * data.lightAngle;
                }

                if (data.lightType == MafiaFormats.Scene2BINLoader.LightType.Fog)
                {
                    light.intensity = 0f;
                    light.color     = Color.blue;
                    light.type      = LightType.Area; // Just to distinguish it easily
                    light.shadows   = LightShadows.None;
                    break;
                }

                if (data.lightType == MafiaFormats.Scene2BINLoader.LightType.Ambient)
                {
                    light.intensity = 0f;
                    light.color     = Color.red;
                    light.type      = LightType.Area; // Just to distinguish it easily
                    light.shadows   = LightShadows.None;
                    break;
                }

                if (data.lightType == MafiaFormats.Scene2BINLoader.LightType.Directional)
                {
                    light.type = LightType.Directional;
                    //Debug.Log(gameObject.name + ": " + (int)data.lightFlags);

                    // TODO
                    if ((int)data.lightFlags != 107)
                    {
                        GameObject.DestroyImmediate(light);
                        break;
                    }
                }

                light.intensity = data.lightPower;
                light.range     = data.lightFar;
                light.color     = new Color(data.lightColour.x, data.lightColour.y, data.lightColour.z);
            }
            break;
            }

            switch (data.specialType)
            {
            case MafiaFormats.Scene2BINLoader.SpecialObjectType.Physical:
            {
                // todo: add proper scripts
                var meshFilter = gameObject.GetComponent <MeshFilter>();

                if (meshFilter != null)
                {
                    var collider = gameObject.AddComponent <MeshCollider>();

                    collider.sharedMesh = meshFilter.sharedMesh;
                    gameObject.AddComponent <Rigidbody>();
                }
            }
            break;

            case MafiaFormats.Scene2BINLoader.SpecialObjectType.Character:
            {
                gameObject.AddComponent <ModelAnimationPlayer>();
                var idlePawn = gameObject.AddComponent <IdlePawnController>();
                //var idlePawn = gameObject.AddComponent<FunnySpinPawnController>();
                idlePawn.pawn = gameObject;

                // rework this
                ObjectFactory.SetUpPawnPhysics(gameObject);
                ObjectFactory.SetUpHuman(gameObject);
            }
            break;

            case MafiaFormats.Scene2BINLoader.SpecialObjectType.Door:
            {
                var meshFilter = GetComponent <MeshFilter>();

                if (meshFilter != null)
                {
                    gameObject.AddComponent <MeshCollider>().sharedMesh = meshFilter.sharedMesh;

                    var door = gameObject.AddComponent <Door>();
                    door.door = data.doorObject;
                }
            }
            break;
            }
        }
        public override GameObject LoadObject(string path, Mission mission)
        {
            GameObject rootObject = LoadCachedObject(path);

            if (rootObject == null)
            {
                rootObject = new GameObject(path);
            }
            else
            {
                return(rootObject);
            }

            Stream fs;

            try
            {
                fs = GameAPI.instance.fileSystem.GetStreamFromPath(path);
            }
            catch
            {
                return(null);
            }

            using (var reader = new BinaryReader(fs))
            {
                var sceneLoader = new MafiaFormats.Scene2BINLoader();
                lastLoader = sceneLoader;

                sceneLoader.Load(reader);
                fs.Close();

                // TODO: Check if refs are null, clear then
                ModelGenerator.cachedTextures.Clear();

                var objects = new List <KeyValuePair <GameObject, MafiaFormats.Scene2BINLoader.Object> >();

                var backdrop = new GameObject("Backdrop sector");
                backdrop.transform.parent = rootObject.transform;
                StoreReference(mission, backdrop.name, backdrop);

                foreach (var obj in sceneLoader.objects)
                {
                    AddNewObject(objects, mission, obj);
                }

                // Handle special cases of objects loaded by external source
                foreach (var obj in sceneLoader.externalObjects)
                {
                    var go = BaseGenerator.FetchCacheReference(mission, obj.Value.name)?.gameObject;

                    if (go != null)
                    {
                        var specialObjDef = go.GetComponent <ObjectDefinition>();

                        if (specialObjDef == null)
                        {
                            specialObjDef = go.AddComponent <ObjectDefinition>();
                        }

                        specialObjDef.data = obj.Value;

                        specialObjDef.Init();
                    }
                    // Seems like object acts on its own, add it anyway
                    else
                    {
                        AddNewObject(objects, mission, obj);
                    }
                }

                var primary = FetchReference(mission, "Primary sector");

                if (primary == null)
                {
                    primary = new GameObject("Primary Sector");
                }

                var objDef          = primary.AddComponent <ObjectDefinition>();
                var dummySectorData = new MafiaFormats.Scene2BINLoader.Object();
                dummySectorData.type     = MafiaFormats.Scene2BINLoader.ObjectType.Sector;
                objDef.data              = dummySectorData;
                primary.transform.parent = rootObject.transform;

                objects = objects.OrderByDescending(x => x.Value.isPatch).ToList();

                foreach (var obj in objects)
                {
                    var newObject = obj.Key;

                    if (obj.Value.isPatch)
                    {
                        var searchName  = obj.Value.name;
                        var redefObject = FindParent(mission, searchName, null);

                        if (redefObject != null)
                        {
                            if (obj.Value.isParentPatched && obj.Value.parentName != null)
                            {
                                var parent = FindParent(mission, obj.Value.parentName);

                                if (parent != null)
                                {
                                    redefObject.transform.parent = parent.transform;
                                }
                            }

                            if (obj.Value.isPositionPatched)
                            {
                                redefObject.transform.localPosition = obj.Value.pos;
                            }

                            /* if (obj.Value.isPosition2Patched)
                             *  redefObject.transform.position = obj.Value.pos2; */

                            if (obj.Value.isRotationPatched)
                            {
                                redefObject.transform.localRotation = obj.Value.rot;
                            }

                            if (obj.Value.isScalePatched)
                            {
                                redefObject.transform.localScale = obj.Value.scale;
                            }

                            redefObject.SetActive(!obj.Value.isHidden);

                            GameObject.DestroyImmediate(newObject, true);
                            continue;
                        }
                    }

                    newObject.transform.parent = FindParent(mission, obj.Value.parentName, rootObject).transform;

                    newObject.transform.localPosition = obj.Value.pos;
                    newObject.transform.localRotation = obj.Value.rot;
                    newObject.transform.localScale    = obj.Value.scale;

                    newObject.SetActive(!obj.Value.isHidden);

                    var specObject = newObject.AddComponent <ObjectDefinition>();
                    specObject.data = obj.Value;
                    specObject.Init();
                }
            }

            // NOTE(zaklaus): Hardcode 'Primary sector' scale to (1,1,1)
            var primarySector = GameObject.Find("Primary sector");

            if (primarySector != null)
            {
                primarySector.transform.localScale = new Vector3(1, 1, 1);
            }

            //StoreChachedObject(path, rootObject);

            return(rootObject);
        }