Instantly share code, notes, and snippets.

@e-sarkis /BombFish.cs Secret
Last active Mar 17, 2018

Embed
What would you like to do?
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