/// <summary> /// Decorates the base idle movement behaviour with other movement behaviours /// appropriate to the character and situation. /// </summary> public void GenerateMovementBehaviours() { movementBehaviour = new IdleMovementBehaviour(); GenerateFleeBehaviours(); GenerateSeekBehaviour(); movementBehaviour = new AvoidMovementBehaviour(movementBehaviour, gameObject, null, GameManager.GameSettings.Constants.Range.AvoidRadius); }
/// <summary> /// Generates movement behaviours to avoid all other characters. Avoid in this sense /// means they will not move on top of them. /// </summary> protected void GenerateFleeBehaviours() { var charactersToAvoid = GameManager.AllCharactersExcept(character); foreach (var character in charactersToAvoid) { movementBehaviour = new FleeMovementBehaviour( movementBehaviour, gameObject, character.gameObject, GameManager.GameSettings.Constants.Range.FleeRadius); } }
/// <summary> /// Base constructor for movement decorators. /// </summary> /// <param name="movementBehaviour">The movement behaviour to decorate.</param> /// <param name="agent">The GameObject that desires this movement behaviour.</param> /// <param name="target">The target of this movement behaviour.</param> public AbstractMovementDecorator(AbstractMovementBehaviour movementBehaviour, GameObject agent, GameObject target, float radius) { this.movementBehaviour = movementBehaviour; this.agent = agent; this.controller = agent.GetComponent <MovementController>(); this.maxSpeed = controller.MaxSpeed; this.maxAccel = GameManager.GameSettings.Constants.Character.Acceleration; this.target = target; this.radius = radius; }
// Update is called once per frame void FixedUpdate() { if (movementBehaviour == null) { movementBehaviour = new IdleMovementBehaviour(); movementBehaviour = new SeekMovementBehaviour(movementBehaviour, gameObject, seekTarget, seekRadius); movementBehaviour = new FleeMovementBehaviour(movementBehaviour, gameObject, avoidTarget, avoidRadius); movementBehaviour = new FleeMovementBehaviour(movementBehaviour, gameObject, avoidTarget2, avoidRadius); movementBehaviour = new FleeMovementBehaviour(movementBehaviour, gameObject, avoidTarget3, avoidRadius); } var newPosition = movementBehaviour.Steering().normalized *controller.MaxSpeed; transform.Translate(newPosition); }
/// <summary> /// Generates movement behaviour to seek the current target. /// </summary> protected virtual void GenerateSeekBehaviour() { try { if (character.CombatController.TargetController != null) { var squareDistance = transform.position.SqrDistance(character.CombatController.TargetController.transform.position); if (squareDistance > SeekTargetDistanceSquared) { var targetLocation = character.CombatController.TargetController.transform.position; var location = targetLocation + ((transform.position - targetLocation).normalized * SeekTargetDistance); movementBehaviour = new WalkMovementBehaviour( movementBehaviour, gameObject, location, SeekTargetDistance); } } else if (character.CombatController.TargetController == null) { // Have allies walk back to hero when they don't have a target. if (character.CharacterType == CharacterType.Ally) //TODO: Make this more generic. { var squareDistance = transform.position.SqrDistance(GameManager.Hero.Location); if (squareDistance > SeekTargetDistanceSquared) { var location = GameManager.Hero.Location + ((transform.position - GameManager.Hero.Location).normalized * SeekTargetDistance); movementBehaviour = new WalkMovementBehaviour( movementBehaviour, gameObject, location, SeekTargetDistance); } } } } catch (NullReferenceException) { character = GetComponent <GameCharacterController>(); } }
/// <summary> /// Constructor for AvoidMovementBehaviour instances. /// </summary> /// <param name="movementBehaviour">The movement behaviour to decorate.</param> /// <param name="agent">The GameObject that desires this movement behaviour.</param> /// <param name="target">The target of this movement behaviour.</param> /// <param name="radius">The radius at which this behaviour is completed.</param> public AvoidMovementBehaviour(AbstractMovementBehaviour movementBehaviour, GameObject agent, GameObject target, float radius) : base(movementBehaviour, agent, target, radius) { }
/// <summary> /// Constructor for WanderMovementBehaviour instances. /// </summary> /// <param name="movementBehaviour">The movement behaviour to decorate.</param> /// <param name="agent">The GameObject that desires this movement behaviour.</param> /// <param name="location">The original location from which the agent will wander.</param> /// <param name="radius">The radius at which this behaviour will wander.</param> public WanderMovementBehaviour(AbstractMovementBehaviour movementBehaviour, GameObject agent, Vector2 location, float radius) : base(movementBehaviour, agent, null, radius) { }