- Timestamp:
- 2011-12-09 18:33:32 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.cs
r2627 r2632 16 16 private int playerX = 4; 17 17 private int playerY = 4; 18 19 20 private Dictionary<char, Color> charColor = new Dictionary<char, Color>();18 19 20 private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>(); 21 21 private readonly Color playerColor = Color.Blue; 22 22 private readonly Color satisfyColor = Color.Green; 23 23 private readonly Color unsatisfyColor = Color.Red; 24 24 25 private List<Block> goals = new List<Block>(); 26 25 27 //[x,y] 26 private GameObject[,] playfield;28 private Block[,] playfield; 27 29 28 30 private string[] testLevel = {"xxxxxxxxx", 29 31 "x x xmx", 30 32 "xb s xbx", 31 "x x x", 32 "xm xxxxxx", 33 "xm x x", 34 "x xxxxxx", 35 "x xx", 33 36 "xxxxxxxxx"}; 34 37 … … 44 47 public void InitCharColors() 45 48 { 46 charColor.Add('x', Color.Black);47 charColor.Add('b', Color.Beige);48 charColor.Add(' ', Color.Transparent);49 charColor.Add('m', unsatisfyColor);50 charColor.Add('s', playerColor);49 charColor.Add('x', BlockType.UnMovable); 50 charColor.Add('b', BlockType.Movable); 51 charColor.Add(' ', BlockType.Empty); 52 charColor.Add('m', BlockType.GoalZone); 53 charColor.Add('s', BlockType.Player); 51 54 } 52 55 … … 58 61 public void MovePlayer(int x, int y) 59 62 { 60 if (IsPassable(playerX + x, playerY + y)) 61 { 62 UpdatePlayer(Color.Transparent); 63 playerX += x; 64 playerY += y; 65 UpdatePlayer(playerColor); 63 switch (Type(playerX + x, playerY + y)) 64 { 65 case BlockType.SatisfiedGoalZone: 66 case BlockType.Movable: 67 int nextX = playerX + x; 68 if (x != 0) { nextX += (x / Math.Abs(x)); } 69 int nextY = playerY + y; 70 if (y != 0) { nextY += (y / Math.Abs(y)); } 71 BlockType t = Type(nextX, nextY); 72 if (t == BlockType.Empty || t == BlockType.GoalZone || t == BlockType.SatisfiedGoalZone) 73 { 74 Move(playerX + x, playerY + y, nextX, nextY); 75 Move(playerX, playerY, playerX + x, playerY + y); 76 playerX += x; 77 playerY += y; 78 } 79 80 break; 81 case BlockType.Empty: 82 Move(playerX, playerY, playerX+x, playerY+y); 83 playerX += x; 84 playerY += y; 85 break; 86 default: 87 break; 88 } 89 90 if (VictoryCheck()) 91 { 92 MessageDisplay.Add("You Win!"); 93 } 94 } 95 96 /// <summary> 97 /// Moves block from place a to place b 98 /// 99 /// Don't move walls! 100 /// </summary> 101 /// <param name="startx">Source X coordinate</param> 102 /// <param name="starty">Source Y coordinate</param> 103 /// <param name="endx">Destination X coordinate</param> 104 /// <param name="endy">Destination Y coordinate</param> 105 public void Move(int startx, int starty, int endx, int endy) 106 { 107 //TODO Works, but fugly. 108 BlockType destType = playfield[endx, endy].Type; 109 BlockType sourType = playfield[startx, starty].Type; 110 bool destGoal = playfield[endx, endy].IsGoalZone; 111 bool sourGoal = playfield[startx, starty].IsGoalZone; 112 113 if (sourGoal) 114 { 115 playfield[startx, starty].Type = BlockType.GoalZone; 116 } 117 else 118 { 119 playfield[startx, starty].Type = BlockType.Empty; 120 } 121 122 if (destGoal) 123 { 124 if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; } 125 if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; } 126 if (sourType == BlockType.Movable) { playfield[endx, endy].Type = BlockType.SatisfiedGoalZone; } 127 } 128 else 129 { 130 if (sourType == BlockType.Player) { playfield[endx, endy].Type = BlockType.Player; } 131 if (sourType == BlockType.SatisfiedGoalZone) { playfield[endx, endy].Type = BlockType.Movable; } 66 132 } 67 133 } … … 69 135 /// <summary> 70 136 /// Checks if given coordinates are empty and can be moved to. 137 /// 138 /// If coordinates are not in playfield, UnMovable is returned. 71 139 /// </summary> 72 140 /// <param name="x">X-coordinate</param> 73 141 /// <param name="y">Y-coordinate</param> 74 /// <returns>True, if slot in given coordinates is passable</returns> 75 public bool IsPassable(int x, int y) 76 { 77 if (x < 0) return false; 78 if (y < 0) return false; 79 if (x >= playfieldWidth) return false; 80 if (y >= playfieldHeight) return false; 81 return playfield[x, y].Color == Color.Transparent; 142 /// <returns>State of block at given coordinates</returns> 143 public BlockType Type(int x, int y) 144 { 145 if (x < 0) return BlockType.UnMovable; 146 if (y < 0) return BlockType.UnMovable; 147 if (x >= playfieldWidth) return BlockType.UnMovable; 148 if (y >= playfieldHeight) return BlockType.UnMovable; 149 150 return playfield[x, y].Type; 82 151 } 83 152 … … 86 155 /// </summary> 87 156 /// <param name="color">Color</param> 88 public void UpdatePlayer( Color color)89 { 90 playfield[playerX, playerY]. Color = color;157 public void UpdatePlayer(BlockType type) 158 { 159 playfield[playerX, playerY].Type = type; 91 160 } 92 161 … … 100 169 playfieldWidth = level[0].Length; 101 170 playfieldHeight = level.Length; 102 playfield = new GameObject[playfieldWidth,playfieldHeight];171 playfield = new Block[playfieldWidth,playfieldHeight]; 103 172 Level.Size = new Vector(playfieldWidth * (blocksize + 1), playfieldHeight * (blocksize + 1)); 104 173 Level.CreateBorders(); 105 174 106 for (int i = 0; i < playfield.Length; i++) 107 { 108 GameObject block = new GameObject(blocksize, blocksize); 109 block.X = (i % playfieldHeight) * (blocksize + 1) + Level.Left + (blocksize/2); 110 block.Y = (i / playfieldWidth) * (blocksize + 1) + Level.Bottom + (blocksize / 2); 111 112 block.Color = charColor[level[i / playfieldWidth][i % playfieldWidth]]; 113 Add(block); 114 playfield[i % playfieldWidth, i / playfieldWidth] = block; 115 } 116 UpdatePlayer(playerColor); 175 for (int i = 0; i < playfield.GetLength(0); i++) 176 { 177 for (int j = 0; j < playfield.GetLength(1); j++) 178 { 179 Block block = new Block(blocksize, blocksize); 180 block.X = i * (blocksize + 1) + Level.Left + (blocksize / 2); 181 block.Y = -(j * (blocksize + 1) + Level.Bottom + (blocksize / 2)); 182 183 block.Type = charColor[level[j][i]]; 184 if (block.Type == BlockType.Player) 185 { 186 playerX = i; 187 playerY = j; 188 } 189 if (block.IsGoalZone) 190 { 191 goals.Add(block); 192 } 193 Add(block); 194 playfield[i,j] = block; 195 } 196 } 197 UpdatePlayer(BlockType.Player); 198 } 199 200 /// <summary> 201 /// Checks if level is completed. 202 /// </summary> 203 /// <returns></returns> 204 public bool VictoryCheck() 205 { 206 foreach (var item in goals) 207 { 208 if (!(item.Type == BlockType.SatisfiedGoalZone)) return false; 209 } 210 return true; 117 211 } 118 212 … … 131 225 public void SetControls() 132 226 { 133 Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, 1);134 Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, -1);227 Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, -1); 228 Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, 1); 135 229 Keyboard.Listen(Key.Left, ButtonState.Pressed, MovePlayer, null, -1, 0); 136 230 Keyboard.Listen(Key.Right, ButtonState.Pressed, MovePlayer, null, 1, 0);
Note: See TracChangeset
for help on using the changeset viewer.