// Ensures objects visual positions are sync'd with their state private void PositionObjects(LevelStateFrame frame) { foreach (ObjectLevelState objectState in frame.Objects) { ObjectWithPosition pos = objectState.Object; pos.transform.position = constants.GetObjectPosition(pos.transform, pos.X, pos.Y); } Vector3 cameraEndPos = cameraRootPosition + new Vector3(0, -frame.YCamera * constants.CelHeight, 0); CameraTransform.localPosition = cameraEndPos; }
// for going back public void PushStates() { LevelStateFrame current = Current; for (int i = 0; i < current.Objects.Length; i++) { ObjectWithPosition pos = current.Objects[i].Object; pos.X = current.Objects[i].X; pos.Y = current.Objects[i].Y; pos.Data = current.Objects[i].Data; pos.Direction = current.Objects[i].Direction; } }
void Update() { Vector2 pos = new Vector2(); if (Input.touchCount > 0) { if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved) { pos = Input.GetTouch(0).position; } } else { pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); } Vector3 position = Camera.main.ScreenToWorldPoint( new Vector3(pos.x, pos.y, cameraDistance) ); ObjectWithPosition owp = new ObjectWithPosition(); owp.name = "Mouse"; owp.x = position.x; owp.y = position.y; string mousePos = JsonUtility.ToJson(owp); //Debug.Log(">>> " + mousePos); if (pipe.ready) { pipe.pipeSendData(mousePos); } if (pipe.isIncomingData) { string data = pipe.pipeReceiveData(); ObjectWithPosition myOwp = JsonUtility.FromJson <ObjectWithPosition>(data); Debug.Log("<<< " + data); if (myOwp.name == "Mouse") { gameObject.transform.position = new Vector3(myOwp.x, myOwp.y, 0); } } }
public static bool CanMove_AndMaybePush(ObjectWithPosition obj, int dx, int dy, GeneratedLevel level, List <ObjectWithPosition> objects, bool[] hasMovedWorker) { int xDest = obj.X + dx; int yDest = obj.Y + dy; if (!level.IsUnblocked(xDest, yDest)) { return(false); } // If it's not blocked by physical geometry, maybe it's blocked by an object for (int i = 0; i < objects.Count; i++) { if (!hasMovedWorker[i] && (objects[i] != obj)) // Although we shouldn't be too worried about ourselves? { if ((objects[i].X == xDest) && (objects[i].Y == yDest)) // It's in our way { if (DoObjectsCollide(obj, objects[i])) { // It's blocking us. Can we move it? if (IsPushable(objects[i]) && CanMove_AndMaybePush(objects[i], dx, dy, level, objects, hasMovedWorker)) { // Ok then. Move it. objects[i].X += dx; objects[i].Y += dy; hasMovedWorker[i] = true; // Mark it moved. } else { // Nope. So it's blocking us. return(false); } } } } } return(true); }
private void Awake() { pos = GetComponent <ObjectWithPosition>(); renderer = GetComponent <SpriteRenderer>(); }
public static GeneratedLevel Generate(ref Constants constants, Level level, ref GameObject playerObject, Transform fixedParent, LevelStateFrame initialFrame, List <ObjectWithPosition> objectsWithPosition) { // Make the ceiling (fixed to camera) Vector3 ceilingPiecePos = constants.TopLeft; Vector3 floorPiecePos = constants.TopLeft; ceilingPiecePos.z = -1f; // In front of stuff floorPiecePos.z = -1f; floorPiecePos.y -= (constants.Height - 1) * constants.CelHeight; for (int i = 0; i < constants.Width; i++) { GameObject.Instantiate(level.LevelConstants.DeathCeiling, ceilingPiecePos, Quaternion.identity, fixedParent); GameObject.Instantiate(level.LevelConstants.DeathFloor, floorPiecePos, Quaternion.identity, fixedParent); ceilingPiecePos.x += constants.CelWidth; floorPiecePos.x += constants.CelWidth; } GeneratedLevel generatedLevel = new GeneratedLevel(); generatedLevel.Level = level; JsonLevel jsonLevel = JsonLevel.CreateFromJSON(level.Data.text); int[] backgroundData = jsonLevel.GetLayerData(Constants.BackgroundLayer); int[] objectsLayer = jsonLevel.GetLayerData(Constants.ObjectsLayer); int[] waterLayer = jsonLevel.GetLayerData(Constants.WaterLayer); string data = level.Data.text; Vector2 celPosition = constants.TopLeft; generatedLevel.Height = jsonLevel.height; generatedLevel.Width = jsonLevel.width; generatedLevel.Pieces = backgroundData; generatedLevel.Water = waterLayer; if (waterLayer == null) { generatedLevel.Water = new int[generatedLevel.Pieces.Length]; } int row = 0; int column = 0; celPosition.x = constants.TopLeft.x; int celCount = backgroundData.Length; for (int i = 0; i < celCount; i++) { int tile = backgroundData[i]; switch (tile) { case Constants.WallPiece: GameObject.Instantiate(level.LevelConstants.Wall, celPosition, Quaternion.identity, constants.Parent); break; case Constants.SupportPiece: GameObject.Instantiate(level.LevelConstants.WallSupport, celPosition, Quaternion.identity, constants.Parent); break; case Constants.Exit: GameObject.Instantiate(level.LevelConstants.Exit, celPosition, Quaternion.identity, constants.Parent); break; } int maybeObject = objectsLayer[i]; GameObject createdObject = null; switch (maybeObject) { case Constants.PlayerPiece: { if (!playerObject) { playerObject = GameObject.Instantiate(level.LevelConstants.Player, celPosition, Quaternion.identity, constants.Parent); } else { playerObject.transform.position = celPosition; } createdObject = playerObject; } break; case Constants.DraggableSupport: { createdObject = GameObject.Instantiate(level.LevelConstants.DraggableSupport, celPosition, Quaternion.identity, constants.Parent); } break; case Constants.Crate: { createdObject = GameObject.Instantiate(level.LevelConstants.Crate, celPosition, Quaternion.identity, constants.Parent); } break; } if (generatedLevel.Water[i] != 0) { GameObject.Instantiate(level.LevelConstants.Water, celPosition, Quaternion.identity, constants.Parent); } if (createdObject) { ObjectWithPosition pc = createdObject.GetComponent <ObjectWithPosition>(); objectsWithPosition.Add(pc); pc.X = column; pc.Y = row; } column++; if (column >= generatedLevel.Width) { column = 0; celPosition.x = constants.TopLeft.x; row++; celPosition.y -= constants.CelHeight; } else { celPosition.x += constants.CelWidth; } } // Sort objectsWithPosition.Sort( (a, b) => a.EvaluationOrder.CompareTo(b.EvaluationOrder) ); initialFrame.Objects = new ObjectLevelState[objectsWithPosition.Count]; for (int i = 0; i < objectsWithPosition.Count; i++) { initialFrame.Objects[i].Object = objectsWithPosition[i]; } return(generatedLevel); }
private void Awake() { pos = GetComponent <ObjectWithPosition>(); }
public static bool DoObjectsCollide(ObjectWithPosition obj1, ObjectWithPosition obj2) { return((obj1.CollisionLayer & obj2.CollisionLayer) != CollisionLayer.None); }
public static bool IsPushable(ObjectWithPosition obj) { return(obj.GetComponent <Draggable>()); }
IEnumerator LerpToNewResults(PlayerController pc, MoveResult moveResult) { ObjectWithPosition playerPos = pc.GetComponent <ObjectWithPosition>(); isLerpingMove = true; LevelStateFrame currentFrame = levelState.Current; // First lerp the player and other objects to the left or right. startLerpWorker.Clear(); endLerpWorker.Clear(); foreach (ObjectLevelState objectState in currentFrame.Objects) { startLerpWorker.Add(objectState.Object.transform.position); //endLerpWorker.Add(constants.GetObjectPositionX(objectState.Object.transform, objectState.Object.X)); endLerpWorker.Add(constants.GetObjectPosition(objectState.Object.transform, objectState.Object.XIntermediate, objectState.Object.YIntermediate)); } { float time = 0; while (time < MechanicParameters.PlayerMoveTime) { time += Time.deltaTime; time = Mathf.Min(MechanicParameters.PlayerMoveTime, time); for (int i = 0; i < startLerpWorker.Count; i++) { ObjectWithPosition objectWithPos = currentFrame.Objects[i].Object; objectWithPos.transform.position = Vector3.Lerp(startLerpWorker[i], endLerpWorker[i], MechanicParameters.PlayerMoveCurve.Evaluate(time / MechanicParameters.PlayerMoveTime) ); } yield return(null); } } // Now do the camera, and objects verticals startLerpWorker.Clear(); endLerpWorker.Clear(); foreach (ObjectLevelState objectState in currentFrame.Objects) { startLerpWorker.Add(objectState.Object.transform.position); endLerpWorker.Add(constants.GetObjectPositionY(objectState.Object.transform, objectState.Object.Y)); } { // Now move the camera (this might kill the player) Vector3 cameraStartPos = CameraTransform.localPosition; Vector3 cameraEndPos = cameraRootPosition + new Vector3(0, -currentFrame.YCamera * constants.CelHeight, 0); float time = 0; while (time < MechanicParameters.LevelFallTime) { time += Time.deltaTime; time = Mathf.Min(MechanicParameters.LevelFallTime, time); CameraTransform.localPosition = Vector3.Lerp(cameraStartPos, cameraEndPos, MechanicParameters.LevelFallCurve.Evaluate(time / MechanicParameters.LevelFallTime) ); for (int i = 0; i < startLerpWorker.Count; i++) { ObjectWithPosition objectWithPos = currentFrame.Objects[i].Object; objectWithPos.transform.position = Vector3.Lerp(startLerpWorker[i], endLerpWorker[i], MechanicParameters.PlayerMoveCurve.Evaluate(time / MechanicParameters.PlayerMoveTime) ); } yield return(null); } } HandleMoveResultDelayed(moveResult); isLerpingMove = false; }
// Handles falling. private MoveResult ProcessMoveResultsImmediately(PlayerController pc) { MoveResult result = MoveResult.None; // Add a new state frame levelState.AddFrame(); // Can we descend the camera? DescendCamera(levelState.Current); // Player falls, maybe - unless they hit water? // This is a bit tricky, because we also need to push things down. ObjectWithPosition thePlayer = pc.GetComponent <ObjectWithPosition>(); bool somethingMoved = true; bool bail = false; foreach (ObjectWithPosition pos in levelState.Objects) { // Store the transitionary states so we can do nice lerping. pos.XIntermediate = pos.X; pos.YIntermediate = pos.Y; } int fallAmount = 0; while (somethingMoved && !bail) { somethingMoved = false; ClearMoveWorker(); for (int i = 0; i < levelState.Objects.Count; i++) { ObjectWithPosition pos = levelState.Objects[i]; Gravity gravity = pos.GetComponent <Gravity>(); int direction = gravity ? gravity.Direction : 0; bool isWater = generatedLevel.IsWater(pos.X, pos.Y); if ((!isWater) && direction != 0) // Things float in water. { int x = pos.X; int y = pos.Y + direction; //if (generatedLevel.IsBlocked(x, y)) if (MoveHelper.CanMove_AndMaybePush(pos, 0, 1, generatedLevel, levelState.Objects, moveWorker)) { pos.Y = y; somethingMoved = true; } } if (isWater) { pos.Direction = 0; // Resets direction } if (thePlayer == pos) { if ((GetBottomDeathRow() == pos.Y) || (pos.Y <= levelState.Current.YCamera)) { result = MoveResult.Death; bail = true; break; // They're gonna die. } if (Constants.Exit == generatedLevel.GetWorldPieceAt(pos.X, pos.Y)) { result = MoveResult.Exit; bail = true; break; } if (somethingMoved) { fallAmount++; if (fallAmount > 3) { FallSound.Play(); } } } } } levelState.SaveStates(); // Does that for the objects only, camera is already updated. return(result); }