Browse Source

Add 2d hud to ragdoll example

JimMarlowe 9 years ago
parent
commit
ea52809f98

+ 37 - 0
FeatureExamples/CSharp/Resources/Components/Ragdoll.cs

@@ -42,6 +42,8 @@ namespace FeatureExamples
 
             if (otherBody.Mass > 0.0f)
             {
+                HitEffect (Node); // bam!
+
                 // We do not need the physics components in the AnimatedModel's root scene node anymore
                 Node.RemoveComponent<RigidBody>();
                 Node.RemoveComponent<CollisionShape>();
@@ -170,5 +172,40 @@ namespace FeatureExamples
             constraint.HighLimit = highLimit;
             constraint.LowLimit = lowLimit;
         }
+
+        void HitEffect ( Node parent ) // ragdoll gets the donuts blown out of him
+        {
+            Random rand = new Random();
+            var cache = GetSubsystem<ResourceCache>();
+            Scene myscene = parent.GetScene ();
+            Vector3 where = parent.GetWorldPosition ();
+    
+            for (int mm=0; mm<5; mm++)
+            {
+                Node xNode = myscene.CreateChild("Stuffing");
+                xNode.SetPosition( new Vector3( (float)rand.Next(0,1), (float)rand.Next(0,4), (float)rand.Next(0,15)) + where );
+                xNode.SetRotation( new Quaternion((float)rand.Next(0,360), 0, (float)rand.Next(0,360)));
+                xNode.SetScale(0.3f);
+
+                //create obj
+                StaticModel boxObject = xNode.CreateComponent<StaticModel>();
+                boxObject.Model = cache.Get<Model>("Models/Torus.mdl");
+                boxObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
+                boxObject.CastShadows = true;
+                RigidBody body = xNode.CreateComponent<RigidBody>();
+                body.Mass = 0.45f;
+                body.RollingFriction = 0.15f;
+                body.Friction = 0.75f;
+                body.UseGravity = true;
+                // Disable collision event signaling to reduce CPU load of the physics simulation a111
+                body.SetCollisionEventMode( CollisionEventMode.COLLISION_NEVER );
+                CollisionShape shape = xNode.CreateComponent<CollisionShape>();
+                shape.SetSphere(1.0f, Vector3.Zero, Quaternion.Identity );
+                float objectVelocity = 22.0f;
+                //Node.Rotation
+                body.SetLinearVelocity( new Quaternion((float)rand.Next(0,360), 0, (float)rand.Next(0,360))* new Vector3(0.0f, 0.02f, 1.0f) * objectVelocity);
+            }
+        }
+
     }
 }

+ 10 - 0
FeatureExamples/CSharp/Resources/PostProcess/FrontPath.xml

@@ -0,0 +1,10 @@
+<renderpath>
+    <command type="scenepass" pass="base" vertexlights="true" metadata="base" />
+    <command type="forwardlights" pass="light" />
+    <command type="scenepass" pass="postopaque" />
+    <command type="scenepass" pass="refract">
+        <texture unit="environment" name="viewport" />
+    </command>
+    <command type="scenepass" pass="alpha" vertexlights="true" sort="backtofront" metadata="alpha" />
+    <command type="scenepass" pass="postalpha" sort="backtofront" />
+</renderpath>

+ 308 - 22
FeatureExamples/CSharp/Resources/Scripts/13_Ragdolls.cs

@@ -22,28 +22,86 @@
 // THE SOFTWARE.
 //
 
+using System.Collections.Generic;
 using AtomicEngine;
 
 namespace FeatureExamples
 {
     public class RagdollSample : Sample
     {
-        Scene scene;
-        bool drawDebug;
-        Camera camera;
+        Scene scene;     // perspective (3d) scene, you know, where the ragdolls are.
+        Camera camera;   // the regular camera
+        bool drawDebug;  // bool to turn on/off debug hud.
+
+        // the  2nd scene for the hud "overlay"
+        Scene hudScene;   // the hud scene
+        Camera hudCamera;  // ortho cam for the (pixel perfect) hud
+        Vector <Sprite2D> filler;  // for bargraph display
+
+        // more fun features...
+        List <float> bulletMass;   // how heavy the bullet is
+        List <float> bulletSpeed;  // how fast the bullet is
+        List <float> bulletSize;   // how big the bullet is
+        int massCount, speedCount, sizeCount; // counters
+
+        float bulletArc;    // shooting inclination
 
         public RagdollSample() : base() { }
 
         public override void Start()
         {
+            massCount = 2;
+            speedCount = 2;
+            sizeCount = 2;
+
+            bulletMass = new List<float>();
+            bulletSpeed = new List<float>();
+            bulletSize = new List<float>();
+
+            filler = new Vector <Sprite2D>();
+
+            bulletMass.Add(1.0f);
+            bulletMass.Add(10.0f);
+            bulletMass.Add(50.0f);
+            bulletMass.Add(100.0f);
+            bulletMass.Add(300.0f);
+            bulletMass.Add(666.0f);
+            bulletMass.Add(1000.0f);
+            bulletSpeed.Add(10.0f);
+            bulletSpeed.Add(22.0f);
+            bulletSpeed.Add(41.0f);
+            bulletSpeed.Add(72.0f);
+            bulletSpeed.Add(116.0f);
+            bulletSpeed.Add(200.0f);
+            bulletSpeed.Add(450.0f);
+            bulletSize.Add(0.25f);
+            bulletSize.Add(0.5f);
+            bulletSize.Add(1.25f);
+            bulletSize.Add(3.25f);
+            bulletSize.Add(5.25f);
+            bulletSize.Add(7.25f);
+            bulletSize.Add(11.25f);
+            bulletSize.Add(16.25f);
+
+            bulletArc = 0.25f;
+
             base.Start();
             CreateScene();
+            CreateHUD(); // add an overlay HUD that tells you settings
             SimpleCreateInstructionsWithWasd(
-                "\nLMB to spawn physics objects\n" +
-                "F5 to save scene, F7 to load\n" +
-                "Space to toggle physics debug geometry");
+                "LMB to spawn physics objects\n" +
+                "U=Mass, I=Speed, O=Size, R=Restart\n" +
+                "Space to toggle physics debug geometry\n");
             SetupViewport();
             SubscribeToEvents();
+
+            RestartJacks ();
+
+            UpdateSpeedHud (speedCount);
+            UpdateMassHud (massCount);
+            UpdateSizeHud (sizeCount);
+
+            GetSubsystem<Input>().SetMouseVisible (false);
         }
 
         void SubscribeToEvents()
@@ -70,23 +128,74 @@ namespace FeatureExamples
                 SpawnObject();
 
             /* TODO: Scene.SaveXML/Scene.LoadXML
-
             if (input.GetKeyPress(Constants.KEY_F5))
                 scene.SaveXml(fileSystem.UserDocumentsDir + "/Scenes/Physics.xml");
-
             if (input.GetKeyPress(Constants.KEY_F7))
                 scene.LoadXml(fileSystem.UserDocumentsDir + "/Scenes/Physics.xml");
-
             */
 
+            if (input.GetKeyPress (Constants.KEY_U)) {
+                massCount++;
+                if ( massCount > 5 )
+                    massCount = 0;
+                UpdateMassHud (massCount);
+            }
+
+            if (input.GetKeyPress (Constants.KEY_I)) {
+                speedCount++;
+                if ( speedCount> 5 )
+                    speedCount = 0;
+                UpdateSpeedHud (speedCount);
+            }
+
+            if (input.GetKeyPress (Constants.KEY_O)) {
+                sizeCount++;
+                if ( sizeCount > 5 )
+                    sizeCount = 0;
+                UpdateSizeHud (sizeCount);
+            }
+
+            if (input.GetKeyPress (Constants.KEY_P)) {
+                if ( bulletArc == 0.0 )
+                    bulletArc = 0.25f;
+                else if ( bulletArc > 0.0 )
+                    bulletArc = 0.0f;
+            }
+
+            if (input.GetKeyPress (Constants.KEY_R)) {
+                RestartJacks ();
+            }
+
             if (input.GetKeyPress(Constants.KEY_SPACE))
                 drawDebug = !drawDebug;
+
+            UpdateFps ();
+            CleanUpSome ();
+        }
+
+        void UpdateFps ()
+        {
+            var eng = GetSubsystem<Engine>();
+            string mystr = "FPS: " + eng.GetFps ().ToString ();
+            mystr += "\nUse WASD keys and mouse/touch to move\n"
+                + "LMB to spawn physics objects\n" 
+                + "U=Mass, I=Speed, O=Size, R=Restart\n" 
+                + "Space to toggle physics debug geometry";
+            SetInstructions ( mystr );
         }
 
         void SetupViewport()
         {
             var renderer = GetSubsystem<Renderer>();
-            renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
+            var cache = GetSubsystem<ResourceCache>();
+
+            renderer.SetNumViewports(2);  // use 2 viewports, 1 for 3d and 1 for the 2d hud
+            var viewport2_ = new Viewport(hudScene, hudCamera );  // hud orthographic viewport, scene and camera
+            var overlayRenderPath = new RenderPath(); 
+            overlayRenderPath.Load(cache.GetResource<XMLFile>("PostProcess/FrontPath.xml"));  //special renderpath that does not clear
+            viewport2_.SetRenderPath(overlayRenderPath);  // apply to hud viewport, so the background is transparent
+            renderer.SetViewport(0, new Viewport(scene, camera)); // perspective viewport, scene and camera
+            renderer.SetViewport(1, viewport2_);  // and add in the HUD viewport
         }
 
         void CreateScene()
@@ -141,7 +250,37 @@ namespace FeatureExamples
                 shape.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
             }
 
-            // Create animated models
+            // Create the camera. Limit far clip distance to match the fog
+            CameraNode = new Node();
+            CameraNode.SetName("Camera");
+            camera = CameraNode.CreateComponent<Camera>();
+            camera.FarClip = 300.0f;
+            // Set an initial position for the camera scene node above the plane
+            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
+        }
+
+        void RestartJacks()
+        {
+            var ll = new Vector <Node>();  //get rid of some old objects
+            scene.GetChildrenWithName ( ll, "Sphere", true); 
+            for ( int ii=0; ii<ll.Size; ii++ ) { 
+                ll[ii].Remove ();
+            }
+
+            var nn = new Vector <Node>();
+            scene.GetChildrenWithName ( nn, "Stuffing", true);
+            for ( int ii=0; ii<nn.Size; ii++ ) { 
+                nn[ii].Remove ();
+            }
+
+            var mm = new Vector <Node>();
+            scene.GetChildrenWithName ( mm, "Jack", true); 
+            for ( int ii=0; ii<mm.Size; ii++ ) { 
+                    mm[ii].Remove ();
+                }
+
+            // Create animated models, you dont know ... jack
+            var cache = GetSubsystem<ResourceCache>();
             for (int z = -1; z <= 1; ++z)
             {
                 for (int x = -4; x <= 4; ++x)
@@ -172,13 +311,146 @@ namespace FeatureExamples
                     modelNode.AddComponent(new Ragdoll());
                 }
             }
+        }
 
-            // Create the camera. Limit far clip distance to match the fog
-            CameraNode = new Node();
-            camera = CameraNode.CreateComponent<Camera>();
-            camera.FarClip = 300.0f;
-            // Set an initial position for the camera scene node above the plane
-            CameraNode.Position = new Vector3(0.0f, 3.0f, -20.0f);
+        void CreateHUD()
+        {
+            float hscal = 0.4f;
+            //
+            // create a 2nd viewport and scene for a hud with sprites.
+            //
+            hudScene = new Scene();
+            hudScene.CreateComponent<Octree>();
+            // Create camera node
+            var hudCam = hudScene.CreateChild("HudCamera");
+            // Set camera's position
+            hudCam.SetPosition(new Vector3(0.0f, 0.0f, -10.0f));
+            hudCamera = hudCam.CreateComponent<Camera>();
+            hudCamera.SetOrthographic(true);
+            var graphics = GetSubsystem<Graphics>();
+            hudCamera.SetOrthoSize ((float)graphics.GetHeight () * PixelSize ); //PIXEL_SIZE
+
+            var cache = GetSubsystem<ResourceCache>();
+
+            // add a crosshair in the center of the screen
+            var sprite = cache.GetResource<Sprite2D>("Textures/NinjaSnowWar/Sight.png");
+            var targetSprite_ = hudScene.CreateChild("targetSprite");
+            targetSprite_.SetPosition2D(new Vector2(0,0));
+            targetSprite_.SetScale2D( new Vector2(0.75f, 0.75f));
+            var staticSprite = targetSprite_.CreateComponent<StaticSprite2D>();
+            staticSprite.SetSprite(sprite);   // Set sprite
+            staticSprite.SetBlendMode( AtomicEngine.BlendMode.BLEND_ALPHA ); // Set blend mode
+            staticSprite.SetAlpha(0.3f);  
+
+            // borrow the spinning coin from the 2DSprite example to show what the possibilities are
+            float halfWidth = graphics.Width * 0.5f * PixelSize;
+            float halfHeight = graphics.Height * 0.5f * PixelSize;
+            // Get animation set
+            AnimationSet2D animationSet = cache.Get<AnimationSet2D>("Urho2D/GoldIcon.scml");
+            if (animationSet == null) return;
+            var spriteNode2 = hudScene.CreateChild("AnimatedSprite2D");
+            AnimatedSprite2D animatedSprite = spriteNode2.CreateComponent<AnimatedSprite2D>();
+            animatedSprite.AnimationSet = animationSet;         // Set animation
+            animatedSprite.SetAnimation("idle", LoopMode2D.LM_DEFAULT);
+            spriteNode2.SetPosition2D(new Vector2(halfWidth - 0.4f, halfHeight - 0.4f));
+
+            // (bullet) mass, speed size feature huds
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill1.png") );
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill2.png") );
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill3.png") );
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill4.png") );
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill5.png") );
+            filler.Push ( cache.GetResource<Sprite2D>("Textures/hudfill6.png") );
+
+            Sprite2D spritem = cache.GetResource<Sprite2D>("Textures/hudmass.png");
+            Node hudm = hudScene.CreateChild("hudMass");
+            hudm.SetScale2D(new Vector2(hscal,hscal));
+            hudm.SetPosition2D( new Vector2( 0f - (halfWidth/3.0f), halfHeight - 0.4f)); 
+            StaticSprite2D hudSpritem = hudm.CreateComponent<StaticSprite2D>();
+            hudSpritem.SetSprite(spritem);
+            hudSpritem.SetAlpha(0.9f);
+            hudSpritem.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSpritem.SetOrderInLayer(3); 
+            Node hudfm = hudm.CreateChild("hudMassFill");
+            hudfm.SetScale2D(new Vector2(1f,1f));
+            hudfm.SetPosition2D( new Vector2( 0f, 0f)); 
+            StaticSprite2D hudSpritefm = hudfm.CreateComponent<StaticSprite2D>();
+            hudSpritefm.SetSprite(filler[0]);
+            hudSpritefm.SetAlpha(0.9f);
+            hudSpritefm.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSpritefm.SetOrderInLayer(-3); 
+
+            Sprite2D sprites = cache.GetResource<Sprite2D>("Textures/hudspeed.png");
+            Node huds = hudScene.CreateChild("hudSpeed");
+            huds.SetScale2D(new Vector2(hscal,hscal));
+            huds.SetPosition2D( new Vector2( 0f, halfHeight - 0.4f)); 
+            StaticSprite2D hudSprites = huds.CreateComponent<StaticSprite2D>();
+            hudSprites.SetSprite(sprites);
+            hudSprites.SetAlpha(0.9f);
+            hudSprites.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSprites.SetOrderInLayer(3); 
+            Node hudsm = huds.CreateChild("hudSpeedFill");
+            hudsm.SetScale2D(new Vector2(1f,1f));
+            hudsm.SetPosition2D( new Vector2( 0f, 0f)); 
+            StaticSprite2D hudSpritesm = hudsm.CreateComponent<StaticSprite2D>();
+            hudSpritesm.SetSprite(filler[0]);
+            hudSpritesm.SetAlpha(0.9f);
+            hudSpritesm.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSpritesm.SetOrderInLayer(-3); 
+
+            Sprite2D spritez = cache.GetResource<Sprite2D>("Textures/hudsize.png");
+            Node hudz = hudScene.CreateChild("hudSize");
+            hudz.SetScale2D(new Vector2(hscal,hscal));
+            hudz.SetPosition2D( new Vector2( 0f + (halfWidth/3.0f), halfHeight - 0.4f)); 
+            StaticSprite2D hudSpritez = hudz.CreateComponent<StaticSprite2D>();
+            hudSpritez.SetSprite(spritez);
+            hudSpritez.SetAlpha(0.9f);
+            hudSpritez.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSpritez.SetOrderInLayer(3); 
+            Node hudzm = hudz.CreateChild("hudSizeFill");
+            hudzm.SetScale2D(new Vector2(1f,1f));
+            hudzm.SetPosition2D( new Vector2( 0f, 0f)); 
+            StaticSprite2D hudSpritezm = hudzm.CreateComponent<StaticSprite2D>();
+            hudSpritezm.SetSprite(filler[0]);
+            hudSpritezm.SetAlpha(0.9f);
+            hudSpritezm.SetBlendMode(AtomicEngine.BlendMode.BLEND_ALPHA);
+            hudSpritezm.SetOrderInLayer(-3);
+        }
+
+        void UpdateMassHud ( int value )
+        {
+            Node xNode = hudScene.GetChild("hudMass", true);
+            if (xNode != null) {
+                Node fillx = xNode.GetChild("hudMassFill");
+                if (fillx != null) {
+                    StaticSprite2D hudSprite = fillx.GetComponent<StaticSprite2D>();
+                    hudSprite.SetSprite(filler[value]);
+                }
+            }
+        }
+
+        void UpdateSpeedHud ( int value )
+        {
+            Node xNode = hudScene.GetChild("hudSpeed", true);
+            if (xNode != null) {
+                Node fillx = xNode.GetChild("hudSpeedFill");
+                if (fillx != null) {
+                    StaticSprite2D hudSprite = fillx.GetComponent<StaticSprite2D>();
+                    hudSprite.SetSprite(filler[value]);
+                }
+            }
+        }
+
+        void UpdateSizeHud ( int value )
+        {
+            Node xNode = hudScene.GetChild("hudSize", true);
+            if (xNode != null) {
+                Node fillx = xNode.GetChild("hudSizeFill");
+                if (fillx != null) {
+                    StaticSprite2D hudSprite = fillx.GetComponent<StaticSprite2D>();
+                    hudSprite.SetSprite(filler[value]);
+                }
+            }
         }
 
         void SpawnObject()
@@ -188,25 +460,39 @@ namespace FeatureExamples
             Node boxNode = scene.CreateChild("Sphere");
             boxNode.Position = CameraNode.Position;
             boxNode.Rotation = CameraNode.Rotation;
-            boxNode.SetScale(0.25f);
+            boxNode.SetScale(bulletSize[sizeCount]);
             StaticModel boxObject = boxNode.CreateComponent<StaticModel>();
             boxObject.Model = cache.Get<Model>("Models/Sphere.mdl");
             boxObject.SetMaterial(cache.Get<Material>("Materials/StoneSmall.xml"));
             boxObject.CastShadows = true;
 
             RigidBody body = boxNode.CreateComponent<RigidBody>();
-            body.Mass = 1.0f;
+            body.Mass = bulletMass[massCount];
             body.RollingFriction = 0.15f;
             CollisionShape shape = boxNode.CreateComponent<CollisionShape>();
             shape.SetSphere(1.0f, Vector3.Zero, Quaternion.Identity);
 
-            const float objectVelocity = 10.0f;
+            float objectVelocity = bulletSpeed[speedCount];
 
             // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
             // to overcome gravity better
-            body.SetLinearVelocity(CameraNode.Rotation * new Vector3(0.0f, 0.25f, 1.0f) * objectVelocity);
+            body.SetLinearVelocity(CameraNode.Rotation * new Vector3(0.0f, bulletArc, 1.0f) * objectVelocity);
         }
 
-        
+        void CleanUpSome() // destroy the bullets when they get far enough away
+        {
+            Node cam = CameraNode; // note - the camera isnt in the scene
+            if ( cam == null )
+                return;
+            var mm = new Vector <Node>();
+            scene.GetChildrenWithName ( mm, "Sphere", true);
+            for ( int ii=0; ii<mm.Size; ii++ ) { 
+              float dist = ( mm[ii].GetWorldPosition() - cam.GetWorldPosition() ).Length;
+                if (dist > 270.0f) {
+                    mm[ii].Remove ();
+                }
+            }
+        }
     }
+
 }

+ 9 - 1
FeatureExamples/CSharp/Resources/Scripts/Sample.cs

@@ -35,6 +35,7 @@ namespace FeatureExamples
         protected float Pitch { get; set; }
         protected bool TouchEnabled { get; set; }
         protected Node CameraNode { get; set; }
+        protected UIEditField label { get; set; }
 
         protected UIView UIView { get; set; }
 
@@ -171,7 +172,7 @@ namespace FeatureExamples
             fontDesc.Id = "Vera";
             fontDesc.Size = 18;
 
-            var label = new UIEditField();
+            label = new UIEditField();
             label.FontDescription = fontDesc;
             label.ReadOnly = true;
             label.Multiline = true;
@@ -183,6 +184,12 @@ namespace FeatureExamples
 
         }
 
+        public void SetInstructions(string text = "")
+        {
+            if ( label != null )
+                label.Text = text;
+        }
+
         void CreateLogo()
         {
         }
@@ -197,6 +204,7 @@ namespace FeatureExamples
 
         protected void BackToSelector()
         {
+            GetSubsystem<Input>().SetMouseVisible(true);
             UnsubscribeFromAllEvents();
             var renderer = GetSubsystem<Renderer>();
             for (uint i = 0; i <  renderer.NumViewports; i++)

BIN
FeatureExamples/CSharp/Resources/Textures/hudfill1.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudfill2.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudfill3.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudfill4.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudfill5.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudfill6.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudmass.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudsize.png


BIN
FeatureExamples/CSharp/Resources/Textures/hudspeed.png