public void SetTileDynamic(TilePosition pos, bool dynamic, bool useGravity, int timeout) { if (tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic != dynamic) { if (tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic) { DynamicTile dynamicTile = dynamicTiles[pos]; dynamicTiles.Remove(pos); if (dynamicTile.timeout > 0) { dynamicTilesTimeout.Remove(pos); } world.cwListener.DestroyObject(dynamicTile); dynamicTile.Clear(); } tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic = dynamic; if (dynamic) { DynamicTile dynamicTile = new DynamicTile(world, pos, true, world.gameplay.NextObjectId()); if (useGravity) { dynamicTile.AddComponent(new TileComponentGravity()); } dynamicTiles[pos] = dynamicTile; if (timeout > 0) { dynamicTile.timeout = timeout; dynamicTilesTimeout[pos] = dynamicTile; } world.cwListener.CreateObject(dynamicTile); } if (reportTileInvalidated) { ReportTileInvalidated(pos); } } else { if (dynamic && timeout >= 0) { SetTileDynamicTimeout(pos, timeout); } } }
public void Load(System.IO.BinaryReader br) { //Read tile definition mapper (oldId -> newId) byte[] mapTileDefinitionMapper = new byte[byte.MaxValue]; int definitions = br.ReadInt32(); for (int i = 0; i < definitions; i++) { byte oldType = br.ReadByte(); string id = br.ReadString(); byte newType = 0; foreach (TileDefinition td in tileDefinitions) { if (td.id == id) { newType = td.tileType; break; } } mapTileDefinitionMapper[oldType] = newType; } //Read map size //This is read in CubeWorld.Load() //int sizeXbits = br.ReadInt32(); //int sizeYbits = br.ReadInt32(); //int sizeZbits = br.ReadInt32(); //Create empty world //The world is already created in CubeLoad.Load() //Create(tileDefinitions, sizeXbits, sizeYbits, sizeZbits); //Read tiles byte[] tileBytes = br.ReadBytes(sizeX * sizeY * sizeZ * 4); Tile tile = new Tile(); for (int n = tiles.Length - 1; n >= 0; n--) { tile.tileType = mapTileDefinitionMapper[tileBytes[(n << 2) | 0]]; tile.luminance = tileBytes[(n << 2) | 1]; tile.extra = tileBytes[(n << 2) | 2]; tile.extra2 = tileBytes[(n << 2) | 3]; tiles[n] = tile; } //Read dynamic tiles int nDyanamicTiles = br.ReadInt32(); for (int n = 0; n < nDyanamicTiles; n++) { int objectId = br.ReadInt32(); TilePosition pos = SerializationUtils.ReadTilePosition(br); int timeout = br.ReadInt32(); DynamicTile dynamicTile = new DynamicTile(world, pos, true, objectId); //TODO: Get gravity attribute from somewhere if (true) { dynamicTile.AddComponent(new TileComponentGravity()); } dynamicTiles[pos] = dynamicTile; if (timeout > 0) { dynamicTile.timeout = timeout; dynamicTilesTimeout[pos] = dynamicTile; } world.cwListener.CreateObject(dynamicTile); } //Read top positions topPositions = br.ReadBytes(sizeX * sizeZ); if (world.gameplay.useEnqueueTileUpdates) { EnqueueTilesWithRules(); } enqueueTileUpdates = world.gameplay.useEnqueueTileUpdates; updateLighting = true; reportTileInvalidated = true; if (world.gameplay.useEnqueueTileUpdates) { UpdateTiles(); } //The read tiles already have the light information updated //LightModelAmbient.InitLuminance(this); //LightModelLightSource.InitLuminance(this); }
public void Load(System.IO.BinaryReader br) { //Read tile definition mapper (oldId -> newId) byte[] mapTileDefinitionMapper = new byte[byte.MaxValue]; int definitions = br.ReadInt32(); for (int i = 0; i < definitions; i++) { byte oldType = br.ReadByte(); string id = br.ReadString(); byte newType = 0; foreach (TileDefinition td in tileDefinitions) { if (td.id == id) { newType = td.tileType; break; } } mapTileDefinitionMapper[oldType] = newType; } //Read map size //This is read in CubeWorld.Load() //int sizeXbits = br.ReadInt32(); //int sizeYbits = br.ReadInt32(); //int sizeZbits = br.ReadInt32(); //Create empty world //The world is already created in CubeLoad.Load() //Create(tileDefinitions, sizeXbits, sizeYbits, sizeZbits); //Read tiles byte[] tileBytes = br.ReadBytes(sizeX * sizeY * sizeZ * 4); Tile tile = new Tile(); for (int n = tiles.Length - 1; n >= 0; n--) { tile.tileType = mapTileDefinitionMapper[tileBytes[(n << 2) | 0]]; tile.luminance = tileBytes[(n << 2) | 1]; tile.extra = tileBytes[(n << 2) | 2]; tile.extra2 = tileBytes[(n << 2) | 3]; tiles[n] = tile; } //Read dynamic tiles int nDyanamicTiles = br.ReadInt32(); for (int n = 0; n < nDyanamicTiles; n++) { int objectId = br.ReadInt32(); TilePosition pos = SerializationUtils.ReadTilePosition(br); int timeout = br.ReadInt32(); DynamicTile dynamicTile = new DynamicTile(world, pos, true, objectId); //TODO: Get gravity attribute from somewhere if (true) dynamicTile.AddComponent(new TileComponentGravity()); dynamicTiles[pos] = dynamicTile; if (timeout > 0) { dynamicTile.timeout = timeout; dynamicTilesTimeout[pos] = dynamicTile; } world.cwListener.CreateObject(dynamicTile); } //Read top positions topPositions = br.ReadBytes(sizeX * sizeZ); if (world.gameplay.useEnqueueTileUpdates) EnqueueTilesWithRules(); enqueueTileUpdates = world.gameplay.useEnqueueTileUpdates; updateLighting = true; reportTileInvalidated = true; if (world.gameplay.useEnqueueTileUpdates) UpdateTiles(); //The read tiles already have the light information updated //LightModelAmbient.InitLuminance(this); //LightModelLightSource.InitLuminance(this); }
public void SetTileDynamic(TilePosition pos, bool dynamic, bool useGravity, int timeout) { if (tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic != dynamic) { if (tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic) { DynamicTile dynamicTile = dynamicTiles[pos]; dynamicTiles.Remove(pos); if (dynamicTile.timeout > 0) dynamicTilesTimeout.Remove(pos); world.cwListener.DestroyObject(dynamicTile); dynamicTile.Clear(); } tiles[pos.x | (pos.y << sizeXbits) | (pos.z << sizeXBitsYBits)].Dynamic = dynamic; if (dynamic) { DynamicTile dynamicTile = new DynamicTile(world, pos, true, world.gameplay.NextObjectId()); if (useGravity) dynamicTile.AddComponent(new TileComponentGravity()); dynamicTiles[pos] = dynamicTile; if (timeout > 0) { dynamicTile.timeout = timeout; dynamicTilesTimeout[pos] = dynamicTile; } world.cwListener.CreateObject(dynamicTile); } if (reportTileInvalidated) ReportTileInvalidated(pos); } else { if (dynamic && timeout >= 0) SetTileDynamicTimeout(pos, timeout); } }