private float CalculateDistance(Entity self, Entity target) { TransformPart selfTf = self.Get <TransformPart>(); TransformPart targetTf = target.Get <TransformPart>(); return((float)Math.Sqrt((double)(Math.Pow((targetTf.GetPositionX - selfTf.GetPositionX), 2) - (Math.Pow((targetTf.GetPositionY - selfTf.GetPositionY), 2))))); }
public override void Update(GameTime gameTime) { if (entity.Has <TransformPart>() && target != null) { TransformPart selfMovement = entity.Get <TransformPart>(); TransformPart targetMovement = target.Get <TransformPart>(); entity.Get <TransformPart>().GetPositionX += speed * ((float)Math.Cos(Math.Atan2(targetMovement.GetPositionY - selfMovement.GetPositionY, targetMovement.GetPositionX - selfMovement.GetPositionX))); entity.Get <TransformPart>().GetPositionY += speed * ((float)Math.Sin(Math.Atan2(targetMovement.GetPositionY - selfMovement.GetPositionY, targetMovement.GetPositionX - selfMovement.GetPositionX))); } else if (target == null) { target = GetAttachedEntity().entityManager.GetByUniqueID("target"); } base.Update(gameTime); }
public override void Update(GameTime gameTime) { ArrayList targets = new ArrayList(); float totalForceRatio = 0; foreach (Entity e in entity.entityManager.GetByCategory("humanoid")) { if (e.Has <TransformPart>() && CalculateDistance(entity, e) < 400 && entity != e) { targets.Add(e); totalForceRatio += (float)(1 / (CalculateDistance(entity, e))); } } ArrayList forces = new ArrayList(); Vector2 final = new Vector2(); for (int x = 0; x < targets.Count; x++) { Entity target = (Entity)targets[x]; TransformPart selfTf = entity.Get <TransformPart>(); TransformPart targetTf = target.Get <TransformPart>(); Vector2 force = new Vector2(); double angle = (float)Math.Atan2((double)(targetTf.GetPositionY - selfTf.GetPositionY), (double)(targetTf.GetPositionX - selfTf.GetPositionX)); float distance = CalculateDistance(target, entity); force.X = (speed * ((1 / distance) / totalForceRatio)) * (float)Math.Cos(angle); force.Y = (speed * ((1 / distance) / totalForceRatio)) * (float)Math.Sin(angle); forces.Add(force); final += force; } entity.Get <TransformPart>().GetPositionX += (-final.X); entity.Get <TransformPart>().GetPositionY += (-final.Y); base.Update(gameTime); }
public override void Update(GameTime gameTime) { if (entity.Has <TransformPart>() && target != null) { TransformPart selfMovement = entity.Get <TransformPart>(); TransformPart targetMovement = target.Get <TransformPart>(); if (targetMovement.GetPosition != prevMovement) { if (targetMovement.GetPosition.X < selfMovement.GetPosition.X) { selfMovement.GetPositionX -= 50 * (float)gameTime.ElapsedGameTime.TotalSeconds; } else if (targetMovement.GetPosition.X > selfMovement.GetPosition.X) { selfMovement.GetPositionX += 50 * (float)gameTime.ElapsedGameTime.TotalSeconds; } if (targetMovement.GetPosition.Y < selfMovement.GetPosition.Y) { selfMovement.GetPositionY -= 50 * (float)gameTime.ElapsedGameTime.TotalSeconds; } else if (targetMovement.GetPosition.Y > selfMovement.GetPosition.Y) { selfMovement.GetPositionY += 50 * (float)gameTime.ElapsedGameTime.TotalSeconds; } } prevMovement = targetMovement.GetPosition; } else if (target == null) { target = GetAttachedEntity().entityManager.GetByUniqueID("player"); } base.Update(gameTime); }