private bool CheckDirectVisibilityByRayCast(Vec3 from, Vec3 targetPosition, MapObject targetObject) { MechUnitAI controlledObj = Owner.ControlledObject; Ray ray = new Ray(from, targetPosition - from); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); foreach (RayCastResult result in piercingResult) { MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body); if (obj != null) { //skip target object if (targetObject != null && obj == targetObject) { continue; } //skip controlled object if (obj == controlledObj) { continue; } } //found body which breaks visibility return(false); } return(true); }
public AttackTask(MechUnitAI owner, Weapon weapon, Vec3 target) { this.owner = owner; this.taskTime = 0f; this.weapon = weapon; this.targetPosition = target; this.targetEntity = null; }
// protected Task(MechUnitAI owner, Vec3 position, MapObject entity) { this.owner = owner; this.taskPosition = position; this.taskEntity = entity; }
protected override void OnTick() { base.OnTick(); MechUnitAI controlledObj = Owner.ControlledObject; if (IsAllowUpdateControlledObject() && controlledObj.GetElapsedTimeSinceLastGroundContact() < .3f)//IsOnGround() ) { float targetDistance = (GetTargetPosition() - controlledObj.Position).Length(); //movement { Vec3 rayCastFrom; if (Owner.weapons.Length != 0) { rayCastFrom = Owner.weapons[0].Position; } else { rayCastFrom = controlledObj.Position; } Range optimalAttackDistanceRange = controlledObj.Type.OptimalAttackDistanceRange; if (targetDistance < optimalAttackDistanceRange.Maximum && CheckDirectVisibilityByRayCast(rayCastFrom, GetTargetPosition(), TaskEntity)) { //destination target is visible controlledObj.SetTurnToPosition(GetTargetPosition()); if (targetDistance < optimalAttackDistanceRange.Minimum) { //move backward Vec2 direction = GetTargetPosition().ToVec2() - controlledObj.Position.ToVec2(); if (direction != Vec2.Zero) { direction = -direction.GetNormalize(); } controlledObj.SetForceMoveVector(direction); } else { //sees the target and stay on optimal attack distance controlledObj.SetForceMoveVector(Vec2.Zero); } } else { //need move //update path controller Owner.pathController.Update(Entity.TickDelta, controlledObj.Position, GetTargetPosition(), false); //update character Vec3 nextPointPosition; if (Owner.pathController.GetNextPointPosition(out nextPointPosition)) { Vec2 vector = nextPointPosition.ToVec2() - controlledObj.Position.ToVec2(); if (vector != Vec2.Zero) { vector.Normalize(); } controlledObj.SetTurnToPosition(nextPointPosition); controlledObj.SetForceMoveVector(vector); } else { controlledObj.SetForceMoveVector(Vec2.Zero); } } } //shot foreach (Weapon weapon in Owner.weapons) { if (weapon.Ready) { Range normalRange = weapon.Type.WeaponNormalMode.UseDistanceRange; bool normalInRange = targetDistance >= normalRange.Minimum && targetDistance <= normalRange.Maximum; Range alternativeRange = weapon.Type.WeaponAlternativeMode.UseDistanceRange; bool alternativeInRange = targetDistance >= alternativeRange.Minimum && targetDistance <= alternativeRange.Maximum; if ((normalInRange || alternativeInRange) && CheckDirectVisibilityByRayCast(weapon.Position, GetTargetPosition(), TaskEntity)) { //update weapon fire orientation { Vec3 pos = GetTargetPosition(); Gun gun = weapon as Gun; if (gun != null) { gun.GetAdvanceAttackTargetPosition(false, TaskEntity, false, out pos); } weapon.SetForceFireRotationLookTo(pos); } controlledObj.SetTurnToPosition(GetTargetPosition()); if (normalInRange) { weapon.TryFire(false); } if (alternativeInRange) { weapon.TryFire(true); } } } } } else { controlledObj.SetForceMoveVector(Vec2.Zero); } }
public AttackTask(MechUnitAI owner, MapObject entity) : base(owner, Vec3.Zero, entity) { }
public AttackTask(MechUnitAI owner, Vec3 position) : base(owner, position, null) { }
public MoveTask(MechUnitAI owner, MapObject entity, float reachDistance) : base(owner, Vec3.Zero, entity) { this.reachDistance = reachDistance; }
// public MoveTask(MechUnitAI owner, Vec3 position, float reachDistance) : base(owner, position, null) { this.reachDistance = reachDistance; }
public IdleTask(MechUnitAI owner) : base(owner, Vec3.Zero, null) { }