-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
327 lines (270 loc) · 9.74 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NLog;
using System.Collections.Generic;
using System.Linq;
namespace klotski
{
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
}
class GameObject
{
public GameObject(Rectangle rectangle, Texture2D image)
{
Rectangle = rectangle;
Image = image;
}
public GameObject(Point location, Texture2D image)
{
Rectangle = new Rectangle(location.X, location.Y, image.Width, image.Height);
Image = image;
}
public Rectangle Rectangle { get; set; }
public Texture2D Image { get; }
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Image, Rectangle, Color.White);
}
public bool Contains(Point location)
{
return Rectangle.Contains(location);
}
public void Move(Point shift)
{
Rectangle.Offset(shift);
}
}
class Block : GameObject
{
public Block(Point location, Texture2D image) : base(location, image) { }
}
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game : Microsoft.Xna.Framework.Game
{
private static Logger logger = LogManager.GetCurrentClassLogger();
const int WINDOW_HEIGHT = 224;
const int WINDOW_WIDTH = 184;
const int BOARD_OFFSET_X = 12;
const int BOARD_OFFSET_Y = 12;
const int MIN_BLOCK_WIDTH = 40;
const int MIN_BLOCK_HEIGHT = 40;
static readonly int[][] blocksInfo = new int[][] {
new int[]{ 0, 0, 1, 2 },
new int[]{ 1, 0, 2, 2 },
new int[]{ 3, 0, 1, 2 },
new int[]{ 0, 2, 1, 2 },
new int[]{ 1, 2, 2, 1 },
new int[]{ 3, 2, 1, 2 },
new int[]{ 1, 3, 1, 1 },
new int[]{ 2, 3, 1, 1 },
new int[]{ 0, 4, 1, 1 },
new int[]{ 3, 4, 1, 1 }
};
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<GameObject> objects;
Rectangle boardRect;
bool buttonPressed;
Point clickPosition;
GameObject selected;
public Game()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
logger.Trace("Initialize");
base.Initialize();
}
private static int BlockSizeToImageIdx(int width, int height)
{
return (width - 1) * 2 + (height - 1);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
logger.Trace("LoadContent");
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
objects = new List<GameObject>();
var blocksImages = new Texture2D[] {
Content.Load<Texture2D>("1x1"), //0
Content.Load<Texture2D>("1x2"), //1
Content.Load<Texture2D>("2x1"), //2
Content.Load<Texture2D>("2x2"), //3
};
objects.Add(new GameObject(new Point(0, 0), Content.Load<Texture2D>("background")));
var frame = new GameObject(new Point(0, 0), Content.Load<Texture2D>("frame"));
objects.Add(frame);
{
var r = frame.Rectangle;
boardRect = new Rectangle(r.X + BOARD_OFFSET_X, r.Y + BOARD_OFFSET_Y, r.Width - BOARD_OFFSET_X * 2, r.Height - BOARD_OFFSET_Y * 2);
}
foreach (var blockInfo in blocksInfo)
{
int x = blockInfo[0];
int y = blockInfo[1];
int width = blockInfo[2];
int height = blockInfo[3];
var image = blocksImages[BlockSizeToImageIdx(width, height)];
objects.Add(new Block(new Point(x * MIN_BLOCK_WIDTH + BOARD_OFFSET_X, y * MIN_BLOCK_HEIGHT + BOARD_OFFSET_Y), image));
}
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// For Mobile devices, this logic will close the Game when the Back button is pressed
// Exit() is obsolete on iOS
#if !__IOS__ && !__TVOS__
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
#endif
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
if (!buttonPressed)
{
OnMousePress(Mouse.GetState().Position);
buttonPressed = true;
}
else
{
OnMouseHold(Mouse.GetState().Position);
}
}
else if (buttonPressed)
{
buttonPressed = false;
OnMouseRelease(Mouse.GetState().Position);
}
base.Update(gameTime);
}
private void OnMousePress(Point position)
{
logger.Debug($"OnMousePress {position}");
clickPosition = position;
selected = objects.FirstOrDefault((obj) => obj is Block && obj.Contains(position));
}
private void OnMouseHold(Point position)
{
if (selected != null)
{
var delta = position - clickPosition;
int dX = delta.X / (MIN_BLOCK_HEIGHT / 2);
int dY = delta.Y / (MIN_BLOCK_WIDTH / 2);
if ((dX != 0 && dY == 0) || (dX == 0 && dY != 0))
{
logger.Debug($"OnMouseHold - moving {position}");
Direction direction;
if (dX > 0)
{
direction = Direction.RIGHT;
}
else if (dX < 0)
{
direction = Direction.LEFT;
}
else if (dY > 0)
{
direction = Direction.DOWN;
}
else //if (dY < 0)
{
direction = Direction.UP;
}
bool success = TryMoveBlock(direction);
if (success)
{
clickPosition = position;
}
}
}
}
private bool TryMoveBlock(Direction direction)
{
logger.Debug($"TryMoveBlock {direction}");
Point offset;
switch (direction)
{
case Direction.LEFT:
offset = new Point(-MIN_BLOCK_WIDTH, 0);
break;
case Direction.RIGHT:
offset = new Point(MIN_BLOCK_WIDTH, 0);
break;
case Direction.UP:
offset = new Point(0, -MIN_BLOCK_HEIGHT);
break;
case Direction.DOWN:
offset = new Point(0, MIN_BLOCK_HEIGHT);
break;
default:
throw new InvalidOperationException();
}
var newRectangle = selected.Rectangle;
newRectangle.Offset(offset);
if (newRectangle.Left < boardRect.Left || newRectangle.Top < boardRect.Top
|| newRectangle.Right > boardRect.Right || newRectangle.Bottom > boardRect.Bottom)
{
// The block is out of the board
return false;
}
if (objects.Any((obj) =>
obj is Block && obj != selected && obj.Rectangle.Intersects(newRectangle)))
{
return false;
}
logger.Debug($"success!");
selected.Rectangle = newRectangle;
return true;
}
private void OnMouseRelease(Point position)
{
if (selected != null)
{
logger.Debug($"OnMouseRelease {position}");
selected = null;
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var obj in objects)
{
obj.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}