Beispiel #1
0
        private void PresentPrimitives(PresentationViewController presentationViewController)
        {
            var count  = 100;
            var spread = 0.0f;

            // create a cube with a sphere shape
            for (int i = 0; i < count; ++i)
            {
                var model = SCNNode.Create();
                model.Position    = GroundNode.ConvertPositionToNode(new SCNVector3(RandFloat(-1, 1), RandFloat(30, 50), RandFloat(-1, 1)), null);
                model.EulerAngles = new SCNVector3(RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2), RandFloat(0, NMath.PI * 2));

                var size          = new SCNVector3(RandFloat(1.0, 1.5), RandFloat(1.0, 1.5), RandFloat(1.0, 1.5));
                var random        = new Random((int)DateTime.Now.Ticks);
                int geometryIndex = random.Next(0, 7);
                switch (geometryIndex)
                {
                case 0:                 // Box
                    model.Geometry = SCNBox.Create(size.X, size.Y, size.Z, 0);
                    break;

                case 1:                 // Pyramid
                    model.Geometry = SCNPyramid.Create(size.X, size.Y, size.Z);
                    break;

                case 2:                 // Sphere
                    model.Geometry = SCNSphere.Create(size.X);
                    break;

                case 3:                 // Cylinder
                    model.Geometry = SCNCylinder.Create(size.X, size.Y);
                    break;

                case 4:                 // Tube
                    model.Geometry = SCNTube.Create(size.X, size.X + size.Z, size.Y);
                    break;

                case 5:                 // Capsule
                    model.Geometry = SCNCapsule.Create(size.X, size.Y + 2 * size.X);
                    break;

                case 6:                 // Torus
                    model.Geometry = SCNTorus.Create(size.X, NMath.Min(size.X, size.Y) / 2);
                    break;

                default:
                    break;
                }

                model.Geometry.FirstMaterial.Multiply.Contents = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/texture", "png"));

                model.PhysicsBody                 = SCNPhysicsBody.CreateDynamicBody();
                model.PhysicsBody.Velocity        = new SCNVector3(RandFloat(-spread, spread), -10, RandFloat(-spread, spread));
                model.PhysicsBody.AngularVelocity = new SCNVector4(RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-1, 1), RandFloat(-3, 3));

                Shapes.Add(model);

                ((SCNView)presentationViewController.View).Scene.RootNode.AddChildNode(model);
            }
        }
Beispiel #2
0
        public PlayerCharacter(SCNNode characterNode) : base(characterNode)
        {
            CategoryBitMask   = NodeCategory.Lava;
            velocity          = SCNVector3.Zero;
            IsWalking         = false;
            changingDirection = false;
            baseWalkSpeed     = 0.0167f;
            JumpBoost         = 0.0f;

            WalkSpeed           = baseWalkSpeed * 2;
            Jumping             = false;
            groundPlaneHeight   = 0.0f;
            playerWalkDirection = WalkDirection.Right;

            cameraHelper = new SCNNode {
                Position = new SCNVector3(1000f, 200f, 0f)
            };

            AddChildNode(cameraHelper);

            CollideSphere = new SCNNode {
                Position = new SCNVector3(0f, 80f, 0f)
            };

            SCNGeometry     geo    = SCNCapsule.Create(90f, 160f);
            SCNPhysicsShape shape2 = SCNPhysicsShape.Create(geo, (NSDictionary)null);

            CollideSphere.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, shape2);

            CollideSphere.PhysicsBody.CollisionBitMask =
                GameCollisionCategory.Banana |
                GameCollisionCategory.Coin |
                GameCollisionCategory.Coconut |
                GameCollisionCategory.Lava;

            CollideSphere.PhysicsBody.CategoryBitMask = GameCollisionCategory.Player;
            AddChildNode(CollideSphere);

            DustPoof = GameSimulation.LoadParticleSystemWithName("dust");
            NSString artResourcePath = (NSString)GameSimulation.PathForArtResource("level/effects/effects_transparent.png");

            DustPoof.ParticleImage    = artResourcePath;
            DustWalking               = GameSimulation.LoadParticleSystemWithName("dustWalking");
            DustWalking.ParticleImage = artResourcePath;
            dustWalkingBirthRate      = DustWalking.BirthRate;

            // Load the animations and store via a lookup table.
            SetupIdleAnimation();
            SetupRunAnimation();
            SetupJumpAnimation();
            SetupBoredAnimation();
            SetupHitAnimation();

            PlayIdle();
        }
Beispiel #3
0
        private void AddCapsule()
        {
            SCNNode capsuleNode = new SCNNode();

            capsuleNode.Geometry = SCNCapsule.Create(0.1f, 0.3f);
            capsuleNode.Geometry.FirstMaterial.Diffuse.Contents  = UIColor.Orange; //Color del objeto
            capsuleNode.Geometry.FirstMaterial.Specular.Contents = UIColor.White;  //Color del reflejo
            capsuleNode.Position = new SCNVector3(-0.1f, -0.1f, 0);

            sceneView.Scene.RootNode.AddChildNode(capsuleNode);
        }
Beispiel #4
0
        private void LoadCharacter()
        {
            // Load character from external file
            var scene = SCNScene.FromFile("art.scnassets/character/max.scn");

            this.model          = scene.RootNode.FindChildNode("Max_rootNode", true);
            this.model.Position = Character.ModelOffset;

            /* setup character hierarchy
             * character
             |_orientationNode
             |_model
             */

            this.characterNode = new SCNNode {
                Name = "character", Position = Character.InitialPosition
            };

            this.characterOrientation = new SCNNode();
            this.characterNode.AddChildNode(this.characterOrientation);
            this.characterOrientation.AddChildNode(this.model);

            var collider = this.model.FindChildNode("collider", true);

            if (collider?.PhysicsBody != null)
            {
                collider.PhysicsBody.CollisionBitMask = (int)(Bitmask.Enemy | Bitmask.Trigger | Bitmask.Collectable);
            }

            // Setup collision shape
            var min = new SCNVector3();
            var max = new SCNVector3();

            this.model.GetBoundingBox(ref min, ref max);
            nfloat collisionCapsuleRadius = (max.X - min.X) * 0.4f;
            nfloat collisionCapsuleHeight = max.Y - min.Y;

            var collisionGeometry = SCNCapsule.Create(collisionCapsuleRadius, collisionCapsuleHeight);

            this.characterCollisionShape = SCNPhysicsShape.Create(collisionGeometry,
                                                                  new NSMutableDictionary()
            {
                { SCNPhysicsShapeOptionsKeys.CollisionMargin, NSNumber.FromFloat(Character.CollisionMargin) }
            });
            this.collisionShapeOffsetFromModel = new SCNVector3(0f, (float)collisionCapsuleHeight * 0.51f, 0f);
        }
Beispiel #5
0
        public static SCNNode SCGaugeNode(string title, ref SCNNode progressNode)
        {
            var gaugeGroup = SCNNode.Create();

            SCNTransaction.Begin();
            SCNTransaction.AnimationDuration = 1;
            var gauge = SCNNode.Create();

            gauge.Geometry = SCNCapsule.Create(0.4f, 8);
            gauge.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant;
            gauge.Rotation = new SCNVector4(0, 0, 1, (float)(Math.PI / 2));
            gauge.Geometry.FirstMaterial.Diffuse.Contents = NSColor.White;
            gauge.Geometry.FirstMaterial.CullMode         = SCNCullMode.Front;

            var gaugeValue = SCNNode.Create();

            gaugeValue.Geometry = SCNCapsule.Create(0.3f, 7.8f);
            gaugeValue.Pivot    = SCNMatrix4.CreateTranslation(new SCNVector3(0, 3.8f, 0));
            gaugeValue.Position = new SCNVector3(0, 3.8f, 0);
            gaugeValue.Scale    = new SCNVector3(1, 0.01f, 1);
            gaugeValue.Opacity  = 0.0f;
            gaugeValue.Geometry.FirstMaterial.Diffuse.Contents  = NSColor.FromDeviceRgba(0, 1, 0, 1);
            gaugeValue.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant;
            gauge.AddChildNode(gaugeValue);

            if (progressNode != null)
            {
                progressNode = gaugeValue;
            }

            var titleNode = Utils.SCLabelNode(title, LabelSize.Normal, false);

            titleNode.Position = new SCNVector3(-8, -0.55f, 0);

            gaugeGroup.AddChildNode(titleNode);
            gaugeGroup.AddChildNode(gauge);
            SCNTransaction.Commit();

            return(gaugeGroup);
        }
Beispiel #6
0
        // Create a carousel of 3D primitives
        private void PresentPrimitives()
        {
            // Create the carousel node. It will host all the primitives as child nodes.
            CarouselNode          = SCNNode.Create();
            CarouselNode.Position = new SCNVector3(0, 0.1f, -5);
            CarouselNode.Scale    = new SCNVector3(0, 0, 0);           // start infinitely small
            ContentNode.AddChildNode(CarouselNode);

            // Animate the scale to achieve a "grow" effect
            SCNTransaction.Begin();
            SCNTransaction.AnimationDuration = 1;
            CarouselNode.Scale = new SCNVector3(1, 1, 1);
            SCNTransaction.Commit();

            // Rotate the carousel forever
            var rotationAnimation = CABasicAnimation.FromKeyPath("rotation");

            rotationAnimation.Duration    = 40.0f;
            rotationAnimation.RepeatCount = float.MaxValue;
            rotationAnimation.To          = NSValue.FromVector(new SCNVector4(0, 1, 0, (float)Math.PI * 2));
            CarouselNode.AddAnimation(rotationAnimation, new NSString("rotationAnimation"));

            // A material shared by all the primitives
            var sharedMaterial = SCNMaterial.Create();

            sharedMaterial.Reflective.Contents  = new NSImage(NSBundle.MainBundle.PathForResource("SharedTextures/envmap", "jpg"));
            sharedMaterial.Reflective.Intensity = 0.2f;
            sharedMaterial.DoubleSided          = true;

            PrimitiveIndex = 0;

            // SCNBox
            var box = SCNBox.Create(5.0f, 5.0f, 5.0f, 5.0f * 0.05f);

            box.WidthSegmentCount   = 4;
            box.HeightSegmentCount  = 4;
            box.LengthSegmentCount  = 4;
            box.ChamferSegmentCount = 4;
            AddPrimitive(box, 5.0f / 2, rotationAnimation, sharedMaterial);

            // SCNPyramid
            var pyramid = SCNPyramid.Create(5.0f * 0.8f, 5.0f, 5.0f * 0.8f);

            pyramid.WidthSegmentCount  = 4;
            pyramid.HeightSegmentCount = 10;
            pyramid.LengthSegmentCount = 4;
            AddPrimitive(pyramid, 0, rotationAnimation, sharedMaterial);

            // SCNCone
            var cone = SCNCone.Create(0, 5.0f / 2, 5.0f);

            cone.RadialSegmentCount = 20;
            cone.HeightSegmentCount = 4;
            AddPrimitive(cone, 5.0f / 2, rotationAnimation, sharedMaterial);

            // SCNTube
            var tube = SCNTube.Create(5.0f * 0.25f, 5.0f * 0.5f, 5.0f);

            tube.HeightSegmentCount = 5;
            tube.RadialSegmentCount = 40;
            AddPrimitive(tube, 5.0f / 2, rotationAnimation, sharedMaterial);

            // SCNCapsule
            var capsule = SCNCapsule.Create(5.0f * 0.4f, 5.0f * 1.4f);

            capsule.HeightSegmentCount = 5;
            capsule.RadialSegmentCount = 20;
            AddPrimitive(capsule, 5.0f * 0.7f, rotationAnimation, sharedMaterial);

            // SCNCylinder
            var cylinder = SCNCylinder.Create(5.0f * 0.5f, 5.0f);

            cylinder.HeightSegmentCount = 5;
            cylinder.RadialSegmentCount = 40;
            AddPrimitive(cylinder, 5.0f / 2, rotationAnimation, sharedMaterial);

            // SCNSphere
            var sphere = SCNSphere.Create(5.0f * 0.5f);

            sphere.SegmentCount = 20;
            AddPrimitive(sphere, 5.0f / 2, rotationAnimation, sharedMaterial);

            // SCNTorus
            var torus = SCNTorus.Create(5.0f * 0.5f, 5.0f * 0.25f);

            torus.RingSegmentCount = 40;
            torus.PipeSegmentCount = 20;
            AddPrimitive(torus, 5.0f / 4, rotationAnimation, sharedMaterial);

            // SCNPlane
            var plane = SCNPlane.Create(5.0f, 5.0f);

            plane.WidthSegmentCount  = 5;
            plane.HeightSegmentCount = 5;
            plane.CornerRadius       = 5.0f * 0.1f;
            AddPrimitive(plane, 5.0f / 2, rotationAnimation, sharedMaterial);
        }
Beispiel #7
0
        public Character()
        {
            for (int i = 0; i < StepsSoundCount; i++)
            {
                steps [i, (int)FloorMaterial.Grass]        = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_grass_0{0}.mp3", i));
                steps [i, (int)FloorMaterial.Grass].Volume = 0.5f;
                steps [i, (int)FloorMaterial.Rock]         = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_rock_0{0}.mp3", i));
                if (i < StepsInWaterSoundCount)
                {
                    steps [i, (int)FloorMaterial.Water] = SCNAudioSource.FromFile(string.Format("game.scnassets/sounds/Step_splash_0{0}.mp3", i));
                    steps [i, (int)FloorMaterial.Water].Load();
                }
                else
                {
                    steps [i, (int)FloorMaterial.Water] = steps [i % StepsInWaterSoundCount, (int)FloorMaterial.Water];
                }

                steps [i, (int)FloorMaterial.Rock].Load();
                steps [i, (int)FloorMaterial.Grass].Load();

                // Load the character.
                SCNScene characterScene        = SCNScene.FromFile("game.scnassets/panda.scn");
                SCNNode  characterTopLevelNode = characterScene.RootNode.ChildNodes [0];

                Node = SCNNode.Create();
                Node.AddChildNode(characterTopLevelNode);

                // Configure the "idle" animation to repeat forever
                foreach (var childNode in characterTopLevelNode.ChildNodes)
                {
                    foreach (var key in childNode.GetAnimationKeys())
                    {
                        CAAnimation animation = childNode.GetAnimation(key);
                        animation.UsesSceneTimeBase = false;
                        animation.RepeatCount       = float.PositiveInfinity;
                        childNode.AddAnimation(animation, key);
                    }
                }

                // retrieve some particle systems and save their birth rate
                fireEmitter   = characterTopLevelNode.FindChildNode("fire", true);
                fireBirthRate = fireEmitter.ParticleSystems [0].BirthRate;
                fireEmitter.ParticleSystems [0].BirthRate = 0;
                fireEmitter.Hidden = false;

                smokeEmitter   = characterTopLevelNode.FindChildNode("smoke", true);
                smokeBirthRate = smokeEmitter.ParticleSystems [0].BirthRate;
                smokeEmitter.ParticleSystems [0].BirthRate = 0;
                smokeEmitter.Hidden = false;

                whiteSmokeEmitter   = characterTopLevelNode.FindChildNode("whiteSmoke", true);
                whiteSmokeBirthRate = whiteSmokeEmitter.ParticleSystems [0].BirthRate;
                whiteSmokeEmitter.ParticleSystems [0].BirthRate = 0;
                whiteSmokeEmitter.Hidden = false;

                SCNVector3 min = SCNVector3.Zero;
                SCNVector3 max = SCNVector3.Zero;

                Node.GetBoundingBox(ref min, ref max);

                float radius = (max.X - min.X) * .4f;
                float height = (max.Y - min.Y);

                // Create a kinematic with capsule.
                SCNNode colliderNode = SCNNode.Create();
                colliderNode.Name        = "collider";
                colliderNode.Position    = new SCNVector3(0f, height * .51f, 0f);
                colliderNode.PhysicsBody = SCNPhysicsBody.CreateBody(
                    SCNPhysicsBodyType.Kinematic,
                    SCNPhysicsShape.Create(SCNCapsule.Create(radius, height))
                    );

                // We want contact notifications with the collectables, enemies and walls.
                colliderNode.PhysicsBody.ContactTestBitMask = (nuint)(int)(Bitmask.SuperCollectable | Bitmask.Collectable | Bitmask.Collision | Bitmask.Enemy);
                Node.AddChildNode(colliderNode);

                walkAnimation = LoadAnimationFromSceneNamed("game.scnassets/walk.scn");
                walkAnimation.UsesSceneTimeBase = false;
                walkAnimation.FadeInDuration    = .3f;
                walkAnimation.FadeOutDuration   = .3f;
                walkAnimation.RepeatCount       = float.PositiveInfinity;
                walkAnimation.Speed             = CharacterSpeedFactor;

                // Play foot steps at specific times in the animation
                walkAnimation.AnimationEvents = new [] {
                    SCNAnimationEvent.Create(.1f, (animation, animatedObject, playingBackward) => PlayFootStep()),
                    SCNAnimationEvent.Create(.6f, (animation, animatedObject, playingBackward) => PlayFootStep())
                };
            }
        }