private IEnumerator MoveToNewNode(WorldMapNode newNode) { canMove = false; // Make the player sprite face in the movement direction bool facingRight = newNode.Position.x > CURRENT_NODE.Position.x; spriteTransform.localScale = new Vector3(facingRight ? 1f : -1f, 1f, 1f) * 0.5f; // Play an animation of moving to the new node float distance = Vector3.Distance(CURRENT_NODE.Position, newNode.Position); float length = distance / movementSpeed; for (float f = 0; f < length; f += Time.deltaTime) { float t = Mathf.SmoothStep(0, 1, f / length); transform.position = Vector3.Lerp(CURRENT_NODE.Position, newNode.Position, t); yield return(null); } // Set the current node to the new node and allow movement again transform.position = newNode.Position; CURRENT_NODE = newNode; canMove = true; }
public WorldMapNode GetNodeInDirection(Vector2 direction) { // Using the stored edge list, go through all the connected nodes and find the one that is // the most in the given direction. The initial value of bestAngle defines the cutoff for // any node being marked as good (basically, how precise you have to be when aiming.) WorldMapNode bestNode = null; float bestAngle = 100; foreach (WorldMapEdge edge in edges) { if (edge.GetCanCross()) { WorldMapNode node = edge.AcrossFrom(this); Vector2 delta = node.Position - Position; float angle = Vector2.Angle(direction, delta); if (angle < bestAngle) { bestAngle = angle; bestNode = node; } } } return(bestNode); }
private void Update() { // Let the player move from node to node or enter a level if (canMove) { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector2 input = new Vector2(h, v); if (input.magnitude > 0.5f) { WorldMapNode newNode = CURRENT_NODE.GetNodeInDirection(input); if (newNode != null) { StartCoroutine(MoveToNewNode(newNode)); } } else if (Input.GetButtonDown("A")) { // TODO: replace this with an actual transition later. // the scene changing itself should probably be done in another class, after a nice fade out SceneManager.LoadScene(CURRENT_NODE.SceneToLoad); } } // Make the player sprite hover slightly float hover = Mathf.Sin(Time.time * 8f) * 0.02f; spriteTransform.localPosition = new Vector3(0, 0.75f + hover, -0.1f); }
private void Awake() { // If the current node is null, it's the first time the world map has loaded. // Set the current node to the game start node. CURRENT_NODE = gameStartNode; // Start the player at the current node. transform.position = CURRENT_NODE.transform.position; }
// Given a node, returns the other connected node. // Returns null if the given node is not A or B. public WorldMapNode AcrossFrom(WorldMapNode node) { if (node == a) { return(b); } if (node == b) { return(a); } return(null); }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } WorldMapNode otherNode = obj as WorldMapNode; if (otherNode.Position.Equals(this.Position)) { return(true); } return(false); }
public void LoadMapState() { GameStateHandler gameStateHandler = GameControllerScript.GetInstance().GetComponent <GameStateHandler>(); WorldMapNode currentNode = nodes[gameStateHandler.GetCurrentNode()]; if (currentNode != null) { GameObject player = GameObject.FindGameObjectWithTag("WorldMapPlayer"); player.transform.position = currentNode.transform.position; } // Enable/Disable edges if (gameStateHandler.GetBossDefeated(EnemyType.Dummy, 2)) { edges[0].SetCanCross(true); edges[0].gameObject.SetActive(true); edges[1].SetCanCross(true); edges[1].gameObject.SetActive(true); edges[2].SetCanCross(true); edges[2].gameObject.SetActive(true); } else { edges[0].SetCanCross(false); edges[0].gameObject.SetActive(false); edges[1].SetCanCross(false); edges[1].gameObject.SetActive(false); edges[2].SetCanCross(false); edges[2].gameObject.SetActive(false); } if (gameStateHandler.GetBossDefeated(EnemyType.Vampire, 1) && gameStateHandler.GetBossDefeated(EnemyType.Psychic, 1)) { edges[3].SetCanCross(true); edges[3].gameObject.SetActive(true); edges[4].SetCanCross(true); edges[4].gameObject.SetActive(true); edges[5].SetCanCross(true); edges[5].gameObject.SetActive(true); } else { edges[3].SetCanCross(false); edges[3].gameObject.SetActive(false); edges[4].SetCanCross(false); edges[4].gameObject.SetActive(false); edges[5].SetCanCross(false); edges[5].gameObject.SetActive(false); } if (gameStateHandler.GetBossDefeated(EnemyType.Vampire, 2)) { edges[6].SetCanCross(true); edges[6].gameObject.SetActive(true); } else { edges[6].SetCanCross(false); edges[6].gameObject.SetActive(false); } if (gameStateHandler.GetBossDefeated(EnemyType.Psychic, 2)) { edges[7].SetCanCross(true); edges[7].gameObject.SetActive(true); edges[8].SetCanCross(true); edges[8].gameObject.SetActive(true); } else { edges[7].SetCanCross(false); edges[7].gameObject.SetActive(false); edges[8].SetCanCross(false); edges[8].gameObject.SetActive(false); } if (gameStateHandler.GetBossDefeated(EnemyType.Vampire, 3) && gameStateHandler.GetBossDefeated(EnemyType.Psychic, 3)) { edges[9].SetCanCross(true); edges[9].gameObject.SetActive(true); edges[10].SetCanCross(true); edges[10].gameObject.SetActive(true); edges[11].SetCanCross(true); edges[11].gameObject.SetActive(true); } else { edges[9].SetCanCross(false); edges[9].gameObject.SetActive(false); edges[10].SetCanCross(false); edges[10].gameObject.SetActive(false); edges[11].SetCanCross(false); edges[11].gameObject.SetActive(false); } }
// Returns if this edge connects to the given node. public bool IsConnectedTo(WorldMapNode node) { return(node == a || node == b); }