File size: 2,823 Bytes
4ee4376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import jszip from 'jszip';
import fileSaver from 'file-saver';

export default class save {
  constructor (options) {
    this.scene = options.scene;
    this.globals = this.scene.sys.game.globals;
    this.city = this.scene.city;
  }

  export () {
    this.data = {
      info: {
        name: this.city.name,
        rotation: this.city.rotation,
        waterLevel: this.city.waterLevel,
        width: this.city.width,
        height: this.city.height,
      },
      cells: []
    };
    
    this.createCellList();
    this.downloadSave();
  }

  createCellList () {
    for (let x = 0; x < this.city.width; x++) {
      for (let y = 0; y < this.city.height; y++) {
        let mapCell = this.city.map.getCell(x, y);
        let cell = {
          x: mapCell.x,
          y: mapCell.y,
          z: mapCell.z,
          cornersTopLeft: mapCell.properties.cornersTopLeft,
          cornersTopRight: mapCell.properties.cornersTopRight,
          cornersBottomLeft: mapCell.properties.cornersBottomLeft,
          cornersBottomRight: mapCell.properties.cornersBottomRight,
          conductive: mapCell.properties.conductive,
          powered: mapCell.properties.powered,
          piped: mapCell.properties.piped,
          watered: mapCell.properties.watered,
          rotate: mapCell.properties.rotate,
          landValueMask: mapCell.properties.landValueMask,
          saltWater: mapCell.properties.saltWater,
          waterCovered: mapCell.properties.waterCovered,
          missileSilo: mapCell.properties.missileSilo,
          waterLevel: mapCell.properties.waterLevel,
          surfaceWater: mapCell.properties.surfaceWater,
          tunnelLevel: mapCell.properties.tunnelLevel,
          altWaterCovered: mapCell.properties.altWaterCovered,
          terrainWaterLevel: mapCell.properties.terrainWaterLevel,
          tiles: {
            building: mapCell.getBuildingTileId(),
            zone: mapCell.getZoneTileId(),
            water: mapCell.getWaterTileId(),
            road: mapCell.getRoadTileId(),
            rail: mapCell.getRailTileId(),
            power: mapCell.getPowerTileId(),
            highway: mapCell.getHighwayTileId(),
            terrain: mapCell.getTerrainTileId(),
            heightmap: mapCell.getHeightmapTileId(),
            subway: mapCell.getSubwayTileId(),
            pipe: mapCell.getPipeTileId(),
          }
        };

        this.data.cells.push(cell);
      }
    }
  }

  downloadSave () {
    let json = JSON.stringify(this.data);
    let zip = new jszip();
    let options = {
      type: 'blob',
      compression: 'DEFLATE',
      compressionOptions: {
        level: 9
      }
    };

    zip.file('data.json', json);
    zip.generateAsync(options).then((content) => {
      fileSaver.saveAs(content, this.data.info.name + '.opensc2k');
    });
  }
}