Example #1
0
        public override void Update()
        {
            if (World.Ticks >= CreatedTicks + TicksToBuild)
            {
                Constructing = false;
            }

            if (!Constructing)
            {
                if (TargetEntity != null)
                {
                    float distance = Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(TargetEntity.Position.X, TargetEntity.Position.Y));
                    if (distance > Range || !TargetEntity.IsAlive)
                    {
                        TargetEntity = null;
                    }
                }
                if (TargetEntity == null)
                {
                    IEntity nearestEnemyHarvester = World.Entities.Where(e => e.Player != Player && e is Harvester)
                                                    .OrderBy(e => Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(e.Position.X, e.Position.Y)))
                                                    .FirstOrDefault();
                    if (nearestEnemyHarvester != null)
                    {
                        float distance = Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(nearestEnemyHarvester.Position.X, nearestEnemyHarvester.Position.Y));
                        if (distance < Range)
                        {
                            TargetEntity = nearestEnemyHarvester;
                        }
                    }
                }

                if (TargetEntity != null)
                {
                    float targetRotation   = VectorHelpers.GetRotationToFace(PositionVector, TargetEntity.PositionVector) ?? Rotation;
                    float rotationDiff     = targetRotation - Rotation;
                    float rotationPerFrame = RotationSpeed;
                    //Console.WriteLine("Source: {0}, Target: {1}, Diff: {2} MaxPerFrame: {3}", Rotation, targetRotation, rotationDiff, rotationPerFrame);
                    if (rotationDiff < rotationPerFrame)
                    {
                        Rotation = targetRotation;
                        if (LastShotTicks == null || World.Ticks - LastShotTicks >= FiringIntervalTicks)
                        {
                            int x = Math.Max(0, Position.X - Sprites.Turret.Width / 2);
                            int y = Math.Max(0, Position.Y - Sprites.Turret.Height / 2);


                            Projectile bullet = new CannonShot(World, Player, Position, Target);
                            World.AddProjectile(bullet);
                            LastShotTicks = World.Ticks;
                            Sounds.CannonShot.Play();
                        }
                    }
                    else
                    {
                        Rotation += rotationPerFrame;
                    }
                }
            }
        }
Example #2
0
        public override void Update()
        {
            A10   a10         = World.GetEntities <A10>().First();
            float a10Distance = Vector2.Distance(a10.PositionVector, PositionVector);

            if (State == SamState.Closed)
            {
                if (a10Distance < Range)
                {
                    State             = SamState.Opening;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Opening)
            {
                if (World.Ticks - StateChangedTicks >= TicksToOpen)
                {
                    State             = SamState.Open;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Open)
            {
                if (a10Distance < Range)
                {
                    Rotation = VectorHelpers.GetRotationToFace(PositionVector, a10.PositionVector) ?? Rotation;

                    if (World.Ticks - LastShotTicks > TicksBetweenShots)
                    {
                        World.AddProjectile(new Patriot(World, Player, Position, a10));
                        World.AddExplosion(new SamMuzzle(World, this, Rotation));
                        Sounds.Rocket1.Play();
                        LastShotTicks = World.Ticks;
                    }
                }
                else
                {
                    State             = SamState.Closing;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Closing)
            {
                if (World.Ticks - StateChangedTicks >= TicksToClose)
                {
                    State             = SamState.Closed;
                    StateChangedTicks = World.Ticks;
                }
            }
            else
            {
                throw new Exception("what");
            }


            base.Update();
        }
 public void GetRotationToFaceTest()
 {
     Assert.AreEqual(null, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, 0)));
     Assert.AreEqual(MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(1, 1)).Value, 0.01);
     Assert.AreEqual(MathHelper.PiOver2, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, 1)).Value, 0.01);
     Assert.AreEqual(3 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, 1)).Value, 0.01);
     Assert.AreEqual(MathHelper.Pi, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, 0)).Value, 0.01);
     Assert.AreEqual(-3 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, -1)).Value, 0.01);
     Assert.AreEqual(-1 * MathHelper.PiOver2, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, -1)).Value, 0.01);
     Assert.AreEqual(-1 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(1, -1)).Value, 0.01);
 }
Example #4
0
 public Projectile(World world, Player player, Point position, Point target, IEntity targetEntity)
 {
     World          = world;
     Player         = player;
     PositionVector = new Vector2(position.X, position.Y);
     Target         = target;
     IsAlive        = true;
     CreatedTicks   = world.Ticks;
     TargetEntity   = targetEntity;
     Rotation       = VectorHelpers.GetRotationToFace(PositionVector, new Vector2(target.X, target.Y)) ?? 0;
 }
Example #5
0
 public override void Update()
 {
     if (Position != Target)
     {
         float targetRotation = VectorHelpers.GetRotationToFace(PositionVector, new Vector2(Target.X, Target.Y)) ?? Rotation;
         if (Rotation == targetRotation)
         {
             PositionVector = VectorHelpers.MoveInDirection(PositionVector, targetRotation, MovementSpeed);
         }
         Rotation = targetRotation;
     }
 }
Example #6
0
        public override void Update()
        {
            A10   a10           = World.GetEntities <A10>().First();
            float a10Distance   = Vector2.Distance(a10.PositionVector, PositionVector);
            float rotationToA10 = VectorHelpers.GetRotationToFace(PositionVector, a10.PositionVector) ?? Rotation;

            if (a10Distance < ShootRange && State != InfantryState.Shoot && World.Ticks >= LastShotTicks + TicksBetweenShots)
            {
                State             = InfantryState.Shoot;
                EnteredStateTicks = World.Ticks;
                LastShotTicks     = World.Ticks;
            }
            else if (a10Distance < ShootRange && State != InfantryState.Shoot && State != InfantryState.Aiming && World.Ticks < LastShotTicks + TicksBetweenShots)
            {
                State             = InfantryState.Aiming;
                EnteredStateTicks = World.Ticks;
            }
            else if (a10Distance < ChaseRange && State == InfantryState.Stand)
            {
                State             = InfantryState.Run;
                EnteredStateTicks = World.Ticks;
            }
            else if (a10Distance < ChaseRange && a10Distance > ShootRange && (State == InfantryState.Shoot || State == InfantryState.Aiming))
            {
                State             = InfantryState.Run;
                EnteredStateTicks = World.Ticks;
            }
            else if (a10Distance > ChaseRange && State != InfantryState.Stand)
            {
                State             = InfantryState.Stand;
                EnteredStateTicks = World.Ticks;
                Rotation          = MathHelper.Pi;
            }

            if (State == InfantryState.Stand)
            {
                // ?
            }
            else if (State == InfantryState.Run)
            {
                Rotation       = rotationToA10;
                PositionVector = VectorHelpers.MoveInDirection(PositionVector, Rotation, MovementPerTick);
            }
            else if (State == InfantryState.Aiming)
            {
                Rotation = rotationToA10;
            }
            else if (State == InfantryState.Shoot)
            {
                Rotation = rotationToA10;
                if ((World.Ticks - EnteredStateTicks) % 64 == 48)
                {
                    Point        missileStartPosition = new Point(Position.X, Position.Y - 6);
                    SmallMissile smallMissile         = new SmallMissile(World, Player.Two, missileStartPosition, a10);
                    World.AddProjectile(smallMissile);
                    Sounds.Bazooka.Play();
                }
                if ((World.Ticks - EnteredStateTicks) % 64 == 63)
                {
                    State = InfantryState.Aiming;
                }
            }
        }
Example #7
0
 public void RotateInstantlyToPointAt(Vector2 target)
 {
     Rotation = VectorHelpers.GetRotationToFace(PositionVector, target) ?? Rotation;
 }