public TileMetaData GetTileAttributesAtPoint(Vector2 point, int layerIndex) { Vector3Int cell = grid.WorldToCell(point); int tileID = mapController.CurrentMap.GetCell("Collisions", cell.x, cell.y); TileMetaData attributes = mapController.CurrentMap.TileAttributes[tileID]; attributes.Action = mapController.CurrentMap.GetAction(layerIndex, cell); return(attributes); }
public void Move(ref Vector2 velocity) { RaycastHit2D[] hits = new RaycastHit2D[10]; ContactFilter2D filter = new ContactFilter2D() { layerMask = collisionMask }; int numHits = collisionBox.Cast(velocity, filter, hits, velocity.magnitude); if (numHits > 0) { // Reset Velocity Vector velocity = Vector2.zero; // Check the cell we have collided with TileMetaData tileMeta = GetTileAttributesAtPoint(hits[0].point, 1); if (tileMeta.Action != "") { string action = tileMeta.Action; if (action.StartsWith("Teleport")) { string[] aParts = action.Split(';'); string mapName = aParts[1]; float playerX = (float)Convert.ToDouble(aParts[2]); float playerY = (float)Convert.ToDouble(aParts[3]); mapController.ChangeMap(mapName, playerX, playerY); } } } transform.Translate(velocity); SpriteRenderer renderer = GetComponent <SpriteRenderer>(); renderer.sortingOrder = (int)((mapController.CurrentMap.MapHeight - transform.position.y) * 100) + 100; }
public TileMetaData GetTileBelowPlayer(Vector2 point) { Vector3Int cell = grid.WorldToCell(point); int tileID = 0; int layer = 1; TileMetaData attributes = new TileMetaData(); while (layer >= 0 && tileID == 0) { string layerName = ""; switch (layer) { case 0: layerName = "Background"; break; case 1: layerName = "Collisions"; break; } tileID = mapController.CurrentMap.GetCell(layerName, cell.x, cell.y); attributes = mapController.CurrentMap.TileAttributes[tileID]; attributes.Action = mapController.CurrentMap.GetAction(layer, cell); layer--; } return(attributes); }
// 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; } } } }
public void AddMetaData(Vector3 v, TileMetaData m) { tiles.Add(v, m); }
public void Initialise() { if (IsInitialised) { return; } // Get Tile Attributes from named map if we have no sheet string json = ""; if (SpriteSheetName != Name) { MapController mapController = GameObject.FindGameObjectWithTag("Globals").GetComponent <MapController>(); if (!mapController.LoadedMaps.ContainsKey(SpriteSheetName)) { mapController.LoadMap(SpriteSheetName); } TileAttributes = mapController.LoadedMaps[SpriteSheetName].TileAttributes; } else { json = "{'Items': " + MetaDataJson + "}"; MetaDataInfo metaData = JsonConvert.DeserializeObject <MetaDataInfo>(json); for (int i = 0; i < metaData.Items.Count; i++) { TileMetaData m = new TileMetaData(); foreach (MetaDataItem meta in metaData.Items[i].Data) { switch (meta.Type) { case "Solid": m.IsSolid = meta.Value == "True"; break; case "Friction": m.Friction = meta.Value; break; case "Damage": m.Damage = Convert.ToInt32(meta.Value == "" ? "0" : meta.Value); break; case "Ladder": m.IsLadder = meta.Value == "True"; break; } } TileAttributes.Add(m); } } json = "{'Layers':" + LayersJson + "}"; MapCellInfo mapCellInfo = JsonConvert.DeserializeObject <MapCellInfo>(json); Layers = new List <MapCells>(); LayerNames = new List <string>(); for (int i = 0; i < mapCellInfo.Layers.Count; i++) { MapCells layer = new MapCells(MapWidth, MapHeight); for (int y = 0; y < MapHeight; y++) { for (int x = 0; x < MapWidth; x++) { int c = y * MapWidth + x; layer.Cells[x, y] = mapCellInfo.Layers[i].Cells[c].TileID; layer.Actions[x, y] = mapCellInfo.Layers[i].Cells[c].Action; } } LayerNames.Add(mapCellInfo.Layers[i].Layer); Layers.Add(layer); } CreatePathFindingArray("Collisions"); // Create NPCs json = "{'NPC' :" + NPCJson + "}"; NPCData npcData = JsonConvert.DeserializeObject <NPCData>(json); foreach (NPCInfo npcInfo in npcData.NPC) { NPC npc = NPCController.CreateNPC(npcInfo.Name, npcInfo.ModelName, npcInfo.X, MapHeight - npcInfo.Y - 1, this); npc.CanMoveToPOI = npcInfo.POI == "True"; npc.CanWander = npcInfo.Wander == "True"; npc.InteractFunction = npcInfo.IntFunc; npc.InteractParams = npcInfo.IntParam; npc.maxSpeed = npcInfo.Speed; npc.VisibilityFunction = npcInfo.VisFunc; npc.VisibilityParams = npcInfo.VisParam; AddNPC(npc); } IsInitialised = true; }