Esempio n. 1
0
        // Use this for initialization
        private void Start()
        {
            _cacheManager         = CacheManager.Instance;
            _cacheManager.Palette = ActPaletteParser.ReadActPalette("t01.act");

            SdfFiles = VirtualFilesystem.Instance.FindAllWithExtension(".sdf").ToArray();

            foreach (string sdfFile in SdfFiles)
            {
                string filename = sdfFile;
                Button button   = Instantiate(ButtonPrefab, Vector3.zero, Quaternion.identity, ListTarget).GetComponent <Button>();
                button.GetComponentInChildren <Text>().text = filename;
                button.onClick.AddListener(() =>
                {
                    OnClickButton(filename);
                });
            }
        }
Esempio n. 2
0
        private CacheManager()
        {
            _colorMaterialPrefab       = Resources.Load <Material>("Materials/AlphaMaterial");
            _textureMaterialPrefab     = Resources.Load <Material>("Materials/TextureMaterial");
            _transparentMaterialPrefab = Resources.Load <Material>("Materials/AlphaMaterial");
            _carMirrorMaterialPrefab   = Resources.Load <Material>("Materials/CarMirror");

            ProjectilePrefab  = Resources.Load <GameObject>("Prefabs/ProjectilePrefab");
            _3DObjectPrefab   = Resources.Load <GameObject>("Prefabs/ObjectPrefab");
            _noColliderPrefab = Resources.Load <GameObject>("Prefabs/NoColliderPrefab");
            _steerWheelPrefab = Resources.Load <RaySusp>("Prefabs/SteerWheelPrefab");
            _driveWheelPrefab = Resources.Load <RaySusp>("Prefabs/DriveWheelPrefab");
            _carBodyPrefab    = Resources.Load <GameObject>("Prefabs/CarBodyPrefab");
            _carPrefab        = Resources.Load <Car>("Prefabs/CarPrefab");

            VirtualFilesystem.Instance.Init();
            _materialCache["default"] = Object.Instantiate(_textureMaterialPrefab);
            Palette = ActPaletteParser.ReadActPalette("p02.act");
        }
Esempio n. 3
0
    void LoadLevel(string msnFilename)
    {
        var cacheManager = FindObjectOfType <CacheManager>();

        var terrainPatches = new Terrain[80, 80];
        var mdef           = MsnMissionParser.ReadMsnMission(msnFilename);

        cacheManager.Palette = ActPaletteParser.ReadActPalette(mdef.PaletteFilePath);
        var _surfaceTexture = TextureParser.ReadMapTexture(mdef.SurfaceTextureFilePath, cacheManager.Palette);

        _surfaceTexture.filterMode = FilterMode.Point;

        FindObjectOfType <Sky>().TextureFilename = mdef.SkyTextureFilePath;
        // var skyTexture = TextureParser.ReadMapTexture(mdef.SkyTextureFilePath, cacheManager.Palette);
        // SkyMaterial.mainTexture = skyTexture;

        var worldGameObject = GameObject.Find("World");

        if (worldGameObject != null)
        {
            Destroy(worldGameObject);
        }
        worldGameObject = new GameObject("World");


        var splatPrototypes = new[]
        {
            new SplatPrototype
            {
                texture    = _surfaceTexture,
                tileSize   = new Vector2(_surfaceTexture.width, _surfaceTexture.height),
                metallic   = 0,
                smoothness = 0
            }
        };

        for (int z = 0; z < 80; z++)
        {
            for (int x = 0; x < 80; x++)
            {
                if (mdef.TerrainPatches[x, z] == null)
                {
                    continue;
                }

                var patchGameObject = new GameObject("Ter " + x + ", " + z);
                patchGameObject.transform.position = new Vector3(x * 640, 0, z * 640);
                patchGameObject.transform.parent   = worldGameObject.transform;

                var terrain = patchGameObject.AddComponent <Terrain>();
                terrain.terrainData = mdef.TerrainPatches[x, z].TerrainData;
                terrain.terrainData.splatPrototypes = splatPrototypes;
                terrain.materialTemplate            = TerrainMaterial;
                terrain.materialType = Terrain.MaterialType.Custom;

                var terrainCollider = patchGameObject.AddComponent <TerrainCollider>();
                terrainCollider.terrainData = terrain.terrainData;

                foreach (var odef in mdef.TerrainPatches[x, z].Objects)
                {
                    if (odef.ClassId == MsnMissionParser.ClassId.Car)
                    {
                        var lblUpper = odef.Label.ToUpper();

                        GameObject carObj;
                        switch (lblUpper)
                        {
                        case "SPAWN":
                            carObj     = Instantiate(SpawnPrefab);
                            carObj.tag = "Spawn";
                            break;

                        case "REGEN":
                            carObj     = Instantiate(RegenPrefab);
                            carObj.tag = "Regen";
                            break;

                        default:
                            carObj = cacheManager.ImportVcf(odef.Label + ".vcf");
                            break;
                        }
                        carObj.transform.parent        = patchGameObject.transform;
                        carObj.transform.localPosition = odef.LocalPosition;
                        carObj.transform.localRotation = odef.LocalRotation;
                    }
                    else if (odef.ClassId != MsnMissionParser.ClassId.Special)
                    {
                        cacheManager.ImportSdf(odef.Label + ".sdf", patchGameObject.transform, odef.LocalPosition, odef.LocalRotation);
                    }
                }

                terrainPatches[x, z] = terrain;
            }
        }

        foreach (var road in mdef.Roads)
        {
            var roadGo = new GameObject("Road");
            roadGo.transform.parent = worldGameObject.transform;
            var meshFilter   = roadGo.AddComponent <MeshFilter>();
            var meshRenderer = roadGo.AddComponent <MeshRenderer>();
            meshRenderer.shadowCastingMode = ShadowCastingMode.Off;


            string roadTextureFilename;
            switch (road.SegmentType)
            {
            case MsnMissionParser.RoadSegmentType.PavedHighway:
                roadTextureFilename = "r2ayr_51";
                break;

            case MsnMissionParser.RoadSegmentType.DirtTrack:
                roadTextureFilename = "r2dnr_37";
                break;

            case MsnMissionParser.RoadSegmentType.RiverBed:
                roadTextureFilename = "r2wnr_39";
                break;

            case MsnMissionParser.RoadSegmentType.FourLaneHighway:
                roadTextureFilename = "r2ayr_51";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            var roadMaterial = cacheManager.GetTextureMaterial(roadTextureFilename, false);
            meshRenderer.material = roadMaterial;

            var mesh     = new Mesh();
            var vertices = new List <Vector3>();
            var uvs      = new List <Vector2>();

            var uvIdx = 0;
            foreach (var roadSegment in road.RoadSegments)
            {
                vertices.Add(roadSegment.Left);
                vertices.Add(roadSegment.Right);
                uvs.Add(new Vector2(0, uvIdx));
                uvs.Add(new Vector2(1, uvIdx));
                uvIdx += 1;
            }

            var indices = new List <int>();
            var idx     = 0;
            for (int i = 0; i < (vertices.Count - 2) / 2; i++)
            {
                indices.Add(idx + 2);
                indices.Add(idx + 1);
                indices.Add(idx);

                indices.Add(idx + 2);
                indices.Add(idx + 3);
                indices.Add(idx + 1);
                idx += 2;
            }

            mesh.vertices  = vertices.ToArray();
            mesh.triangles = indices.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.RecalculateNormals();
            meshFilter.sharedMesh = mesh;
        }

        foreach (var ldef in mdef.StringObjects)
        {
            var sdfObj = cacheManager.ImportSdf(ldef.Label + ".sdf", null, Vector3.zero, Quaternion.identity);

            for (int i = 0; i < ldef.StringPositions.Count; i++)
            {
                var pos           = ldef.StringPositions[i];
                var localPosition = new Vector3(pos.x % 640, pos.y, pos.z % 640);
                var patchPosX     = (int)(pos.x / 640.0f);
                var patchPosZ     = (int)(pos.z / 640.0f);
                sdfObj.name                    = ldef.Label + " " + i;
                sdfObj.transform.parent        = terrainPatches[patchPosX, patchPosZ].transform;
                sdfObj.transform.localPosition = localPosition;
                if (i < ldef.StringPositions.Count - 1)
                {
                    sdfObj.transform.LookAt(ldef.StringPositions[i + 1], Vector3.up);
                }
                else
                {
                    sdfObj.transform.LookAt(ldef.StringPositions[i - 1], Vector3.up);
                    sdfObj.transform.localRotation *= Quaternion.AngleAxis(180, Vector3.up);
                }

                if (i < ldef.StringPositions.Count - 1)
                {
                    sdfObj = Instantiate(sdfObj);
                }
            }
        }

        worldGameObject.transform.position = new Vector3(-mdef.Middle.x * 640, 0, -mdef.Middle.y * 640);


        FindObjectOfType <Light>().color = cacheManager.Palette[176];
        Camera.main.backgroundColor      = cacheManager.Palette[239];
        RenderSettings.fogColor          = cacheManager.Palette[239];
        RenderSettings.ambientLight      = cacheManager.Palette[247];

        if (MissionFile.ToLower().StartsWith("m"))
        {
            var importedVcf = cacheManager.ImportVcf(VcfToLoad);
            importedVcf.AddComponent <InputCarController>();

            var spawnPoint = GameObject.FindGameObjectsWithTag("Spawn")[0];
            importedVcf.transform.position = spawnPoint.transform.position;
            importedVcf.transform.rotation = spawnPoint.transform.rotation;

            Camera.main.GetComponent <SmoothFollow>().Target = importedVcf.transform;
        }
    }
Esempio n. 4
0
        public IEnumerator LoadLevel(string msnFilename)
        {
            _game.LevelName = msnFilename;
            string sceneName = SceneManager.GetActiveScene().name;

            if (sceneName != "Level")
            {
                AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(1, LoadSceneMode.Single);
                sceneLoad.allowSceneActivation = true;
                while (!sceneLoad.isDone)
                {
                    yield return(null);
                }

                yield break;
            }

            Terrain[,] terrainPatches = new Terrain[80, 80];
            MsnMissionParser.MissonDefinition mdef = MsnMissionParser.ReadMsnMission(msnFilename);

            _cacheManager.Palette = ActPaletteParser.ReadActPalette(mdef.PaletteFilePath);
            Texture2D surfaceTexture = TextureParser.ReadMapTexture(mdef.SurfaceTextureFilePath, _cacheManager.Palette, TextureFormat.RGB24, true, FilterMode.Point);

            GameObject.Find("Sky").GetComponent <Sky>().TextureFilename = mdef.SkyTextureFilePath;

            GameObject worldGameObject = GameObject.Find("World");

            if (worldGameObject != null)
            {
                Object.Destroy(worldGameObject);
            }

            worldGameObject = new GameObject("World");

            TerrainLayer[] terrainLayers = new[]
            {
                new TerrainLayer
                {
                    diffuseTexture = surfaceTexture,
                    tileSize       = new Vector2(surfaceTexture.width, surfaceTexture.height) / 10.0f,
                    metallic       = 0,
                    smoothness     = 0
                }
            };

            for (int z = 0; z < 80; z++)
            {
                for (int x = 0; x < 80; x++)
                {
                    if (mdef.TerrainPatches[x, z] == null)
                    {
                        continue;
                    }

                    GameObject patchGameObject = new GameObject("Ter " + x + ", " + z);
                    patchGameObject.layer = LayerMask.NameToLayer("Terrain");
                    patchGameObject.transform.position = new Vector3(x * 640, 0, z * 640);
                    patchGameObject.transform.parent   = worldGameObject.transform;

                    Terrain terrain = patchGameObject.AddComponent <Terrain>();
                    terrain.terrainData = mdef.TerrainPatches[x, z].TerrainData;
                    terrain.terrainData.terrainLayers = terrainLayers;
                    terrain.materialTemplate          = _terrainMaterial;
                    terrain.materialType = Terrain.MaterialType.Custom;

                    TerrainCollider terrainCollider = patchGameObject.AddComponent <TerrainCollider>();
                    terrainCollider.terrainData = terrain.terrainData;

                    foreach (MsnMissionParser.Odef odef in mdef.TerrainPatches[x, z].Objects)
                    {
                        GameObject go = null;
                        if (odef.ClassId == MsnMissionParser.ClassId.Car)
                        {
                            string lblUpper = odef.Label.ToUpper();

                            // Training mission uses hardcoded VCF for player.
                            string vcfName = odef.Label;
                            if (msnFilename.ToLower() == "a01.msn" && vcfName == "vppirna1")
                            {
                                vcfName = "vppa01";
                            }

                            switch (lblUpper)
                            {
                            case "SPAWN":
                                go     = Object.Instantiate(_spawnPrefab);
                                go.tag = "Spawn";
                                break;

                            case "REGEN":
                                go     = Object.Instantiate(_regenPrefab);
                                go.tag = "Regen";
                                break;

                            default:
                                go = _cacheManager.ImportVcf(vcfName + ".vcf", odef.IsPlayer, out _);
                                Car car = go.GetComponent <Car>();
                                car.TeamId   = odef.TeamId;
                                car.IsPlayer = odef.IsPlayer;
                                break;
                            }

                            go.transform.parent        = patchGameObject.transform;
                            go.transform.localPosition = odef.LocalPosition;
                            go.transform.localRotation = odef.LocalRotation;

                            if (odef.IsPlayer)
                            {
                                CameraManager.Instance.MainCamera.GetComponent <SmoothFollow>().Target = go.transform;
                                go.AddComponent <CarInput>();
                            }
                        }
                        else if (odef.ClassId != MsnMissionParser.ClassId.Special)
                        {
                            bool canWreck = odef.ClassId == MsnMissionParser.ClassId.Struct1 ||
                                            odef.ClassId == MsnMissionParser.ClassId.Ramp ||
                                            odef.ClassId == MsnMissionParser.ClassId.Struct2;

                            go = _cacheManager.ImportSdf(odef.Label + ".sdf", patchGameObject.transform, odef.LocalPosition, odef.LocalRotation, canWreck, out Sdf sdf, out GameObject wreckedPart);
                            if (odef.ClassId == MsnMissionParser.ClassId.Sign)
                            {
                                go.AddComponent <Sign>();
                            }
                            else if (canWreck)
                            {
                                Building building = go.AddComponent <Building>();
                                building.Initialise(sdf, wreckedPart);
                            }
                        }

                        if (go != null)
                        {
                            go.name = odef.Label + "_" + odef.Id;

                            if (mdef.FSM != null)
                            {
                                FSMEntity[] entities = mdef.FSM.EntityTable;
                                for (int i = 0; i < entities.Length; ++i)
                                {
                                    if (entities[i].Value == odef.Label && entities[i].Id == odef.Id)
                                    {
                                        WorldEntity worldEntity = go.GetComponent <WorldEntity>();
                                        if (worldEntity != null)
                                        {
                                            entities[i].WorldEntity = worldEntity;
                                            worldEntity.Id          = i;
                                        }

                                        entities[i].Object = go;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    terrainPatches[x, z] = terrain;
                }
            }

            RoadManager roadManager = RoadManager.Instance;

            foreach (MsnMissionParser.Road road in mdef.Roads)
            {
                roadManager.CreateRoadObject(road, mdef.Middle * 640);
            }

            foreach (MsnMissionParser.Ldef ldef in mdef.StringObjects)
            {
                GameObject sdfObj = _cacheManager.ImportSdf(ldef.Label + ".sdf", null, Vector3.zero, Quaternion.identity, false, out _, out _);

                for (int i = 0; i < ldef.StringPositions.Length; i++)
                {
                    Vector3 pos           = ldef.StringPositions[i];
                    Vector3 localPosition = new Vector3(pos.x % 640, pos.y, pos.z % 640);
                    int     patchPosX     = (int)(pos.x / 640.0f);
                    int     patchPosZ     = (int)(pos.z / 640.0f);
                    sdfObj.name                    = ldef.Label + " " + i;
                    sdfObj.transform.parent        = terrainPatches[patchPosX, patchPosZ].transform;
                    sdfObj.transform.localPosition = localPosition;
                    if (i < ldef.StringPositions.Length - 1)
                    {
                        sdfObj.transform.LookAt(ldef.StringPositions[i + 1], Vector3.up);
                    }
                    else
                    {
                        sdfObj.transform.LookAt(ldef.StringPositions[i - 1], Vector3.up);
                        sdfObj.transform.localRotation *= Quaternion.AngleAxis(180, Vector3.up);
                    }

                    if (i < ldef.StringPositions.Length - 1)
                    {
                        sdfObj = Object.Instantiate(sdfObj);
                    }
                }
            }

            worldGameObject.transform.position = new Vector3(-mdef.Middle.x * 640, 0, -mdef.Middle.y * 640);

            Object.FindObjectOfType <Light>().color = _cacheManager.Palette[176];
            UnityEngine.Camera.main.backgroundColor = _cacheManager.Palette[239];
            RenderSettings.fogColor     = _cacheManager.Palette[239];
            RenderSettings.ambientLight = _cacheManager.Palette[247];

            List <Car> cars = EntityManager.Instance.Cars;

            foreach (Car car in cars)
            {
                car.transform.parent = null;
            }

            if (mdef.FSM != null)
            {
                foreach (StackMachine machine in mdef.FSM.StackMachines)
                {
                    machine.Reset();
                    machine.Constants = new IntRef[machine.InitialArguments.Length];
                    for (int i = 0; i < machine.Constants.Length; i++)
                    {
                        int stackValue = machine.InitialArguments[i];
                        machine.Constants[i] = mdef.FSM.Constants[stackValue];
                    }
                }

                FSMRunner fsmRunner = FSMRunner.Instance;
                fsmRunner.FSM = mdef.FSM;
            }
        }