public void OnStoppedFollowing() { entity_toFollow = null; direction = Vector2.Zero; State = STATE_CmpAI_Follower.NoneToFollow; if (StoppedFollowing != null) // there is a hook { StoppedFollowing.Invoke(this.parent, new EntityArgs(this.parent, this)); } }
protected void OnDestinationReached() { destination_set = false; entity_toFollow = null; direction = Vector2.Zero; State = STATE_CmpAI_Follower.NoneToFollow; if (DestinationReached != null) { DestinationReached.Invoke(this, new EntityArgs(this.parent, this)); } }
public override void Update(float delta) { if (destination_set) { direction = (Vector2.Normalize(Vector2.Subtract(destination, parent.GetCenterPosition()))); pos_previous = parent.GetCenterPosition(); parent.pos += (direction * speed); if (GetDistanceFromDestination() < 1f) { OnDestinationReached(); } CheckIfStuck(destination); } else if (entity_toFollow != null) { float dist = GetDistance(); if (dist > distance_whenToSeparate) // if distance is big enough to separate { State = STATE_CmpAI_Follower.NoneToFollow; } else if (dist > distance_whenToFollow) // if distance is big enough to follow but not separate { direction = (Vector2.Normalize(Vector2.Subtract(entity_toFollow.GetCenterPosition(), parent.GetCenterPosition()))); parent.pos += (direction * speed); State = STATE_CmpAI_Follower.Following; } } else { // State check before invoke, to prevent invoking event every frame if (State != STATE_CmpAI_Follower.NoneToFollow) { OnStoppedFollowing(); } } }
public void RemoveLeader() { this.State = STATE_CmpAI_Follower.NoneToFollow; }
public void SetLeader(Entity entity) { entity_toFollow = entity; this.State = STATE_CmpAI_Follower.Following; }