Example #1
0
        /// <summary>
        /// Is called when an entity is created for this item (e.g. if dropped).
        /// This function is supposed to add renderjobs and physicscomponents.
        /// Don't forget to add components to the item entity!
        /// </summary>
        public override void SetupItemEntity(ItemEntity itemEntity, Entity entity)
        {
            // Create an appropriate physics shape.
            CollisionShape collShape;
            MeshResource   mesh;
            mat4           scaling;

            switch (Shape)
            {
            case MaterialShape.Cube:
                collShape = new BoxShape(Size / 2f);
                scaling   = mat4.Scale(Size / 2f);
                mesh      = Resources.UseMesh("Box", UpvoidMiner.ModDomain);
                break;

            case MaterialShape.Sphere:
                collShape = new SphereShape(Size.x);
                scaling   = mat4.Scale(Size);
                mesh      = Resources.UseMesh("Sphere", UpvoidMiner.ModDomain);
                break;

            case MaterialShape.Cylinder:
                collShape = new CylinderShape(Size.x, Size.y);
                mesh      = Resources.UseMesh("Cylinder", UpvoidMiner.ModDomain);
                scaling   = mat4.Scale(new vec3(Size.x, Size.y / 2f, Size.z));
                break;

            default: throw new NotImplementedException("Invalid Shape");
            }

            // Create the physical representation of the item.
            RigidBody body = new RigidBody(
                50f,
                entity.Transform,
                collShape
                );

            itemEntity.ContainingWorld.Physics.AddRigidBody(body);

            itemEntity.AddPhysicsComponent(new PhysicsComponent(body, mat4.Identity));

            MaterialResource material;

            if (Material is SolidTerrainResource)
            {
                material = (Material as SolidTerrainResource).RenderMaterial;
            }
            else
            {
                throw new NotImplementedException("Unknown terrain resource");
            }

            // Create the graphical representation of the item.
            MeshRenderJob renderJob = new MeshRenderJob(
                Renderer.Opaque.Mesh,
                material,
                mesh,
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJob, scaling, true));

            MeshRenderJob renderJobShadow = new MeshRenderJob(
                Renderer.Shadow.Mesh,
                Resources.UseMaterial("::Shadow", UpvoidMiner.ModDomain),
                mesh,
                mat4.Identity
                );

            itemEntity.AddRenderComponent(new RenderComponent(renderJobShadow, scaling, true));
        }