public Node HasNeighboor(Vec2Int currentGrid) { for (int i = 0; i < _neighbors.Count; ++i) { if (_neighbors[i]._gridPosition == currentGrid) { return(_neighbors[i]); } } return(null); }
public void CheckFake(Vec2Int currentGrid) { for (int i = 0; i < _possibleNeighboors.Count; ++i) { if (_possibleNeighboors[i].offset + gridPosition == currentGrid) { GameObject.Destroy(_possibleNeighboors[i].sprite); _possibleNeighboors.RemoveAt(i); } } }
public Node(Vector2 worldPosition, Vec2Int gridPosition, GameObject roomSprite, Transform canvasTransform, GameObject fakeRoom) { _fakeRoom = fakeRoom; _worldPosition = worldPosition; _gridPosition = gridPosition; _canvasTransform = canvasTransform; Vector2 centerPosition = Camera.main.WorldToScreenPoint(Camera.main.transform.position); _roomSprite = GameObject.Instantiate(roomSprite, canvasTransform.Find("layer1")); _roomSprite.SetActive(false); }
public void DrawGrid() { HideGrid(); for (int i = 0; i < _objectToEnable.Count; ++i) { _objectToEnable[i].SetActive(true); } Vec2Int roomOffset = _currentNode.gridPosition * -1; _currentNode.Draw(_tileSize, _canvas, roomOffset); }
public void GoToRoom(Transform transform) { Vector2 delta = new Vector2(transform.position.x, transform.position.y) - _currentNode.worldPosition; if (delta.x != 0) { delta.x /= Mathf.Abs(delta.x); } if (delta.y != 0) { delta.y /= Mathf.Abs(delta.y); } Vec2Int myDelta = new Vec2Int((int)delta.x, (int)delta.y); currentGrid += myDelta; Node neighboor = _currentNode.HasNeighboor(currentGrid); if (neighboor == null) { for (int i = 0; i < _knownNodes.Count; ++i) { if (_knownNodes[i].gridPosition == currentGrid) { neighboor = _knownNodes[i]; break; } } } if (neighboor != null) { //currentGrid -= myDelta; } else { neighboor = new Node(transform.position, currentGrid, _roomSprite, _canvas.transform, _fakeSprite); _knownNodes.Add(neighboor); //loop though all the known nodes and see if any of them have a fake room that is in position of the room we just created for (int i = 0; i < _knownNodes.Count; ++i) { _knownNodes[i].CheckFake(currentGrid); } GetFakeRooms(transform.position, neighboor); _playerSprite.transform.SetAsLastSibling(); neighboor.AddNeighboor(_currentNode); _currentNode.AddNeighboor(neighboor); } _currentNode = neighboor; if (_isEnabled) { DrawGrid(); } }
// Use this for initialization void Start() { currentGrid = Vec2Int.zero; _roomSprite.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _tileSize); _roomSprite.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _tileSize); _canvas = GetComponent <Canvas>(); startPos = FindObjectOfType <CameraManager>().targetRoom.transform.position; Vector2 currentRoomPos = FindObjectOfType <CameraManager>().targetRoom.transform.position; Vec2Int gridPosition = currentGrid; _startNode = new Node(currentRoomPos, gridPosition, _roomSprite, _canvas.transform, _fakeSprite); GetFakeRooms(currentRoomPos, _startNode); _currentNode = _startNode; _knownNodes.Add(_currentNode); _playerSprite.transform.SetAsLastSibling(); }
public void AddPossibleNeighboor(Vec2Int deltaPosition, GameObject doorSprite, float doorSize) { _doorSprite = doorSprite; for (int i = 0; i < _neighbors.Count; ++i) { if (_neighbors[i]._gridPosition == _gridPosition + deltaPosition) { return; } } bool exists = false; for (int i = 0; i < _possibleNeighboors.Count; ++i) { if (_possibleNeighboors[i].offset == deltaPosition) { exists = true; break; } } if (!exists) { PossibleNeighboor newPossible = new PossibleNeighboor(); newPossible.offset = deltaPosition; _worldPosition = worldPosition; _gridPosition = gridPosition; Vector2 centerPosition = Camera.main.WorldToScreenPoint(Camera.main.transform.position); GameObject newSprite = GameObject.Instantiate(_fakeRoom, _canvasTransform.Find("layer1")); GameObject newDoorSprite = GameObject.Instantiate(doorSprite, _canvasTransform.Find("layer2")); RectTransform recTransform = newDoorSprite.GetComponent <RectTransform>(); recTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, doorSize); recTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, doorSize); newDoorSprite.SetActive(false); newSprite.SetActive(false); newPossible.sprite = newSprite; _possibleNeighboors.Add(newPossible); PossibleNeighboor newDoor = new PossibleNeighboor(); newDoor.sprite = newDoorSprite; newDoor.offset = deltaPosition; _doors.Add(newDoor); } }
public void Draw(float offset, Canvas canvas, Vec2Int roomOffset) { if (drawn) { return; } drawn = true; Vector2 centerPosition = Camera.main.WorldToScreenPoint(Camera.main.transform.position); _roomSprite.SetActive(true); Vector2 floatposition = new Vector2(gridPosition.x, gridPosition.y); Vector2 floatOffset = new Vector2(roomOffset.x, roomOffset.y); Vector2 drawPosition = centerPosition + (floatposition * offset) + (floatOffset * offset); _roomSprite.transform.position = drawPosition; //GameObject.Instantiate(roomSprite, centerPosition + (gridPosition * offset) + (roomOffset * offset), Quaternion.identity, canvas.transform); for (int i = 0; i < _neighbors.Count; ++i) { _neighbors[i].Draw(offset, canvas, roomOffset); } for (int i = 0; i < _possibleNeighboors.Count; ++i) { _possibleNeighboors[i].sprite.SetActive(true); Vec2Int calc1 = (gridPosition + _possibleNeighboors[i].offset); Vector2 neighboorOffsetFLoat = new Vector2(calc1.x, calc1.y); Vector2 floatPositionNew = centerPosition + (neighboorOffsetFLoat * offset) + (floatOffset * offset); _possibleNeighboors[i].sprite.transform.position = floatPositionNew; } for (int i = 0; i < _doors.Count; ++i) { _doors[i].sprite.SetActive(true); Vector2 floatOffsetDoors = new Vector2(_doors[i].offset.x, _doors[i].offset.y); floatOffsetDoors /= 2; Vec2Int calc1 = (gridPosition + _doors[i].offset); Vector2 neighboorOffsetFLoat = floatposition + floatOffsetDoors; //neighboorOffsetFLoat /= 2; Vector2 floatPositionNew = centerPosition + (neighboorOffsetFLoat * (offset)) + (floatOffset * offset); _doors[i].sprite.transform.position = floatPositionNew; } }
public Vec2Int Subtract(Vec2Int other) { return(new Vec2Int(other.x - this.x, other.y - this.y)); }
public Vec2Int Add(Vec2Int other) { return(new Vec2Int(other.x + this.x, other.y + this.y)); }
public bool Equals(Vec2Int other) { return(other.x == this.x && other.y == this.y); }