bool checkWalkable(Vector3 position, Vector3 direction) { Vector3 walkPosition = position + direction; Vector3 floorCubePosition = walkPosition + new Vector3(0, -1, 0); SimplePathElement walkElement = null; SimplePathElement floorElement = null; level.GetAllElements().TryGetValue(walkPosition, out walkElement); level.GetAllElements().TryGetValue(floorCubePosition, out floorElement); if (floorElement != null) { if (floorElement.topBarrier == Barrier.Walkable) { if (walkElement != null) { if (walkElement.getBarrierByDirection(-direction) == Barrier.Walkable) { return(true); } } else { return(true); } } } return(false); }
public void addToAllElements(Vector3 pos, SimplePathElement element) { if (!allElements.ContainsKey(pos)) { allElements.Add(pos, element); } else { //TODO check which ELement is stronger } }
public void updateElementPosition(Vector3 oldPosition, Vector3 newPosition, SimplePathElement element) { SimplePathElement oldPositionElement; allElements.TryGetValue(oldPosition, out oldPositionElement); if (oldPositionElement.Equals(element)) { allElements.Remove(oldPosition); } if (allElements.ContainsKey(newPosition)) { removeFromAllElements(newPosition); } allElements.Add(newPosition, element); }
//Liste aller benachbarten PathElements List <AStarObject> findAdjacents(AStarObject ao) { List <AStarObject> adjacents = new List <AStarObject>(); //Check if curretn Position is PathElement SimplePathElement currentPositionElement = null; level.GetAllElements().TryGetValue(ao.getPosition(), out currentPositionElement); Vector3[] directions = { Vector3.forward, Vector3.back, Vector3.left, Vector3.right }; if (currentPositionElement != null) { foreach (Vector3 direction in directions) { if (currentPositionElement.getBarrierByDirection(direction) == Barrier.Walkable) { AStarObject newAO = getAdjacent(ao, ao.getPosition(), direction); if (newAO != null) { adjacents.Add(newAO); } } } } else { foreach (Vector3 direction in directions) { AStarObject newAO = getAdjacent(ao, ao.getPosition(), direction); if (newAO != null) { adjacents.Add(newAO); } } } return(adjacents); }
AStarObject getAdjacent(AStarObject ao, Vector3 position, Vector3 direction) { Vector3 walkPosition = position + direction; Vector3 floorCubePosition = walkPosition + new Vector3(0, -1, 0); SimplePathElement walkElement = null; SimplePathElement floorElement = null; level.GetAllElements().TryGetValue(walkPosition, out walkElement); level.GetAllElements().TryGetValue(floorCubePosition, out floorElement); //Normal Walk if (walkElement != null) { if (walkElement.bottomBarrier == Barrier.Walkable && walkElement.getBarrierByDirection(-direction) == Barrier.Walkable) { //Adjazenz Block existiert return(new AStarObject(ao, position + direction)); } } else { if (floorElement != null) { if (floorElement.topBarrier == Barrier.Walkable) { //Adjazenz Block existiert return(new AStarObject(ao, position + direction)); } } } //Stairs Up if (walkElement != null) { if (walkElement.getBarrierByDirection(-direction) == Barrier.Stairs) { SimplePathElement aboveStairEntry = null; Vector3 aboveStairEntryPosition = position + Vector3.up; level.GetAllElements().TryGetValue(aboveStairEntryPosition, out aboveStairEntry); if (aboveStairEntry == null) { //checke Treppen Ende return(getAdjacent(ao, position + direction + Vector3.up, direction)); } } } //Stairs Down if (walkElement == null) { if (floorElement != null) { if (floorElement.topBarrier == Barrier.Stairs) { if (floorElement.getBarrierByDirection(direction) == Barrier.Stairs) { SimplePathElement aboveStairExit = null; Vector3 aboveStairExitPosition = walkPosition + direction; level.GetAllElements().TryGetValue(aboveStairExitPosition, out aboveStairExit); if (aboveStairExit == null) { //checke Treppen Start return(getAdjacent(ao, position + direction + Vector3.down, direction)); } } } } } return(null); }
// Update is called once per frame void Update() { //Wenn man auf UI klickt, keine Inputs annehmen if (!IsPointerOverUIObject()) { clickedPathElement = null; if (Input.GetMouseButtonUp(0)) { if (!dragging && !draggingNotMovable) { RaycastHit hit; Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000, layer_mask)) { if (hit.collider.gameObject.GetComponent <SimplePathElement> () != null) { clickedPathElement = hit.collider.gameObject.GetComponent <SimplePathElement> (); if (hasClickedPathElement != null) { hasClickedPathElement(clickedPathElement); } } } } if (dragging) { //EndDragging if (slidingElement != null) { slidingElement.endDragging(); if (endedDragging != null) { endedDragging(slidingElement); } } } dragging = false; draggingNotMovable = false; if (currentSlidingElement != null) { currentSlidingElement = null; //ReleasedSlidingElement slidingElement.hasReleased(); if (endedHoldingMovableElement != null) { endedHoldingMovableElement(slidingElement); } } } if (Input.GetMouseButtonDown(0)) { dragStartPosition = Input.mousePosition; RaycastHit hit; Ray ray = mainCamera.ScreenPointToRay(dragStartPosition); if (Physics.Raycast(ray, out hit, 1000, layer_mask)) { if (hit.collider.gameObject.GetComponent <SimpleMovableElement> () != null) { currentSlidingElement = hit.collider.gameObject.GetComponent <SimpleMovableElement> (); if (currentSlidingElement != null) { slidingElement = currentSlidingElement; //startHoldingSlidingElement slidingElement.hasClicked(); if (startedHoldingMovableElement != null) { startedHoldingMovableElement(slidingElement); } } dragStartDistance = (mainCamera.transform.position - currentSlidingElement.transform.position).magnitude; } } } pointer_x = Input.GetAxisRaw("Mouse X"); pointer_y = Input.GetAxisRaw("Mouse Y"); if (Input.touchCount > 0) { pointer_x = Input.touches [0].deltaPosition.x; pointer_y = Input.touches [0].deltaPosition.y; } if (pointer_x != 0 || pointer_y != 0) { float dragDistance = (dragStartPosition - new Vector2(Input.mousePosition.x, Input.mousePosition.y)).magnitude; if (currentSlidingElement != null) { if (dragDistance > 5f) { if (dragging == false) { //StartDragging slidingElement.startDragging(); if (startedDragging != null) { startedDragging(slidingElement); } } dragging = true; dragPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, dragStartDistance)); slidingElement.isDragging(dragPosition); if (isDraggingMovableElement != null) { isDraggingMovableElement(slidingElement, dragPosition); } } else { dragging = false; } } else { //Not Sliding a movalble Element if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) || Input.GetMouseButton(0)) { if (isDragging != null) { if (dragDistance > 5f) { isDragging(pointer_x, pointer_y); draggingNotMovable = true; } } } } } } }
public void updateMoveRange() { Vector3[] directions = { Vector3.forward, Vector3.back, Vector3.left, Vector3.right, Vector3.up, Vector3.down }; List <Vector3> moveRange = new List <Vector3> (); moveRange.Add(transform.position); bool playerInsideElement = false; List <Vector3> playerOnTopRange = new List <Vector3> (); SimplePathElement firstPlayerPositionElement = null; getLevel().GetAllElements().TryGetValue(transform.position + Vector3.up, out firstPlayerPositionElement); if (firstPlayerPositionElement == null) { playerOnTopRange.Add(transform.position); } else { playerInsideElement = true; } sliderOnMaxWayX = null; sliderOnMinWayX = null; sliderOnMaxWayY = null; sliderOnMinWayY = null; sliderOnMaxWayZ = null; sliderOnMinWayZ = null; foreach (Vector3 direction in directions) { bool hasFoundWay; Vector3 newDirection = direction; bool playerRangeEnded = playerInsideElement; do { hasFoundWay = false; Vector3 walkPosition = transform.position + newDirection; Vector3 playerElementPosition = walkPosition + Vector3.up; SimplePathElement walkElement = null; SimplePathElement playerPositionElement = null; getLevel().GetAllElements().TryGetValue(walkPosition, out walkElement); getLevel().GetAllElements().TryGetValue(playerElementPosition, out playerPositionElement); if (direction != Vector3.up && getLevel().GetPlayer().isPositionInWalkingPath(walkPosition)) { hasFoundWay = false; continue; } if (walkElement != null) { if (walkElement.gameObject.GetComponent <SimpleMovableElement>() != null) { if (getSliderOnWayByDirection(direction) == null && !walkElement.gameObject.Equals(gameObject)) { setSliderOnWayByDirection(direction, walkElement.gameObject.GetComponent <SimpleMovableElement>()); } } } //Normal Walk if (walkElement == null || (walkElement.gameObject.GetComponent <SimpleMovableElement>() != null)) { if (getLevel().getRange().isPointInRange(walkPosition)) { if (direction != Vector3.down && !playerRangeEnded) { if (playerPositionElement == null) { playerOnTopRange.Add(transform.position + newDirection); } else { playerRangeEnded = true; } } if (direction == Vector3.down) { if (playerPositionElement == this || playerPositionElement == null) { playerOnTopRange.Add(transform.position + newDirection); } else { playerRangeEnded = true; } } moveRange.Add(transform.position + newDirection); hasFoundWay = true; } } newDirection = newDirection + direction; } while(hasFoundWay); } movementRange.setRangeFromArray(moveRange.ToArray()); playerOnTopMovementRange.setRangeFromArray(playerOnTopRange.ToArray()); }