SCNNode CreateBanana() { //Create model if (bananaCollectable == null) { bananaCollectable = GameSimulation.LoadNodeWithName("banana", GameSimulation.PathForArtResource("level/banana.dae")); bananaCollectable.Scale = new SCNVector3(0.5f, 0.5f, 0.5f); SCNSphere sphereGeometry = SCNSphere.Create(40); SCNPhysicsShape physicsShape = SCNPhysicsShape.Create(sphereGeometry, new SCNPhysicsShapeOptions()); bananaCollectable.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, physicsShape); // Only collide with player and ground bananaCollectable.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground; // Declare self in the banana category bananaCollectable.PhysicsBody.CategoryBitMask = GameCollisionCategory.Banana; // Rotate and Hover forever. bananaCollectable.Rotation = new SCNVector4(0.5f, 1f, 0.5f, -(nfloat)Math.PI / 4); SCNAction idleHoverGroupAction = SCNAction.Group(new SCNAction[] { BananaIdleAction, HoverAction }); SCNAction repeatForeverAction = SCNAction.RepeatActionForever(idleHoverGroupAction); bananaCollectable.RunAction(repeatForeverAction); } return(bananaCollectable.Clone()); }
public SCNNode GetBox() { if (_prefab != null) { return(_prefab.Clone()); } var g = new SCNBox { Height = .066f, Width = .066f, Length = .066f }; g.FirstMaterial.Diffuse.Contents = UIImage.FromFile("xamagon-fill"); var box = new SCNNode { Geometry = g }; box.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, SCNPhysicsShape.Create(g, new SCNPhysicsShapeOptions { ShapeType = SCNPhysicsShapeType.BoundingBox })); box.PhysicsBody.ContinuousCollisionDetectionThreshold = g.Width * 2; _prefab = box; return(_prefab.Clone()); }
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(); }
public SCNPhysicsBody MakePhysicsBody(SCNGeometry geometry) { var option = new SCNPhysicsShapeOptions(); option.KeepAsCompound = true; option.ShapeType = SCNPhysicsShapeType.ConcavePolyhedron; var shape = SCNPhysicsShape.Create(geometry, option); return(SCNPhysicsBody.CreateBody(type: SCNPhysicsBodyType.Static, shape)); }
void CreateLavaAnimation() { var dummyFront = RootNode.FindChildNode("dummy_front", true); var lavaNodes = new List <SCNNode> (); foreach (var dummyFrontNode in dummyFront.ChildNodes) { foreach (var lavaNode in dummyFrontNode.ChildNodes) { if (!string.IsNullOrEmpty(lavaNode.Name) && lavaNode.Name.Contains("lava_0")) { lavaNodes.Add(lavaNode); } } } foreach (SCNNode lava in lavaNodes) { var childrenWithGeometry = new List <SCNNode> (); foreach (var child in lava.ChildNodes) { if (child.Geometry != null) { childrenWithGeometry.Add(child); } } if (childrenWithGeometry.Count == 0) { continue; } SCNNode lavaGeometry = childrenWithGeometry [0]; lavaGeometry.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Static, SCNPhysicsShape.Create(lavaGeometry.Geometry, new SCNPhysicsShapeOptions { ShapeType = SCNPhysicsShapeType.ConcavePolyhedron })); lavaGeometry.PhysicsBody.CategoryBitMask = GameCollisionCategory.Lava; lavaGeometry.CategoryBitMask = NodeCategory.Lava; string shaderCode = "uniform float speed;\n" + "#pragma body\n" + "_geometry.texcoords[0] += vec2(sin(_geometry.position.z*0.1 + u_time * 0.1) * 0.1, -1.0* 0.05 * u_time);\n"; lavaGeometry.Geometry.ShaderModifiers = new SCNShaderModifiers { EntryPointGeometry = shaderCode }; } }
SCNNode MakeBallNode() { var sphere = SCNSphere.Create((nfloat)0.05); var sphereNode = SCNNode.Create(); var material = new SCNMaterial(); sphereNode.Geometry = sphere; material.Diffuse.Contents = MakeRandomColor(); sphere.FirstMaterial = material; var shape = SCNPhysicsShape.Create(sphere); var body = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, shape); body.Restitution = (nfloat)0.1; body.Friction = (nfloat)0.9; body.Mass = (nfloat)0.1; sphereNode.PhysicsBody = body; return(sphereNode); }
private void AddCar() { SCNNode pointOfView = sceneView.PointOfView; if (pointOfView == null) { return; } SCNMatrix4 transform = pointOfView.Transform; SCNVector3 orientation = new SCNVector3(-transform.M31, -transform.M32, -transform.M33); SCNVector3 location = new SCNVector3(transform.M41, transform.M42, transform.M43); SCNVector3 currentPositionOfCamera = orientation + location; //TODO 4.2 Creando el coche SCNScene carScene = SCNScene.FromFile("art.scnassets/CarScene.scn"); SCNNode carNode = carScene.RootNode.FindChildNode("frame", false); SCNNode frontLeftWheel = carNode.FindChildNode("frontLeftParent", false); SCNPhysicsVehicleWheel v_frontLeftWheel = SCNPhysicsVehicleWheel.Create(frontLeftWheel); SCNNode frontRightWheel = carNode.FindChildNode("frontRightParent", false); SCNPhysicsVehicleWheel v_frontRightWheel = SCNPhysicsVehicleWheel.Create(frontRightWheel); SCNNode rearLeftWheel = carNode.FindChildNode("rearLeftParent", false); SCNPhysicsVehicleWheel v_rearLeftWheel = SCNPhysicsVehicleWheel.Create(rearLeftWheel); SCNNode rearRightWheel = carNode.FindChildNode("rearRightParent", false); SCNPhysicsVehicleWheel v_rearRightWheel = SCNPhysicsVehicleWheel.Create(rearRightWheel); carNode.Position = currentPositionOfCamera; SCNPhysicsBody body = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, SCNPhysicsShape.Create(carNode, keepAsCompound: true)); carNode.PhysicsBody = body; PhysicsVehicle = SCNPhysicsVehicle.Create(carNode.PhysicsBody, new SCNPhysicsVehicleWheel[] { v_frontLeftWheel, v_frontRightWheel, v_rearLeftWheel, v_rearRightWheel }); sceneView.Scene.PhysicsWorld.AddBehavior(PhysicsVehicle); sceneView.Scene.RootNode.AddChildNode(carNode); }
SCNNode CreateLargeBanana() { //Create model if (largeBananaCollectable == null) { var node = GameSimulation.LoadNodeWithName("banana", GameSimulation.PathForArtResource("level/banana.dae")); float scaleMode = 0.5f * 10 / 4; node.Scale = new SCNVector3(scaleMode, scaleMode, scaleMode); SCNSphere sphereGeometry = SCNSphere.Create(100); SCNPhysicsShape physicsShape = SCNPhysicsShape.Create(sphereGeometry, new SCNPhysicsShapeOptions()); node.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Kinematic, physicsShape); // Only collide with player and ground node.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground; // Declare self in the banana category node.PhysicsBody.CategoryBitMask = GameCollisionCategory.Coin; // Rotate forever. SCNAction rotateCoin = SCNAction.RotateBy(0f, 8f, 0f, 2f); SCNAction repeat = SCNAction.RepeatActionForever(rotateCoin); node.Rotation = new SCNVector4(0f, 1f, 0f, (nfloat)Math.PI / 2); node.RunAction(repeat); largeBananaCollectable = node; } SCNNode nodeSparkle = largeBananaCollectable.Clone(); SCNParticleSystem newSystem = GameSimulation.LoadParticleSystemWithName("sparkle"); nodeSparkle.AddParticleSystem(newSystem); return(nodeSparkle); }
void SetupPathColliders() { // Collect all the nodes that start with path_ under the dummy_front object. // Set those objects as Physics category ground and create a static concave mesh collider. // The simulation will use these as the ground to walk on. SCNNode front = RootNode.FindChildNode("dummy_front", true); foreach (var fronChild in front.ChildNodes) { foreach (var child in fronChild.ChildNodes) { if (child.Name.Contains("path_")) { //the geometry is attached to the first child node of the node named path SCNNode path = child.ChildNodes [0]; var options = new SCNPhysicsShapeOptions { ShapeType = SCNPhysicsShapeType.ConcavePolyhedron }; path.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Static, SCNPhysicsShape.Create(path.Geometry, options)); path.PhysicsBody.CategoryBitMask = GameCollisionCategory.Ground; } } } }
void SetupGetCoconutAnimation() { SCNAnimationEventHandler pickupEventBlock = (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { if (coconutInHand != null) { coconutInHand.RemoveFromParentNode(); } coconutInHand = Coconut.CoconutProtoObject; rightHand.AddChildNode(coconutInHand); hasCoconut = true; }; CAAnimation getAnimation = LoadAndCacheAnimation(GameSimulation.PathForArtResource("characters/monkey/monkey_get_coconut"), "monkey_get_coconut-1"); if (getAnimation.AnimationEvents == null) { getAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.4f, pickupEventBlock) } } ; getAnimation.RepeatCount = 1; } void SetupThrowAnimation() { CAAnimation throwAnimation = LoadAndCacheAnimation(GameSimulation.PathForArtResource("characters/monkey/monkey_throw_coconut"), "monkey_throw_coconut-1"); throwAnimation.Speed = 1.5f; if (throwAnimation.AnimationEvents == null || throwAnimation.AnimationEvents.Length == 0) { SCNAnimationEventHandler throwEventBlock = ThrowCoconut; throwAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.35f, throwEventBlock) }; } throwAnimation.RepeatCount = 0; } void ThrowCoconut(CAAnimation animation, NSObject animatedObject, bool playingBackward) { if (!hasCoconut) { return; } SCNMatrix4 worldMtx = coconutInHand.PresentationNode.WorldTransform; coconutInHand.RemoveFromParentNode(); Coconut node = Coconut.CoconutThrowProtoObject; SCNPhysicsShape coconutPhysicsShape = Coconut.CoconutPhysicsShape; node.PhysicsBody = SCNPhysicsBody.CreateBody(SCNPhysicsBodyType.Dynamic, coconutPhysicsShape); node.PhysicsBody.Restitution = 0.9f; node.PhysicsBody.CollisionBitMask = GameCollisionCategory.Player | GameCollisionCategory.Ground; node.PhysicsBody.CategoryBitMask = GameCollisionCategory.Coconut; node.Transform = worldMtx; GameSimulation.Sim.RootNode.AddChildNode(node); GameSimulation.Sim.GameLevel.Coconuts.Add(node); node.PhysicsBody.ApplyForce(new SCNVector3(-200, 500, 300), true); hasCoconut = false; isIdle = true; } }
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()) }; } }