Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1.  
  2.         WorldRenderer worldRenderer = CoreRegistry.get(WorldRenderer.class);
  3.         if (worldRenderer != null) {
  4.             Camera activeCamera = worldRenderer.getActiveCamera();
  5.             EntityManager entityManager = CoreRegistry.get(EntityManager.class);
  6.  
  7.             // Draw Entity Overlays first
  8.             for (EntityRef entityRef : entityManager.getEntitiesWith(CharacterComponent.class, MeshComponent.class, LocationComponent.class)) {
  9.                 CharacterComponent characterComponent = entityRef.getComponent(CharacterComponent.class);
  10.                 LocationComponent locationComponent = entityRef.getComponent(LocationComponent.class);
  11.  
  12.                 // Use the name closest to the root
  13.                 DisplayNameComponent nameComponent = entityRef.getComponent(DisplayNameComponent.class);
  14.                 if (nameComponent == null) {
  15.                     // Get the name of a networked client
  16.                     ClientComponent clientComponent = characterComponent.controller.getComponent(ClientComponent.class);
  17.                     if (clientComponent != null) {
  18.                         EntityRef clientInfo = clientComponent.clientInfo;
  19.                         nameComponent = clientInfo.getComponent(DisplayNameComponent.class);
  20.                     }
  21.                 }
  22.  
  23.                 if (nameComponent == null || nameComponent.name.equals("Player"))
  24.                     continue; // can't display a nameplate without a name
  25.  
  26.                 MeshComponent meshComponent = entityRef.getComponent(MeshComponent.class);
  27.  
  28.                 // Skip entities that are outside of visible range (i.e. behind us)
  29.                 Transform transform = new Transform(locationComponent.getTransformMatrix());
  30.                 AABB aabb = meshComponent.mesh.getAABB().transform(transform);
  31.                 if (!worldRenderer.isAABBVisible(aabb))
  32.                     continue;
  33.  
  34.                 // Calculate the center point on the top plane of the AABB
  35.                 Point3f centerOnTop = new Point3f(
  36.                     (aabb.minX() + aabb.maxX()) / 2,
  37.                     aabb.maxY(),
  38.                     (aabb.minZ() + aabb.maxZ()) / 2
  39.                 );
  40.  
  41.                 // Calculate the position of the "top center" in screen space
  42.                 Vector2f screenSpace = activeCamera.fromWorldToScreenSpace(centerOnTop);
  43.  
  44.                 float x = screenSpace.x;
  45.                 float y = screenSpace.y ;
  46.                 System.out.println(x+  "," + y);
  47.  
  48.                 UILabel label = new UILabel(nameComponent.name);
  49.                 Vector2i preferredSize = label.getPreferredContentSize(canvas, new Vector2i(Integer.MAX_VALUE, Integer.MAX_VALUE));
  50.  
  51.                 int minX = TeraMath.floorToInt(x * Display.getWidth() - preferredSize.x / 2.0f);
  52.                 int minY = TeraMath.floorToInt(y * Display.getHeight() - preferredSize.y);
  53.                 int sizeX = preferredSize.x;
  54.                 int sizeY = preferredSize.y;
  55.                 Rect2i region = Rect2i.createFromMinAndSize(minX, minY, sizeX, sizeY);
  56.  
  57.                 canvas.drawWidget(label, region);
  58.  
  59.             }
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement