public void addBox(Box box,int row,int column) { if (boxes[row, column] == null) { //box.HorizontalAlignment = HorizontalAlignment.Left; //box.VerticalAlignment = VerticalAlignment.Top; box.RenderTransform = new TranslateTransform(0,0); box.SetValue(Grid.ColumnProperty, column); box.SetValue(Grid.RowProperty, row); boxes[row, column] = box; boxPositions[box] = new Int32Point(column, row); Children.Add(box); } }
public void loadBoxLayer(TileLayer tileLayer) { boxLayer = new BoxLayer(tileMap.height, tileMap.width, tileMap.tileheight, tileMap.tilewidth); for (int i = 0; i < tileLayer.data.Length; i++) { if (tileLayer.data[i] != 0) { int x = i % tileMap.width; int y = i / tileMap.height; bool canDestroy = tileProp.ContainsKey(tileLayer.data[i]-1) && tileProp[tileLayer.data[i]-1].canDestroy!=null; bool canKick = tileProp.ContainsKey(tileLayer.data[i]-1) && tileProp[tileLayer.data[i]-1].canKick != null; Box box = new Box(canDestroy,canKick); box.Source = tileImgSrcs[tileLayer.data[i] - 1]; boxLayer.addBox(box, y, x); } } }
public bool moveBoxOne(Box box,WalkDirection dir,Player player) { Int32Point boxPos = boxPositions[box]; if (boxPos == null) { return false; } Int32Point newBoxPos = new Int32Point(boxPos.x, boxPos.y); Box neighborBox = null; switch (dir) { case WalkDirection.DOWN: if (boxPos.y + 1 < boxes.GetLength(0) && scene.obstacleLayer.obstacles[boxPos.y+1,boxPos.x]==null) { if (boxes[boxPos.y + 1, boxPos.x] != null && boxes[boxPos.y + 1, boxPos.x].canKick) { neighborBox = boxes[boxPos.y + 1, boxPos.x]; } } else { return false; } break; case WalkDirection.UP: if (boxPos.y - 1 >= 0 && scene.obstacleLayer.obstacles[boxPos.y - 1, boxPos.x] == null) { if (boxes[boxPos.y - 1, boxPos.x] != null && boxes[boxPos.y - 1, boxPos.x].canKick) { neighborBox = boxes[boxPos.y - 1, boxPos.x]; } } else { return false; } break; case WalkDirection.LEFT: if (boxPos.x - 1 >= 0 && scene.obstacleLayer.obstacles[boxPos.y,boxPos.x-1]==null) { if (boxes[boxPos.y, boxPos.x - 1] != null && boxes[boxPos.y, boxPos.x - 1].canKick) { neighborBox = boxes[boxPos.y, boxPos.x - 1]; } } else { return false; } break; case WalkDirection.RIGHT: if (boxPos.x + 1 < boxes.GetLength(1) && scene.obstacleLayer.obstacles[boxPos.y,boxPos.x+1]==null) { if (boxes[boxPos.y, boxPos.x + 1] != null && boxes[boxPos.y, boxPos.x + 1].canKick) { neighborBox = boxes[boxPos.y, boxPos.x + 1]; } } else { return false; } break; } if (neighborBox != null ) { if (!moveBoxOne(neighborBox, dir, player)) { return false; } } box.canKick = false; DoubleAnimation ani = null; switch (dir) { case WalkDirection.DOWN: case WalkDirection.UP: ani = createBoxMoveAni(box, dir, player.speed, scene.tileHeight); break; case WalkDirection.RIGHT: case WalkDirection.LEFT: ani = createBoxMoveAni(box, dir, player.speed, scene.tileWidth); break; } Storyboard stb = new Storyboard(); stb.Children.Add(ani); ani.Completed+=delegate(object sender,EventArgs e) { switch (dir) { case WalkDirection.DOWN: newBoxPos.y += 1; break; case WalkDirection.UP: newBoxPos.y -= 1; break; case WalkDirection.LEFT: newBoxPos.x -= 1; break; case WalkDirection.RIGHT: newBoxPos.x += 1; break; } box.RenderTransform = new TranslateTransform(0, 0); box.SetValue(Grid.RowProperty,newBoxPos.y); box.SetValue(Grid.ColumnProperty, newBoxPos.x); boxes[boxPos.y, boxPos.x] = null; boxes[newBoxPos.y, newBoxPos.x] = box; boxPositions[box] = newBoxPos; box.canKick = true; }; stb.Begin(); return true; }
private DoubleAnimation createBoxMoveAni(Box box,WalkDirection dir,double walkSpped, double by) { DoubleAnimation retVal = new DoubleAnimation(); Storyboard.SetTarget(retVal, box); switch (dir) { case WalkDirection.DOWN: retVal.By = scene.tileHeight; Storyboard.SetTargetProperty(retVal, new PropertyPath("RenderTransform.(TranslateTransform.Y)")); break; case WalkDirection.RIGHT: retVal.By = scene.tileWidth; Storyboard.SetTargetProperty(retVal, new PropertyPath("RenderTransform.(TranslateTransform.X)")); break; case WalkDirection.LEFT: retVal.By = -scene.tileWidth; Storyboard.SetTargetProperty(retVal, new PropertyPath("RenderTransform.(TranslateTransform.X)")); break; case WalkDirection.UP: retVal.By = -scene.tileHeight; Storyboard.SetTargetProperty(retVal, new PropertyPath("RenderTransform.(TranslateTransform.Y)")); break; } retVal.Duration = TimeSpan.FromSeconds(1 / walkSpped); retVal.FillBehavior = FillBehavior.HoldEnd; return retVal; }
public void destroy(Box box) { Int32Point pos = boxPositions[box]; if (pos == null) { return; } boxes[pos.y, pos.x] = null; boxPositions[box] = null; Children.Remove(box); }