Esempio n. 1
0
    /// <summary>
    /// Spawns a creature on the map
    /// </summary>
    /// <param name="permanentPrefab">The prefab of a creature to spawn</param>
    /// <param name="caster">Who is the owner?</param>
    /// <param name="pos">Where on the map to spawn it</param>
    /// <returns></returns>
    public static void SpawnPermanent(GameObject permanentPrefab, Owner caster, MapPosition pos)
    {
        GameObject    permanent = GameObject.Instantiate(permanentPrefab) as GameObject;
        CreatureStats stats     = permanent.GetComponent <CreatureStats>();

        // Error checking
        if (Utils.OutOfBounds(pos))
        {
            Debug.LogError("Error, " + stats.name + " was attempted to be spawned in invalid position: " + pos);
        }

        if (permanentMap[pos.x, pos.y] != null)
        {
            Debug.LogError("Error, " + stats.name +
                           " was attempted to be spawned on top of another permanentPrefab at: " +
                           pos);
        }

        // Set up stats
        stats.OwnedBy      = caster;
        stats.GridPosition = pos;

        //Set the transform position
        permanent.transform.position = GridToWorld(pos);

        // Flip the creature if it is an enemy
        int     xScale = stats.OwnedBy == Owner.PLAYER ? 1 : -1;
        Vector3 scale  = new Vector3(xScale, 1f, 1f);

        permanent.transform.localScale = scale;

        permanentMap[pos.x, pos.y] = stats;

        stats.OnSpawned();


        EventManager.InvokeCreatureSpawned(stats, pos);
    }