Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using log4net;
- using SharpDX;
- namespace Ramp.Simulation
- {
- /// <summary>
- /// A link that, when activated by touching or script, moves the activator to another level.
- /// </summary>
- public class LevelLinkBehavior : Behavior, IActivatable
- {
- private static readonly ILog Log = LogManager.GetLogger(typeof(LevelLinkBehavior));
- private Collider _collider;
- public LevelLinkBehavior(ActorComponentInitializer aci)
- : base(aci)
- {
- aci.Bind(out _collider, true);
- _collider.TouchBegan += OnTouchBegan;
- TargetName = aci.GetSpawnArgument<string>("LinkLevel");
- TargetPosition = aci.GetSpawnArgument<Vector2>("LinkPosition");
- }
- /// <summary>
- /// Name of the target level.
- /// </summary>
- public string TargetName { get; set; }
- /// <summary>
- /// Target position to place the actor in.
- /// </summary>
- public Vector2 TargetPosition { get; set; }
- /// <summary>
- /// Activate the level link, causing the target to be moved to the destination level and position.
- /// </summary>
- public virtual async void Activate(Actor target)
- {
- if (!Enabled || target == Actor || !target.CanChangeLevels)
- return;
- Debug.Assert(!target.IsLevelChanging, "!target.IsLevelChanging");
- try
- {
- await target.ChangeLevelAync(TargetName, TargetPosition);
- }
- catch (InvalidOperationException ex)
- {
- Log.Error("Level link activation failed", ex);
- }
- }
- protected virtual void OnTouchBegan(object sender, ActorCollisionEventArgs e)
- {
- Activate(e.Instigator.Actor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement