public override IEnumerable <RunStatus> Execute(object context)
        {
            BTAIContext ctx = context as BTAIContext;
            var         tc  = ctx.Entity.GetComponent <ThingComp>();
            var         tcc = ctx.Entity.GetComponent <ThingControlComp>();

            // only attack if not blocked there.
            List <Entity> col = tc.DetectCollisions(ctx.Entity, tc.FacingDirection);

            foreach (Entity e in col)
            {
                var tc2 = e.GetComponent <ThingComp>();
                if (tc2.Faction != tc.Faction && tc2.Faction != Faction.NEUTRAL)
                {
                    IsAttacking               = true;
                    tcc.TargetMove            = tc.FacingDirection;
                    tcc.DeltaTimeBetweenMoves = this.DeltaTimeBetweenMoves;

                    // TODO color set! and health decrease values parameterize.
                    Level.Current.Subtitles.Show(3, AttackStrings[RandomMath.RandomIntBetween(0, AttackStrings.Length - 1)], 3.5f, tc.Color);
                    ctx.Entity.GetComponent <HealthComp>().DecreaseHealth(RandomMath.RandomBetween(1f, 3f));
                    yield return(RunStatus.Success);

                    yield break;
                }
            }
            yield return(RunStatus.Failure);
        }
Esempio n. 2
0
        public override IEnumerable <RunStatus> Execute(object context)
        {
            BTAIContext ctx = context as BTAIContext;

            // enable, or keep alive, the random wander comp.
            var rwc = ctx.Entity.GetComponent <RandomWanderComp>();

            ctx.BTComp.EnableComp(rwc);

            // take the computed move and apply to the move of this Thing
            var tcc = ctx.Entity.GetComponent <ThingControlComp>();

            tcc.TargetMove            = rwc.TargetMove;
            tcc.DeltaTimeBetweenMoves = this.DeltaTimeBetweenMoves;
            yield return(RunStatus.Success);
        }
        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);
        }
 /// <summary>
 /// the external execution of the behavior, can be overridden. Default implementation sets the
 /// new CurrentDirection into VelocityComp of the Entity
 /// </summary>
 /// <param name="context">BT Entity/processing information</param>
 protected virtual void OnExecute(BTAIContext context)
 {
     context.Entity.GetComponent <VelocityComp>().Velocity = new Vector3(CurrentDirection.X, CurrentDirection.Y, 0f);
 }