Changeset 2633
- Timestamp:
- 2011-12-10 15:12:19 (11 years ago)
- Location:
- 2010/23/hniemi/BlockPusher
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.cs
r2632 r2633 6 6 using Jypeli.Effects; 7 7 using Jypeli.Widgets; 8 using System.IO; 8 9 9 10 public class Blockpusher : Game 10 11 { 11 p rivate const int blocksize = 20;12 public static readonly int BlockSize = 20; 12 13 13 private int playfieldWidth = 20;14 private int playfieldHeight = 20;15 16 private int playerX = 4;17 private int playerY = 4;18 19 20 private Dictionary<char, BlockType> charColor = new Dictionary<char, BlockType>();21 14 private readonly Color playerColor = Color.Blue; 22 15 private readonly Color satisfyColor = Color.Green; 23 16 private readonly Color unsatisfyColor = Color.Red; 24 17 25 private List<Block> goals = new List<Block>();18 private BlockLevel currentLevel; 26 19 27 //[x,y] 28 private Block[,] playfield; 29 30 private string[] testLevel = {"xxxxxxxxx", 20 private List<String> testLevel = new List<string>(new String[] 21 {"xxxxxxxxx", 31 22 "x x xmx", 32 "x b sxbx",23 "xs p xbx", 33 24 "xm x x", 34 25 "x xxxxxx", 35 26 "x xx", 36 "xxxxxxxxx"} ;27 "xxxxxxxxx"}); 37 28 38 29 public override void Begin() 39 30 { 40 InitCharColors();41 31 PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 42 32 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 43 CreatePlayfield(testLevel); 33 34 currentLevel = new BlockLevel(this); 35 currentLevel.CreateField(testLevel); 44 36 SetControls(); 45 37 } 46 38 47 public void InitCharColors() 39 40 41 /// <summary> 42 /// Reads lines from file 43 /// </summary> 44 /// <param name="file">Sourcefile</param> 45 public List<String> ReadFile(string file) 48 46 { 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); 47 List<String> lines = new List<string>(); 48 try 49 { 50 using (StreamReader sr = new StreamReader(file)) 51 { 52 String line; 53 while ((line = sr.ReadLine()) != null) 54 { 55 lines.Add(line); 56 } 57 } 58 } 59 catch (Exception e) 60 { 61 return null; 62 } 63 return lines; 54 64 } 55 65 56 66 /// <summary> 57 /// Moves player x and y steps67 /// Moves player in current level 58 68 /// </summary> 59 /// <param name="x ">X Movement</param>60 /// <param name="y ">Y Movement</param>61 public void MovePlayer(int x , int y)69 /// <param name="xMov">X-movement</param> 70 /// <param name="yMov">Y-movement</param> 71 public void MovePlayer(int xMov, int yMov) 62 72 { 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; } 132 } 133 } 134 135 /// <summary> 136 /// Checks if given coordinates are empty and can be moved to. 137 /// 138 /// If coordinates are not in playfield, UnMovable is returned. 139 /// </summary> 140 /// <param name="x">X-coordinate</param> 141 /// <param name="y">Y-coordinate</param> 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; 151 } 152 153 /// <summary> 154 /// Changes color of players coordinates 155 /// </summary> 156 /// <param name="color">Color</param> 157 public void UpdatePlayer(BlockType type) 158 { 159 playfield[playerX, playerY].Type = type; 160 } 161 162 /// <summary> 163 /// Creates new gamefield and fills it with blocks. 164 /// 165 /// playfieldWidth and playfieldHeight should be set before using this. 166 /// </summary> 167 public void CreatePlayfield(string[] level) 168 { 169 playfieldWidth = level[0].Length; 170 playfieldHeight = level.Length; 171 playfield = new Block[playfieldWidth,playfieldHeight]; 172 Level.Size = new Vector(playfieldWidth * (blocksize + 1), playfieldHeight * (blocksize + 1)); 173 Level.CreateBorders(); 174 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; 211 } 212 213 /// <summary> 214 /// Loads playfield from file 215 /// </summary> 216 /// <param name="file">Sourcefile</param> 217 public void LoadPlayfield(string file) 218 { 219 //TODO Toteuta lukeminen 73 currentLevel.MovePlayer(xMov, yMov); 220 74 } 221 75 -
2010/23/hniemi/BlockPusher/Blockpusher/Blockpusher/Blockpusher.csproj
r2632 r2633 113 113 <Compile Include="Block.cs" /> 114 114 <Compile Include="Blockpusher.cs" /> 115 <Compile Include="BlockLevel.cs" /> 115 116 <Compile Include="Properties\AssemblyInfo.cs" /> 116 117 <Compile Include="Ohjelma.cs" />
Note: See TracChangeset
for help on using the changeset viewer.