private void InitializeIfNeeded() { if (_mapCreationBehaviour == null) { _mapCreationBehaviour = GameObject.FindObjectOfType <MapCreationBehaviour>(); } if (_playerRepresentation == null) { _playerRepresentation = new GameObject("Player"); _playerRepresentation.AddComponent <AudioListener>(); _currentPosition = _mapCreationBehaviour.startPositionIJ; MoveRepresetnation(); } }
void Awake() { var map = mapProvider.GetMap(); centerPoint = new IJ { x = Mathf.RoundToInt(map.GetLength(0) / 2), y = Mathf.RoundToInt(map.GetLength(1) / 2) }; nodes = new List <Node>(); for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { var newNodeDefinition = map[i, j].ToTileDefinition(mapProvider); GameObject toInstantiate = newNodeDefinition.prefab; var newGridCell = Instantiate(toInstantiate, transform); var localPosition = new Vector3(i, 0, j); var newNode = new Node() { isCollectable = map[i, j] == mapProvider.collectableColor, isObstacle = map[i, j] == mapProvider.obstacleColor, isEnterPoint = map[i, j] == mapProvider.enterpointColor, isExitPoint = map[i, j] == mapProvider.exitpointColor, interaction = newNodeDefinition.interaction }; newNode.coordinates = new IJ() { x = i, y = j }; nodes.Add(newNode); if (newNode.isCollectable) { collectablesCount++; } if (newNode.isEnterPoint) { startPositionIJ = newNode.coordinates; } newGridCell.transform.localPosition = localPosition; } } }
public void Move(EMoveDirection direction) { InitializeIfNeeded(); var newStep = _currentPosition; switch (direction) { case EMoveDirection.Down: { newStep.y--; break; } case EMoveDirection.Up: { newStep.y++; break; } case EMoveDirection.Left: { newStep.x--; break; } case EMoveDirection.Right: { newStep.x++; break; } } var node = _mapCreationBehaviour.GetNode(newStep); if (node != null) { node.interaction.PerformInteraction(new Vector3(newStep.x, 0f, newStep.y)); if (!node.isObstacle) { _currentPosition = newStep; MoveRepresetnation(); } } }
public Node GetNode(IJ coordinates) { var result = nodes.FirstOrDefault(node => node.coordinates.x == coordinates.x && node.coordinates.y == coordinates.y); return(result); }