Advertisement
Inverness

LevelLinkBehavior.cs

Jun 18th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using log4net;
  4. using SharpDX;
  5.  
  6. namespace Ramp.Simulation
  7. {
  8.     /// <summary>
  9.     ///     A link that, when activated by touching or script, moves the activator to another level.
  10.     /// </summary>
  11.     public class LevelLinkBehavior : Behavior, IActivatable
  12.     {
  13.         private static readonly ILog Log = LogManager.GetLogger(typeof(LevelLinkBehavior));
  14.  
  15.         private Collider _collider;
  16.  
  17.         public LevelLinkBehavior(ActorComponentInitializer aci)
  18.             : base(aci)
  19.         {
  20.             aci.Bind(out _collider, true);
  21.             _collider.TouchBegan += OnTouchBegan;
  22.  
  23.             TargetName = aci.GetSpawnArgument<string>("LinkLevel");
  24.             TargetPosition = aci.GetSpawnArgument<Vector2>("LinkPosition");
  25.         }
  26.  
  27.         /// <summary>
  28.         ///     Name of the target level.
  29.         /// </summary>
  30.         public string TargetName { get; set; }
  31.  
  32.         /// <summary>
  33.         ///     Target position to place the actor in.
  34.         /// </summary>
  35.         public Vector2 TargetPosition { get; set; }
  36.  
  37.         /// <summary>
  38.         ///     Activate the level link, causing the target to be moved to the destination level and position.
  39.         /// </summary>
  40.         public virtual async void Activate(Actor target)
  41.         {
  42.             if (!Enabled || target == Actor || !target.CanChangeLevels)
  43.                 return;
  44.  
  45.             Debug.Assert(!target.IsLevelChanging, "!target.IsLevelChanging");
  46.  
  47.             try
  48.             {
  49.                 await target.ChangeLevelAync(TargetName, TargetPosition);
  50.             }
  51.             catch (InvalidOperationException ex)
  52.             {
  53.                 Log.Error("Level link activation failed", ex);
  54.             }
  55.         }
  56.  
  57.         protected virtual void OnTouchBegan(object sender, ActorCollisionEventArgs e)
  58.         {
  59.             Activate(e.Instigator.Actor);
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement