void MakeNoise() { Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 100, 1 << gameObject.layer); for (int i = 0; i != colliders.Length; ++i) { if (colliders[i].gameObject != this) { // victim has heard us if (wind.windBlowsFromTo(transform.position, colliders[i].transform.position)) { VictimScript victim = colliders[i].GetComponent <VictimScript>(); victim.ScareEvent(transform.position); } } } }
// Update is called once per frame void Update() { float inputV = Input.GetAxis("Vertical"); float inputH = Input.GetAxis("Horizontal"); float inputDeadZone = 0.1f; NoiseSourceScript.NoiseType noiseType = NoiseSourceScript.NoiseType.None; if (Mathf.Abs(inputV) >= inputDeadZone) { direction = (inputV > 0) ? 1 : 3; //noiseType = NoiseSourceScript.NoiseType.Uniform; } else if (Mathf.Abs(inputH) >= inputDeadZone) { direction = (inputH > 0) ? 0 : 2; //noiseType = NoiseSourceScript.NoiseType.Uniform; } //if (Mathf.Abs(inputH) < inputDeadZone) inputH = 0.0f; //if (Mathf.Abs(inputV) < inputDeadZone) inputV = 0.0f; GetComponent <SpriteRenderer>().sprite = yetiDirectionSprites[direction]; //transform.position = transform.position + Time.deltaTime * speed * (new Vector3(inputH, inputV, 0.0f)); Vector2 velocity = speed * (new Vector2(inputH, inputV)).normalized; if (wind.enabled) { if ((((wind.speed > 0) && (velocity.x > 0)) || ((wind.speed < 0) && (velocity.x < 0))) && (Mathf.Abs(wind.speed) >= Mathf.Abs(velocity.x))) { velocity.x = 0.0f; } else if (Mathf.Abs(velocity.x) > 0) { velocity.x += wind.speed; } if ((Mathf.Abs(wind.speed) > 0.01f) && (Mathf.Abs(velocity.magnitude) > 0)) { noiseType = (wind.speed > 0) ? NoiseSourceScript.NoiseType.WindRight : NoiseSourceScript.NoiseType.WindLeft; } } currentVelocity = velocity; GetComponent <NoiseSourceScript> ().SetNoiseType(noiseType); // grabbing victims bool grab = Input.GetAxis("Action") >= inputDeadZone; Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 20, layerVictim); for (int i = 0; i != colliders.Length; ++i) { VictimScript victim = colliders[i].GetComponent <VictimScript>(); victim.Grab(grab); if (grab) { victim.Kill(); } } // scaring victims by our appearance and sound colliders = Physics2D.OverlapCircleAll(transform.position, 100, layerVictim); for (int i = 0; i != colliders.Length; ++i) { float directionSound = colliders[i].transform.position.x - transform.position.x; float directionWind = wind.speed; // victim has seen us if ((Vector2.Distance(transform.position, colliders[i].transform.position) <= 50.0f) // victim has heard us || (wind.enabled && ((Mathf.Abs(velocity.magnitude) > 10.0f) && wind.windBlowsFromTo(transform.position, colliders[i].transform.position)))) { VictimScript victim = colliders[i].GetComponent <VictimScript>(); victim.ScareEvent(transform.position); } } if (shelter == null) { hp -= Time.deltaTime * hpDropOutside; } else { if (hp < maxHp) { EatAndHeal(ref shelter.meal); } hp -= Time.deltaTime * hpDropShelter; } // yeti loses if (hp <= 0.0f) { GameObject dialog = GameObject.Find("Dialog"); dialog.transform.Find("Text").GetComponent <Text>().text = "I'm hungry!!!\n\n[Watch over the pink health bar at the bottom]"; dialog.GetComponent <DialogScript>().restartLevelAfterClosing = true; dialog.GetComponent <DialogScript>().Show(); //Application.LoadLevel(Application.loadedLevel); } }