示例#1
0
    private GameObject SpawnEnemy(GameObject enemyPrefab)
    {
        if (enemyPrefab == null)
        {
            return(null);
        }

        Vector2 position = GameManager.Instance.MainPlayer.transform.position;

        // also we should spawn them so we don't see them appearing
        // from a x-axis point of view
        // assuming we can see from [ -1 ... 1 ]
        // that means we are interested in spawning them from [ -2 ... -1 , 1 ... 2 ]

        bool xSign = RandomManager.Boolean();
        bool ySign = RandomManager.Boolean();

        float xDistance = RandomManager.Next(0.0f, m_spawnVariableDistance);
        float yDistance = RandomManager.Next(0.0f, m_spawnVariableDistance);

        Vector2 spawnPosition = position +
                                new Vector2(xSign ? m_spawnMinDistance + xDistance : -m_spawnMinDistance - xDistance,
                                            ySign ? m_spawnMinDistance + yDistance : -m_spawnMinDistance - yDistance);

        GameObject obj = Instantiate(enemyPrefab);

        if (obj == null)
        {
            return(null);
        }

        obj.transform.position = spawnPosition;

        return(obj);
    }
示例#2
0
    protected override void OnUpdate()
    {
        base.OnUpdate();

        m_time += TimeManager.Dt;
        switch (CurrentState)
        {
        case State.E_Idle:
            if (m_time >= m_idleTime)
            {
                CurrentState = State.E_Seeking;
                m_time       = 0;
            }
            break;

        case State.E_Seeking:
            if (m_time >= m_seekTime)
            {
                CurrentState = State.E_Idle;
                m_time       = 0;
            }
            break;
        }

        if (CurrentState == State.E_Idle)
        {
            return;
        }

        // just go toward the main player
        Vector2 playerPos = GameManager.Instance.MainPlayer.transform.position;

        string currentGroup = m_er ? m_er.GetCurrentGroup() : "";

        string  newFrameGroup = "Up";
        bool    setFrame      = false;
        Vector2 wantedNewPos  = transform.position;

        // we prefer to continue in direction we are going if possible
        // there are 4 possible directions, but basically horizontal vs vertical
        bool preferVertical;

        if (currentGroup == "Up" || currentGroup == "Down")
        {
            preferVertical = true;
        }
        else if (currentGroup == "Left" || currentGroup == "Right")
        {
            preferVertical = false;
        }
        else
        {
            preferVertical = RandomManager.Boolean();
            Debug.Log("Slime perfer vertical is " + preferVertical.ToString());
        }

        // TODO: Extract into functions to avoid this copy pasta between preferVertical and non perferVertical
        if (preferVertical)
        {
            if (playerPos.y > transform.position.y)
            {
                wantedNewPos.y = transform.position.y + (TimeManager.Dt * m_speed);
                if (wantedNewPos.y > playerPos.y)
                {
                    wantedNewPos.y = playerPos.y;
                }

                newFrameGroup = "Up";
                setFrame      = true;
            }
            else if (playerPos.y < transform.position.y)
            {
                wantedNewPos.y = transform.position.y - (TimeManager.Dt * m_speed);
                if (wantedNewPos.y < playerPos.y)
                {
                    wantedNewPos.y = playerPos.y;
                }

                newFrameGroup = "Down";
                setFrame      = true;
            }
            else if (playerPos.x < transform.position.x)
            {
                wantedNewPos.x = transform.position.x - (TimeManager.Dt * m_speed);
                if (wantedNewPos.x < playerPos.x)
                {
                    wantedNewPos.x = playerPos.x;
                }

                newFrameGroup = "Left";
                setFrame      = true;
            }
            else if (playerPos.x > transform.position.x)
            {
                wantedNewPos.x = transform.position.x + (TimeManager.Dt * m_speed);
                if (wantedNewPos.x > playerPos.x)
                {
                    wantedNewPos.x = playerPos.x;
                }

                newFrameGroup = "Right";
                setFrame      = true;
            }
        }
        else
        {
            if (playerPos.x < transform.position.x)
            {
                wantedNewPos.x = transform.position.x - (TimeManager.Dt * m_speed);
                if (wantedNewPos.x < playerPos.x)
                {
                    wantedNewPos.x = playerPos.x;
                }

                newFrameGroup = "Left";
                setFrame      = true;
            }
            else if (playerPos.x > transform.position.x)
            {
                wantedNewPos.x = transform.position.x + (TimeManager.Dt * m_speed);
                if (wantedNewPos.x > playerPos.x)
                {
                    wantedNewPos.x = playerPos.x;
                }

                newFrameGroup = "Right";
                setFrame      = true;
            }
            else if (playerPos.y > transform.position.y)
            {
                wantedNewPos.y = transform.position.y + (TimeManager.Dt * m_speed);
                if (wantedNewPos.y > playerPos.y)
                {
                    wantedNewPos.y = playerPos.y;
                }

                newFrameGroup = "Up";
                setFrame      = true;
            }
            else if (playerPos.y < transform.position.y)
            {
                wantedNewPos.y = transform.position.y - (TimeManager.Dt * m_speed);
                if (wantedNewPos.y < playerPos.y)
                {
                    wantedNewPos.y = playerPos.y;
                }

                newFrameGroup = "Down";
                setFrame      = true;
            }
        }

        transform.position = wantedNewPos;

        if (setFrame && m_er)
        {
            m_er.SetCurrentGroup(newFrameGroup);
        }
    }