use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories-core by Hugobros3.
the class ItemMiningTool method tickInHand.
@Override
public void tickInHand(Entity owner, ItemPile itemPile) {
World world = owner.getWorld();
if (owner instanceof EntityControllable && owner instanceof EntityWorldModifier) {
EntityControllable entityControllable = (EntityControllable) owner;
Controller controller = entityControllable.getController();
if (controller != null && controller instanceof Player) {
InputsManager inputs = controller.getInputsManager();
Location lookingAt = entityControllable.getBlockLookingAt(true);
if (lookingAt != null && lookingAt.distance(owner.getLocation()) > 7f)
lookingAt = null;
if (inputs.getInputByName("mouse.left").isPressed() && lookingAt != null) {
WorldCell cell = world.peekSafely(lookingAt);
// Cancel mining if looking away or the block changed by itself
if (lookingAt == null || (progress != null && (lookingAt.distance(progress.loc) > 0 || !cell.getVoxel().sameKind(progress.voxel)))) {
progress = null;
}
if (progress == null) {
// Try starting mining something
if (lookingAt != null)
progress = new MiningProgress(world.peekSafely(lookingAt));
} else {
// Progress using efficiency / ticks per second
progress.progress += ItemMiningTool.this.miningEfficiency / 60f / progress.materialHardnessForThisTool;
if (progress.progress >= 1.0f) {
if (owner.getWorld() instanceof WorldMaster) {
FutureCell future = new FutureCell(cell);
future.setVoxel(this.getDefinition().store().parent().voxels().air());
// Check no one minds
PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, (WorldModificationCause) entityControllable, (Player) controller);
owner.getWorld().getGameContext().getPluginManager().fireEvent(event);
// Break the block
if (!event.isCancelled()) {
Vector3d rnd = new Vector3d();
for (int i = 0; i < 40; i++) {
rnd.set(progress.loc);
rnd.add(Math.random() * 0.98, Math.random() * 0.98, Math.random() * 0.98);
world.getParticlesManager().spawnParticleAtPosition("voxel_frag", rnd);
}
world.getSoundManager().playSoundEffect("sounds/gameplay/voxel_remove.ogg", Mode.NORMAL, progress.loc, 1.0f, 1.0f);
Location itemSpawnLocation = new Location(world, progress.loc);
itemSpawnLocation.add(0.5, 0.0, 0.5);
// ItemPile droppedItemPile = null;
for (ItemPile droppedItemPile : cell.getVoxel().getLoot(cell, (WorldModificationCause) entityControllable)) {
EntityGroundItem thrownItem = (EntityGroundItem) getDefinition().store().parent().entities().getEntityDefinition("groundItem").create(itemSpawnLocation);
thrownItem.positionComponent.setPosition(itemSpawnLocation);
thrownItem.velocityComponent.setVelocity(new Vector3d(Math.random() * 0.125 - 0.0625, 0.1, Math.random() * 0.125 - 0.0625));
thrownItem.setItemPile(droppedItemPile);
world.addEntity(thrownItem);
}
try {
world.poke(future, (WorldModificationCause) entityControllable);
} catch (WorldException e) {
// Didn't work
// TODO make some ingame effect so as to clue in the player why it failed
}
}
}
progress = null;
}
}
} else {
progress = null;
}
Player player = (Player) controller;
if (player.getContext() instanceof ClientInterface) {
Player me = ((ClientInterface) player.getContext()).getPlayer();
if (me.equals(player)) {
myProgress = progress;
}
}
}
}
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories-core by Hugobros3.
the class VoxelChest method handleInteraction.
@Override
public boolean handleInteraction(Entity entity, ChunkCell voxelContext, Input input) {
if (input.getName().equals("mouse.right") && voxelContext.getWorld() instanceof WorldMaster) {
// Only actual players can open that kind of stuff
if (entity instanceof EntityControllable) {
EntityControllable e = (EntityControllable) entity;
Controller c = e.getController();
if (c instanceof Player && ((Player) c).getLocation().distance(voxelContext.getLocation()) <= 5) {
Player p = (Player) c;
p.openInventory(getInventory(voxelContext));
}
}
}
return false;
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories-core by Hugobros3.
the class VoxelDoor method handleInteraction.
// Meta
// 0x0 -> open/close
// 0x1 -> left/right hinge || left = 0 right = 1 (left is default)
// 0x2-0x4 -> side ( VoxelSide << 2 )
@Override
public boolean handleInteraction(Entity entity, ChunkCell voxelContext, Input input) {
if (!input.getName().equals("mouse.right"))
return false;
if (!(entity.getWorld() instanceof WorldMaster))
return true;
boolean isOpen = ((voxelContext.getMetaData() >> 0) & 0x1) == 1;
boolean hingeSide = ((voxelContext.getMetaData() >> 1) & 0x1) == 1;
int facingPassed = (voxelContext.getMetaData() >> 2) & 0x3;
boolean newState = !isOpen;
int newData = computeMeta(newState, hingeSide, facingPassed);
Location otherPartLocation = voxelContext.getLocation();
if (top)
otherPartLocation.add(0.0, -1.0, 0.0);
else
otherPartLocation.add(0.0, 1.0, 0.0);
EditableCell otherLocationPeek = voxelContext.getWorld().peekSafely(otherPartLocation);
if (otherLocationPeek.getVoxel() instanceof VoxelDoor) {
System.out.println("new door status : " + newState);
voxelContext.getWorld().getSoundManager().playSoundEffect("sounds/voxels/door.ogg", Mode.NORMAL, voxelContext.getLocation(), 1.0f, 1.0f);
voxelContext.setMetaData(newData);
otherLocationPeek.setMetaData(newData);
// otherPartLocation.setVoxelDataAtLocation(VoxelFormat.changeMeta(otherPartLocation.getVoxelDataAtLocation(), newData));
} else {
store.parent().logger().error("Incomplete door @ " + otherPartLocation);
}
return true;
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.
the class WorldImplementation method addEntity.
@Override
public void addEntity(final Entity entity) {
// Assign an UUID to entities lacking one
if (this instanceof WorldMaster && entity.getUUID() == -1) {
long nextUUID = nextEntityId();
entity.setUUID(nextUUID);
}
Entity check = this.getEntityByUUID(entity.getUUID());
if (check != null) {
logger().error("Added an entity twice " + check + " conflits with " + entity + " UUID: " + entity.getUUID());
// logger().save();
Thread.dumpStack();
// System.exit(-1);
return;
}
// Add it to the world
((EntityBase) entity).markHasSpawned();
assert entity.getWorld() == this;
Chunk chunk = this.getChunkWorldCoordinates(entity.getLocation());
if (chunk != null) {
((EntityBase) entity).positionComponent.trySnappingToChunk();
}
this.entities.insertEntity(entity);
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.
the class Ingame method destroy.
@Override
public void destroy() {
// Logout sequence: Save the player entity
if (world instanceof WorldMaster) {
Player player = getPlayer();
PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
world.getPluginManager().fireEvent(playerDisconnectionEvent);
if (this.playerEntity != null) {
SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
playerEntityFile.write(this.playerEntity);
}
}
// Stop the game logic and save
if (world instanceof WorldMaster) {
// TODO: Stop simulation
Fence fence = ((WorldMaster) world).stopLogic();
// exitButton.text = "#{world.saving}";
fence.traverse();
fence = world.saveEverything();
// exitButton.text = "#{world.saving}";
fence.traverse();
}
// Disables plugins
world.getPluginManager().disablePlugins();
this.world.getWorldRenderer().destroy();
}
Aggregations