public void ChainAnimation(string firstKey, string secondKey, float fadeTime) { CAAnimation firstAnim = animationsDict [firstKey]; CAAnimation secondAnim = animationsDict [secondKey]; if (firstAnim == null || secondAnim == null) { return; } SCNAnimationEventHandler chainEventBlock = (CAAnimation animation, NSObject animatedObject, bool playingBackward) => { mainSkeleton.AddAnimation(secondAnim, secondKey); }; if (firstAnim.AnimationEvents == null || firstAnim.AnimationEvents.Length == 0) { firstAnim.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(fadeTime, chainEventBlock) }; } else { var pastEvents = new List <SCNAnimationEvent> (firstAnim.AnimationEvents); pastEvents.Add(SCNAnimationEvent.Create(fadeTime, chainEventBlock)); firstAnim.AnimationEvents = pastEvents.ToArray(); } }
private void SetAnimation(CharacterAnimation index, string animationName, string sceneName) { // Load the DAE using SCNSceneSource in order to be able to retrieve the animation by its identifier var sceneURL = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Scenes.scnassets/boss/" + sceneName, "dae")); var sceneSource = SCNSceneSource.FromUrl(sceneURL, (NSDictionary)null); var bossAnimation = (CAAnimation)sceneSource.GetEntryWithIdentifier(animationName, new Class("CAAnimation")); Animations [(int)index] = bossAnimation; // Blend animations for smoother transitions bossAnimation.FadeInDuration = 0.3f; bossAnimation.FadeOutDuration = 0.3f; if (index == CharacterAnimation.Attack) { // Create an animation event and set it to the animation var attackSoundEvent = new SCNAnimationEventHandler((CAAnimation animation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread(delegate { var soundUrl = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Sounds/attack4", "wav")); new NSSound(soundUrl, false).Play(); }); }); bossAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.4f, attackSoundEvent) }; } }
void SetupRunAnimation() { NSString runKey = KeyForAnimationType(CharacterAnimation.Run); NSString runStartKey = KeyForAnimationType(CharacterAnimation.RunStart); NSString runStopKey = KeyForAnimationType(CharacterAnimation.RunStop); CAAnimation runAnim = LoadAndCacheAnimation("art.scnassets/characters/explorer/run", runKey); CAAnimation runStartAnim = LoadAndCacheAnimation("art.scnassets/characters/explorer/run_start", runStartKey); CAAnimation runStopAnim = LoadAndCacheAnimation("art.scnassets/characters/explorer/run_stop", runStopKey); runAnim.RepeatCount = float.MaxValue; runStartAnim.RepeatCount = 0; runStopAnim.RepeatCount = 0; runAnim.FadeInDuration = 0.05f; runAnim.FadeOutDuration = 0.05f; runStartAnim.FadeInDuration = 0.05f; runStartAnim.FadeOutDuration = 0.05f; runStopAnim.FadeInDuration = 0.05f; runStopAnim.FadeOutDuration = 0.05f; SCNAnimationEventHandler stepLeftBlock = (animation, animatedObject, playingBackward) => { GameSimulation.Sim.PlaySound("leftstep.caf"); }; SCNAnimationEventHandler stepRightBlock = (animation, animatedObject, playingBackward) => { GameSimulation.Sim.PlaySound("rightstep.caf"); }; SCNAnimationEventHandler startWalkStateBlock = (animation, animatedObject, playingBackward) => { if (InRunAnimation) { IsWalking = true; } else { mainSkeleton.RemoveAnimation(runKey, 0.15f); } }; SCNAnimationEventHandler stopWalkStateBlock = (animation, animatedObject, playingBackward) => { IsWalking = false; TurnOffWalkingDust(); if (changingDirection) { inRunAnimation = false; InRunAnimation = true; changingDirection = false; playerWalkDirection = playerWalkDirection == WalkDirection.Left ? WalkDirection.Right : WalkDirection.Left; } }; runStopAnim.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(1f, stopWalkStateBlock) }; runAnim.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.0f, startWalkStateBlock), SCNAnimationEvent.Create(0.25f, stepRightBlock), SCNAnimationEvent.Create(0.75f, stepLeftBlock) }; }
public static SCNAnimationEvent Create(nfloat keyTime, SCNAnimationEventHandler eventHandler) { var handler = new Action <IntPtr, NSObject, bool> ((animationPtr, animatedObject, playingBackward) => { var animation = Runtime.GetINativeObject <AnimationType> (animationPtr, true); eventHandler(animation, animatedObject, playingBackward); }); return(Create(keyTime, handler)); }
void SetupJumpAnimation() { NSString jumpKey = KeyForAnimationType(CharacterAnimation.Jump); NSString fallingKey = KeyForAnimationType(CharacterAnimation.JumpFalling); NSString landKey = KeyForAnimationType(CharacterAnimation.JumpLand); NSString idleKey = KeyForAnimationType(CharacterAnimation.Idle); CAAnimation jumpAnimation = LoadAndCacheAnimation("art.scnassets/characters/explorer/jump_start", jumpKey); CAAnimation fallAnimation = LoadAndCacheAnimation("art.scnassets/characters/explorer/jump_falling", fallingKey); CAAnimation landAnimation = LoadAndCacheAnimation("art.scnassets/characters/explorer/jump_land", landKey); jumpAnimation.FadeInDuration = 0.15f; jumpAnimation.FadeOutDuration = 0.15f; fallAnimation.FadeInDuration = 0.15f; landAnimation.FadeInDuration = 0.15f; landAnimation.FadeOutDuration = 0.15f; jumpAnimation.RepeatCount = 0; fallAnimation.RepeatCount = 0; landAnimation.RepeatCount = 0; jumpForce = 7.0f; SCNAnimationEventHandler leaveGroundBlock = (animation, animatedObject, playingBackward) => { var jumpVelocity = new SCNVector3(0f, jumpForce * 2.1f, 0f); velocity = SCNVector3.Add(velocity, jumpVelocity); Launching = false; InJumpAnimation = false; }; SCNAnimationEventHandler pause = (animation, animatedObject, playingBackward) => { mainSkeleton.PauseAnimation(fallingKey); }; jumpAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.25f, leaveGroundBlock) }; fallAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.5f, pause) }; // Animation Sequence is to Jump -> Fall -> Land -> Idle. ChainAnimation(jumpKey, fallingKey); ChainAnimation(landKey, idleKey); }
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) }; }
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; } }
private void SetAnimation(CharacterAnimation index, string animationName, string sceneName) { // Load the DAE using SCNSceneSource in order to be able to retrieve the animation by its identifier var path = NSBundle.MainBundle.PathForResource("Scenes/hero/" + sceneName, "dae"); var sceneURL = NSUrl.FromFilename(path); var sceneSource = SCNSceneSource.FromUrl(sceneURL, (NSDictionary)null); var animation = (CAAnimation)sceneSource.GetEntryWithIdentifier(animationName, new Class("CAAnimation")); Animations [(int)index] = animation; // Blend animations for smoother transitions animation.FadeInDuration = 0.3f; animation.FadeOutDuration = 0.3f; if (index == CharacterAnimation.Die) { // We want the "death" animation to remain at its final state at the end of the animation animation.RemovedOnCompletion = false; animation.FillMode = CAFillMode.Both; // Create animation events and set them to the animation var swipeSoundEventHandler = new SCNAnimationEventHandler((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread(delegate { var soundUrl = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Sounds/swipe", "wav")); new NSSound(soundUrl, false).Play(); }); }); var deathSoundEventBlock = new SCNAnimationEventHandler((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread(delegate { var soundUrl = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Sounds/death", "wav")); new NSSound(soundUrl, false).Play(); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.0f, swipeSoundEventHandler), SCNAnimationEvent.Create(0.3f, deathSoundEventBlock) }; } if (index == CharacterAnimation.Attack) { // Create an animation event and set it to the animation var swordSoundEventHandler = new SCNAnimationEventHandler((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread(delegate { var soundUrl = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Sounds/sword", "wav")); var attackSound = new NSSound(soundUrl, false); attackSound.Play(); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.4f, swordSoundEventHandler) }; } if (index == CharacterAnimation.Walk) { // Repeat the walk animation 3 times animation.RepeatCount = 3; // Create an animation event and set it to the animation var stepSoundEventHandler = new SCNAnimationEventHandler((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread(delegate { var soundUrl = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource("Sounds/walk", "wav")); new NSSound(soundUrl, false).Play(); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create(0.2f, stepSoundEventHandler), SCNAnimationEvent.Create(0.7f, stepSoundEventHandler) }; } }
void SetAnimation (CharacterAnimation index, string animationName, string sceneName) { // Load the DAE using SCNSceneSource in order to be able to retrieve the animation by its identifier var path = NSBundle.MainBundle.PathForResource ("Scenes/hero/" + sceneName, "dae"); var sceneURL = NSUrl.FromFilename (path); var sceneSource = SCNSceneSource.FromUrl (sceneURL, (NSDictionary)null); var animation = (CAAnimation)sceneSource.GetEntryWithIdentifier (animationName, new Class ("CAAnimation")); Animations [(int)index] = animation; // Blend animations for smoother transitions animation.FadeInDuration = 0.3f; animation.FadeOutDuration = 0.3f; if (index == CharacterAnimation.Die) { // We want the "death" animation to remain at its final state at the end of the animation animation.RemovedOnCompletion = false; animation.FillMode = CAFillMode.Both; // Create animation events and set them to the animation var swipeSoundEventHandler = new SCNAnimationEventHandler ((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread (delegate { var soundUrl = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Sounds/swipe", "wav")); new NSSound (soundUrl, false).Play (); }); }); var deathSoundEventBlock = new SCNAnimationEventHandler ((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread (delegate { var soundUrl = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Sounds/death", "wav")); new NSSound (soundUrl, false).Play (); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create (0.0f, swipeSoundEventHandler), SCNAnimationEvent.Create (0.3f, deathSoundEventBlock) }; } if (index == CharacterAnimation.Attack) { // Create an animation event and set it to the animation var swordSoundEventHandler = new SCNAnimationEventHandler ((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread (delegate { var soundUrl = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Sounds/sword", "wav")); var attackSound = new NSSound (soundUrl, false); attackSound.Play (); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create (0.4f, swordSoundEventHandler) }; } if (index == CharacterAnimation.Walk) { // Repeat the walk animation 3 times animation.RepeatCount = 3; // Create an animation event and set it to the animation var stepSoundEventHandler = new SCNAnimationEventHandler ((CAAnimation handlerAnimation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread (delegate { var soundUrl = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Sounds/walk", "wav")); new NSSound (soundUrl, false).Play (); }); }); animation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create (0.2f, stepSoundEventHandler), SCNAnimationEvent.Create (0.7f, stepSoundEventHandler) }; } }
private void SetAnimation (CharacterAnimation index, string animationName, string sceneName) { // Load the DAE using SCNSceneSource in order to be able to retrieve the animation by its identifier var sceneURL = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Scenes.scnassets/boss/" + sceneName, "dae")); var sceneSource = SCNSceneSource.FromUrl (sceneURL, (NSDictionary)null); var bossAnimation = (CAAnimation)sceneSource.GetEntryWithIdentifier (animationName, new Class ("CAAnimation")); Animations [(int)index] = bossAnimation; // Blend animations for smoother transitions bossAnimation.FadeInDuration = 0.3f; bossAnimation.FadeOutDuration = 0.3f; if (index == CharacterAnimation.Attack) { // Create an animation event and set it to the animation var attackSoundEvent = new SCNAnimationEventHandler ((CAAnimation animation, NSObject animatedObject, bool playingBackward) => { InvokeOnMainThread (delegate { var soundUrl = NSUrl.FromFilename (NSBundle.MainBundle.PathForResource ("Sounds/attack4", "wav")); new NSSound (soundUrl, false).Play (); }); }); bossAnimation.AnimationEvents = new SCNAnimationEvent[] { SCNAnimationEvent.Create (0.4f, attackSoundEvent) }; } }