Selaa lähdekoodia

Demonstrate Text3D usage in NinjaSnowWar (nametags shown for players other than you.)
Do not load font or attempt to create font faces in headless mode.

Lasse Öörni 12 vuotta sitten
vanhempi
sitoutus
51c1dd3575
2 muutettua tiedostoa jossa 28 lisäystä ja 2 poistoa
  1. 16 0
      Bin/Data/Scripts/NinjaSnowWar.as
  2. 12 2
      Engine/UI/Font.cpp

+ 16 - 0
Bin/Data/Scripts/NinjaSnowWar.as

@@ -383,6 +383,18 @@ void SpawnPlayer(Connection@ connection)
         VariantMap eventData;
         VariantMap eventData;
         eventData["NodeID"] = playerNode.id;
         eventData["NodeID"] = playerNode.id;
         connection.SendRemoteEvent("PlayerSpawned", true, eventData);
         connection.SendRemoteEvent("PlayerSpawned", true, eventData);
+        
+        // Create name tag (Text3D component) for players in multiplayer
+        Node@ textNode = playerNode.CreateChild("NameTag");
+        textNode.position = Vector3(0, 1.2, 0);
+        Text3D@ text3D = textNode.CreateComponent("Text3D");
+        Font@ font = cache.GetResource("Font", "Fonts/BlueHighway.ttf");
+        text3D.SetFont(font, 24);
+        text3D.color = Color(1, 1, 0);
+        text3D.text = players[playerIndex].name;
+        text3D.horizontalAlignment = HA_CENTER;
+        text3D.verticalAlignment = VA_CENTER;
+        text3D.faceCamera = true;
     }
     }
 }
 }
 
 
@@ -592,6 +604,10 @@ void HandlePlayerSpawned(StringHash eventType, VariantMap& eventData)
     {
     {
         playerControls.yaw = playerNode.rotation.yaw;
         playerControls.yaw = playerNode.rotation.yaw;
         playerControls.pitch = 0;
         playerControls.pitch = 0;
+
+        // Disable the nametag from own character
+        Node@ nameTag = playerNode.GetChild("NameTag");
+        nameTag.enabled = false;
     }
     }
 }
 }
 
 

+ 12 - 2
Engine/UI/Font.cpp

@@ -161,6 +161,11 @@ bool Font::Load(Deserializer& source)
 {
 {
     PROFILE(LoadFont);
     PROFILE(LoadFont);
     
     
+    // In headless mode, do not actually load, just return success
+    Graphics* graphics = GetSubsystem<Graphics>();
+    if (!graphics)
+        return true;
+        
     faces_.Clear();
     faces_.Clear();
     
     
     fontDataSize_ = source.GetSize();
     fontDataSize_ = source.GetSize();
@@ -288,6 +293,11 @@ bool Font::SaveXML(Serializer& dest, int pointSize, bool usedGlyphs)
 
 
 const FontFace* Font::GetFace(int pointSize)
 const FontFace* Font::GetFace(int pointSize)
 {
 {
+    // In headless mode, always return null
+    Graphics* graphics = GetSubsystem<Graphics>();
+    if (!graphics)
+        return 0;
+    
     // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
     // For bitmap font type, always return the same font face provided by the font's bitmap file regardless of the actual requested point size
     if (fontType_ == FONT_BITMAP)
     if (fontType_ == FONT_BITMAP)
         pointSize = 0;
         pointSize = 0;
@@ -618,7 +628,7 @@ const FontFace* Font::GetFaceBitmap(int pointSize)
         }
         }
         SharedPtr<Texture> texture = LoadFaceTexture(fontImage);
         SharedPtr<Texture> texture = LoadFaceTexture(fontImage);
         if (!texture)
         if (!texture)
-            return 0;        
+            return 0;
         newFace->textures_.Push(texture);
         newFace->textures_.Push(texture);
         totalTextureSize += fontImage->GetWidth() * fontImage->GetHeight() * fontImage->GetComponents();
         totalTextureSize += fontImage->GetWidth() * fontImage->GetHeight() * fontImage->GetComponents();
         
         
@@ -793,7 +803,7 @@ SharedPtr<FontFace> Font::Pack(const FontFace* fontFace)
     
     
     return packedFontFace;
     return packedFontFace;
 }
 }
-    
+
 SharedPtr<Texture> Font::LoadFaceTexture(SharedPtr<Image> image)
 SharedPtr<Texture> Font::LoadFaceTexture(SharedPtr<Image> image)
 {
 {
     Texture2D* texture = new Texture2D(context_);
     Texture2D* texture = new Texture2D(context_);