//Create public method "Move(Vector2D direction)" to move the object in the given direction public virtual void Move(Vector2D direction) { if (direction.X <= 1 && direction.X >= -1 && direction.Y <= 1 && direction.Y >= -1) { position += direction; } }
private static List<GameObject> BuildBoxObjectsList() { List<GameObject> objectsList = new List<GameObject>(); foreach (int[] objParams in MapBuilder.AbstractMap.GameObjects["box"]) { int x = objParams[0]; int y = objParams[1]; Vector2D coordinates = new Vector2D(x, y); objectsList.Add(new Box(coordinates)); } return objectsList; }
public ConsoleRenderer(int visibleConsoleRows, int visibleConsoleCols, Vector2D origin) { renderContextMatrix = new char[visibleConsoleRows, visibleConsoleCols]; this.renderContextMatrixRows = renderContextMatrix.GetLength(0); this.renderContextMatrixCols = renderContextMatrix.GetLength(1); this.origin = origin; Console.Title = "Sokoban"; Console.CursorVisible = false; Console.OutputEncoding = UnicodeEncoding.Unicode; this.ClearQueue(); this.Clear(); }
public bool Equals(Vector2D otherVector) { return (this.X == otherVector.X && this.Y == otherVector.Y); }
// Methods // --- Constructors public Player(Vector2D position, int playerCount) : base(position) { this.body = images[playerCount % 2]; }
private static Vector2D BuildSize() { int width = MapBuilder.AbstractMap.Size[0]; int height = MapBuilder.AbstractMap.Size[1]; Vector2D size = new Vector2D(width, height); return size; }
private static List<GameObject> BuildPlayerObjectsList() { List<GameObject> objectsList = new List<GameObject>(); foreach (int[] objParams in MapBuilder.AbstractMap.GameObjects["player"]) { int x = objParams[0]; int y = objParams[1]; Vector2D coordinates = new Vector2D(x, y); objectsList.Add(new Player(coordinates, MapBuilder.AbstractMap.GameObjects["player"].Count)); } return objectsList; }
private static Vector2D BuildOrigin() { int x = MapBuilder.AbstractMap.Origin[0]; int y = MapBuilder.AbstractMap.Origin[1]; Vector2D origin = new Vector2D(x, y); return origin; }
protected GameObject(Vector2D position) { this.position = position; this.body = ' '; this.IsVisible = true; }
protected StaticObject(Vector2D position) : base(position) { }
protected MoveableObject(Vector2D position) : base(position) { }
//Wrapper method for all methods executed when objects move private void PerformMove(Vector2D direction, int playerType) { HandleCollisions(direction,playerType); DetermineTargetVisibility(); RenderAllObjects(); hasPlayerWon = Referee.CheckForWin(allObjects["target"], allObjects["box"]); }
//Move objects according to collision occurrence private void HandleCollisions(Vector2D direction, int playerType) { //Get a player refference Player player; if (allObjects.Keys.Contains("player")) { if (playerType != 0) { if (allObjects["player"].Count <= 1) { return; } player = (Player)allObjects["player"][playerType % 2]; } else { player = (Player)allObjects["player"][0]; } } else { throw new ArgumentException("No players in game"); } List<GameObject> movingObjects = new List<GameObject>(); //Objects that are enqueued to be moved List<GameObject> movedObjects = new List<GameObject>(); //Objects that have already been moved movingObjects.Add(player); int count = 0; // Number of boxes that have once been added to movingObjects list. bool collisionFound = false; //Perform moving objects while (movingObjects.Count > 0 && count <= NumberPushableBoxes && !collisionFound) { MoveableObject objectToMove = (MoveableObject)movingObjects.ElementAt(0); //Take the first object that has to be moved movingObjects.RemoveAt(0); //Remove that object from the list objectToMove.Move(direction); //Move the object movedObjects.Add(objectToMove); //Add the object to movedObjects list List<GameObject> walls = CollisionDetector.CheckForCollision(objectToMove, allObjects["wall"]); //Check for collisions with walls List<GameObject> otherPlayers = CollisionDetector.CheckForCollision(objectToMove, allObjects["player"]); //Check for collisions with other players if (walls.Count > 0 || otherPlayers.Count > 0) { collisionFound = true; } List<GameObject> boxes = CollisionDetector.CheckForCollision(objectToMove, allObjects["box"]); //Check for collisions with boxes movingObjects.AddRange(boxes); count += boxes.Count; } //Check if moving objects is valid. If not - move them back. if (movingObjects.Count > 0 || collisionFound) { Vector2D oppositeDirection = direction * (-1); foreach (MoveableObject obj in movedObjects) //Could occur problem because movedObjects are GameObjects { obj.Move(oppositeDirection); } } }
// Methods // --- Constructors public Target(Vector2D position) : base(position) { this.body = 'x'; }
// Methods // --- Constructors public Box(Vector2D position) : base(position) { this.body = '\u25a0'; }
// Methods // --- Constructors public Wall(Vector2D position) : base(position) { this.body = '\u2593'; }