void CreateSwingingTorch() { var scnene = SCNScene.FromFile("torch", GameSimulation.PathForArtResource("level/"), new NSDictionary()); SCNNode torchSwing = scnene.RootNode.FindChildNode("dummy_master", true); RootNode.AddChildNode(torchSwing); }
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 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(); }
void SetupTauntAnimation() { string path = GameSimulation.PathForArtResource("characters/monkey/monkey_tree_hang_taunt"); CAAnimation taunt = LoadAndCacheAnimation(path, "monkey_tree_hang_taunt-1"); taunt.RepeatCount = 0; SCNAnimationEventHandler ackBlock = (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { GameSimulation.Sim.PlaySound("ack.caf"); }; SCNAnimationEventHandler idleBlock = (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { isIdle = true; }; taunt.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.35f, ackBlock), SCNAnimationEvent.Create(1.0f, idleBlock) }; }
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 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; } }
void SetupHangAnimation() { CAAnimation hang = LoadAndCacheAnimation(GameSimulation.PathForArtResource("characters/monkey/monkey_tree_hang"), "monkey_tree_hang-1"); hang.RepeatCount = float.MaxValue; }
public SCNNode CreateLevel() { RootNode = new SCNNode(); // load level dae and add all root children to the scene. var options = new SCNSceneLoadingOptions { ConvertToYUp = true }; SCNScene scene = SCNScene.FromFile("level", GameSimulation.PathForArtResource("level/"), options); foreach (SCNNode node in scene.RootNode.ChildNodes) { RootNode.AddChildNode(node); } // retrieve the main camera Camera = RootNode.FindChildNode("camera_game", true); // create our path that the player character will follow. CalculatePathPositions(); // Sun/Moon light SunLight = RootNode.FindChildNode("FDirect001", true); SunLight.EulerAngles = new SCNVector3(7.1f * (nfloat)Math.PI / 4, (nfloat)Math.PI / 4, 0f); SunLight.Light.ShadowSampleCount = 1; lightOffsetFromCharacter = new SCNVector3(1500f, 2000f, 1000f); //workaround directional light deserialization issue SunLight.Light.ZNear = 100f; SunLight.Light.ZFar = 5000f; SunLight.Light.OrthographicScale = 1000f; // Torches var torchesPos = new float[] { 0f, -1f, 0.092467f, -1f, -1f, 0.5f, 0.792f, 0.95383f }; for (int i = 0; i < torchesPos.Length; i++) { if (torchesPos [i] != -1) { SCNVector3 location = LocationAlongPath(torchesPos [i]); location.Y += 50; location.Z += 150; SCNNode node = CreateTorchNode(); node.Position = location; RootNode.AddChildNode(node); } } // After load, we add nodes that are dynamic / animated / or otherwise not static. CreateLavaAnimation(); CreateSwingingTorch(); AnimateDynamicNodes(); // Create our player character SCNNode characterRoot = GameSimulation.LoadNodeWithName(string.Empty, "art.scnassets/characters/explorer/explorer_skinned.dae"); PlayerCharacter = new PlayerCharacter(characterRoot); TimeAlongPath = 0; PlayerCharacter.Position = LocationAlongPath(TimeAlongPath); PlayerCharacter.Rotation = GetPlayerDirectionFromCurrentPosition(); RootNode.AddChildNode(PlayerCharacter); // Optimize lighting and shadows // only the charadcter should cast shadows foreach (var child in RootNode.ChildNodes) { child.CastsShadow = false; } foreach (var child in PlayerCharacter.ChildNodes) { child.CastsShadow = true; } // Add some monkeys to the scene. AddMonkeyAtPosition(new SCNVector3(0f, -30f, -400f), 0f); AddMonkeyAtPosition(new SCNVector3(3211f, 146f, -400f), -(nfloat)Math.PI / 4f); AddMonkeyAtPosition(new SCNVector3(5200f, 330f, 600f), 0f); // Volcano SCNNode oldVolcano = RootNode.FindChildNode("volcano", true); string volcanoDaeName = GameSimulation.PathForArtResource("level/volcano_effects.dae"); SCNNode newVolcano = GameSimulation.LoadNodeWithName("dummy_master", volcanoDaeName); oldVolcano.AddChildNode(newVolcano); oldVolcano.Geometry = null; oldVolcano = newVolcano.FindChildNode("volcano", true); oldVolcano = oldVolcano.ChildNodes [0]; // Animate our dynamic volcano node. string shaderCode = "uniform float speed;\n" + "_geometry.color = vec4(a_color.r, a_color.r, a_color.r, a_color.r);\n" + "_geometry.texcoords[0] += (vec2(0.0, 1.0) * 0.05 * u_time);\n"; string fragmentShaderCode = "#pragma transparent\n"; // Dim background SCNNode back = RootNode.FindChildNode("dumy_rear", true); foreach (var child in back.ChildNodes) { child.CastsShadow = false; if (child.Geometry == null) { continue; } foreach (SCNMaterial material in child.Geometry.Materials) { material.LightingModelName = SCNLightingModel.Constant; material.Multiply.Contents = AppKit.NSColor.FromDeviceWhite(0.3f, 1f); material.Multiply.Intensity = 1; } } SCNNode backMiddle = RootNode.FindChildNode("dummy_middle", true); foreach (var child in backMiddle.ChildNodes) { if (child.Geometry == null) { continue; } foreach (SCNMaterial material in child.Geometry.Materials) { material.LightingModelName = SCNLightingModel.Constant; } } foreach (var child in newVolcano.ChildNodes) { foreach (var volc in child.ChildNodes) { if (volc != oldVolcano && volc.Geometry != null) { volc.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; volc.Geometry.FirstMaterial.Multiply.Contents = AppKit.NSColor.White; volc.Geometry.ShaderModifiers = new SCNShaderModifiers { EntryPointGeometry = shaderCode, EntryPointFragment = fragmentShaderCode }; } } } Coconuts = new List <Coconut> (); return(RootNode); }