Skip to content

Instantly share code, notes, and snippets.

@e-sarkis
Last active March 17, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-sarkis/7c818e8db0eba7da206d1267efff5645 to your computer and use it in GitHub Desktop.
Save e-sarkis/7c818e8db0eba7da206d1267efff5645 to your computer and use it in GitHub Desktop.
public class BombFish : MonoBehaviour
{
public enum State { Enter, Attack, Exit };
public State currentState;
private EnemyController EC;
public enum DropStrategy { DropOnPlayer, DropOnDestination, DropXCoordinate }
public DropStrategy Drop;
public float XTarget = 0f;
private Bomb ourBomb;
private Transform PlayerTransform;
void Awake ()
{
EC = GetComponent<EnemyController>();
ourBomb = GetComponentInChildren<Bomb>();
currentState = State.Enter;
PlayerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
switch (currentState)
{
case State.Enter:
EnterUpdate();
break;
case State.Attack:
AttackUpdate();
break;
case State.Exit:
ExitUpdate();
break;
default:
break;
}
}
private void ExitUpdate()
{
// Just keep swimming
}
private void AttackUpdate()
{
DropBomb();
currentState = State.Exit;
}
private void EnterUpdate()
{
switch (Drop)
{
case DropStrategy.DropOnPlayer:
if (Mathf.Abs(PlayerTransform.position.x - transform.position.x) < .5f)
{
currentState = State.Attack;
}
break;
case DropStrategy.DropXCoordinate:
if (Mathf.Abs(XTarget - transform.position.x) < .5f)
{
currentState = State.Attack;
}
break;
default:
// Drop Destination
if (EC.atDestinationX && EC.atDestinationY)
{
currentState = State.Attack;
}
break;
}
}
private void DropBomb()
{
ourBomb.ActivateBomb();
int xVector = 0;
if (transform.localScale.x < 0)
{
xVector = -1;
}
else
{
xVector = 1;
}
EC.SetMovementVector(xVector, 0);
currentState = State.Exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment