1 | |
---|
2 | using System; |
---|
3 | using System.Collections.Generic; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | using AdvanceMath; |
---|
10 | |
---|
11 | namespace Pallospeli |
---|
12 | { |
---|
13 | /// <summary> |
---|
14 | /// Playfield manages gamelogic |
---|
15 | /// |
---|
16 | /// </summary> |
---|
17 | /// <example> |
---|
18 | /// Playfield logic = new Playfield(10,10); |
---|
19 | /// logic.Balls = null; |
---|
20 | /// |
---|
21 | /// logic.Destroy(new location(4,5)); |
---|
22 | /// </example> |
---|
23 | public class Playfield |
---|
24 | { |
---|
25 | private int width; |
---|
26 | private int height; |
---|
27 | |
---|
28 | private Color[] colorset; |
---|
29 | /// <summary> |
---|
30 | /// Colors used in game |
---|
31 | /// </summary> |
---|
32 | public Color[] Colorset { get { return colorset; } } |
---|
33 | |
---|
34 | private Location[][] directions = new Location[2][]; |
---|
35 | private Location[] directionseteven = { |
---|
36 | new Location(-1 , 0), |
---|
37 | new Location( 1 , 0), |
---|
38 | new Location( -1 , -1), |
---|
39 | new Location( -1 , 1), |
---|
40 | new Location( 0 , 1), |
---|
41 | new Location( 0 , -1)}; |
---|
42 | private Location[] directionsetuneven = { |
---|
43 | new Location(-1 , 0), |
---|
44 | new Location( 1 , 0), |
---|
45 | new Location( 0 , -1), |
---|
46 | new Location( 0 , 1), |
---|
47 | new Location( 1 , 1), |
---|
48 | new Location( 1 , -1)}; |
---|
49 | |
---|
50 | /// <summary> |
---|
51 | /// Balls.Length is height, Balls[0].Length is width. |
---|
52 | /// </summary> |
---|
53 | private Ball[][] balls; |
---|
54 | /// <summary> |
---|
55 | /// Balls used in game. |
---|
56 | /// |
---|
57 | /// Is value is null, RandomBalls is used. |
---|
58 | /// </summary> |
---|
59 | public Ball[][] Balls |
---|
60 | { |
---|
61 | get { return balls; } |
---|
62 | set |
---|
63 | { |
---|
64 | if (value == null) |
---|
65 | { RandomBalls(); } |
---|
66 | else balls = value; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | /// <summary> |
---|
71 | /// Creates playfield from given width and height |
---|
72 | /// |
---|
73 | /// Doesn't fill playfield with balls. |
---|
74 | /// </summary> |
---|
75 | /// <param name="width"></param> |
---|
76 | /// <param name="height"></param> |
---|
77 | public Playfield(int width, int height) |
---|
78 | { |
---|
79 | this.width = width; |
---|
80 | this.height = height; |
---|
81 | |
---|
82 | directions[0] = directionseteven; |
---|
83 | directions[1] = directionsetuneven; |
---|
84 | } |
---|
85 | |
---|
86 | /// <summary> |
---|
87 | /// Creates playfield from given balls |
---|
88 | /// </summary> |
---|
89 | /// <param name="balls"></param> |
---|
90 | public Playfield(Ball[][] balls) |
---|
91 | { |
---|
92 | Balls = balls; |
---|
93 | width = balls[0].Length; |
---|
94 | height = balls.Length; |
---|
95 | directions[0] = directionseteven; |
---|
96 | directions[1] = directionsetuneven; |
---|
97 | } |
---|
98 | |
---|
99 | /// <summary> |
---|
100 | /// Fills playfield with balls |
---|
101 | /// </summary> |
---|
102 | public void RandomBalls() |
---|
103 | { |
---|
104 | balls = new Ball[height][]; |
---|
105 | for (int i = 0; i < balls.Length; i++) |
---|
106 | { |
---|
107 | balls[i] = new Ball[width - (i % 2)]; |
---|
108 | for (int j = 0; j < balls[i].Length; j++) |
---|
109 | { |
---|
110 | balls[i][j] = new Ball(i, j, colorset[RandomGen.NextInt(colorset.Length)]); |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /// <summary> |
---|
116 | /// Destroys ball at location and neighbouring balls of same color. |
---|
117 | /// |
---|
118 | /// If location is invalid, nothing happens. |
---|
119 | /// </summary> |
---|
120 | /// <param name="location">Location to be destroyed</param> |
---|
121 | public void Destroy(Location location) |
---|
122 | { |
---|
123 | Ball ball = Get(location); |
---|
124 | if (ball == null) return; |
---|
125 | ball.Presentation.Destroy(); |
---|
126 | |
---|
127 | balls[location.Y][location.X] = null; |
---|
128 | |
---|
129 | List<Ball> near = GetNear(location); |
---|
130 | foreach (Ball item in near) |
---|
131 | { |
---|
132 | if (item.Color == ball.Color) Destroy(item.Location); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /// <summary> |
---|
137 | /// Checks if balls are stabile. Destroys unstabile. |
---|
138 | /// </summary> |
---|
139 | public void StabilityCheck() |
---|
140 | { |
---|
141 | |
---|
142 | for (int i = 0; i < balls.Length; i++) |
---|
143 | { |
---|
144 | for (int j = 0; j < balls[i].Length; j++) |
---|
145 | { |
---|
146 | if (balls[i][j] != null && !IsStabile(balls[i][j].Location)) |
---|
147 | { |
---|
148 | Destroy(balls[i][j].Location); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /// <summary> |
---|
155 | /// Checks if ball in location is stabile. |
---|
156 | /// </summary> |
---|
157 | /// <param name="location">Location to of ball</param> |
---|
158 | /// <returns>True, is stabile</returns> |
---|
159 | public bool IsStabile(Location location) |
---|
160 | { |
---|
161 | if (location.Y < 0) return true; |
---|
162 | if (Get(location) == null) return false; |
---|
163 | return IsStabile(location + new Location(0, -1)) || IsStabile(location + new Location((location.Y%2 == 0 ? -1: 1), -1)); |
---|
164 | } |
---|
165 | |
---|
166 | /// <summary> |
---|
167 | /// Returns list of balls near location. |
---|
168 | /// |
---|
169 | /// If location has no neighbours, empty list is returned. |
---|
170 | /// </summary> |
---|
171 | /// <param name="location">Location</param> |
---|
172 | /// <returns>List of balls near location</returns> |
---|
173 | public List<Ball> GetNear(Location location) |
---|
174 | { |
---|
175 | List<Ball> list = new List<Ball>(); |
---|
176 | for (int i = 0; i < directions[0].Length; i++) |
---|
177 | { |
---|
178 | Ball ball = Get(location + directions[location.Y%2][i] ); |
---|
179 | if (ball != null) list.Add(ball); |
---|
180 | } |
---|
181 | return list; |
---|
182 | } |
---|
183 | |
---|
184 | /// <summary> |
---|
185 | /// Returns ball from coordinates |
---|
186 | /// |
---|
187 | /// If coordinates has no balls, null is returned. |
---|
188 | /// </summary> |
---|
189 | /// <param name="x">X-coordinate of ball</param> |
---|
190 | /// <param name="y">Y-coordinate of ball</param> |
---|
191 | /// <returns></returns> |
---|
192 | public Ball Get(int x, int y) |
---|
193 | { |
---|
194 | if (x < 0 || y < 0 || y >= height || x >= width - (y % 2)) return null; |
---|
195 | if (balls[y][x] == null) return null; |
---|
196 | return balls[y][x]; |
---|
197 | } |
---|
198 | |
---|
199 | /// <summary> |
---|
200 | /// Returns ball from given location |
---|
201 | /// |
---|
202 | /// If location has no balls, null is returned. |
---|
203 | /// </summary> |
---|
204 | /// <param name="location">Location</param> |
---|
205 | /// <returns>Ball at location</returns> |
---|
206 | public Ball Get(Location location) |
---|
207 | { |
---|
208 | return Get(location.X, location.Y); |
---|
209 | } |
---|
210 | |
---|
211 | public void FillColorset(int colors) |
---|
212 | { |
---|
213 | colorset = new Color[colors]; |
---|
214 | for (int i = 0; i < colorset.Length; i++) |
---|
215 | { |
---|
216 | colorset[i] = RandomGen.NextColor(); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | /// <summary> |
---|
221 | /// Repairs Balls[x][x].Locations |
---|
222 | /// </summary> |
---|
223 | public void RepairBallLocations() |
---|
224 | { |
---|
225 | for (int i = 0; i < balls.Length; i++) |
---|
226 | { |
---|
227 | for (int j = 0; j < balls[i].Length; j++) |
---|
228 | { |
---|
229 | if (balls[i][j] != null) |
---|
230 | { |
---|
231 | balls[i][j].Location.X = j; |
---|
232 | balls[i][j].Location.Y = i; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|