1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class Blockpusher : Game |
---|
10 | { |
---|
11 | private const int blocksize = 20; |
---|
12 | |
---|
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 | private readonly Color playerColor = Color.Blue; |
---|
22 | private readonly Color satisfyColor = Color.Green; |
---|
23 | private readonly Color unsatisfyColor = Color.Red; |
---|
24 | |
---|
25 | private List<Block> goals = new List<Block>(); |
---|
26 | |
---|
27 | //[x,y] |
---|
28 | private Block[,] playfield; |
---|
29 | |
---|
30 | private string[] testLevel = {"xxxxxxxxx", |
---|
31 | "x x xmx", |
---|
32 | "xb s xbx", |
---|
33 | "xm x x", |
---|
34 | "x xxxxxx", |
---|
35 | "x xx", |
---|
36 | "xxxxxxxxx"}; |
---|
37 | |
---|
38 | public override void Begin() |
---|
39 | { |
---|
40 | InitCharColors(); |
---|
41 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
42 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
43 | CreatePlayfield(testLevel); |
---|
44 | SetControls(); |
---|
45 | } |
---|
46 | |
---|
47 | public void InitCharColors() |
---|
48 | { |
---|
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); |
---|
54 | } |
---|
55 | |
---|
56 | /// <summary> |
---|
57 | /// Moves player x and y steps |
---|
58 | /// </summary> |
---|
59 | /// <param name="x">X Movement</param> |
---|
60 | /// <param name="y">Y Movement</param> |
---|
61 | public void MovePlayer(int x, int y) |
---|
62 | { |
---|
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 |
---|
220 | } |
---|
221 | |
---|
222 | /// <summary> |
---|
223 | /// Sets controllisteners for keyboard |
---|
224 | /// </summary> |
---|
225 | public void SetControls() |
---|
226 | { |
---|
227 | Keyboard.Listen(Key.Up, ButtonState.Pressed, MovePlayer, null, 0, -1); |
---|
228 | Keyboard.Listen(Key.Down, ButtonState.Pressed, MovePlayer, null, 0, 1); |
---|
229 | Keyboard.Listen(Key.Left, ButtonState.Pressed, MovePlayer, null, -1, 0); |
---|
230 | Keyboard.Listen(Key.Right, ButtonState.Pressed, MovePlayer, null, 1, 0); |
---|
231 | } |
---|
232 | } |
---|