-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
254 lines (221 loc) · 10.5 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
//How to play: Left and right key to move around. Press enter to restart.
//
//Problems:
// Some refactoring is needed to not have a bunch of "global" variables
// If the framerate is low collisiondetection can potentially fail at high movement speeds, like in stages 1 and 2.
//Aside from that the game itself is actually enjoyable.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SuperSquare
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D circle;
private Player player;
private Vector2 center;
private Color gameColor;
private float centerSquareHalfWidth;
private List<Wall> walls;
private readonly Color[] GAME_COLORS = { new Color(150, 10, 0) , new Color(10, 10, 180), new Color(0,150, 15), new Color(150, 150, 15), new Color(0, 150, 150), new Color(120, 0,210) };
private Random random = new Random();
private const float wallSpawnDistance = 400;
private float score;
private float highScore;
private SpriteFont font;
private int currentStage;
private Stage[] stages;
private float gameRotation;
private float gameRPM;
private float wallSpeed;
private float wallsPerSecond;
private float wallSpawnTime;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
_graphics.PreferredBackBufferWidth = 600;
_graphics.PreferredBackBufferHeight = 600;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
center = new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight/2);
centerSquareHalfWidth = 25;
player = new Player(5,new Vector2(1,0), centerSquareHalfWidth +20, 100);
highScore = 0;
Stage stage0 = new Stage(0, 0, () =>
{
player.RPM = 100;
gameColor = GAME_COLORS[random.Next(GAME_COLORS.Length)];
wallSpeed = 200f;
gameRotation = 0;
gameRPM = 0;
wallsPerSecond = 4;
});
Stage stage1 = new Stage(1, 10, () =>
{
gameRPM = 15;
player.RPM *= 1.25f;
wallSpeed *= 1.25f;
wallsPerSecond = 6;
gameColor = Color.Lerp(gameColor, Color.White, 0.2f);
});
Stage stage2 = new Stage(2, 20, () =>
{
gameRPM = -30;
player.RPM *= 1.25f;
wallSpeed *= 1.25f;
wallsPerSecond = 8;
gameColor = Color.Lerp(gameColor, Color.White, 0.2f);
});
stages = new Stage[]{ stage0,stage1,stage2};
InitGame();
}
private void InitGame()
{
walls = new List<Wall> { };
player.IsDead = false;
score = 0;
currentStage = -1;
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
circle = Content.Load<Texture2D>("circle");
font = Content.Load<SpriteFont>("Score");
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
for (int i = 0; i < stages.Length; i++)
{
if (currentStage == i - 1 && score >= stages[i].StartTime)
{
walls.Clear();
currentStage = i;
stages[i].StageModifiers();
}
}
if (player.IsDead)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { InitGame(); }
return;
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Right)) { player.Rotate(deltaTime * MathHelper.Tau * player.RPM / 60); }
if (Keyboard.GetState().IsKeyDown(Keys.Left)) { player.Rotate(-deltaTime * MathHelper.Tau * player.RPM / 60); }
wallSpawnTime -= deltaTime;
while(wallSpawnTime < 0)
{
TryAddWall();
wallSpawnTime += 1 / wallsPerSecond;
}
foreach(Wall wall in walls)
{
wall.Distance -= wall.Speed * deltaTime;
if (PlayerHitBy(wall)) {
GameOver();
}
}
walls.RemoveAll(w => w.Distance < centerSquareHalfWidth);
score += deltaTime;
gameRotation += (float)Math.Tau * deltaTime * gameRPM / 60;
base.Update(gameTime);
}
private void TryAddWall()
{
Vector2[] directions = { new Vector2(1, 0), new Vector2(-1, 0), new Vector2(0, 1), new Vector2(0, -1) };
int i = random.Next(4);
Vector2 direction = directions[i];
if (walls.Any(w => w.Rotation.Equals(directions[0 + (i <= 0 ? 1 : 0)]) && wallSpawnDistance - w.Distance < player.Radius*10) &&
walls.Any(w => w.Rotation.Equals(directions[1 + (i <= 1 ? 1 : 0)]) && wallSpawnDistance - w.Distance < player.Radius * 10) &&
walls.Any(w => w.Rotation.Equals(directions[2 + (i <= 2 ? 1 : 0)]) && wallSpawnDistance - w.Distance < player.Radius * 10)) { return; }
walls.Add(new Wall(direction, wallSpawnDistance, wallSpeed));
}
private bool PlayerHitBy(Wall wall)
{
Vector2[] relativePoints = wall.Points();
Vector2[] wallPoints = relativePoints.Select(x => x + center).ToArray();
Vector2 playerPoint = center + player.Rotation * player.Distance;
double distance = MinimumDistance(wallPoints[0], wallPoints[1],playerPoint);
return distance < player.Radius;
}
private void GameOver()
{
player.IsDead = true;
if (score > highScore) { highScore = score; }
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Lerp(gameColor,Color.Black,0.8f));
Matrix centerAndRotate = Matrix.CreateTranslation(center.X, center.Y, 0) * Matrix.CreateTranslation(-center.X, -center.Y, 0) * Matrix.CreateRotationZ(gameRotation) * Matrix.CreateTranslation(center.X, center.Y, 0);
_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend,null,null,null,null,centerAndRotate);
_spriteBatch.DrawPolygon(Vector2.Zero,new Polygon(new Vector2[]{ Vector2.Zero, new Vector2(1000,1000), new Vector2(1000,-1000)}),Color.Lerp(gameColor, Color.Black, 0.75f),1000, 1);
foreach ( Wall wall in walls)
{
DrawWall(wall);
}
DrawPlayer();
DrawCenterSquare();
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
DrawScore();
_spriteBatch.End();
base.Draw(gameTime);
}
private void DrawScore()
{
_spriteBatch.DrawString(font, "Score: " + Math.Round(score,2), new Vector2(10, 10), Color.Lerp(gameColor, Color.Black, 0.1F));
_spriteBatch.DrawString(font, "Highscore: " + Math.Round(highScore, 2), new Vector2(10, 30), Color.Lerp(gameColor, Color.Black, 0.1F));
}
private void DrawWall(Wall wall)
{
Vector2[] ps = wall.Points();
Color c = Color.Lerp(gameColor, Color.Black, 0.3F);
_spriteBatch.DrawLine( ps[0], ps[1],c,5,0f);
}
private void DrawPlayer()
{
_spriteBatch.Draw(circle, player.Distance*player.Rotation, null, gameColor, 0f, new Vector2(circle.Width / 2, circle.Height / 2), 2 * player.Radius / circle.Width, SpriteEffects.None, 0f); ;
}
/*private void DrawCenterCircle()
{
Color c = Color.Lerp(gameColor, Color.Black, 0.3F);
_spriteBatch.Draw(circle, center, null, c, 0f, new Vector2(circle.Width / 2, circle.Height / 2), 2* centerSquareHalfWidth / circle.Width, SpriteEffects.None, 0f); ;
}*/
private void DrawCenterSquare()
{
Color c = Color.Lerp(gameColor, Color.Black, 0.3F);
float thickness = 3f;
_spriteBatch.DrawLine( new Vector2(centerSquareHalfWidth, centerSquareHalfWidth), new Vector2(centerSquareHalfWidth, -centerSquareHalfWidth), c, thickness, 0);
_spriteBatch.DrawLine( new Vector2(centerSquareHalfWidth, -centerSquareHalfWidth), new Vector2(-centerSquareHalfWidth, -centerSquareHalfWidth), c, thickness, 0);
_spriteBatch.DrawLine( new Vector2(-centerSquareHalfWidth, -centerSquareHalfWidth), new Vector2(-centerSquareHalfWidth, centerSquareHalfWidth), c, thickness, 0);
_spriteBatch.DrawLine( new Vector2(-centerSquareHalfWidth, centerSquareHalfWidth), new Vector2(centerSquareHalfWidth, centerSquareHalfWidth), c, thickness, 0);
}
static private float MinimumDistance(Vector2 v, Vector2 w, Vector2 p)
{
// Return minimum distance between line segment vw and point p
double l2 = (w - v).Length() * (w - v).Length(); // i.e. |w-v|^2 - avoid a sqrt
if (l2 == 0.0) return (p - v).Length(); // v == w case
// Consider the line extending the segment, parameterized as v + t (w - v).
// We find projection of point p onto the line.
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
// We clamp t from [0,1] to handle points outside the segment vw.
float t = (float)Math.Max(0, Math.Min(1, (p - v).Dot(w - v) / l2));
Vector2 projection = v + t * (w - v); // Projection falls on the segment
return (p - projection).Length();
}
}
}