public void SpotFoe(int actorId) { AggroRecord aggro; if (!mFoes.ContainsKey(actorId)) { aggro = new AggroRecord(); aggro.Enmity = AggroRecord.NEW_FOE_ENMITY; mFoes.Add(actorId, aggro); } else { aggro = mFoes[actorId]; } Actor foe = GameResources.ActorManager.GetActorById(actorId); BipedControllerComponent bcc = foe.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); aggro.PositionLastSensed = BepuConverter.Convert(bcc.Controller.Body.Position); if (aggro.TimeLastVisible < GameResources.ActorManager.CurrentTime.Subtract(SharedResources.Game.TargetElapsedTime)) { // They reappeared just now. aggro.TimeBecameVisible = GameResources.ActorManager.CurrentTime; } aggro.TimeLastSensed = GameResources.ActorManager.CurrentTime; aggro.TimeLastVisible = GameResources.ActorManager.CurrentTime; }
// Come to a smooth stop at a target. private Vector2 Arrive(Actor owner) { BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); Vector3 relativeTarget = Target - BepuConverter.Convert(bcc.Controller.Body.Position); relativeTarget.Y = 0.0f; if (relativeTarget.LengthSquared() < ARRIVE_TOLERANCE) { return(Vector2.Zero); } float distToTarget = relativeTarget.Length(); relativeTarget /= distToTarget; float normalizedSpeedToTarget = Vector3.Dot(BepuConverter.Convert(bcc.Controller.Body.LinearVelocity), relativeTarget) / bcc.RunSpeed; // Rotate into controller space. Matrix controllerRotation = Matrix.CreateLookAt(Vector3.Zero, BepuConverter.Convert(bcc.Controller.HorizontalViewDirection), Vector3.Up); relativeTarget = Vector3.Transform(relativeTarget, controllerRotation); Vector2 seekResult = new Vector2(relativeTarget.X, -relativeTarget.Z); const float DECELERATION_RADIUS = 24.0f; // Tweak this float closeness = Math.Max(0.0f, normalizedSpeedToTarget * normalizedSpeedToTarget - distToTarget / DECELERATION_RADIUS * Urgency); float brakes = closeness > 1.0f ? 1.0f : (float)(Math.Asin(closeness) / MathHelper.PiOver2); return((1.0f - brakes) * seekResult); }
public override void Update(/* inout */ SteeringBlender steering, Actor owner, IAgentStateManager agent) { if (agent.HasProperty(AgentPropertyName.ActiveOpponent)) { ZombieCombatState zcs = new ZombieCombatState(); agent.CurrentState = zcs; return; } else if (agent.TimeInState.Ticks >= mDurationTicks) { ZombieWaitState zws = new ZombieWaitState(8.0f); agent.CurrentState = zws; return; } BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); Vector3 displacementFromHome = BepuConverter.Convert(bcc.Controller.Body.Position) - steering.Target; displacementFromHome.Y = 0.0f; float tr = agent.GetProperty <float>(AgentPropertyName.TerritoryRadius); steering.Weights[(int)SteeringBlender.WeightType.Seek] = Math.Min(Math.Max(displacementFromHome.Length() - tr * 0.75f, 0.0f) / tr, 1.0f); steering.Weights[(int)SteeringBlender.WeightType.Wander] = 1.0f - steering.Weights[(int)SteeringBlender.WeightType.Seek]; }
public void UpdateInputState(bool inputState, BipedControllerComponent bipedController) { if (inputState) { if (mReturnAttention == null) { mReturnAttention = bipedController.TryGetAttention(); if (mReturnAttention != null) { CurrentOperation = WeaponFunctions.TriggerPulled; } else { CurrentOperation = WeaponFunctions.Neutral; } } else { CurrentOperation = WeaponFunctions.TriggerPulled; } } else if (CurrentOperation == WeaponFunctions.TriggerPulled) { CurrentOperation = WeaponFunctions.Neutral; } }
public void SetControllerComponent(BipedControllerComponent bipedController) { mBipedControl = bipedController; if (mBipedControl != null && mSkillPalette == null) { mBipedControl.Owner.ActorDespawning += ActorDespawningHandler; } }
private void ActorInitializedHandler(object sender, EventArgs e) { BipedControllerComponent bcc = Owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); mAgentProperties.Add(AgentPropertyName.HomePosition, BepuConverter.Convert(bcc.Controller.Body.Position)); mAgentProperties.Add(AgentPropertyName.TerritoryRadius, 150.0f); CurrentState = new ZombieWaitState(6.0f); }
public override void Enter(SteeringBlender steering, Actor owner, IAgentStateManager agent) { ZombieSkillSet zss = owner.GetBehaviorThatImplementsType <ZombieSkillSet>(); //zss.RangedSkill.CurrentOperation = WeaponFunctions.Neutral; BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); zss.RangedSkill.UpdateInputState(false, bcc); zss.MeleeSkill.UpdateInputState(false, bcc); int opponentId = agent.GetProperty <int>(AgentPropertyName.ActiveOpponent); Actor opponent = GameResources.ActorManager.GetActorById(opponentId); BipedControllerComponent opponentBcc = opponent.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); BepuVec3 towardOpponent = opponentBcc.Controller.Body.Position - bcc.Controller.Body.Position; towardOpponent.Y = 0.0f; towardOpponent.Normalize(); const float MOVE_SECS_MAX = 2.5f; const float MOVE_SECS_MIN = 0.5f; float moveRand = SlagMath.Get0To1UpperFloat(GameResources.ActorManager.Random); switch (mMovementType) { case MovementType.Lateral: BepuVec3 right = BepuVec3.Cross(towardOpponent, BepuVec3.Up); // Make the random symmetrical about 0.5f so that we can divide it into two equal segments for right and left. moveRand = Math.Abs(moveRand - 0.5f); mDurationTicks = (long)(TimeSpan.TicksPerSecond * (moveRand * 2.0f * (MOVE_SECS_MAX - MOVE_SECS_MIN) + MOVE_SECS_MIN)); steering.Target = BepuConverter.Convert(bcc.Controller.Body.Position + (moveRand > 0.5f ? right : -right) * 100.0f); break; case MovementType.Close: steering.Target = BepuConverter.Convert(opponentBcc.Controller.Body.Position + towardOpponent * 100.0f); mDurationTicks = (long)(TimeSpan.TicksPerSecond * (moveRand * (MOVE_SECS_MAX - MOVE_SECS_MIN) + MOVE_SECS_MIN)); break; case MovementType.Retreat: steering.Target = BepuConverter.Convert(opponentBcc.Controller.Body.Position - towardOpponent * 100.0f); mDurationTicks = (long)(TimeSpan.TicksPerSecond * (moveRand * (MOVE_SECS_MAX - MOVE_SECS_MIN) + MOVE_SECS_MIN)); break; default: steering.Target = BepuConverter.Convert(towardOpponent * 100.0f); mDurationTicks = 0; break; } steering.Weights[(int)SteeringBlender.WeightType.Arrive] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Seek] = 0.67f; steering.Weights[(int)SteeringBlender.WeightType.Wander] = 0.33f; steering.Weights[(int)SteeringBlender.WeightType.Wait] = 0.0f; steering.Urgency = 1.0f; steering.ForceScale = 1.0f; }
private void ComponentsCreatedHandler(object sender, EventArgs e) { mBipedControl = Owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); if (mBipedControl == null) { throw new LevelManifestException("Expected ActorComponent missing."); } mBipedControl.StateChanged += ControllerStateChangedHandler; }
// This was my original attempt at seek, but it's not sure of what it needs to do. There are some interesting // computations in here that I might want to use later though. private Vector3 AbandonedSeek(Actor owner, Vector3 target, Plane surface) { BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); // The location of the feet is the most reliable way to get current position in a nav surface Vector3 bipedFeetPosition = BepuConverter.Convert(bcc.Controller.Body.Position) - BepuConverter.Convert(bcc.Controller.Down) * bcc.Controller.SupportFinder.RayLengthToBottom; Vector3 relativeTarget = target - bipedFeetPosition; // Project the target onto the surface plane. Vector3 surfaceProjectedRelativeTarget = relativeTarget - surface.Normal * Vector3.Dot(relativeTarget, surface.Normal); // Unlikely that we're spot on, but we must avoid divide by zero on Normalize. if (surfaceProjectedRelativeTarget.LengthSquared() == 0.0f) { return(Vector3.Zero); } surfaceProjectedRelativeTarget.Normalize(); // Difference between actual and ideal velocity. This will be the direction of our impulse with a few exceptions. Vector3 velocityDiff = surfaceProjectedRelativeTarget * bcc.RunSpeed - BepuConverter.Convert(bcc.Controller.Body.LinearVelocity); // Exception 1: We don't want to remove excess velocity that's in the target's direction. float deceleration = Vector3.Dot(velocityDiff, surfaceProjectedRelativeTarget); if (deceleration < 0) { velocityDiff -= surfaceProjectedRelativeTarget * deceleration; } // Normalize velocityDiff in terms of max speed. velocityDiff /= bcc.RunSpeed; float lengthSq = velocityDiff.LengthSquared(); if (lengthSq < 1.0f) { // Exception 2 : Use up the extra impulse capacity to push against friction. Calculating the amount to add // so that the resulting length is one is a bit tricky: float fwdComp = Vector3.Dot(velocityDiff, surfaceProjectedRelativeTarget); float latComp = (velocityDiff - surfaceProjectedRelativeTarget * fwdComp).Length(); float makeupLength = (float)(Math.Sqrt(1.0f - latComp * latComp)) - fwdComp; velocityDiff += makeupLength * surfaceProjectedRelativeTarget; } else if (lengthSq > 1.0f) { // Clamp velocityDiff.Normalize(); } return(velocityDiff); }
public void UpdateInputState(bool inputState, BipedControllerComponent bipedController) { if (inputState) { bipedController.DesiredMovementActions |= BipedControllerComponent.MovementActions.Boosting; } else { bipedController.DesiredMovementActions &= ~BipedControllerComponent.MovementActions.Boosting; } }
public TPBipedPaletteInputProcessor(PlayerIndex inputIndex) : base(inputIndex) { mBipedControl = null; mSkillPalette = null; mSkillActions = new BinaryControlActions[6]; mSkillActions[0] = BinaryControlActions.Skill1; mSkillActions[1] = BinaryControlActions.Skill2; mSkillActions[2] = BinaryControlActions.Skill3; mSkillActions[3] = BinaryControlActions.Skill4; mSkillActions[4] = BinaryControlActions.Skill5; mSkillActions[5] = BinaryControlActions.Skill6; }
public override void Enter(SteeringBlender steering, Actor owner, IAgentStateManager agent) { float theta = SlagMath.Get0To1UpperFloat(GameResources.ActorManager.Random) * MathHelper.TwoPi; BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); steering.Weights[(int)SteeringBlender.WeightType.Arrive] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Seek] = 1.0f; steering.Weights[(int)SteeringBlender.WeightType.Wander] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Wait] = 0.0f; steering.Urgency = 1.0f; steering.Target = BepuConverter.Convert(bcc.Controller.Body.Position) + new Vector3((float)(Math.Sin(theta)), 0.0f, (float)(Math.Cos(theta))); steering.ForceScale = Single.Epsilon; }
public override void Enter(/* inout */ SteeringBlender steering, Actor owner, IAgentStateManager agent) { // We just want to change our facing to track our foe. steering.Weights[(int)SteeringBlender.WeightType.Arrive] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Seek] = 1.0f; steering.Weights[(int)SteeringBlender.WeightType.Wander] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Wait] = 0.0f; steering.Urgency = 1.0f; steering.ForceScale = 0.0001f; // Temporarily set the target directly ahead. BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); steering.Target = BepuConverter.Convert(bcc.Controller.Body.Position + bcc.Controller.HorizontalViewDirection); }
public override void Enter(SteeringBlender steering, Actor owner, IAgentStateManager agent) { steering.Weights[(int)SteeringBlender.WeightType.Arrive] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Seek] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Wander] = 0.0f; steering.Weights[(int)SteeringBlender.WeightType.Wait] = 1.0f; steering.Urgency = 1.0f; ZombieSkillSet zss = owner.GetBehaviorThatImplementsType <ZombieSkillSet>(); //zss.RangedSkill.CurrentOperation = WeaponFunctions.Neutral; BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); zss.RangedSkill.UpdateInputState(false, bcc); zss.MeleeSkill.UpdateInputState(false, bcc); }
public override void Fire() { const float ATTACK_RADIUS = 3.0f; const float ATTACK_LENGTH = 4.0f; // Play 'thwack' sound Actor owner = GameResources.ActorManager.GetActorById(OwnerActorId); BipedControllerComponent bipedControl = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); RigidTransform alignCapsule = new RigidTransform(BepuVec3.Forward * ATTACK_LENGTH * 0.5f + BepuConverter.Convert(MuzzleOffset), BepuQuaternion.CreateFromAxisAngle(BepuVec3.Right, MathHelper.PiOver2)); Vector3 aim = (bipedControl.WorldAim.HasValue ? bipedControl.WorldAim.Value : BepuConverter.Convert(bipedControl.Controller.ViewDirection)); RigidTransform positionAndAim = new RigidTransform(bipedControl.Controller.Body.Position, BepuConverter.Convert( SpaceUtils.GetOrientation(aim, Vector3.Up))); RigidTransform attackTransform; RigidTransform.Transform(ref alignCapsule, ref positionAndAim, out attackTransform); ConvexShape bashShape = new CapsuleShape(ATTACK_LENGTH, ATTACK_RADIUS); BepuVec3 noSweep = BepuVec3.Zero; List <RayCastResult> dudesBashed = new List <RayCastResult>(); AttackFilter filter = new AttackFilter(GameResources.ActorManager.IsMob(OwnerActorId)); GameResources.ActorManager.SimSpace.ConvexCast(bashShape, ref attackTransform, ref noSweep, filter.Test, dudesBashed); foreach (RayCastResult dude in dudesBashed) { EntityCollidable otherEntityCollidable = dude.HitObject as EntityCollidable; Terrain otherTerrain = dude.HitObject as Terrain; if (otherEntityCollidable != null && otherEntityCollidable.Entity != null && otherEntityCollidable.Entity.Tag != null) { Actor actorHit = GameResources.ActorManager.GetActorById((int)(otherEntityCollidable.Entity.Tag)); IDamagable damage = actorHit.GetBehaviorThatImplementsType <IDamagable>(); if (damage != null) { damage.TakeDamage(Damage); // TODO: P2: Query hit actor for appropiate damage effect e.g. blood and create it; } BashDust(dude.HitData.Location); } else if (otherTerrain != null) { BashDust(dude.HitData.Location); } } }
public override void Fire() { // Play 'pew' sound Actor bolt = GameResources.ActorManager.SpawnTemplate(ProjectileTemplateName); Actor owner = GameResources.ActorManager.GetActorById(OwnerActorId); BipedControllerComponent bipedControl = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); TransformComponent boltXform = bolt.GetComponent <TransformComponent>(ActorComponent.ComponentType.Transform); Vector3 aim = (bipedControl.WorldAim.HasValue ? bipedControl.WorldAim.Value : BepuConverter.Convert(bipedControl.Controller.ViewDirection)); boltXform.Transform = Matrix.CreateTranslation(MuzzleOffset) * Matrix.CreateWorld(BepuConverter.Convert( bipedControl.Controller.Body.Position), aim, Vector3.Up); EnergyProjectile boltProj = bolt.GetBehavior <EnergyProjectile>(); boltProj.Propel(OwnerActorId); }
protected override void ComponentsCreatedHandler(object sender, EventArgs e) { BipedControllerComponent bipedController = Owner.GetComponent <BipedControllerComponent>(ComponentType.Control); if (bipedController == null) { throw new LevelManifestException("BipedCollisionComponents expect to be accompanied by BipedControllerComponents."); } mSimController = bipedController.Controller; mSimController.StanceManager.StandingHeight = mHeight; mSimController.StanceManager.CrouchingHeight = mHeight * 0.5f; mSimController.BodyRadius = mRadius; mSimController.Body.Mass = mMass; base.ComponentsCreatedHandler(sender, e); mSimController.ViewDirection = BepuConverter.Convert(Vector3.Transform(Vector3.Forward, mTransformComponent.Orientation)); }
// Get to a target ASAP. private Vector2 Seek(Actor owner) { BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); Vector3 relativeTarget = Target - BepuConverter.Convert(bcc.Controller.Body.Position); if (relativeTarget.X == 0.0f && relativeTarget.Z == 0.0f) { return(Vector2.Zero); } // Rotate into controller space. Matrix controllerRotation = Matrix.CreateLookAt(Vector3.Zero, BepuConverter.Convert(bcc.Controller.HorizontalViewDirection), Vector3.Up); relativeTarget = Vector3.Transform(relativeTarget, controllerRotation); // Project the target onto the horizontal plane and normalize (via simple trig since we know Y == 0.0f). // Take theta = 0.0f is facing forward (-Z) double theta = Math.Atan2(-relativeTarget.X, -relativeTarget.Z); return(new Vector2(-(float)(Math.Sin(theta)), (float)(Math.Cos(theta)))); }
// For non-vision memory input. public void SenseFoe(int actorId) { AggroRecord aggro; if (!mFoes.ContainsKey(actorId)) { aggro = new AggroRecord(); aggro.Enmity = AggroRecord.NEW_FOE_ENMITY; mFoes.Add(actorId, aggro); } else { aggro = mFoes[actorId]; } Actor foe = GameResources.ActorManager.GetActorById(actorId); BipedControllerComponent bcc = foe.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); aggro.PositionLastSensed = BepuConverter.Convert(bcc.Controller.Body.Position); aggro.TimeLastSensed = GameResources.ActorManager.CurrentTime; }
public override void Fire() { // Play 'pop' sound Actor owner = GameResources.ActorManager.GetActorById(OwnerActorId); WeaponResource wr = owner.GetBehavior <WeaponResource>(); wr.Value -= ResourceCostToUse; BipedControllerComponent bipedControl = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); Vector3 aim = (bipedControl.WorldAim.HasValue ? bipedControl.WorldAim.Value : BepuConverter.Convert(bipedControl.Controller.ViewDirection)); Matrix muzzleTransform = Matrix.CreateTranslation(MuzzleOffset) * Matrix.CreateWorld(BepuConverter.Convert( bipedControl.Controller.Body.Position), aim, Vector3.Up); BepuRay shootRay = new BepuRay(BepuConverter.Convert(muzzleTransform.Translation), BepuConverter.Convert(muzzleTransform.Forward)); RayCastResult result; GameResources.ActorManager.SimSpace.RayCast(shootRay, 500.0f, out result); EntityCollidable otherEntityCollidable = result.HitObject as EntityCollidable; Terrain otherTerrain = result.HitObject as Terrain; if (otherEntityCollidable != null && otherEntityCollidable.Entity != null && otherEntityCollidable.Entity.Tag != null) { Actor actorHit = GameResources.ActorManager.GetActorById((int)(otherEntityCollidable.Entity.Tag)); IDamagable damage = actorHit.GetBehaviorThatImplementsType <IDamagable>(); if (damage != null) { damage.TakeDamage(Damage); // TODO: P2: Query hit actor for appropiate damage effect e.g. blood and create it; } HitSparks(result.HitData.Location); } else if (otherTerrain != null) { HitSparks(result.HitData.Location); } }
public override void AssignAvatar(int actorId) { base.AssignAvatar(actorId); PlayerIndex inputIndex = GameResources.PlaySession.LocalPlayers[PlayerId]; // Biped Actor avatar = GameResources.ActorManager.GetActorById(actorId); BipedControllerComponent bipedControl = avatar.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); TPBipedFixedControlsInputProcessor bipedProc; if (mInputProcs.ContainsKey(typeof(TPBipedFixedControlsInputProcessor))) { bipedProc = (TPBipedFixedControlsInputProcessor)(mInputProcs[typeof(TPBipedFixedControlsInputProcessor)]); } else { bipedProc = new TPBipedFixedControlsInputProcessor(inputIndex, DrawSegment.MainWindow.Camera); mInputProcs.Add(typeof(TPBipedFixedControlsInputProcessor), bipedProc); } bipedProc.ActiveInputMap = mPlayerInputMap; bipedProc.SetControllerComponent(bipedControl); }
public TPAimingBipedMovementInputProcessor(PlayerIndex inputIndex) : base(inputIndex) { mBipedControl = null; }
private void PreAnimationUpdateHandler(object sender, UpdateStepEventArgs e) { float elapsedTime = (float)(e.GameTime.ElapsedGameTime.TotalSeconds); Actor avatar = GameResources.ActorManager.GetActorById(ActorId); BipedControllerComponent bipedControl = avatar.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); // Update the camera. Vector3 desiredCameraPosition; if (mInputMode == InputMode.Aloof) { Matrix cameraRotation = Matrix.CreateFromYawPitchRoll(mMmoCameraDesc.Yaw, mMmoCameraDesc.Pitch, 0.0f); BepuRay boomRay = new BepuRay(mAvatarBepuEntity.Position, BepuConverter.Convert(cameraRotation.Backward)); RayCastResult result; GameResources.ActorManager.SimSpace.RayCast(boomRay, mMmoCameraDesc.Distance, CameraClipFilter, out result); desiredCameraPosition = result.HitObject != null? BepuConverter.Convert(BEPUutilities.Vector3.Lerp(result.HitData.Location, mAvatarBepuEntity.Position, 0.05f)) : BepuConverter.Convert(mAvatarBepuEntity.Position) + mMmoCameraDesc.Distance * cameraRotation.Backward; } else if (mInputMode == InputMode.Aiming) { Matrix viewRotation = Matrix.CreateWorld(Vector3.Zero, BepuConverter.Convert( bipedControl.Controller.ViewDirection), Vector3.Up); desiredCameraPosition = BepuConverter.Convert(mAvatarBepuEntity.Position) + Vector3.Transform( mAimingCameraOffset, viewRotation); } else { desiredCameraPosition = mCamera.Transform.Translation; } Vector3 newCameraPosition = desiredCameraPosition; Vector3 desiredCameraDirection; if (mInputMode == InputMode.Aloof) { desiredCameraDirection = BepuConverter.Convert(mAvatarBepuEntity.Position) - newCameraPosition; } else if (mInputMode == InputMode.Aiming) { desiredCameraDirection = BepuConverter.Convert(bipedControl.Controller.ViewDirection); } else { desiredCameraDirection = mCamera.Transform.Forward; } desiredCameraDirection.Normalize(); Vector3 newCameraDirection = desiredCameraDirection; if (mCameraSmoothingEngaged) { Vector3 positionDelta = desiredCameraPosition - mCamera.Transform.Translation; Quaternion directionDelta = SpaceUtils.GetSweptQuaternion(mCamera.Transform.Forward, desiredCameraDirection); const float POSITION_DELTA_THRESHHOLD = 4.0f; const float DIRECTION_DELTA_THRESHHOLD = MathHelper.Pi / 16.0f; float positionDeltaLength = positionDelta.Length(); float directionDeltaAngle = (float)(SpaceUtils.GetQuaternionAngle(directionDelta)); float fractionComplete = Math.Min(POSITION_DELTA_THRESHHOLD / positionDeltaLength, DIRECTION_DELTA_THRESHHOLD / directionDeltaAngle); if (fractionComplete < 1.0f) { newCameraPosition = Vector3.Lerp(mCamera.Transform.Translation, desiredCameraPosition, fractionComplete); Quaternion smoothedCamRotation = Quaternion.Slerp(Quaternion.Identity, directionDelta, fractionComplete); newCameraDirection = Vector3.Transform(mCamera.Transform.Forward, smoothedCamRotation); } } else { mCameraSmoothingEngaged = true; } mCamera.Transform = Matrix.CreateWorld(newCameraPosition, newCameraDirection, Vector3.Up); }
public override void AssignAvatar(int actorId) { base.AssignAvatar(actorId); PlayerIndex inputIndex = GameResources.PlaySession.LocalPlayers[PlayerId]; Actor avatar = GameResources.ActorManager.GetActorById(ActorId); BipedControllerComponent bipedControl = avatar.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); bipedControl.AimCheck = delegate() { return(mInputMode == InputMode.Aiming); }; // Begin InputProcessors configuration: // Biped, aloof mode: TPBipedFixedControlsInputProcessor bipedProc; if (mInputProcs[(int)(InputMode.Aloof)].ContainsKey(typeof(TPBipedFixedControlsInputProcessor))) { bipedProc = (TPBipedFixedControlsInputProcessor)(mInputProcs[(int)(InputMode.Aloof)][typeof(TPBipedFixedControlsInputProcessor)]); } else { bipedProc = new TPBipedFixedControlsInputProcessor(inputIndex, mCamera); mInputProcs[(int)(InputMode.Aloof)].Add(typeof(TPBipedFixedControlsInputProcessor), bipedProc); } bipedProc.ActiveInputMap = mPlayerInputMap; bipedProc.SetControllerComponent(bipedControl); // Biped, aiming mode: TPAimingBipedMovementInputProcessor aimingBipedProc; if (mInputProcs[(int)(InputMode.Aiming)].ContainsKey(typeof(TPAimingBipedMovementInputProcessor))) { aimingBipedProc = (TPAimingBipedMovementInputProcessor)(mInputProcs[(int)(InputMode.Aiming)][typeof(TPAimingBipedMovementInputProcessor)]); } else { aimingBipedProc = new TPAimingBipedMovementInputProcessor(inputIndex); mInputProcs[(int)(InputMode.Aiming)].Add(typeof(TPAimingBipedMovementInputProcessor), aimingBipedProc); } aimingBipedProc.ActiveInputMap = mPlayerInputMap; aimingBipedProc.SetControllerComponent(bipedControl); // Skill palette, shared between aiming and aloof modes: TPBipedPaletteInputProcessor paletteProc; if (mInputProcs[(int)(InputMode.Aloof)].ContainsKey(typeof(TPBipedPaletteInputProcessor))) { paletteProc = (TPBipedPaletteInputProcessor)(mInputProcs[(int)(InputMode.Aloof)][typeof(TPBipedPaletteInputProcessor)]); } else { paletteProc = new TPBipedPaletteInputProcessor(inputIndex); mInputProcs[(int)(InputMode.Aloof)].Add(typeof(TPBipedPaletteInputProcessor), paletteProc); } if (!(mInputProcs[(int)(InputMode.Aiming)].ContainsKey(typeof(TPBipedPaletteInputProcessor)))) { mInputProcs[(int)(InputMode.Aiming)].Add(typeof(TPBipedPaletteInputProcessor), paletteProc); } paletteProc.ActiveInputMap = mPlayerInputMap; paletteProc.SetControllerComponent(bipedControl); BipedSkillPalette skillPalette = avatar.GetBehaviorThatImplementsType <BipedSkillPalette>(); paletteProc.SetSkillPalette(skillPalette); // Camera, aloof mode: TPCameraInputProcessor camera; if (mInputProcs[(int)(InputMode.Aloof)].ContainsKey(typeof(TPCameraInputProcessor))) { camera = (TPCameraInputProcessor)(mInputProcs[(int)(InputMode.Aloof)][typeof(TPCameraInputProcessor)]); } else { camera = new TPCameraInputProcessor(inputIndex, mMmoCameraDesc); mInputProcs[(int)(InputMode.Aloof)].Add(typeof(TPCameraInputProcessor), camera); } camera.ActiveInputMap = mPlayerInputMap; // Done with InputProcessor configuration. // Reset camera placement: mMmoCameraDesc.Distance = 38.0f; mMmoCameraDesc.Pitch = -MathHelper.Pi / 6.0f; // Positive pitch will move the camera -Y since it's on the +Z side mMmoCameraDesc.Yaw = 0.0f; DynamicCollisionComponent dcc = avatar.GetComponent <DynamicCollisionComponent>(ActorComponent.ComponentType.Physics); mAvatarBepuEntity = dcc.Entity; }
protected override void PrepareInputProcessors(InputManager input) { PlayerIndex inputIndex = GameResources.PlaySession.LocalPlayers[PlayerId]; InputMode newMode = mInputMode; // TODO: P3: This needs a real state system to manage transitions: // Compute next mode: if (mInputMode == InputMode.Aiming && !(input.CheckForBinaryInput(mPlayerInputMap, BinaryControlActions.Aim, inputIndex))) { newMode = InputMode.Aloof; } else if (mInputMode == InputMode.Aloof && input.CheckForBinaryInput(mPlayerInputMap, BinaryControlActions.Aim, inputIndex)) { newMode = InputMode.Aiming; } // Take some special actions if there is a mode change: if (newMode != mInputMode) { // Exiting mode tasks: switch (mInputMode) { case InputMode.Aiming: Actor avatar = GameResources.ActorManager.GetActorById(ActorId); BipedControllerComponent bipedControl = avatar.GetComponent <BipedControllerComponent>( ActorComponent.ComponentType.Control); bipedControl.Controller.ViewDirection = bipedControl.Controller.HorizontalViewDirection; mMmoCameraDesc.Pitch = -MathHelper.Pi / 12.0f; mMmoCameraDesc.Yaw = (float)(Math.Atan2(-bipedControl.Controller.HorizontalViewDirection.X, -bipedControl.Controller.HorizontalViewDirection.Z)); DrawSegment.MainWindow.UIElements.Remove(UIElementDepth.CROSSHAIRS); break; default: break; } // Entering mode tasks: switch (newMode) { case InputMode.Aiming: DrawSegment.MainWindow.UIElements.Add(UIElementDepth.CROSSHAIRS, mCrosshairs); break; default: break; } } mInputMode = newMode; // Handle some misc input functions: // Inventory window: if (input.CheckForNewBinaryInput(mPlayerInputMap, BinaryControlActions.OpenInventory, inputIndex)) { DrawSegment.AddWindow(mInventoryPanel, inputIndex); mInventoryPanel.RefreshItems(); } // TODO: P2: Remove nonessential/debug functionality: PlayerIndex dummyPlayerIndex; if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.V, inputIndex, out dummyPlayerIndex)) { mShadowViewMode = !mShadowViewMode; DrawSegment.MainWindow.UIElements.Clear(); if (mShadowViewMode) { DrawSegment.MainWindow.UIElements.Add(UIElementDepth.DEBUG_SHADOW_MAP, new ShadowMapVisual()); } else { DrawSegment.MainWindow.UIElements.Add(UIElementDepth.STANDARD_3D_PERSP, new Standard3dPerspective()); } } if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.P, inputIndex, out dummyPlayerIndex)) { Debugger.Break(); } if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.U, inputIndex, out dummyPlayerIndex)) { mBright += 0.01f; } if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.J, inputIndex, out dummyPlayerIndex)) { mBright -= 0.01f; } if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.I, inputIndex, out dummyPlayerIndex)) { mContrast += 0.1f; } if (input.IsNewKeyPress(Microsoft.Xna.Framework.Input.Keys.K, inputIndex, out dummyPlayerIndex)) { mContrast -= 0.1f; } //mBrightParam.SetValue(mBright); //mContrastParam.SetValue(mContrast); }
public MmoBipedInputProcessor(PlayerIndex inputIndex, MmoCameraDesc cameraDesc) : base(inputIndex) { mBipedControl = null; mCameraDesc = cameraDesc; }
public void SetControllerComponent(BipedControllerComponent bipedController) { mBipedControl = bipedController; mBipedControl.Owner.ActorDespawning += ActorDespawningHandler; }
public ControllerStateChangedEventArgs(BipedControllerComponent.ControllerState newState, BipedControllerComponent.ControllerState oldState) : base() { NewState = newState; OldState = oldState; }
public override void Update(/* inout */ SteeringBlender steering, Actor owner, IAgentStateManager agent) { if (!agent.HasProperty(AgentPropertyName.ActiveOpponent)) { ZombieWaitState zws = new ZombieWaitState(6.0f); agent.CurrentState = zws; return; } Actor opponent = GameResources.ActorManager.GetActorById(agent.GetProperty <int>(AgentPropertyName.ActiveOpponent)); BipedControllerComponent opponentBcc = opponent.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); steering.Target = BepuConverter.Convert(opponentBcc.Controller.Body.Position); BipedControllerComponent bcc = owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); BepuVec3 toOpponent = opponentBcc.Controller.Body.Position - bcc.Controller.Body.Position; float distance = toOpponent.Length(); ZombieSkillSet zss = owner.GetBehaviorThatImplementsType <ZombieSkillSet>(); BipedWeapon chosenAttack = distance <= zss.MeleeSkill.EffectiveRangeMax ? zss.MeleeSkill : zss.RangedSkill; Matrix attackTransform = Matrix.CreateTranslation(chosenAttack.MuzzleOffset) * Matrix.CreateWorld( BepuConverter.Convert(bcc.Controller.Body.Position), BepuConverter.Convert(bcc.Controller.ViewDirection), Vector3.Up); BepuVec3 bulletPath = opponentBcc.Controller.Body.Position - BepuConverter.Convert(attackTransform.Translation); // If we don't have a shot, we need to specify what kind of movement we need to remedy that. ZombieTacticalMovementState.MovementType movement = ZombieTacticalMovementState.MovementType.None; if (distance < chosenAttack.EffectiveRangeMin) { movement = ZombieTacticalMovementState.MovementType.Retreat; } else if (distance > chosenAttack.EffectiveRangeMax) { movement = ZombieTacticalMovementState.MovementType.Close; } else { BepuRay loeRay = new BepuRay(BepuConverter.Convert(attackTransform.Translation), bulletPath); LOSFilter filter = new LOSFilter(bcc.Controller.Body.CollisionInformation, opponentBcc.Controller.Body.CollisionInformation); RayCastResult loeResult; GameResources.ActorManager.SimSpace.RayCast(loeRay, chosenAttack.EffectiveRangeMax * 1.5f, filter.Test, out loeResult); EntityCollidable otherEntityCollidable = loeResult.HitObject as EntityCollidable; if (otherEntityCollidable != null && otherEntityCollidable.Entity != null && otherEntityCollidable.Entity.Tag != null && (int)(otherEntityCollidable.Entity.Tag) == opponent.Id) { toOpponent.Y = 0.0f; toOpponent.Normalize(); float aimTheta = (float)(Math.Acos(MathHelper.Clamp(BepuVec3.Dot(toOpponent, bcc.Controller.ViewDirection), 0.0f, 1.0f))); const float AIM_CONE_RADIANS = MathHelper.Pi / 12.0f; if (aimTheta <= AIM_CONE_RADIANS) { // TODO: P2: Add some wander to this value: bcc.WorldAim = BepuConverter.Convert(bulletPath); //chosenAttack.CurrentOperation = WeaponFunctions.TriggerPulled; BipedWeapon otherAttack = chosenAttack == zss.MeleeSkill ? zss.RangedSkill : zss.MeleeSkill; otherAttack.UpdateInputState(false, bcc); chosenAttack.UpdateInputState(true, bcc); return; } } else { movement = ZombieTacticalMovementState.MovementType.Lateral; } } if (movement != ZombieTacticalMovementState.MovementType.None) { ZombieTacticalMovementState ztms = new ZombieTacticalMovementState(movement); agent.CurrentState = ztms; } }
private void ProcessAIStepHandler(object sender, UpdateStepEventArgs e) { // Check FOV, add any new foes to memory. And update existing ones. We may also have gained new memories by other means. // Get players and mobs in field of vision: List <RayCastResult> actorsInView = new List <RayCastResult>(); ConeShape visionCone = new ConeShape(VisionDistance, VisionDistance); BipedControllerComponent bcc = Owner.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); RigidTransform tipOverCone = new RigidTransform(BepuVec3.Forward * VisionDistance * 0.75f, BepuQuaternion.CreateFromAxisAngle(BepuVec3.Right, MathHelper.PiOver2)); RigidTransform eyeLevelAndFacing = new RigidTransform(bcc.Controller.Body.Position - bcc.Controller.Down * bcc.Controller.Body.Height * 0.45f, BepuConverter.Convert(SpaceUtils.GetOrientation(BepuConverter.Convert(bcc.Controller.ViewDirection), Vector3.Up))); RigidTransform visionConeTransform; RigidTransform.Transform(ref tipOverCone, ref eyeLevelAndFacing, out visionConeTransform); BepuVec3 noSweep = BepuVec3.Zero; ViewInterestFilter filter = new ViewInterestFilter(bcc.Controller.Body.CollisionInformation); GameResources.ActorManager.SimSpace.ConvexCast(visionCone, ref visionConeTransform, ref noSweep, filter.Test, actorsInView); for (int a = 0; a < actorsInView.Count; ++a) { // Does this actor warrant an addition to be made to our memory? // If so, check for LOS and recheck range. If those tests pass, modify the memory. EntityCollidable otherEntityCollidable = actorsInView[a].HitObject as EntityCollidable; // We can jump to the Id in the Tag property because we know the filter has validated this. int actorId = (int)(otherEntityCollidable.Entity.Tag); Actor viewedActor = GameResources.ActorManager.GetActorById(actorId); BipedControllerComponent viewedActorBcc = viewedActor.GetComponent <BipedControllerComponent>(ActorComponent.ComponentType.Control); BepuVec3 toSubject = viewedActorBcc.Controller.Body.Position - eyeLevelAndFacing.Position; // Check range: if (toSubject.LengthSquared() <= VisionDistance * VisionDistance) { BepuRay losRay = new BepuRay(eyeLevelAndFacing.Position, toSubject); RayCastResult losResult; LOSFilter losFilter = new LOSFilter(bcc.Controller.Body.CollisionInformation, otherEntityCollidable); GameResources.ActorManager.SimSpace.RayCast(losRay, VisionDistance, losFilter.Test, out losResult); EntityCollidable losEC = losResult.HitObject as EntityCollidable; // Test for LOS: if (losEC != null && losEC.Entity != null && losEC.Entity.Tag != null && (int)(losEC.Entity.Tag) == actorId) { // The viewed actor is either a player(foe) or a mob(ally). if (GameResources.ActorManager.IsPlayer(actorId)) { mMemory.SpotFoe(actorId); } else { IAgentStateManager agent = viewedActor.GetBehaviorThatImplementsType <IAgentStateManager>(); if (agent != null && agent.HasProperty(AgentPropertyName.ActiveOpponent)) { int mobFoe = agent.GetProperty <int>(AgentPropertyName.ActiveOpponent); mMemory.SenseFoe(mobFoe); } } } } } // Evaluate current threats and select one to engage: int enemyId = mMemory.GetLargestThreat(); if (enemyId != Actor.INVALID_ACTOR_ID) { if (mAgentProperties.ContainsKey(AgentPropertyName.ActiveOpponent)) { if ((int)(mAgentProperties[AgentPropertyName.ActiveOpponent]) != enemyId) { mAgentProperties[AgentPropertyName.ActiveOpponent] = enemyId; } } else { mAgentProperties.Add(AgentPropertyName.ActiveOpponent, enemyId); } } else { if (mAgentProperties.ContainsKey(AgentPropertyName.ActiveOpponent)) { mAgentProperties.Remove(AgentPropertyName.ActiveOpponent); } } TimeInState += e.GameTime.ElapsedGameTime; CurrentState.Update(mSteering, Owner, this); Vector2 locomotion = mSteering.ComputeForce(Owner); if (locomotion.LengthSquared() == 0.0f) { bcc.OrientationChange = Quaternion.Identity; bcc.HorizontalMovement = Vector2.Zero; } else { bcc.OrientationChange = Quaternion.CreateFromAxisAngle(Vector3.Up, (float)(Math.Atan2(-locomotion.X, locomotion.Y))); bcc.HorizontalMovement = locomotion.Length() * Vector2.UnitY; } mMemory.Fade(e.GameTime); }
public void SetControllerComponent(BipedControllerComponent bipedController) { mBipedControl = bipedController; if (mBipedControl != null && mSkillPalette == null) mBipedControl.Owner.ActorDespawning += ActorDespawningHandler; }
private void ActorDespawningHandler(object sender, EventArgs e) { mBipedControl = null; mSkillPalette = null; }
private void ActorDespawningHandler(object sender, EventArgs e) { mBipedControl = null; }
private void ComponentsCreatedHandler(object sender, EventArgs e) { mBipedControl = Owner.GetComponent<BipedControllerComponent>(ActorComponent.ComponentType.Control); if (mBipedControl == null) throw new LevelManifestException("Expected ActorComponent missing."); mBipedControl.StateChanged += ControllerStateChangedHandler; }
public TPBipedFixedControlsInputProcessor(PlayerIndex inputIndex, ICamera referenceCam) : base(inputIndex) { mReferenceCam = referenceCam; mBipedControl = null; }