void spawnResource()
    {
        GameObject obj = Instantiate(m_resource_prefab, Vector3Calc.randomDirection() * 8f, Quaternion.identity);

        obj.GetComponent <Resource>().Initalize(RandomCalc.Rand(new Range <float>(10f, 100f)));
        ObjectLogger.log(obj, "RESOURCE");
    }
    //Shooting

    public static DInput creatureInShotline(ResourceFightDNCreature p_cre)
    {
        return(() =>
        {
            RaycastHit2D hit = Physics2D.Raycast(p_cre.transform.position + Vector3Calc.fromVec2(p_cre.m_forward * 0.13f), p_cre.m_forward, 10f, 1 << 10);
            //Debug.DrawLine(p_cre.transform.position + Vector3Calc.fromVec2(p_cre.m_forward*0.13f) ,p_cre.transform.position+ Vector3Calc.fromVec2(p_cre.m_forward)*3, Color.red, 10f);
            return hit ? 1f : 0f;
        });
    }
    //----------------------------------------------------------
    // Concrete Actions
    private void shoot()
    {
        if (!m_cooldowns.isCooldownOver("SHOOT"))
        {
            return;
        }

        if (m_attack_speed < 0)
        {
            m_cooldowns.activate("SHOOT", float.PositiveInfinity);
            return;
        }

        m_cooldowns.activate("SHOOT", m_attack_speed);
        Bullet bullet = Instantiate(m_bullet, transform.position + Vector3Calc.fromVec2(m_forward) * 0.24f, transform.rotation).GetComponent <Bullet>();

        bullet.Initalize(m_forward, m_damage, gameObject);

        energyConsumer(2f);
    }
Exemplo n.º 4
0
 ///<summary>Assumes common point is origin</summary>
 public static Vector2 projection(Vector2 p_point, Vector2 p_normal)
 {
     return(fromVector3(Vector3.Project(Vector3Calc.fromVec2(p_point), Vector3Calc.fromVec2(p_normal))));
 }
    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;
        });
    }