private static void ChangeVelocity( Comps.Velocity velocity, Orientation.Cardinal direction, float moveSpeed ) { velocity.data = direction.GetPhysicalVector2() * moveSpeed; velocity.decay = 1; }
public static void Run(Registry registry) { HashSet <Entity> entities = registry.GetEntities( typeof(Comps.Velocity), typeof(Comps.Orientations.Cardinal), typeof(Comps.Autonomous) ); Tick tick = Global.tick; Random random = Global.random; foreach (Entity entity in entities) { Comps.Autonomous autonomous = registry .GetComponentUnsafe <Comps.Autonomous>(entity); Comps.Velocity velocity = registry .GetComponentUnsafe <Comps.Velocity>(entity); Comps.Orientations.Cardinal orientation = registry .GetComponentUnsafe <Comps.Orientations.Cardinal>(entity); float moveSpeed = autonomous.moveSpeed; float changePeriod = 60 * autonomous.changePeriod; if (tick % changePeriod == 0) { if (autonomous.attackChance > random.NextDouble()) { var p = Factories.Projectile.Create(autonomous.attackType, entity, registry); if (autonomous.spreadShots) { CreateSpreadshots(entity, autonomous, p, registry); } if (autonomous.stopWhileAttacking) { velocity.data = PhysicalVector2.Zero; continue; } } } switch (autonomous.kind) { case Comps.Autonomous.Kind.Basic: if (tick % changePeriod == 0) { int r = random.Next(4); ChangeVelocity(velocity, GetCardinal(r), moveSpeed, orientation); } break; case Comps.Autonomous.Kind.Charging: if (tick % (2 * changePeriod) == 0) { int r = 1 + 2 * random.Next(2); moveSpeed *= 1.5f; ChangeVelocity(velocity, GetCardinal(r), moveSpeed, orientation); } else if (tick % changePeriod == 0) { int r = 2 * random.Next(2); moveSpeed *= 0.5f; ChangeVelocity(velocity, GetCardinal(r), moveSpeed); } break; case Comps.Autonomous.Kind.Flying: moveSpeed *= (float)(1 + Math.Sin((tick / (changePeriod * 5)) * 2 * Math.PI)); if (tick % changePeriod == 0) { int r = random.Next(4); ChangeVelocity(velocity, GetCardinal(r), moveSpeed, orientation); } else { ChangeVelocity(velocity, orientation.data, moveSpeed); } break; case Comps.Autonomous.Kind.Shuffling: if (tick % changePeriod == 0) { if (velocity.data.Length() == 0) { ChangeVelocity(velocity, orientation.data, moveSpeed); } else { velocity.data *= -1; } } break; default: break; } } }