void Reset() { if (face && transform.position.x > anchor.x) { face = false; } else if (!face && transform.position.x < anchor.x) { face = true; } Vector3 temp = transform.position; if (face) { temp.x += walkSpeed * Time.deltaTime; } else { temp.x -= walkSpeed * Time.deltaTime; } transform.position = temp; if (transform.position.x + .1 > anchor.x || transform.position.x - .1 < anchor.x) { state = ENMY_STATES.PATROL; } }
// This function handles advanced pathing. // This function is refactored code with the purpose of changing the // unit's state. This is made with plans to also attach the particle system // and sound alerts to this function. // Parameters: The ENMY_STATE is what the currState is reassigned to. // Returns: void void ChangeENMYState(ENMY_STATES change) { if (change == ENMY_STATES.SEARCH && currState != ENMY_STATES.ATTACK) { GetComponentInChildren <AlertSpritesBehavior> ().ChangeSprite(qMark); GetComponentInChildren <AlertSpritesBehavior> ().PlayClip(qSound); } else if (change == ENMY_STATES.SEARCH && currState == ENMY_STATES.ATTACK) { GetComponentInChildren <AlertSpritesBehavior> ().ChangeSprite(qMark); GetComponentInChildren <AlertSpritesBehavior> ().PlayClip(fSound); } else if (change == ENMY_STATES.ATTACK) { GetComponentInChildren <AlertSpritesBehavior> ().ChangeSprite(xMark); GetComponentInChildren <AlertSpritesBehavior> ().PlayClip(xSound); } else { GetComponentInChildren <AlertSpritesBehavior> ().ChangeSprite(null); } currState = change; }
// Use this for initialization void Start() { anchor = transform.position; state = ENMY_STATES.PATROL; walkSpeed = 3; srchSpeed = 2; atkSpeed = 5; face = true; delay = 30.0f; leash = 3; scent = false; detRange = 2.0f; searchTime = 5.0f; atkTime = 3.0f; }
void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag ("Waypoint")) { if (state == ENMY_STATES.PATROL) face = !face; else { inDetectionRange = false; state = ENMY_STATES.PATROL; face = !face; } } }
void ScentDetect() { if (state == ENMY_STATES.PATROL || state == ENMY_STATES.RESET) { state = ENMY_STATES.SEARCH; target = Player.transform.position; } else if (state == ENMY_STATES.SEARCH) { delay -= Time.deltaTime; if (delay <= 0) { state = ENMY_STATES.ATTACK; } } }
void AttackBehavior() { Vector3 temp = transform.position; if (transform.position.x < player.transform.position.x && !face) face = true; else if (transform.position.x > player.transform.position.x && face) face = false; if (face) temp.x += attackSpd * Time.fixedDeltaTime; else temp.x -= attackSpd * Time.fixedDeltaTime; transform.position = temp; if (playerMoving == false || player.GetComponent<PlayerController>().hiding == true) state = ENMY_STATES.PATROL; }
void AttackBehavior() { if (Player.GetComponent<PlayerController> ().hiding == true) { state = ENMY_STATES.RESET; return; } Vector3 temp = transform.position; if (transform.position.x < Player.transform.position.x && !face) face = true; else if (transform.position.x > Player.transform.position.x && face) face = false; if (face) temp.x += atkSpeed * Time.fixedDeltaTime; else temp.x -= atkSpeed * Time.fixedDeltaTime; transform.position = temp; }
// This function handles advanced pathing. // This function is refactored code with the purpose of changing the // unit's state. This is made with plans to also attach the particle system // and sound alerts to this function. // Parameters: The ENMY_STATE is what the currState is reassigned to. // Returns: void void ChangeENMYState(ENMY_STATES change) { if (change == ENMY_STATES.SEARCH && currState != ENMY_STATES.ATTACK) { GetComponentInChildren<AlertSpritesBehavior> ().ChangeSprite (qMark); GetComponentInChildren<AlertSpritesBehavior> ().PlayClip (qSound); } else if (change == ENMY_STATES.SEARCH && currState == ENMY_STATES.ATTACK) { GetComponentInChildren<AlertSpritesBehavior> ().ChangeSprite (qMark); GetComponentInChildren<AlertSpritesBehavior> ().PlayClip (fSound); } else if (change == ENMY_STATES.ATTACK) { GetComponentInChildren<AlertSpritesBehavior> ().ChangeSprite(xMark); GetComponentInChildren<AlertSpritesBehavior> ().PlayClip(xSound); } else GetComponentInChildren<AlertSpritesBehavior> ().ChangeSprite(null); currState = change; }
// Use this for initialization void Start() { anchor = transform.position; walkSpd = 1.5f; attackSpd = 5.5f; face = true; state = ENMY_STATES.PATROL; inDetectionRange = false; playerMoving = false; killedPlayer = false; timer = 0; }
void Detect() { float tempRange = detRange * Player.GetComponent<Invisiblilityscript> ().LightExposure (); RaycastHit2D targ; if (state == ENMY_STATES.PATROL) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); if (targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; state = ENMY_STATES.SEARCH; } else return; } else if (state == ENMY_STATES.SEARCH) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); if (targ.collider != null && targ.collider.gameObject.tag == "Player") { state = ENMY_STATES.ATTACK; return; } else { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } if(targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; } else { searchTime -= Time.deltaTime; if (searchTime <= 0) { searchTime = 5.0f; state = ENMY_STATES.RESET; } else return; } } else if (state == ENMY_STATES.ATTACK) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); if(targ.collider == null || targ.collider.gameObject.tag != "Player") { atkTime -= Time.deltaTime; if(atkTime <= 0) { atkTime = 3.0f; state = ENMY_STATES.SEARCH; } } else return; } }
void ScentDetect() { if (state == ENMY_STATES.PATROL || state == ENMY_STATES.RESET) { state = ENMY_STATES.SEARCH; target = Player.transform.position; } else if (state == ENMY_STATES.SEARCH) { delay -= Time.deltaTime; if (delay <= 0) state = ENMY_STATES.ATTACK; } }
void Reset() { if (face && transform.position.x > anchor.x) face = false; else if (!face && transform.position.x < anchor.x) face = true; Vector3 temp = transform.position; if (face) temp.x += walkSpeed * Time.deltaTime; else temp.x -= walkSpeed * Time.deltaTime; transform.position = temp; if (transform.position.x + .1 > anchor.x || transform.position.x - .1 < anchor.x) state = ENMY_STATES.PATROL; }
// Use this for initialization void Start() { anchor = transform.position; state = ENMY_STATES.PATROL; //walkSpeed = 3; srchSpeed = 2; atkSpeed = 5; face = true; delay = .8f; //leash = 3; scent = false; detRange = 2.0f; searchTime = 0.0f; atkTime = 0.0f; alertDelay = .5f; }
// Update is called once per frame void Update() { if (Player == null) Player = GameObject.FindGameObjectWithTag ("Player"); if (face) transform.localScale = new Vector3(-3.7024f, 3.7024f, 1.0f); else transform.localScale = new Vector3(3.7024f, 3.7024f, 1.0f); if (GameObject.FindGameObjectWithTag ("Pause") != null && !GameObject.FindGameObjectWithTag ("Pause").GetComponent<PauseMenu> ().gPause) { if (state != ENMY_STATES.RESET && Player.GetComponent<PlayerController>().hiding == false) { if (!scent) Detect (); else ScentDetect (); } else state = ENMY_STATES.PATROL; switch (state) { case ENMY_STATES.PATROL: PatrolBehavior (); break; case ENMY_STATES.SEARCH: SearchBehavior (); break; case ENMY_STATES.ATTACK: AttackBehavior (); break; case ENMY_STATES.RESET: Reset (); break; } } }
void Detect() { if (Player.GetComponent<PlayerController> ().hiding) { state = ENMY_STATES.PATROL; return; } if (Player.GetComponent<PlayerController> ().lightExpo > 0) { float tempRange = detRange * Player.GetComponent<Invisiblilityscript> ().LightExposure (); RaycastHit2D targ; if (state == ENMY_STATES.PATROL) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); if (targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; state = ENMY_STATES.SEARCH; alertDelay = .5f; //GetComponent<AudioSource> ().Stop (); //GetComponent<AudioSource> ().PlayOneShot (growl); SFXVolume.Stop(); SFXVolume.PlayOneShot(growl); } else return; } else if (state == ENMY_STATES.SEARCH) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); if (targ.collider != null && targ.collider.gameObject.tag == "Player") { //GetComponent<AudioSource> ().Stop (); //GetComponent<AudioSource> ().PlayOneShot (bark); SFXVolume.Stop(); SFXVolume.PlayOneShot(bark); state = ENMY_STATES.ATTACK; return; } else { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } if (targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; } else { searchTime -= Time.fixedDeltaTime; if (searchTime <= 0) { searchTime = 0.0f; state = ENMY_STATES.RESET; //GetComponent<AudioSource> ().Stop (); //GetComponent<AudioSource> ().PlayOneShot (whimper); SFXVolume.Stop(); SFXVolume.PlayOneShot(whimper); } else return; } } else if (state == ENMY_STATES.ATTACK) { if (!face) targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); else targ = Physics2D.Raycast ((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); if (targ.collider == null || targ.collider.gameObject.tag != "Player") { atkTime -= Time.fixedDeltaTime; if (atkTime <= 0) { atkTime = 3.0f; state = ENMY_STATES.SEARCH; //GetComponent<AudioSource> ().Stop (); //GetComponent<AudioSource> ().PlayOneShot (growl); SFXVolume.Stop(); SFXVolume.PlayOneShot(growl); } } else return; } } }
void Detect() { float tempRange = detRange * Player.GetComponent <Invisiblilityscript> ().LightExposure(); RaycastHit2D targ; if (state == ENMY_STATES.PATROL) { if (!face) { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } else { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } if (targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; state = ENMY_STATES.SEARCH; } else { return; } } else if (state == ENMY_STATES.SEARCH) { if (!face) { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); } else { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); } if (targ.collider != null && targ.collider.gameObject.tag == "Player") { state = ENMY_STATES.ATTACK; return; } else { if (!face) { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.left, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } else { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.right, tempRange, Physics2D.DefaultRaycastLayers, -1.0f); } } if (targ.collider != null && targ.collider.gameObject.tag == "Player") { target = targ.transform.position; } else { searchTime -= Time.deltaTime; if (searchTime <= 0) { searchTime = 5.0f; state = ENMY_STATES.RESET; } else { return; } } } else if (state == ENMY_STATES.ATTACK) { if (!face) { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.left, detRange, Physics2D.DefaultRaycastLayers, -1.0f); } else { targ = Physics2D.Raycast((Vector2)transform.position, Vector2.right, detRange, Physics2D.DefaultRaycastLayers, -1.0f); } if (targ.collider == null || targ.collider.gameObject.tag != "Player") { atkTime -= Time.deltaTime; if (atkTime <= 0) { atkTime = 3.0f; state = ENMY_STATES.SEARCH; } } else { return; } } }
void PatrolBehavior() { //check to see if at end of leash if (transform.position.x - leash > anchor.x && face) face = false; else if (transform.position.x + leash < anchor.x && !face) face = true; //movement Vector3 temp = transform.position; if (face) temp.x += walkSpd * Time.fixedDeltaTime; else temp.x -= walkSpd * Time.fixedDeltaTime; transform.position = temp; //If the player is in range and he is moving go into attack state. if (inDetectionRange == true && playerMoving == true && player.GetComponent<PlayerController>().hiding == false) { state = ENMY_STATES.ATTACK; SFXVolume.PlayOneShot(detect); } }
// This function will check the player's light exposure. The function // // Parameters: The GameObject is a reference to the player and // is used to extract information about the light // exposure. The ENMY_STATES enum is used to detemine // which switch statement is used when called. // // Returns; void void CheckPlayerExposure(GameObject player, ENMY_STATES cState) { if (cState == ENMY_STATES.PATROL) { switch ((int)player.GetComponent<Invisiblilityscript> ().LightExposure ()) { // If the light exposure is 0 or 1 case 0: case 1: { //ChangeENMYState(ENMY_STATES.PATROL); break; } // If the light exposure is 2 or 3 case 2: case 3: { ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent<Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent<Invisiblilityscript> ().LightExposure () < 0) ChangeENMYState(ENMY_STATES.PATROL); else if (player.GetComponent<Invisiblilityscript> ().LightExposure () > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; } break; } } } else if (cState == ENMY_STATES.SEARCH) { switch ((int)player.GetComponent<Invisiblilityscript> ().LightExposure ()) { // If the light exposure is 0 or 1 case 0: case 1: { //ChangeENMYState(ENMY_STATES.SEARCH); break; } // If the light exposure is 2 or 3 case 2: case 3: { //ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent<Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent<Invisiblilityscript> ().LightExposure () < 0) ChangeENMYState(ENMY_STATES.SEARCH); else if (player.GetComponent<Invisiblilityscript> ().LightExposure () > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; } break; } } } else if (cState == ENMY_STATES.ATTACK) { switch (player.GetComponent<Invisiblilityscript> ().LightExposure ()) { // If the light exposure is 0 or 1 case 0: case 1: { ChangeENMYState(ENMY_STATES.SEARCH); break; } // If the light exposure is 2 or 3 case 2: case 3: { ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent<Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { //ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent<Invisiblilityscript> ().LightExposure () < 0) ChangeENMYState(ENMY_STATES.SEARCH); else if (player.GetComponent<Invisiblilityscript> ().LightExposure () > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent<Transform> ().position; } break; } } } }
// This function will check the player's light exposure. The function // // Parameters: The GameObject is a reference to the player and // is used to extract information about the light // exposure. The ENMY_STATES enum is used to detemine // which switch statement is used when called. // // Returns; void void CheckPlayerExposure(GameObject player, ENMY_STATES cState) { if (cState == ENMY_STATES.PATROL) { switch ((int)player.GetComponent <Invisiblilityscript> ().LightExposure()) { // If the light exposure is 0 or 1 case 0: case 1: { //ChangeENMYState(ENMY_STATES.PATROL); break; } // If the light exposure is 2 or 3 case 2: case 3: { ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent <Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent <Invisiblilityscript> ().LightExposure() < 0) { ChangeENMYState(ENMY_STATES.PATROL); } else if (player.GetComponent <Invisiblilityscript> ().LightExposure() > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; } break; } } } else if (cState == ENMY_STATES.SEARCH) { switch ((int)player.GetComponent <Invisiblilityscript> ().LightExposure()) { // If the light exposure is 0 or 1 case 0: case 1: { //ChangeENMYState(ENMY_STATES.SEARCH); break; } // If the light exposure is 2 or 3 case 2: case 3: { //ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent <Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent <Invisiblilityscript> ().LightExposure() < 0) { ChangeENMYState(ENMY_STATES.SEARCH); } else if (player.GetComponent <Invisiblilityscript> ().LightExposure() > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; } break; } } } else if (cState == ENMY_STATES.ATTACK) { switch (player.GetComponent <Invisiblilityscript> ().LightExposure()) { // If the light exposure is 0 or 1 case 0: case 1: { ChangeENMYState(ENMY_STATES.SEARCH); break; } // If the light exposure is 2 or 3 case 2: case 3: { ChangeENMYState(ENMY_STATES.SEARCH); targPos = player.GetComponent <Transform> ().position; break; } // If the light exposure is greater than or equal to 4 case 4: case 5: case 6: case 7: case 8: case 9: case 10: { //ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; break; } // Any other light exposures, hopefully there will never be an exposure greater than 10 default: { if (player.GetComponent <Invisiblilityscript> ().LightExposure() < 0) { ChangeENMYState(ENMY_STATES.SEARCH); } else if (player.GetComponent <Invisiblilityscript> ().LightExposure() > 10) { ChangeENMYState(ENMY_STATES.ATTACK); targPos = player.GetComponent <Transform> ().position; } break; } } } }
void Reset() { if (face && transform.position.x > anchor.x) face = false; else if (!face && transform.position.x < anchor.x) face = true; Vector3 temp = transform.position; if (face) temp.x += walkSpeed * Time.fixedDeltaTime; else temp.x -= walkSpeed * Time.fixedDeltaTime; transform.position = temp; if (transform.position.x + .1 > anchor.x || transform.position.x - .1 < anchor.x) { state = ENMY_STATES.PATROL; //GetComponent<AudioSource>().Stop (); //GetComponent<AudioSource>().PlayOneShot(sniff); SFXVolume.Stop(); SFXVolume.PlayOneShot(sniff); } }