public override IEnumerable <RunStatus> Execute(object context) { BTAIContext ctx = context as BTAIContext; // time keeping timeSinceLastChange += ctx.Dt; // direction changing if (timeSinceLastChange >= dirChangeTime) { timeSinceLastChange = 0f; // TODO: define a double functino also dirChangeTime = (double)RandomMath.RandomBetween((float)MinDirectionChangeTime, (float)MaxDirectionChangeTime); // TODO: length-preservation in VelocityComp Vector3 v = ctx.Entity.GetComponent <VelocityComp>().Velocity; CurrentDirection = RandomMath.RandomDirection() * v.Length(); OnExecute(ctx); } yield return(RunStatus.Success); }
protected override void OnNextMove() { base.OnNextMove(); Vector2 dir = CurrentDirection; if (dir.Length() < 0.1f) { dir = Vector2.Zero; } else { // choose one direction randomly, if diagonals would be required if (dir.X != 0f && dir.Y != 0f) { float r = RandomMath.RandomUnit(); if (r > 0.5f) { dir.X = 0f; } else { dir.Y = 0f; } } dir.Normalize(); } TargetMove = dir; // direction changing if (timeSinceLastChange >= dirChangeTime) { timeSinceLastChange = 0f; dirChangeTime = RandomMath.RandomBetween(MinDirectionChangeTime, MaxDirectionChangeTime); CurrentDirection = RandomMath.RandomDirection(); } }
public override void Process(Entity entity, ThingComp tc, RandomWanderComp rwc) { if (!rwc.IsActive) { return; } rwc.UpdateComp(dt); rwc.CurrentDirectionChangeTime -= dt; Vector2 dir = rwc.CurrentDirection; if (dir.Length() < 0.1f) { dir = Vector2.Zero; } rwc.TargetMove = dir; // direction changing after some random time if (rwc.CurrentDirectionChangeTime <= 0) { // TODO double version of randombetween rwc.CurrentDirectionChangeTime = RandomMath.RandomBetween((float)rwc.MinDirectionChangeTime, (float)rwc.MaxDirectionChangeTime); rwc.CurrentDirection = RandomMath.RandomDirection(); } }