void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag == "NPC" || other.gameObject.tag == "Player") { // Box Collider is for interactions if (other is BoxCollider2D && other.gameObject.tag == "Player") { NPC npc = Globals.GetComponent <MapController>().CurrentMap.GetNPC(this.gameObject.name.Replace("NPC_", "")); other.gameObject.GetComponent <InputController>().SetInteractableNPC(npc); } else { // Polygon collider is for actual collisions if (this.gameObject.tag != "Player") { // Get Reference to the NPC object for this character string npcName = this.gameObject.name.Replace("NPC_", ""); MapObject currentMap = Globals.GetComponent <MapController>().CurrentMap; NPC npc = currentMap.NPCs[currentMap.NPCNames.IndexOf(npcName)]; if (npc.CanMove) { npc.RecalculatePath(other.gameObject); } } } } }
// Public Methods public NPC(string n, GameObject go, MapObject map, GlobalObjects globals) { name = n; gameObject = go; animator = go.GetComponent <SpriteAnimator>(); animator.SetSpriteRenderer(go.GetComponent <SpriteRenderer>()); pathFinder = go.GetComponent <PathFinder>(); grid = GameObject.FindGameObjectWithTag("Grid").GetComponent <Grid>(); globalObjects = globals; currentMap = map; globals.GetComponent <DialogueController>().LoadNPCConversation(n); }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } // Dialogue Control if (globalObjects.GameState == GameStates.InDialogue) { if (Input.GetKeyDown(KeyCode.Return)) { globalObjects.GetComponent <DialogueController>().NextStep(); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { globalObjects.GetComponent <DialogueController>().NextOption(); } else if (Input.GetKeyDown(KeyCode.UpArrow)) { globalObjects.GetComponent <DialogueController>().PreviousOption(); } return; } // Get Information Cell Below Player's Feet TileMetaData tileMeta = movementController.GetTileBelowPlayer(transform.position + new Vector3(0, -0.5f, 0)); if (globalObjects.GameState == GameStates.Playing) { Vector2 velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); Vector2.ClampMagnitude(velocity, MAXSPEED); velocity *= (MAXSPEED * Time.deltaTime); movementController.Move(ref velocity); string animationName = ""; if (tileMeta.IsLadder & velocity.y != 0) { animationName = "Walk Ladder"; } else if (velocity.y > 0) { animationName = "Walk North"; } else if (velocity.y < 0) { animationName = "Walk South"; } else if (velocity.x > 0) { animationName = "Walk East"; } else if (velocity.x < 0) { animationName = "Walk West"; } else { animationName = lastAnimation.Replace("Walk", "Idle"); } if (animationName != lastAnimation) { lastAnimation = animationName; anim.Play(animationName); } } // Interaction Handler if (Input.GetKeyDown(KeyCode.Return)) { // Check if we are near enough to interact with an NPC if (InteractableNPC != null) { string[] actionParams = InteractableNPC.InteractFunction.Split(';'); switch (actionParams[0]) { case "Talk": globalObjects.GetComponent <DialogueController>().StartConversation(InteractableNPC.name); globalObjects.DialogueCanvas.gameObject.SetActive(true); break; } } else if (tileMeta.Action != "") { string[] actionParameters = tileMeta.Action.Split(';'); switch (actionParameters[0]) { case "ShowSign": if (globalObjects.GameState == GameStates.InSign) { globalObjects.HideSign(); } else { globalObjects.ShowSign(actionParameters[1]); } break; } } } }