-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
294 lines (245 loc) · 10.8 KB
/
Game1.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Rapid_Prototype_1.Tools;
using System.Globalization;
using System.Collections.Generic;
using System;
using System.Diagnostics;
namespace Rapid_Prototype_1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
const int WINDOW_WIDTH = 1920;
const int WINDOW_HEIGHT = 1080;
const float THRESHOLD_FOR_PLACING_PIECES = 100.0f;
GraphicsDeviceManager graphics;
private PresentationParameters pp;
SpriteBatch spriteBatch;
Song song;
private RenderTarget2D renderTarget1, renderTarget2;
Flickering flickering;
float bloomSatPulse = 1f, bloomSatDir = .04f;
Texture2D background_Sprite;
SpriteFont spriteFont;
MouseState mouseState;
MouseState lastMouseState;
Button startButton;
//shapes temporary
FallingShapes fallingShapes;
//**************//
int piecesPlaced = 0;
float timer = 0f;
private bool gameStarted = false;
private bool gameWon = false;
GameBoard gameBoard;
Shape draggedShape = null;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// if we wanted to set a window size
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
IsMouseVisible = true;
Window.AllowUserResizing = false;
}
/// <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()
{
// TODO: Add your initialization logic here
gameBoard = new GameBoard(GameBoard.BoardName.Unicorn);
gameBoard.Initialize();
pp = GraphicsDevice.PresentationParameters;
renderTarget1 = new RenderTarget2D(GraphicsDevice,
pp.BackBufferWidth, pp.BackBufferHeight,
false, pp.BackBufferFormat, pp.DepthStencilFormat,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
renderTarget2 = new RenderTarget2D(GraphicsDevice,
pp.BackBufferWidth, pp.BackBufferHeight,
false, pp.BackBufferFormat, pp.DepthStencilFormat,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
lastMouseState = Mouse.GetState();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
flickering = new Flickering(GraphicsDevice, spriteBatch);
background_Sprite = Content.Load<Texture2D>("v2_ui");
spriteFont = Content.Load<SpriteFont>("font");
song = Content.Load<Song>("Chiptronical");
MediaPlayer.Play(song);
startButton = new Button("start", Content)
{
Position = new Vector2(WINDOW_WIDTH - 320 , WINDOW_HEIGHT - 120),
};
startButton.Click += StartButton_Click;
fallingShapes = new FallingShapes(Content);
flickering.LoadContent(Content, pp);
gameBoard.LoadContent(Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
private void StartButton_Click(object sender, System.EventArgs e)
{
timer = 0;
piecesPlaced = 0;
gameWon = false;
gameBoard.ClearBoard();
gameStarted = true;
}
private bool showBackground = true;
/// <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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
startButton.Update(gameTime);
//we can change this but for now the pieces start falling only after start button clicked
if (gameStarted && piecesPlaced < gameBoard.boardPieceCount)
{
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
fallingShapes.Update(gameTime);
fallingShapes.UpdateShatteredShapes(gameTime, Content);
flickering.Settings = FlickeringSettings.PresetSettings[0];
}
else
{
if(piecesPlaced == gameBoard.boardPieceCount)
{
gameWon = true;
flickering.Settings = FlickeringSettings.PresetSettings[0];
}
fallingShapes.ClearShapes(Content);
gameStarted = false;
}
base.Update(gameTime);
mouseState = Mouse.GetState();
// Mouse down event
if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton != ButtonState.Pressed && gameStarted)
{
//Console.WriteLine("Mouse clicked!");
List<int> indexesOfShapesClicked = ShapeClicker.GetIndexesOfShapesClicked(fallingShapes.GetFallingShapes(), mouseState.X, mouseState.Y);
foreach(int index in indexesOfShapesClicked)
{
draggedShape = fallingShapes.GetFallingShapes()[index]; // This will ultimately result in the top piece being selected
}
if (draggedShape != null)
{
fallingShapes.RemoveShape(draggedShape);
}
}
else if ((mouseState.LeftButton != ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Pressed && gameStarted))
{
// Mouse up event
bool aPieceWasPlaced = false;
// If this piece was placed
if (draggedShape != null && gameBoard.SaturateIfNamePrefixMatch(mouseState.X, mouseState.Y, draggedShape.GetName(), THRESHOLD_FOR_PLACING_PIECES))
{
// TODO: Remove this piece from the list of pieces that can fall
aPieceWasPlaced = true;
//increase counter of pieces placed
piecesPlaced++;
}
if (!aPieceWasPlaced)
{
if(draggedShape != null)
{
fallingShapes.AddShape(draggedShape);
}
}
// Regardless, drop the shape
draggedShape = null;
}
if(draggedShape != null)
{
draggedShape.SetPosition(new Vector2(mouseState.X - draggedShape.GetCenter().X, mouseState.Y - draggedShape.GetCenter().Y));
}
lastMouseState = mouseState;
bloomSatPulse += bloomSatDir;
if (bloomSatPulse > 2.5f) bloomSatDir = -0.04f;
if (bloomSatPulse < 0.1f) bloomSatDir = 0.04f;
flickering.Settings.BloomSaturation = bloomSatPulse;
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
if(gameWon)
{
GraphicsDevice.SetRenderTarget(renderTarget1);
GraphicsDevice.Clear(Color.TransparentBlack);
spriteBatch.Begin();
gameBoard.Draw(spriteBatch, gameWon);
spriteBatch.End();
flickering.Draw(renderTarget1, renderTarget2);
GraphicsDevice.SetRenderTarget(null);
}
else
{
GraphicsDevice.SetRenderTarget(renderTarget1);
GraphicsDevice.Clear(Color.TransparentBlack);
flickering.Draw(renderTarget1, renderTarget2);
GraphicsDevice.SetRenderTarget(null);
}
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
if (showBackground) {
spriteBatch.Draw(background_Sprite, GraphicsDevice.Viewport.Bounds, Color.White);
}
else {
spriteBatch.Draw(background_Sprite, GraphicsDevice.Viewport.Bounds, Color.White * 0.95f);
showBackground = true;
}
if(!gameWon)
{
gameBoard.Draw(spriteBatch, gameWon);
}
if (gameStarted)
{
fallingShapes.Draw(spriteBatch);
if (draggedShape != null)
{
draggedShape.Draw(spriteBatch);
}
}
else
startButton.Draw(spriteBatch);
Vector2 stringPos = timer < 10 ? new Vector2(WINDOW_WIDTH - 180 , 35 ) : new Vector2(WINDOW_WIDTH - 200, 35);
spriteBatch.DrawString(spriteFont, Math.Ceiling(timer).ToString(), stringPos, Color.White, 0f, Vector2.Zero, 2.5f, SpriteEffects.None, 0);
spriteBatch.End();
spriteBatch.Begin(0, BlendState.AlphaBlend);
spriteBatch.Draw(renderTarget2, new Rectangle(0, 0, pp.BackBufferWidth, pp.BackBufferHeight), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}