Пример #1
0
    public static Map Load(
        string name,
        GameObject mapRoot,
        MeshFilter tilePool,
        FieldTile fieldPrototype,
        FieldTile verticalFieldPrototype,
        FieldCollider fieldColliderPrototype,
        WalkerBehavior walkerPrototype,
        Action <Map, MeshFilter, int, int> onEachTile
        )
    {
        string    path  = Path.Combine(MapLoader.MapJsonDirectory, name);
        TextAsset asset = Resources.Load <TextAsset>(path);

        if (asset == null)
        {
            Debug.Log("could not load map file: " + path);
            return(new Map());
        }

        // JsonUtility does not support nested array

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Map));
        RawMap raw = JsonUtility.FromJson <RawMap>(asset.text);

        MapLoader.RawMapToMap(raw, out MapLoader.currentMap);


        MapLoader.currentMap.height = (int)Mathf.Floor(MapLoader.currentMap.terrains[0].Length / MapLoader.currentMap.width) + 1;

        MapLoader.InstantiateFields(mapRoot, tilePool, fieldPrototype, verticalFieldPrototype, fieldColliderPrototype, onEachTile);
        MapLoader.InstantiateWalkerBehaviors(mapRoot, walkerPrototype);

        return(MapLoader.currentMap);
    }
Пример #2
0
    private static GameObject InstantiateWalkerBehaviors(GameObject mapRoot, WalkerBehavior prototype)
    {
        Map map = MapLoader.currentMap;

        for (int i = 0; i < map.walkers.Length; i++)
        {
            Walker         data   = map.walkers[i];
            GameObject     go     = GameObject.Instantiate <GameObject>(prototype.gameObject);
            WalkerBehavior walker = go.GetComponent <WalkerBehavior>();
            walker.data               = data;
            walker.transform.parent   = mapRoot.transform;
            walker.transform.position = new Vector3(data.address % map.width + 0.5f, 0.5f, -Mathf.Floor(data.address / map.width) - 0.5f);

            go.SetActive(true);
        }

        return(mapRoot);
    }
Пример #3
0
    private void Start()
    {
        this.map = MapLoader.Load("Field2", this.mapRoot, this.tilePool, this.fieldPool, this.verticalFieldPool, this.fieldCollierPool, this.walkerPool, null);
        if (this.map.fields.Length == 0)
        {
            Debug.Log("could not load map");
            return;
        }

        this.uiField.OnRequestRotateRight = this.TryRotateCameraRight;
        this.uiField.OnRequestRotateLeft  = TryRotateCameraLeft;

        FieldCollider[] colliders = this.mapRoot.GetComponentsInChildren <FieldCollider>();
        for (int i = 0; i < colliders.Length; i++)
        {
            // TODO: overhead
            colliders[i].SetPhysicsRaycastHitCallback(this.TryWalkerWalk);
        }

        WalkerBehavior[] walkers = this.mapRoot.GetComponentsInChildren <WalkerBehavior>();
        for (int i = 0; i < walkers.Length; i++)
        {
            WalkerBehavior walker = walkers[i];
            if (walker.data.cameraFollow == true)
            {
                this.mainWalker = walker;
                this.mainCamera.SetTarget(this.mainWalker.transform);
            }
            walker.SetLookAtCamera(this.mainCamera.GetComponent <Camera>());
            walker.OnStopped = this.RetrieveTile;
        }

        this.game.SetMap(this.map, this.CurrentFloor());

        this.game.OnCalcPathDistanceRequested = this.GetDistance;
        this.game.OnConsumedItem = this.GetItem;
        this.game.OnSpawnedItem  = this.PlaceItem;

        this.game.SpawnItem(Game.AddressItem.Passenger);
    }
Пример #4
0
 // Behaviors:
 private bool changeBehavior(WalkerBehavior newBehavior)
 {
     return this.Behavior.ChangeTo(newBehavior);
 }