Esempio n. 1
0
    /// <summary>
    /// Casts a ray to see if we hit an NPC and, if so, we interact
    /// </summary>
    void TryInteract()
    {
        if (discussionPartner)
        {
            DoInteraction();
            return;
        }

        // Multi ray
        RaycastHit2D[] rHit = Physics2D.RaycastAll(new Vector2(transform.position.x, transform.position.y + 3), Vector2.right * rm.facingDirection, reachDistance, -1);
        if (rHit.Length > 0)
        {
            foreach (RaycastHit2D hit in rHit)
            {
                if (hit.collider.tag == "NPC")
                {
                    // Check for a ghost object
                    if (hit.collider.name.StartsWith("Ghost"))
                    {
                        // Get the ghost's original from the character pair list - not needed if ghost is original's child
                        //CharacterPair cp = FindObjectOfType<GhostManager>().characters.Find(c => c.Ghost == hit.collider.gameObject);

                        discussionPartner = hit.collider.transform.parent.gameObject;
                    }
                    else
                    {
                        discussionPartner = hit.collider.gameObject;
                    }

                    if (!discussionPartner.GetComponent <VIDE_Assign>())
                    {
                        return;
                    }

                    discussionPartnerRM   = discussionPartner.GetComponent <RayMovement>();
                    discussionPartnerRNPC = discussionPartner.GetComponent <RayNPC>();
                    isControlledNPC       = discussionPartner.GetComponent <NPCControl>();

                    discussionPartnerRM.allowMovement = false;
                    discussionPartnerRM.FaceTarget(GetComponentInChildren <SpriteRenderer>());
                    discussionPartnerRNPC.SetAIBehaviourActive(false);

                    DoInteraction();

                    // Break the loop so that only one conversation is active in case many NPC's got raycasted
                    break;
                }
            }
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Activates/deactivates dialogue mode.
    /// </summary>
    /// <param name="active">Active state wanted.</param>
    public void SetDialogueModeActive(bool active)
    {
        CharacterManager.GetCurrentCharacterObject().GetComponent <RayMovement>().allowMovement        = !active;
        CharacterManager.GetCurrentCharacterObject().GetComponent <RayPlayerInput>().enablePlayerInput = !active;

        RayMovement partnerRM   = discussionPartner.GetComponent <RayMovement>();
        RayNPC      partnerRNPC = discussionPartner.GetComponent <RayNPC>();

        partnerRM.allowMovement = !active;
        partnerRNPC.SetAIBehaviourActive(!active);

        if (active)
        {
            partnerRM.FaceTarget(CharacterManager.GetCurrentCharacterObject().GetComponentInChildren <SpriteRenderer>());
        }
        if (!active && dialogueUI.dialogue.isLoaded)
        {
            dialogueUI.dialogue.EndDialogue();
            discussionPartner = null;
        }
    }
Esempio n. 3
0
    GameObject SpawnEntity(Entity entity, Vector3 position = default(Vector3))
    {
        // Initiates default spawn position (-5y because floor is about -6y)
        Vector3 spawnPosition = new Vector3(0f, -5f, 0f);

        // Changes position if it is set in parameters
        if (position != default(Vector3))
        {
            spawnPosition = position;
        }

        // Creates the gameobject from prefab with character name
        GameObject go = (GameObject)Instantiate(characterPrefab, spawnPosition, Quaternion.identity);

        go.name = entity.character.characterName;

        // If conversation is set, gives it to the spawned character
        if (entity.character.VideConversation != null)
        {
            go.GetComponent <VIDE_Assign>().assignedDialogue = entity.character.VideConversation;
            go.GetComponent <VIDE_Assign>().assignedIndex    = entity.character.VideConversationIndex;
        }

        // If animator is set, gives it to the spawned character
        if (entity.character.animator != null)
        {
            go.GetComponentInChildren <Animator>().runtimeAnimatorController = entity.character.animator;
        }

        // If pose sprite is set, changes the sprite to that
        // Not probably needed
        //if (character.characterPoseSprite != null)
        //{
        //    Debug.Log(character.name + " had pose sprite.");
        //    Debug.Log("Changing sprite to " + character.characterPoseSprite.name);
        //    go.GetComponentInChildren<SpriteRenderer>().sprite = character.characterPoseSprite;
        //}

        // Gets components to memory for easy access
        RayNPC               rnpc = go.GetComponent <RayNPC>();
        RayPlayerInput       rpi  = go.GetComponent <RayPlayerInput>();
        CharacterInteraction ci   = go.GetComponent <CharacterInteraction>();

        // Checks if character is currently NPC
        if (entity.isNPC)
        {
            // Enables/sets NPC stuff and disables player stuff
            go.transform.SetParent(GameObject.FindGameObjectWithTag("NPC_Container").transform);
            go.tag       = "NPC";
            rnpc.enabled = true;
            rnpc.SetAIBehaviourActive(true);
            rpi.enabled = false;
            ci.enabled  = false;
        }
        else
        {
            // Enables/sets player stuff and disables NPC stuff
            go.transform.parent = FindObjectOfType <AdvancedHivemind>().transform;
            go.tag = "Player";
            rnpc.SetAIBehaviourActive(false);
            rnpc.enabled = false;
            rpi.enabled  = true;
            ci.enabled   = true;
        }

        //go.AddComponent<Entity>().CopyStats(entity);
        //allCharacters[allCharacters.IndexOf(entity)] = go.GetComponent<Entity>();
        //charactersOnLevel.Add(go.GetComponent<Entity>());

        //Debug.Log("Entity: " + go.GetComponent<Entity>().character.characterName);

        //if (go.GetComponent<Entity>().isInfected) infectedCharacters.Add(go.GetComponent<Entity>());

        //go.AddComponent<Entity>().character = entity.character;
        //Debug.Log(go.GetComponent<Entity>().character.characterName);

        Entity e = go.GetComponent <Entity>();

        //e.character = entity.character;
        CopyEntities(ref e, entity);
        Debug.Log(allCharacters[allCharacters.IndexOf(entity)]);

        allCharacters[allCharacters.IndexOf(entity)] = go.GetComponent <Entity>();
        charactersOnLevel.Add(go.GetComponent <Entity>());
        if (go.GetComponent <Entity>().isInfected)
        {
            infectedCharacters.Add(go.GetComponent <Entity>());
        }

        // Hides comment box
        go.transform.GetComponentInChildren <RandomComment>().transform.parent.gameObject.SetActive(false);

        return(go);
    }
Esempio n. 4
0
    /// <summary>
    /// Spawns a character based on information from Entity class.
    /// </summary>
    /// <param name="entity">Character to spawn.</param>
    /// <param name="position">Position to spawn the character to.</param>
    /// <returns>Returns the whole character gameobject.</returns>
    GameObject SpawnEntity(int indexOfEntity, Vector3 position = default(Vector3))
    {
        // If character's infection is in its final stage and no time is left, does not spawn this character
        if (allCharacters[indexOfEntity].currentStateOfInfection == CharacterEnums.InfectionState.Final && allCharacters[indexOfEntity].currentInfectionStageDuration <= 0)
        {
            Entity en = allCharacters[indexOfEntity];
            en.isAlive = false;
            return(null);
        }

        // Gets map's width and randomizes x position to be somewhere in the map
        float mapWidth = FindObjectOfType <BackgroundGenerator>().GetBackgroundWidth();
        float xPos     = UnityEngine.Random.Range(-mapWidth / 2, mapWidth / 2);

        // Initiates default spawn position with random x position and -5.9y, because floor is about -6y
        Vector3 spawnPosition = new Vector3(xPos, -5.9f, 0f);

        // Changes position if it is set in parameters
        if (position != default(Vector3))
        {
            spawnPosition = position;
        }

        // Creates the gameobject from prefab with character name
        GameObject go = (GameObject)Instantiate(characterPrefab, spawnPosition, Quaternion.identity);

        go.name = allCharacters[indexOfEntity].character.characterName;

        // If conversation is set, gives it to the spawned character
        if (allCharacters[indexOfEntity].character.VideConversation != null)
        {
            go.GetComponent <VIDE_Assign>().assignedDialogue = allCharacters[indexOfEntity].character.VideConversation;
            go.GetComponent <VIDE_Assign>().assignedIndex    = allCharacters[indexOfEntity].character.VideConversationIndex;
        }

        // If animator is set, gives it to the spawned character
        if (allCharacters[indexOfEntity].character.animator != null)
        {
            go.GetComponentInChildren <Animator>().runtimeAnimatorController = allCharacters[indexOfEntity].character.animator;
        }

        // If pose sprite is set, changes the sprite to that
        // Not probably needed
        //if (character.characterPoseSprite != null)
        //{
        //    go.GetComponentInChildren<SpriteRenderer>().sprite = character.characterPoseSprite;
        //}

        // Gets components to memory for easy access
        Entity               e    = go.GetComponent <Entity>();
        RayNPC               rnpc = go.GetComponent <RayNPC>();
        RayPlayerInput       rpi  = go.GetComponent <RayPlayerInput>();
        CharacterInteraction ci   = go.GetComponent <CharacterInteraction>();

        // Checks if character is currently NPC
        if (allCharacters[indexOfEntity].isNPC)
        {
            // Enables/sets NPC stuff and disables player stuff
            go.transform.SetParent(GameObject.FindGameObjectWithTag("NPC_Container").transform);
            go.tag       = "NPC";
            rnpc.enabled = true;
            rnpc.SetAIBehaviourActive(true);
            rpi.enabled = false;
            ci.enabled  = false;
        }
        else
        {
            // Enables/sets player stuff and disables NPC stuff
            go.transform.parent = FindObjectOfType <AdvancedHivemind>().transform;
            go.tag = "Player";
            rnpc.SetAIBehaviourActive(false);
            rnpc.enabled = false;
            rpi.enabled  = true;
            ci.enabled   = true;
            infectedCharacters.Add(go.GetComponent <Entity>());
            if (allCharacters[indexOfEntity].currentStateOfInfection == CharacterEnums.InfectionState.None)
            {
                allCharacters[indexOfEntity].currentStateOfInfection = CharacterEnums.InfectionState.State1;
            }
        }

        // Updates character's entity class
        CopyEntities(ref e, allCharacters[indexOfEntity]);

        // Adds the entity the lists of entities
        allCharacters[indexOfEntity] = go.GetComponent <Entity>();
        charactersOnLevel.Add(go.GetComponent <Entity>());

        // Hides comment box
        go.transform.GetComponentInChildren <RandomComment>().transform.parent.gameObject.SetActive(false);

        return(go);
    }