Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WorldRenderer worldRenderer = CoreRegistry.get(WorldRenderer.class);
- if (worldRenderer != null) {
- Camera activeCamera = worldRenderer.getActiveCamera();
- EntityManager entityManager = CoreRegistry.get(EntityManager.class);
- // Draw Entity Overlays first
- for (EntityRef entityRef : entityManager.getEntitiesWith(CharacterComponent.class, MeshComponent.class, LocationComponent.class)) {
- CharacterComponent characterComponent = entityRef.getComponent(CharacterComponent.class);
- LocationComponent locationComponent = entityRef.getComponent(LocationComponent.class);
- // Use the name closest to the root
- DisplayNameComponent nameComponent = entityRef.getComponent(DisplayNameComponent.class);
- if (nameComponent == null) {
- // Get the name of a networked client
- ClientComponent clientComponent = characterComponent.controller.getComponent(ClientComponent.class);
- if (clientComponent != null) {
- EntityRef clientInfo = clientComponent.clientInfo;
- nameComponent = clientInfo.getComponent(DisplayNameComponent.class);
- }
- }
- if (nameComponent == null || nameComponent.name.equals("Player"))
- continue; // can't display a nameplate without a name
- MeshComponent meshComponent = entityRef.getComponent(MeshComponent.class);
- // Skip entities that are outside of visible range (i.e. behind us)
- Transform transform = new Transform(locationComponent.getTransformMatrix());
- AABB aabb = meshComponent.mesh.getAABB().transform(transform);
- if (!worldRenderer.isAABBVisible(aabb))
- continue;
- // Calculate the center point on the top plane of the AABB
- Point3f centerOnTop = new Point3f(
- (aabb.minX() + aabb.maxX()) / 2,
- aabb.maxY(),
- (aabb.minZ() + aabb.maxZ()) / 2
- );
- // Calculate the position of the "top center" in screen space
- Vector2f screenSpace = activeCamera.fromWorldToScreenSpace(centerOnTop);
- float x = screenSpace.x;
- float y = screenSpace.y ;
- System.out.println(x+ "," + y);
- UILabel label = new UILabel(nameComponent.name);
- Vector2i preferredSize = label.getPreferredContentSize(canvas, new Vector2i(Integer.MAX_VALUE, Integer.MAX_VALUE));
- int minX = TeraMath.floorToInt(x * Display.getWidth() - preferredSize.x / 2.0f);
- int minY = TeraMath.floorToInt(y * Display.getHeight() - preferredSize.y);
- int sizeX = preferredSize.x;
- int sizeY = preferredSize.y;
- Rect2i region = Rect2i.createFromMinAndSize(minX, minY, sizeX, sizeY);
- canvas.drawWidget(label, region);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement