// set other Actor as target
        public void SetTarget(Actor2D otherActor, float targetDistance, bool newDetermination = false)
        {
            if (_determined == true && newDetermination == false)
            {
                return;
            }
            _determined = newDetermination;

            DisableTarget();

            if (otherActor == null)
            {
                return;
            }

            type                  = TargetType.Actor;
            _otherActor           = otherActor;
            _targetTransform      = otherActor.transform;
            targetReachedDistance = targetDistance;
            _isReached            = false;
            _determined           = newDetermination;

            _otherActor.stateChanged += Actor_StateChanged;

            _calculatePathAtNextPossibility = true;
        }
        public Actor2D GetNearestActor()
        {
            UpdateList();

            if (actors.Count == 0)
            {
                return(null);
            }

            Vector2 position      = _transform.position;
            Actor2D nearestActor  = actors[0];
            float   foundDistance = (position - nearestActor.position2D).sqrMagnitude;

            for (int i = 1; i < actors.Count; i++)
            {
                float newDistance = (position - actors[i].position2D).sqrMagnitude;
                if (newDistance < foundDistance)
                {
                    foundDistance = newDistance;
                    nearestActor  = actors[i];
                }
            }

            return(nearestActor);
        }
 void OnTriggerExit2D(Collider2D other)
 {
     if (other.tag == Tag)
     {
         Actor2D actor = other.GetComponent <Actor2D>();
         RemoveActor(actor);
     }
 }
		// ================================================================================
		//  unity methods
		// --------------------------------------------------------------------------------

		void Awake()
		{
			_transform = transform;

			_actor = GetComponent<Actor2D>();
			if (_actor != null)
				_actor.stateChanged += ActorStateChanged;
		}
        // ================================================================================
        //  constructor
        // --------------------------------------------------------------------------------

        public ActorTarget(Actor2D protagonist, Transform protagonistTransform)
        {
            _protagonist          = protagonist;
            _protagonistTransform = protagonistTransform;

            _ticker = Random.Range(0, _maxTicker);

            _path = new Vector2Path(80);
        }
		void Awake()
		{
			if (source == null)
				source = GetComponent<AudioSource>();

			_actor = GetComponent<Actor2D>();

			source.loop = true;
			source.Stop();
		}
        // ================================================================================
        //  unity methods
        // --------------------------------------------------------------------------------

        void Awake()
        {
            _transform = transform;

            _actor = GetComponent <Actor2D>();
            if (_actor != null)
            {
                _actor.stateChanged += ActorStateChanged;
            }
        }
Example #8
0
        void Awake()
        {
            if (source == null)
            {
                source = GetComponent <AudioSource>();
            }

            _actor = GetComponent <Actor2D>();

            source.loop = true;
            source.Stop();
        }
        // ================================================================================
        //  private methods
        // --------------------------------------------------------------------------------

        private void RemoveActor(Actor2D actor)
        {
            if (actor != null && actors.Contains(actor))
            {
                actors.Remove(actor);

                actor.stateChanged -= Actor_StateChanged;

                if (sensorEvent != null)
                {
                    sensorEvent(SensorEvent.ActorLeft, actor);
                }
            }
        }
Example #10
0
        public void TakeAction(Actor2D targetActor)
        {
            if (!isAlive)
            {
                return;
            }

            _actionTimer = new Timer(action.cooldown);
            isMoving     = false;

            // update look direction
            SetLookDirectionToTarget(targetActor.position2D);

            state = ActorState.TakingAction;

            action.Execute();
        }
Example #11
0
        // other actor as new target, e.g. enemy or friendly unit to be healed
        public void SetTarget(Actor2D otherActor, bool determined = false)
        {
            if (!isAlive)
            {
                return;
            }

            if (action != null)
            {
                target.SetTarget(otherActor, action.range * 0.9f, determined);
            }

            if (state == ActorState.Idle)
            {
                state = ActorState.Moving;
            }
        }
Example #12
0
        void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == Tag)
            {
                Actor2D actor = other.GetComponent <Actor2D>();
                if (actor != null && !actors.Contains(actor) && actor.isAlive)
                {
                    actors.Add(actor);

                    actor.stateChanged += Actor_StateChanged;

                    if (sensorEvent != null)
                    {
                        sensorEvent(SensorEvent.ActorDetected, actor);
                    }
                }
            }
        }
        public void DisableTarget()
        {
            type = TargetType.None;

            if (_otherActor != null)
            {
                _otherActor.stateChanged -= Actor_StateChanged;
                _otherActor = null;
            }
            _targetTransform = null;

            // path parameters
            _hasPath = false;
            _calculatePathAtNextPossibility = false;

            _isReached = true;

            _determined = false;
        }
		// ================================================================================
		//  Unity methods
		// --------------------------------------------------------------------------------

		private void Awake()
		{
			_actor = GetComponent<Actor2D>();
			if (_actor != null)
				_actor.stateChanged += ActorStateChangedHandler;

			if (displayObject != null)
				_animator = displayObject.GetComponentInChildren<Animator>();

			if (displayObject != null)
			{
				GatherRendererMaterials();
			}
			else
			{
				Debug.Log("Actor " + gameObject.name + " has AnimationController but no display object assigned");
			}

			_isInitialized = true;
		}
Example #15
0
        // ================================================================================
        //  Unity methods
        // --------------------------------------------------------------------------------

        private void Awake()
        {
            _actor = GetComponent <Actor2D>();
            if (_actor != null)
            {
                _actor.stateChanged += ActorStateChangedHandler;
            }

            if (displayObject != null)
            {
                _animator = displayObject.GetComponentInChildren <Animator>();
            }

            if (displayObject != null)
            {
                GatherRendererMaterials();
            }
            else
            {
                Debug.Log("Actor " + gameObject.name + " has AnimationController but no display object assigned");
            }

            _isInitialized = true;
        }
Example #16
0
        // ================================================================================
        //  public methods
        // --------------------------------------------------------------------------------

        public Actor2D GetMostWoundedActor()
        {
            UpdateList();

            if (actors.Count == 0)
            {
                return(null);
            }

            Actor2D targetActor = actors[0];
            float   woundAmount = targetActor.health.missingAmount;

            for (int i = 1; i < actors.Count; i++)
            {
                float newHealthAmount = actors[i].health.missingAmount;
                if (newHealthAmount > woundAmount)
                {
                    woundAmount = newHealthAmount;
                    targetActor = actors[i];
                }
            }

            return(targetActor);
        }
Example #17
0
		public void TakeAction(Actor2D targetActor)
		{
			if (!isAlive)
				return;

			_actionTimer = new Timer(action.cooldown);
			isMoving = false;

			// update look direction
			SetLookDirectionToTarget(targetActor.position2D);

			state = ActorState.TakingAction;

			action.Execute();
		}
Example #18
0
		// other actor as new target, e.g. enemy or friendly unit to be healed
		public void SetTarget(Actor2D otherActor, bool determined = false)
		{
			if (!isAlive)
				return;

			if (action != null)
			{
				target.SetTarget(otherActor, action.range * 0.9f, determined);
			}

			if (state == ActorState.Idle)
				state = ActorState.Moving;
		}
        public void DisableTarget()
        {
            type = TargetType.None;

            if (_otherActor != null)
            {
                _otherActor.stateChanged -= Actor_StateChanged;
                _otherActor = null;
            }
            _targetTransform = null;

            // path parameters
            _hasPath = false;
            _calculatePathAtNextPossibility = false;

            _isReached = true;

            _determined = false;
        }
        // set other Actor as target
        public void SetTarget(Actor2D otherActor, float targetDistance, bool newDetermination = false)
        {
            if (_determined == true && newDetermination == false)
            {
                return;
            }
            _determined = newDetermination;

            DisableTarget();

            if (otherActor == null)
                return;

            type = TargetType.Actor;
            _otherActor = otherActor;
            _targetTransform = otherActor.transform;
            targetReachedDistance = targetDistance;
            _isReached = false;
            _determined = newDetermination;

            _otherActor.stateChanged += Actor_StateChanged;

            _calculatePathAtNextPossibility = true;
        }
        // ================================================================================
        //  constructor
        // --------------------------------------------------------------------------------

        public ActorTarget(Actor2D protagonist, Transform protagonistTransform)
        {
            _protagonist = protagonist;
            _protagonistTransform = protagonistTransform;

            _ticker = Random.Range(0, _maxTicker);

            _path = new Vector2Path(80);
        }
        void Awake()
        {
            Actor2D actor = GetComponent <Actor2D>();

            actor.movementSpeed += Random.Range(1f, multiplier);
        }
Example #23
0
        void Start()
        {
            Actor2D actor = GetComponent <Actor2D>();

            actor.target.SetPathField(FindObjectOfType <LevelGrid>());
        }
Example #24
0
		// ================================================================================
		//  private methods
		// --------------------------------------------------------------------------------

		private void RemoveActor(Actor2D actor)
		{
			if (actor != null && actors.Contains(actor))
			{
				actors.Remove(actor);

				actor.stateChanged -= Actor_StateChanged;

				if (sensorEvent != null)
					sensorEvent(SensorEvent.ActorLeft, actor);
			}
		}