void CheckCollision(Collision2D collision2D) { if (collision2D.collider.TryGetComponent(out Food food)) { return; } if (IsWall(collision2D, out ContactPoint2D contactPoint)) { _wall = collision2D.collider.GetComponentInParent <LevelElement>(); _lastWallNormal = contactPoint.normal; SetState(PlayerState.SlideInWall); if (State == PlayerState.Walk) { MirrorScale(_lastWallNormal); } return; } if (IsFloor(collision2D, out contactPoint)) { if (_floorPoint.position.y < contactPoint.point.y) { return; } _floor = collision2D.collider.GetComponentInParent <LevelElement>();; SetState(PlayerState.Walk); } }
bool TrySpawnElement(LevelElement prefabElement, List <Bounds> gridRow) { if (!prefabElement) { return(false); } var findPos = FindRandomPosition(prefabElement.Bounds, gridRow, out var position); if (!findPos) { return(false); } var elementBounds = prefabElement.Bounds; elementBounds.center = position; if (IsIntersectsWithElement(elementBounds)) { return(false); } var element = Instantiate(prefabElement, transform); Transform elementTransform; var localCenter = (Vector2)(elementTransform = element.transform).InverseTransformPoint(element.Bounds.center); position -= localCenter; elementTransform.position = position; element.TryMirror(); _allElements.Add(element); EventManager.Fire(new SpawnLevelElementEvent(element)); return(true); }
void OnCollisionExit2D(Collision2D other) { var levelElement = other.collider.GetComponentInParent <LevelElement>();; if (levelElement == _floor) { _floor = null; } if (levelElement == _wall) { _wall = null; } if (!_floor && !_wall) { SetState(PlayerState.Flight); } }