-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
225 lines (201 loc) · 8.14 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using System;
namespace FallingSand
{
public static class Storage
{
public static GraphicsDeviceManager GDM;
public static ContentManager CM;
public static SpriteBatch SB;
public static Game1 game;
public static Texture2D texture;
public static Rectangle drawRec = new Rectangle(0, 0, 1, 1);
public static Particle[,] particles;
}
public class Particle
{
public int x_velocity, y_velocity;
public Color color;
}
public class Game1 : Game
{
public const int size_x = 64;
public const int size_y = size_x;
public int timer = 0;
public Game1()
{
Storage.GDM = new GraphicsDeviceManager(this);
Storage.GDM.GraphicsProfile = GraphicsProfile.HiDef;
Storage.CM = Content;
Storage.game = this;
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromMilliseconds(16.667); //60 frames/sec = 16.667ms pre frame
}
protected override void Initialize()
{
Storage.GDM.PreferredBackBufferWidth = 512;
Storage.GDM.PreferredBackBufferHeight = 512;
Storage.GDM.ApplyChanges();
Storage.particles = new Particle[size_x, size_y];
for (int x = 0; x < size_x; x++)
{
for (int y = 0; y < size_y; y++)
{
Storage.particles[x, y] = null;
}
}
base.Initialize();
}
protected override void LoadContent()
{
Storage.SB = new SpriteBatch(GraphicsDevice);
if (Storage.texture == null)
{
// Texture used for drawing, will be scaled later.
Storage.texture = new Texture2D(Storage.GDM.GraphicsDevice, 1, 1);
Storage.texture.SetData<Color>(new Color[] { Color.SandyBrown });
}
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
for (int x = 0; x < size_x; x++)
{
for (int y = 0; y < size_y; y++)
{
var particle = Storage.particles[x, y];
if (particle != null)
{
particle.y_velocity += 1;
if (MathF.Abs(particle.y_velocity) > 1)
{
particle.y_velocity = 1 * Math.Sign(particle.y_velocity);
}
particle.x_velocity += 0;
if (MathF.Abs(particle.x_velocity) > 1)
{
particle.x_velocity = 1 * Math.Sign(particle.x_velocity);
}
int next_x = x + particle.x_velocity;
int next_y = y + particle.y_velocity;
if (!find_collision(next_x, next_y))
{
// Go there.
}
else if (!find_collision(next_x - 1, next_y))
{
next_x -= 1;
}
else if (!find_collision(next_x + 1, next_y))
{
next_x += 1;
}
// Simulate water. Move side to side if you can.
/*
else if (!find_collision(next_x - 1, y))
{
next_x -= 1;
next_y = y;
particle.y_velocity = 0;
}
else if (!find_collision(next_x + 1, y))
{
next_x += 1;
next_y = y;
particle.y_velocity = 0;
}
*/
else
{
// Can't move.
next_x = x;
particle.x_velocity = 0;
next_y = y;
particle.y_velocity = 0;
}
Storage.particles[x, y] = null;
x = next_x;
y = next_y;
Storage.particles[x, y] = particle;
}
}
}
// Delay how fast you can create particles.
timer += gameTime.ElapsedGameTime.Milliseconds;
var mouse_state = Mouse.GetState();
if (mouse_state.LeftButton == ButtonState.Pressed && timer > 40)
{
timer = 0;
var x = mouse_state.Position.X * size_x / Storage.GDM.PreferredBackBufferWidth;
var y = mouse_state.Position.Y * size_y / Storage.GDM.PreferredBackBufferHeight;
// Only create a particle in this grid location if nothing is currently there.
if (x >= 0 && x < size_x && y >= 0 && y < size_y && Storage.particles[x, y] == null)
{
Particle particle = new Particle();
Storage.particles[x, y] = particle;
}
}
base.Update(gameTime);
}
bool find_collision(int x, int y)
{
if (y >= size_x)
{
return true;
}
if (x <= -1 || x >= size_y)
{
return true;
}
return Storage.particles[x, y] != null;
}
protected override void Draw(GameTime gameTime)
{
SpriteBatch targetBatch = new SpriteBatch(GraphicsDevice);
RenderTarget2D target = new RenderTarget2D(GraphicsDevice, size_x, size_y);
GraphicsDevice.SetRenderTarget(target);
GraphicsDevice.Clear(Color.CornflowerBlue);
// Storage.SB.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
Storage.SB.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp);
int depth = 0;
for (int x = 0; x < size_x; x++)
{
for (int y = 0; y < size_y; y++)
{
depth += 1;
Particle particle = Storage.particles[x, y];
if (particle != null)
{
Vector2 pos = new Vector2(x, y);
// Draw each particle as a sprite.
Storage.SB.Draw(Storage.texture,
pos,
Storage.drawRec,
Color.SandyBrown,
0,
Vector2.Zero,
1.0f, // Scale
SpriteEffects.None,
depth * 0.00001f);
}
}
}
Storage.SB.End();
// Set rendering back to the back buffer.
GraphicsDevice.SetRenderTarget(null);
// Render target to back buffer.
targetBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
// Scale target from 64x64 to 512x512.
targetBatch.Draw(target, new Rectangle(0, 0, Storage.GDM.PreferredBackBufferWidth, Storage.GDM.PreferredBackBufferHeight), Color.White);
targetBatch.End();
base.Draw(gameTime);
}
}
}