private GameObject senseClosest(string p_type) { GameObject[] sensed = sense(p_type); if (sensed.Length == 0) { return(null); } GameObject closest = null; float closeness = float.PositiveInfinity; foreach (GameObject ob in sensed) { float prox = Vector2Calc.proximity(ob.transform.position, gameObject.transform.position); if (prox < closeness) { closest = ob; closeness = prox; } } return(closest); }
private bool canGather() { GameObject[] obs = sense("RESOURCE"); foreach (GameObject o in obs) { if (Vector2Calc.proximity(gameObject.transform.position, o.transform.position) < 1f) { return(true); } } return(false); }
//Dodging public static DInput bulletCollisionImminent(ResourceFightDNCreature p_cre) { DActivationFunction prox_activate = ActivationFactory.generateSigmoid(1.5f, 2, false, true, true); DActivationFunction angle_activate = ActivationFactory.generateSigmoid(10f, 2, false, true, true); DActivationFunction full_activate = ActivationFactory.generateSigmoid(1, 1, false, true, false); return(() => { GameObject[] bullets = p_cre.sense("BULLET"); List <GameObject> bullets_close_and_coming_towards = new List <GameObject>(); foreach (GameObject bul in bullets) { if (Vector2Calc.proximity(bul.transform.position, p_cre.transform.position) < 2 && Vector2Calc.checkAngle(p_cre.transform.position - bul.transform.position, bul.GetComponent <Rigidbody2D>().velocity, 10)) { bullets_close_and_coming_towards.Add(bul); } } float activation = 0; foreach (GameObject bul in bullets_close_and_coming_towards) { float bul_prox = prox_activate(Vector2Calc.proximity(bul.transform.position, p_cre.transform.position) - 0.5f); float bul_angle = angle_activate(Mathf.Abs(Vector2Calc.getAngle(p_cre.transform.position - bul.transform.position, bul.GetComponent <Rigidbody2D>().velocity))); float bul_active = full_activate(bul_prox * bul_angle); if (bul_active > activation) { activation = bul_active; } } //if(activation >0 ) Debug.Log(activation); return activation; }); }
public static DInputFactory <ResourceFightDNCreature> isInRangeFactory(string p_type, float p_range) { return((ResourceFightDNCreature p_cre) => { return () => { GameObject[] sensed = p_cre.sense(p_type); bool any_in_range = false; foreach (GameObject ob in sensed) { if (Vector2Calc.proximity(ob.transform.position, p_cre.transform.position) <= p_range) { any_in_range = true; break; } } // if (any_in_range) Debug.Log("InRange " + p_type); return any_in_range ? 1f : 0f; }; }); }
private void gather() { //Get list of gatherable resources GameObject[] obs = sense("RESOURCE"); List <GameObject> resources = new List <GameObject>(); Vector3 this_position = gameObject.transform.position; foreach (GameObject o in obs) { if (Vector2Calc.proximity(this_position, o.transform.position) < 1f) { resources.Add(o); } } //If there are none, return if (resources.Count == 0) { return; } //Get the closest resource in gather arc float closeness = float.PositiveInfinity; GameObject to_harvest = null; foreach (GameObject o in resources) { float proximity = Vector2Calc.proximity(this_position, o.transform.position); if (!(proximity < closeness)) { continue; } closeness = proximity; to_harvest = o; } //Gather the resource m_brain_stop = true; m_rb.velocity = Vector3.zero; m_rb.angularVelocity = 0; m_actions.flush(); m_cooldowns.activate("GATHER", 0.5f); GameObject line = LineCreator.createLine(this_position + (Vector3Calc.fromVec2(m_forward) * 0.1f), to_harvest.transform.position, Color.green, 0.05f, 0.5f); Resource harvesting = to_harvest.GetComponent <Resource>(); DIntervalListener energy_suck = () => { float harvest_power = m_energy.Max - m_energy.Value; if (harvest_power > 10f) { harvest_power = 10f; } if (to_harvest != null) { m_energy.add(harvesting.collect(harvest_power)); } }; m_im.addListener(0.1f, energy_suck); m_tm.addTimeout(0.5f, () => { m_im.removeListener(0.1f, energy_suck); m_brain_stop = false; }); }