Archived
0

переименование модуля

This commit is contained in:
2018-08-25 18:28:15 +03:00
parent 1941291a5b
commit 3b98a2b5a9
6 changed files with 9 additions and 9 deletions

39
simple_world/README.MD Normal file
View File

@@ -0,0 +1,39 @@
# Simple world
Простая реализация мира
## Spring bean
```xml
<bean id="simpleWorld" class="mc.world.simple.SimpleWorld">
<property name="spawn">
<bean class="mc.core.EntityLocation">
<constructor-arg index="0" type="double" value="8"/>
<constructor-arg index="1" type="double" value="6"/>
<constructor-arg index="2" type="double" value="8"/>
<constructor-arg index="3" type="float" value="0"/>
<constructor-arg index="4" type="float" value="0"/>
<constructor-arg index="5" type="mc.core.world.World">
<null/>
</constructor-arg>
</bean>
</property>
<property name="layersBlock">
<list value-type="java.lang.String">
<value>1;BEDROCK</value>
<value>2;DIRT</value>
<value>1;GRASS</value>
</list>
</property>
</bean>
```
`spawn` - точка спавна.
При указании точки спавна, указывать шестой параметр `World` не имеет смысла,
т.к. `SimpleWorld` всё равно перезапишет этот параметр.
`layersBlock` - слои блоков.
В качестве значения указывается спиток строк, каждая из которых описывает слой блоков.
Формат строк такой: `кол-во_слоёв;тип_блока`. Порядок строк такой: сверху нижние слои, а снизу - верхние.

View File

@@ -0,0 +1,7 @@
group 'mc'
version '1.0-SNAPSHOT'
dependencies {
/* Core */
compile_excludeCopy project(':core')
}

View File

@@ -0,0 +1,61 @@
package mc.world.simple;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import mc.core.exception.ResourceUnloadedException;
import mc.core.world.Biome;
import mc.core.world.World;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
@Slf4j
public class SimpleChunk implements Chunk {
@Getter
private int x, z;
private Reference<World> refWorld;
private ChunkSection chunkSection;
private final Biome biome = Biome.PLAINS;
public SimpleChunk(int x, int z, World world) {
this.x = x;
this.z = z;
setWorld(world);
}
@Override
public ChunkSection getChunkSection(int height) {
return chunkSection;
}
@Override
public void setChunkSection(int height, ChunkSection chunkSection) {
this.chunkSection = chunkSection;
}
@Override
public Biome getBiome(int localX, int localZ) {
return biome;
}
@Override
public void setBiome(int localX, int localZ, Biome biome) {
// ignore
}
@Override
public World getWorld() {
if (refWorld.get() == null) {
throw new ResourceUnloadedException("World unloaded");
} else {
return refWorld.get();
}
}
@Override
public void setWorld(World world) {
this.refWorld = new WeakReference<>(world);
}
}

View File

@@ -0,0 +1,93 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-28
*/
package mc.world.simple;
import mc.core.exception.ResourceUnloadedException;
import mc.core.world.Biome;
import mc.core.world.World;
import mc.core.world.block.Block;
import mc.core.world.block.BlockFactory;
import mc.core.world.block.BlockType;
import mc.core.world.chunk.ChunkSection;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.List;
public class SimpleChunkSection implements ChunkSection {
private final BlockFactory blockFactory = new BlockFactory();
private final List<BlockType> layersBlock;
private Reference<World> refWorld;
public SimpleChunkSection(List<BlockType> layersBlock, World world) {
this.layersBlock = layersBlock;
this.refWorld = new WeakReference<>(world);
}
@Override
public int getSkyLight(int x, int y, int z) {
if (y <= 3) return 0;
else return 15;
}
@Override
public void setSkyLight(int x, int y, int z, int lightLevel) {
}
@Override
public int getAddition(int x, int y, int z) {
return 0;
}
@Override
public void setAddition(int x, int y, int z, int value) {
}
@Override
public Biome getBiome(int localX, int localZ) {
return Biome.PLAINS;
}
@Override
public int getX() {
return 0;
}
@Override
public int getY() {
return 0;
}
@Override
public int getZ() {
return 0;
}
@Override
public void setBlock(Block block) {
}
@Override
public Block getBlock(int x, int y, int z) {
if (y >= layersBlock.size()) {
return blockFactory.create(BlockType.AIR, x, y, z, getWorld());
}
BlockType blockType = layersBlock.get(y);
if (blockType == null) return blockFactory.create(BlockType.AIR, x, y, z, getWorld());
return blockFactory.create(blockType, x, y, z, getWorld());
}
@Override
public World getWorld() {
World world = refWorld.get();
if (world == null) {
throw new ResourceUnloadedException("World unloaded");
}
return world;
}
}

View File

@@ -0,0 +1,74 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-28
*/
package mc.world.simple;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import mc.core.EntityLocation;
import mc.core.world.World;
import mc.core.world.WorldType;
import mc.core.world.block.BlockType;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class SimpleWorld implements World {
@Getter
private final WorldType worldType = WorldType.FLAT;
private EntityLocation spawn;
private ChunkSection chunkSection;
@Override
public EntityLocation getSpawn() {
if (this.spawn == null) {
log.warn("Spawn is not defined! Set spawn [0, 6, 0]");
this.spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f, this);
}
return this.spawn;
}
@Override
public void setSpawn(EntityLocation location) {
this.spawn = location;
this.spawn.setWorld(this);
}
@Override
public Chunk getChunk(int x, int z) {
Chunk chunk = new SimpleChunk(x, z, this);
chunk.setChunkSection(0, chunkSection);
return chunk;
}
@Override
public void setChunk(int x, int z, Chunk chunk) {
throw new UnsupportedOperationException();
}
public void setLayersBlock(List<String> listOfLayers) {
List<BlockType> layoutsBlock = new ArrayList<>();
for (String value : listOfLayers) {
String[] splitValue = value.split(";");
BlockType blockType;
try {
blockType = BlockType.valueOf(splitValue[1]);
} catch (IllegalArgumentException e) {
continue;
}
for (int i = 0; i < Integer.parseInt(splitValue[0]); i++) {
layoutsBlock.add(blockType);
}
}
this.chunkSection = new SimpleChunkSection(layoutsBlock, this);
}
}