use of io.xol.chunkstories.api.entity.interfaces.EntityCreative in project chunkstories-core by Hugobros3.
the class ItemFirearm method onControllerInput.
@Override
public boolean onControllerInput(Entity user, ItemPile pile, Input input, Controller controller) {
// Don't do anything with the left mouse click
if (input.getName().startsWith("mouse.")) {
return true;
}
if (input.getName().equals("shootGun")) {
if (user instanceof EntityLiving) {
EntityLiving shooter = (EntityLiving) user;
// Serverside checks
// if (user.getWorld() instanceof WorldMaster)
{
// Is the reload cooldown done
if (cooldownEnd > System.currentTimeMillis())
return false;
// Do we have any bullets to shoot
boolean bulletPresence = (user instanceof EntityCreative && ((EntityCreative) user).isCreativeMode()) || checkBullet(pile);
if (!bulletPresence) {
// Dry.ogg
return true;
} else if (!(user instanceof EntityCreative && ((EntityCreative) user).isCreativeMode())) {
consumeBullet(pile);
}
}
// Jerk client view a bit
if (shooter.getWorld() instanceof WorldClient) {
EntityComponentRotation rot = ((EntityLiving) user).getEntityRotationComponent();
rot.applyInpulse(shake * (Math.random() - 0.5) * 3.0, shake * -(Math.random() - 0.25) * 5.0);
}
// Play sounds
if (controller != null) {
controller.getSoundManager().playSoundEffect(this.soundName, Mode.NORMAL, user.getLocation(), 1.0f, 1.0f, 1.0f, (float) soundRange);
}
playAnimation();
// Raytrace shot
Vector3d eyeLocation = new Vector3d(shooter.getLocation());
if (shooter instanceof EntityPlayer)
eyeLocation.add(new Vector3d(0.0, ((EntityPlayer) shooter).eyePosition, 0.0));
// For each shot
for (int ss = 0; ss < shots; ss++) {
Vector3d direction = new Vector3d(shooter.getDirectionLookingAt());
direction.add(new Vector3d(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize().mul(accuracy / 100d));
direction.normalize();
// Find wall collision
Location shotBlock = user.getWorld().collisionsManager().raytraceSolid(eyeLocation, direction, range);
Vector3dc nearestLocation = null;
// Loops to try and break blocks
boolean brokeLastBlock = false;
while (user.getWorld() instanceof WorldMaster && shotBlock != null) {
WorldCell peek = user.getWorld().peekSafely(shotBlock);
// int data = peek.getData();
Voxel voxel = peek.getVoxel();
brokeLastBlock = false;
if (!voxel.isAir() && voxel.getMaterial().resolveProperty("bulletBreakable") != null && voxel.getMaterial().resolveProperty("bulletBreakable").equals("true")) {
// TODO Spawn an event to check if it's okay
// Destroy it
peek.setVoxel(voxel.store().air());
brokeLastBlock = true;
for (int i = 0; i < 25; i++) {
Vector3d smashedVoxelParticleDirection = new Vector3d(direction);
smashedVoxelParticleDirection.mul(2.0);
smashedVoxelParticleDirection.add(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5);
smashedVoxelParticleDirection.normalize();
user.getWorld().getParticlesManager().spawnParticleAtPositionWithVelocity("voxel_frag", shotBlock, smashedVoxelParticleDirection);
}
user.getWorld().getSoundManager().playSoundEffect("sounds/environment/glass.ogg", Mode.NORMAL, shotBlock, (float) Math.random() * 0.2f + 0.9f, 1.0f);
// Re-raytrace the ray
shotBlock = user.getWorld().collisionsManager().raytraceSolid(eyeLocation, direction, range);
} else
break;
}
// Spawn decal and particles on block the bullet embedded itself in
if (shotBlock != null && !brokeLastBlock) {
Location shotBlockOuter = user.getWorld().collisionsManager().raytraceSolidOuter(eyeLocation, direction, range);
if (shotBlockOuter != null) {
Vector3d normal = shotBlockOuter.sub(shotBlock);
double NbyI2x = 2.0 * direction.dot(normal);
Vector3d NxNbyI2x = new Vector3d(normal);
NxNbyI2x.mul(NbyI2x);
Vector3d reflected = new Vector3d(direction);
reflected.sub(NxNbyI2x);
// Vector3d.sub(direction, NxNbyI2x, reflected);
// shotBlock.setX(shotBlock.getX() + 1);
WorldCell peek = user.getWorld().peekSafely(shotBlock);
for (CollisionBox box : peek.getTranslatedCollisionBoxes()) {
Vector3dc thisLocation = box.lineIntersection(eyeLocation, direction);
if (thisLocation != null) {
if (nearestLocation == null || nearestLocation.distance(eyeLocation) > thisLocation.distance(eyeLocation))
nearestLocation = thisLocation;
}
}
Vector3d particleSpawnPosition = new Vector3d(nearestLocation);
// Position adjustements so shot blocks always shoot proper particles
if (shotBlock.x() - particleSpawnPosition.x() <= -1.0)
particleSpawnPosition.add(-0.01, 0d, 0d);
if (shotBlock.y() - particleSpawnPosition.y() <= -1.0)
particleSpawnPosition.add(0d, -0.01, 0d);
if (shotBlock.z() - particleSpawnPosition.z() <= -1.0)
particleSpawnPosition.add(0d, 0d, -0.01);
for (int i = 0; i < 25; i++) {
Vector3d untouchedReflection = new Vector3d(reflected);
Vector3d random = new Vector3d(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0);
random.mul(0.5);
untouchedReflection.add(random);
untouchedReflection.normalize();
untouchedReflection.mul(0.25);
// Vector3d ppos = new Vector3d(particleSpawnPosition);
controller.getParticlesManager().spawnParticleAtPositionWithVelocity("voxel_frag", particleSpawnPosition, untouchedReflection);
}
controller.getSoundManager().playSoundEffect(peek.getVoxel().getMaterial().resolveProperty("landingSounds"), Mode.NORMAL, particleSpawnPosition, 1, 0.05f);
/*double bspeed = 5/60.0 * (1 + Math.random() * 3 * Math.random());
Vector3d ppos = new Vector3d(reflected);
ppos.normalize();
ppos.scale(0.5);
ppos.add(nearestLocation);
WorldEffects.createFireball(shooter.getWorld(), ppos, 1f, damage*0.15*bspeed, (float) (0.0 + 0.05*damage));
*/
controller.getDecalsManager().drawDecal(nearestLocation, normal.negate(), new Vector3d(0.5), "bullethole");
}
}
// Hitreg takes place on server bois
if (shooter.getWorld() instanceof WorldMaster) {
// Iterate over each found entities
Iterator<Entity> shotEntities = user.getWorld().collisionsManager().rayTraceEntities(eyeLocation, direction, 256f);
while (shotEntities.hasNext()) {
Entity shotEntity = shotEntities.next();
// Don't shoot itself & only living things get shot
if (!shotEntity.equals(shooter) && shotEntity instanceof EntityLiving) {
// Get hit location
for (HitBox hitBox : ((EntityLiving) shotEntity).getHitBoxes()) {
Vector3dc hitPoint = hitBox.lineIntersection(eyeLocation, direction);
if (hitPoint == null)
continue;
// System.out.println("shot" + hitBox.getName());
// Deal damage
((EntityLiving) shotEntity).damage(pileAsDamageCause(pile), hitBox, (float) damage);
// Spawn blood particles
Vector3d bloodDir = direction.normalize().mul(0.75);
for (int i = 0; i < 120; i++) {
Vector3d random = new Vector3d(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0);
random.mul(0.25);
random.add(bloodDir);
shooter.getWorld().getParticlesManager().spawnParticleAtPositionWithVelocity("blood", hitPoint, random);
}
// Spawn blood on walls
if (nearestLocation != null)
shooter.getWorld().getDecalsManager().drawDecal(nearestLocation, bloodDir, new Vector3d(Math.min(3, shots) * damage / 20f), "blood");
}
}
}
}
}
controller.getParticlesManager().spawnParticleAtPosition("muzzle", eyeLocation);
FirearmShotEvent event = new FirearmShotEvent(this, shooter, controller);
shooter.getWorld().getGameContext().getPluginManager().fireEvent(event);
return (shooter.getWorld() instanceof WorldMaster);
}
}
return false;
}
use of io.xol.chunkstories.api.entity.interfaces.EntityCreative in project chunkstories-core by Hugobros3.
the class ItemFirearm method tickInHand.
/**
* Should be called when the owner has this item selected
*
* @param owner
*/
@Override
public void tickInHand(Entity owner, ItemPile itemPile) {
if (owner instanceof EntityControllable && ((EntityControllable) owner).getController() != null) {
EntityControllable owner2 = ((EntityControllable) owner);
Controller controller = owner2.getController();
// For now only client-side players can trigger shooting actions
if (controller instanceof LocalPlayer) {
if (!((LocalPlayer) controller).hasFocus())
return;
LocalPlayer LocalPlayer = (LocalPlayer) controller;
if (LocalPlayer.getInputsManager().getInputByName("mouse.left").isPressed()) {
// Check for bullet presence (or creative mode)
boolean bulletPresence = (owner instanceof EntityCreative && ((EntityCreative) owner).isCreativeMode()) || checkBullet(itemPile);
if (!bulletPresence && !wasTriggerPressedLastTick) {
// Play sounds
if (LocalPlayer != null)
LocalPlayer.getSoundManager().playSoundEffect("sounds/dogez/weapon/default/dry.ogg", Mode.NORMAL, owner.getLocation(), 1.0f, 1.0f, 1f, (float) soundRange);
// Dry.ogg
// return;
} else if ((automatic || !wasTriggerPressedLastTick) && (System.currentTimeMillis() - lastShot) / 1000.0d > 1.0 / (rpm / 60.0)) {
// Fire virtual input
// ClientInputPressedEvent event = new ClientInputPressedEvent(controller.getInputsManager().getInputByName("shootGun"));
// Client.getInstance().getPluginManager().fireEvent(event);
LocalPlayer.getInputsManager().onInputPressed(controller.getInputsManager().getInputByName("shootGun"));
lastShot = System.currentTimeMillis();
}
}
isScoped = this.isScopedWeapon() && controller.getInputsManager().getInputByName("mouse.right").isPressed();
wasTriggerPressedLastTick = controller.getInputsManager().getInputByName("mouse.left").isPressed();
}
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityCreative in project chunkstories by Hugobros3.
the class Ingame method render.
@Override
public void render(RenderingInterface renderer) {
// Update client entity
if ((playerEntity == null || playerEntity != getPlayer().getControlledEntity()) && getPlayer().getControlledEntity() != null) {
playerEntity = getPlayer().getControlledEntity();
if (playerEntity instanceof EntityWithSelectedItem)
inventoryBarDrawer = ((EntityWithSelectedItem) playerEntity).getInventory() == null ? null : new InventoryGridRenderer((EntityWithSelectedItem) playerEntity);
else
inventoryBarDrawer = null;
}
if (playerEntity != null && ((EntityLiving) playerEntity).isDead() && !(gameWindow.getLayer() instanceof DeathScreen))
gameWindow.setLayer(new DeathScreen(gameWindow, this));
// Update the player
if (playerEntity instanceof EntityControllable)
((EntityControllable) playerEntity).onEachFrame(getPlayer());
Location selectedBlock = null;
if (playerEntity instanceof EntityControllable)
selectedBlock = ((EntityControllable) playerEntity).getBlockLookingAt(true);
world.getPluginManager().fireEvent(new CameraSetupEvent(renderer.getCamera()));
// Main render call
world.getWorldRenderer().renderWorld(renderer);
// Debug draws
if (client.getConfiguration().getBooleanOption("client.debug.physicsVisualization") && playerEntity != null) {
wireframeDebugger.render(renderer);
}
if (!guiHidden && selectedBlock != null && playerEntity instanceof EntityCreative && ((EntityCreative) playerEntity).getCreativeModeComponent().get())
selectionRenderer.drawSelectionBox(renderer, selectedBlock);
// Fades in & out the overlay
if (!isCovered()) {
if (pauseOverlayFade > 0.0)
pauseOverlayFade -= 0.1;
} else {
float maxFade = 1.0f;
if (gameWindow.getLayer() instanceof ChatPanelOverlay)
maxFade = 0.25f;
if (pauseOverlayFade < maxFade)
pauseOverlayFade += 0.1;
}
// Blit the final 3d image
world.getWorldRenderer().blitFinalImage(renderer, guiHidden);
// Draw the GUI
if (!guiHidden) {
chatManager.render(renderer);
// Draw inventory
if (playerEntity != null && inventoryBarDrawer != null)
inventoryBarDrawer.drawPlayerInventorySummary(renderer, renderer.getWindow().getWidth() / 2 - 7, 64 + 64);
// Draw debug info
if (client.getConfiguration().getBooleanOption("client.debug.showDebugInfo"))
debugInfoRenderer.drawF3debugMenu(renderer);
renderer.getGuiRenderer().drawBoxWindowsSpaceWithSize(getGameWindow().getWidth() / 2 - 8, getGameWindow().getHeight() / 2 - 8, 16, 16, 0, 1, 1, 0, renderer.textures().getTexture("./textures/gui/cursor.png"), false, true, null);
}
// Lack of overlay should infer autofocus
if (!isCovered())
focus(true);
// Check connection didn't died and change scene if it has
if (world instanceof WorldClientRemote) {
if (!((WorldClientRemote) world).getConnection().isOpen())
gameWindow.getClient().exitToMainMenu("Connection terminated : " + "(TODO: not this way)");
}
// Auto-switch to pause if it detects the game isn't in focus anymore
if (!gameWindow.hasFocus() && !isCovered()) {
focus(false);
gameWindow.setLayer(new PauseMenu(gameWindow, gameWindow.getLayer()));
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityCreative in project chunkstories-api by Hugobros3.
the class PacketInventoryMoveItemPile method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
if (!(processor instanceof ServerPlayerPacketsProcessor)) {
processor.logger().warn("Received a " + this.getClass().getSimpleName() + " but this GameContext isn't providen with a packet processor made to deal with it");
return;
}
ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
Player player = sppc.getPlayer();
EntityControllable playerEntity = player.getControlledEntity();
oldX = in.readInt();
oldY = in.readInt();
newX = in.readInt();
newY = in.readInt();
amount = in.readInt();
from = InventoryTranslator.obtainInventoryHandle(in, processor);
to = InventoryTranslator.obtainInventoryHandle(in, processor);
// If this pile is spawned from the void
if (// || from == InventoryTranslator.INVENTORY_CREATIVE_TRASH)
from == null) {
try {
itemPile = ItemPile.obtainItemPileFromStream(player.getWorld().getContentTranslator(), in);
} catch (NullItemException e) {
// This ... isn't supposed to happen
processor.logger().info("User " + sender + " is trying to spawn a null ItemPile for some reason.");
} catch (UndefinedItemTypeException e) {
// This is slightly more problematic
processor.logger().warn(e.getMessage());
// e.printStackTrace(processor.getLogger().getPrintWriter());
}
} else {
itemPile = from.getItemPileAt(oldX, oldY);
}
// Check access
if (to != null && playerEntity != null) {
if (!to.isAccessibleTo(playerEntity)) {
player.sendMessage("You don't have access to this.");
return;
}
}
// Check using event
PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, itemPile, from, to, oldX, oldY, newX, newY, amount);
player.getContext().getPluginManager().fireEvent(moveItemEvent);
if (!moveItemEvent.isCancelled()) {
// Restrict item spawning
if (// || from instanceof InventoryLocalCreativeMenu)
from == null) {
if (player.hasPermission("items.spawn") || (player.getControlledEntity() != null && player.getControlledEntity() instanceof EntityCreative && ((EntityCreative) player.getControlledEntity()).getCreativeModeComponent().get())) {
// Let it happen when in creative mode or owns items.spawn perm
} else {
player.sendMessage("#C00000You are neither in creative mode nor have the items.spawn permission.");
return;
}
}
// If target inventory is null, this means the item was dropped
if (to == null) {
if (playerEntity == null) {
System.out.println("Dropping items isn't possible if the player doesn't control any entity.");
return;
}
// If we're pulling this out of an inventory ( and not /dev/null ), we need to remove it from that
Inventory sourceInventory = itemPile.getInventory();
Location loc = playerEntity.getLocation();
EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, itemPile);
player.getContext().getPluginManager().fireEvent(dropItemEvent);
if (!dropItemEvent.isCancelled()) {
if (sourceInventory != null)
sourceInventory.setItemPileAt(itemPile.getX(), itemPile.getY(), null);
if (dropItemEvent.getItemEntity() != null)
loc.getWorld().addEntity(dropItemEvent.getItemEntity());
}
return;
}
itemPile.moveItemPileTo(to, newX, newY, amount);
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityCreative in project chunkstories-api by Hugobros3.
the class ItemVoxel method onControllerInput.
@Override
public boolean onControllerInput(Entity entity, ItemPile pile, Input input, Controller controller) {
try {
if (entity.getWorld() instanceof WorldMaster && input.getName().equals("mouse.right")) {
// Require entities to be of the right kind
if (!(entity instanceof EntityWorldModifier))
return true;
if (!(entity instanceof EntityControllable))
return true;
EntityWorldModifier modifierEntity = (EntityWorldModifier) entity;
EntityControllable playerEntity = (EntityControllable) entity;
boolean isEntityCreativeMode = (entity instanceof EntityCreative) && (((EntityCreative) entity).isCreativeMode());
Location blockLocation = null;
blockLocation = playerEntity.getBlockLookingAt(false);
if (blockLocation != null) {
FutureCell fvc = new FutureCell(entity.getWorld().peekSafely(blockLocation));
fvc.setVoxel(voxel);
// Opaque blocks overwrite the original light with zero.
if (voxel.getDefinition().isOpaque()) {
fvc.setBlocklight(0);
fvc.setSunlight(0);
}
// Glowy stuff should glow
// if(voxel.getDefinition().getEmittedLightLevel() > 0)
fvc.setBlocklight(voxel.getEmittedLightLevel(fvc));
// Player events mod
if (controller instanceof Player) {
Player player = (Player) controller;
CellData ctx = entity.getWorld().peek(blockLocation);
PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(ctx, fvc, isEntityCreativeMode ? EntityCreative.CREATIVE_MODE : this, player);
// Anyone has objections ?
entity.getWorld().getGameContext().getPluginManager().fireEvent(event);
if (event.isCancelled())
return true;
entity.getWorld().getSoundManager().playSoundEffect("sounds/gameplay/voxel_place.ogg", Mode.NORMAL, fvc.getLocation(), 1.0f, 1.0f);
}
entity.getWorld().poke(fvc, modifierEntity);
// Decrease stack size
if (!isEntityCreativeMode) {
int currentAmount = pile.getAmount();
currentAmount--;
pile.setAmount(currentAmount);
}
} else {
// No space found :/
return true;
}
}
} catch (WorldException e) {
}
return false;
}
Aggregations