private void onKeyPressed(Player player, Keyboard key, boolean pressed, char ch) { final PlayerKeyEvent event = Spout.getEventManager().callEvent(new PlayerKeyEvent(player, key, pressed, ch)); if (event.isCancelled()) { return; } if (Spout.debugMode()) { Spout.log("Key " + key + " was " + (pressed ? "pressed" : "released")); } // SHIFT + TAB changes the focus index Screen in = getInputScreen(); if (key == FOCUS_KEY && pressed) { if (in != null) { if (org.lwjgl.input.Keyboard.isKeyDown(Keyboard.KEY_LSHIFT.getId()) || org.lwjgl.input.Keyboard.isKeyDown(Keyboard.KEY_RSHIFT.getId())) { in.previousFocus(FocusReason.KEYBOARD_TAB); } else { in.nextFocus(FocusReason.KEYBOARD_TAB); } } } // send event to the focused widget if (in != null) { Widget w = in.getFocusedWidget(); if (w != null) { w.onKey(event); } } executeBindings(getKeyBindingsFor(key), player, pressed); }
private void readObject(java.io.ObjectInputStream in) throws IOException { float x = in.readFloat(); float y = in.readFloat(); float z = in.readFloat(); String world = in.readUTF(); World w = Spout.getEngine().getWorld(world); try { Field field; field = Vector3.class.getDeclaredField("x"); field.setAccessible(true); field.set(this, x); field = Vector3.class.getDeclaredField("y"); field.setAccessible(true); field.set(this, y); field = Vector3.class.getDeclaredField("z"); field.setAccessible(true); field.set(this, z); field = Point.class.getDeclaredField("world"); field.setAccessible(true); field.set(this, w); } catch (Exception e) { if (Spout.debugMode()) { e.printStackTrace(); } } }
private void onMouseClicked(Player player, Mouse button, boolean pressed, int x, int y) { PlayerClickEvent event = Spout.getEventManager() .callEvent(new PlayerClickEvent(player, button, pressed, new IntVector2(x, y))); if (event.isCancelled()) { return; } if (Spout.debugMode()) { Spout.log("Mouse clicked at {" + x + "," + y + "} was " + (pressed ? "pressed" : "released")); } Screen s = getInputScreen(); if (s != null) { Widget w = s.getWidgetAt(x, y); if (w != null) { Widget fw = s.getFocusedWidget(); if (fw != null && !fw.equals(w)) { s.setFocus(w, FocusReason.CLICKED); } w.onClicked(event); } } executeBindings(getMouseBindingsFor(button), player, pressed); }
@Override public byte[] compress() { Serializable value = data.get(); if (value instanceof ByteArrayWrapper) { return ((ByteArrayWrapper) value).getArray(); } ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try { ObjectOutputStream objOut = new ObjectOutputStream(byteOut); objOut.writeObject(value); objOut.flush(); objOut.close(); } catch (IOException e) { if (Spout.debugMode()) { Spout.getLogger() .log( Level.SEVERE, "Unable to serialize " + value + " (type: " + (value != null ? value.getClass().getSimpleName() : "null") + ")", e); } return null; } return byteOut.toByteArray(); }
private boolean shouldForce(int entityId) { int hash = HASH_SEED; hash += (hash << 5) + entityId; hash += (hash << 5) + tickCounter; return (hash & FORCE_MASK) == 0 || (Spout.debugMode() && getPlayer().has(ForceMessagesComponent.class)); }
@Override public void onTick(float dt) { final Random random = GenericMath.getRandom(); float secondsUntilWeatherChange = sky.getData().get(VanillaData.WEATHER_CHANGE_TIME); secondsUntilWeatherChange -= dt; if (forceWeatherUpdate.compareAndSet(true, false) || secondsUntilWeatherChange <= 0) { this.sky.updateWeather(getCurrent(), getForecast()); sky.getData().put(VanillaData.WORLD_WEATHER, getForecast()); final Weather current = getCurrent(); Weather forecast = current; while (forecast == current) { // When Rain/Snow or Thunderstorms occur, always go to Clear after. if (current == Weather.RAIN || current == Weather.THUNDERSTORM) { forecast = Weather.CLEAR; } else { forecast = Weather.get(random.nextInt(3)); } setForecast(forecast); } setForecast(forecast); secondsUntilWeatherChange = current.getBaseWeatherTime() + random.nextInt(current.getRandomWeatherTime()); if (Spout.debugMode()) { Spout.getLogger() .info( "Weather changed to: " + current + ", next change in " + secondsUntilWeatherChange / 1000F + "s"); } } float currentRainStrength = sky.getData().get(VanillaData.CURRENT_RAIN_STRENGTH); sky.getData().put(VanillaData.PREVIOUS_RAIN_STRENGTH, currentRainStrength); if (this.isRaining()) { currentRainStrength = Math.min(1.0f, currentRainStrength + 0.01f); } else { currentRainStrength = Math.max(0.0f, currentRainStrength - 0.01f); } sky.getData().put(VanillaData.CURRENT_RAIN_STRENGTH, currentRainStrength); if (hasLightning()) { lightning.onTick(dt); } if (getCurrent().isRaining()) { snowfall.onTick(dt); } sky.getData().put(VanillaData.WEATHER_CHANGE_TIME, secondsUntilWeatherChange); }
/** Called when the tick is finished and collisions need to be resolved and move events fired */ public void resolve() { if (Spout.debugMode()) { // System.out.println("COLLISION DEBUGGING"); // System.out.println("Current Collision: " + this.collision.toString()); } List<CollisionVolume> colliding = ((SpoutWorld) collisionPoint.getWorld()).getCollidingObject(this.collision); Vector3 offset = this.lastTransform.getPosition().subtract(collisionPoint); for (CollisionVolume box : colliding) { if (Spout.debugMode()) { // System.out.println("Colliding box: " + box.toString()); } Vector3 collision = this.collision.resolve(box); if (Spout.debugMode()) { // System.out.println("Collision vector: " + collision.toString()); } if (collision != null) { collision = collision.subtract(collisionPoint); if (Spout.debugMode()) { // System.out.println("Collision point: " + collision.toString() + " Collision vector: " + // collision); } if (collision.getX() != 0F) { offset = new Vector3(collision.getX(), offset.getY(), offset.getZ()); } if (collision.getY() != 0F) { offset = new Vector3(offset.getX(), collision.getY(), offset.getZ()); } if (collision.getZ() != 0F) { offset = new Vector3(offset.getX(), offset.getY(), collision.getZ()); } if (Spout.debugMode()) { // System.out.println("Collision offset: " + offset.toString()); } if (this.getCollision().getStrategy() == CollisionStrategy.SOLID && box.getStrategy() == CollisionStrategy.SOLID) { this.setPosition(collisionPoint.add(offset)); if (Spout.debugMode()) { // System.out.println("New Position: " + this.getPosition()); } } controllerLive.get().onCollide(getWorld().getBlock(box.getPosition())); } } // Check to see if we should fire off a Move event }
@Override public void onTick(float dt) { Controller cont = controllerLive.get(); // Pulse all player messages here, so they can interact with the entities position safely if (cont instanceof PlayerController) { Player player = ((PlayerController) cont).getPlayer(); if (player != null && player.getSession() != null) { ((SpoutSession) player.getSession()).pulse(); } } // Tick the controller if (cont != null) { // Sanity check if (cont.getParent() != this) { if (Spout.debugMode()) { throw new IllegalStateException("Controller parent does not match entity"); } else { cont.attachToEntity(this); } } // If this is the first tick, we need to attach the controller // Controller is attached here instead of inside of the constructor // because the constructor can not access getChunk if the entity is being deserialized if (!attached) { cont.onAttached(); attached = true; } cont.onTick(dt); } // Copy values last (position may change during controller or session pulses) if (!isDead() && this.transform.getPosition() != null && this.transform.getPosition().getWorld() != null) { // Note: if the chunk is null, this effectively kills the entity (since dead: {chunkLive.get() // == null}) chunkLive.set( transform.getPosition().getWorld().getChunkFromBlock(transform.getPosition(), false)); entityManagerLive.set(((SpoutRegion) getRegion()).getEntityManager()); lastTransform = transform.copy(); } }
protected void debug(Level level, String msg) { if (Spout.debugMode()) { Spout.getLogger().log(level, msg); } }