/
Game1.cs
311 lines (259 loc) · 13.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpriteSheetRuntime;
using Rain.Objects;
namespace Rain
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SortedList<String, Layer> layers;
Camera camera;
Controller controller;
List<Line> lines;
Random randomNum;
SortedDictionary<String, AnimationTable> animationTables = new SortedDictionary<string, AnimationTable>();
SpriteSheet spritesheet;
SpriteSheet spritesheet2;
CollisionManager collisionManager;
Player player;
SpriteFont font;
// State Machine
CStateMachine _StateMachine;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 480;
Content.RootDirectory = "Content";
}
/// <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()
{
base.Initialize();
// CMainMenuState _MainMenu = new CMainMenuState();
// _MainMenu.Enter();
CGamePlayState _GamePlay = new CGamePlayState();
_GamePlay.Enter();
_StateMachine = new CStateMachine();
_StateMachine.PushState(_GamePlay);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
LineBatch.Init(GraphicsDevice);
layers = new SortedList<String, Layer>();
camera = new Camera(Vector2.Zero);
lines = new List<Line>();
randomNum = new Random();
collisionManager = new CollisionManager();
spriteBatch = new SpriteBatch(GraphicsDevice);
controller = new Controller();
camera = new Camera(Vector2.Zero);
spritesheet = Content.Load<SpriteSheet>("sheet");
spritesheet2 = Content.Load<SpriteSheet>("sheet2");
loadAnimationTables();
font = Content.Load<SpriteFont>("FromWhere");
loadLayers();
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
_StateMachine.UpdateState(gameTime.ElapsedGameTime.Ticks);
controller.update();
handleControls(gameTime);
player.update(gameTime);
handleCollisions(gameTime);
foreach (KeyValuePair<String, Layer> layer in layers)
{
layers[layer.Key].update(gameTime);
}
updateCamera(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_StateMachine.RenderState();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
foreach (KeyValuePair<String, Layer> pair in layers) {
layers[pair.Key].update(gameTime);
foreach (GameObject drawable in layers[pair.Key].Objects)
{
if (drawable.Visible)
{
spriteBatch.Draw(drawable.AnimationTable.SpriteSheet.Texture,
camera.getRenderPosition(drawable.Position, layers[pair.Key].ScrollRateX, layers[pair.Key].ScrollRateY),
drawable.AnimationTable.SpriteSourceRectangle,
drawable.Color,
drawable.Rotation,
new Vector2(drawable.AnimationTable.SpriteSourceRectangle.Width / 2, drawable.AnimationTable.SpriteSourceRectangle.Height / 2),
drawable.Scale,
drawable.FlipHorizontally,
drawable.ZOrder + layers[pair.Key].ZOrder);
}
}
}
foreach (Line line in lines)
LineBatch.DrawLine(spriteBatch, camera, new Color(randomNum.Next(100, 255), randomNum.Next(255), randomNum.Next(255)), line);
// spriteBatch.DrawString(font, "Hello World!", new Vector2(150, 150), Color.Yellow);
// DrawText();
spriteBatch.End();
base.Draw(gameTime);
}
protected void handleCollisions(GameTime gametime) {
foreach (GameObject drop in layers["drops"].Objects)
{
if (collisionManager.testCollision(player, drop) != Vector2.Zero)
{
player.addDrop();
drop.Remove = true;
}
}
foreach (GameObject drop in layers["acid"].Objects)
{
if (collisionManager.testCollision(player, drop) != Vector2.Zero)
{
player.addAcid();
drop.Remove = true;
}
}
}
protected void loadLayers()
{
player = new Player(new Vector2(100, 100), animationTables["player"], controller);
player.setAnimation("stand");
player.ZOrder =1;
layers.Add("main", new Layer(1f, 1f, 1));
layers.Add("drops", new Layer(1f, 1f, 2));
layers.Add("background", new Layer(1f, 0.01f, 1));
layers.Add("cloud1", new Layer(1f, 0.1f, 10));
layers.Add("cloud2", new Layer(1f, 0.6f, 5));
layers.Add("cloud3", new Layer(1f, 1.1f, 3));
layers.Add("cloud4", new Layer(1f, 1.5f, -1));
layers.Add("acid", new Layer(1f, 1f, 1));
layers.Add("smokestack", new Layer(1f, 1f, -1));
layers.Add("building", new Layer(1f, 1f, -1));
for (int i = 0; i < 1000; i++)
{
layers["drops"].add(new Droplet(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), animationTables["drops"]));
}
for (int i = 0; i < 450; i++)
{
layers["acid"].add(new Droplet(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), animationTables["acid"]));
}
for (int i = 0; i < 32; i++)
{
GameObject cloud = new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"]));
cloud.setAnimation("cloud1");
layers["cloud1"].add(cloud);
cloud = new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"]));
cloud.setAnimation("cloud2");
layers["cloud2"].add(new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"])));
cloud = new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"]));
cloud.setAnimation("cloud3");
cloud.ZOrder = -1;
layers["cloud3"].add(new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"])));
cloud = new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"]));
cloud.setAnimation("cloud2");
cloud.ZOrder = -1;
layers["cloud4"].add(new GameObject(new Vector2(randomNum.Next(400) + 40, randomNum.Next(90000) + 100), new AnimationTable(animationTables["clouds"])));
}
for (int i = 0; i < 10; i++)
{
GameObject smokestack = new GameObject(new Vector2(30, 10000+ (i*198)),new AnimationTable(animationTables["buildings"]));
smokestack.ZOrder = i;
smokestack.setAnimation("smokestack");
layers["smokestack"].add(smokestack);
}
GameObject building = new GameObject(new Vector2(240, 10000 +(11*198) -256), new AnimationTable(animationTables["buildings"]));
building.setAnimation("powerplant");
layers["building"].add(building);
GameObject background = new GameObject(new Vector2(96, 0), animationTables["background"]);
background.ZOrder = 1000000;
layers["main"].add(player);
layers["background"].add(background);
}
protected void loadAnimationTables()
{
AnimationTable player = new AnimationTable(spritesheet);
AnimationTable ground = new AnimationTable(spritesheet);
AnimationTable drops = new AnimationTable(spritesheet);
AnimationTable background = new AnimationTable(spritesheet2);
AnimationTable clouds = new AnimationTable(spritesheet);
AnimationTable acid = new AnimationTable(spritesheet);
AnimationTable buildings = new AnimationTable(spritesheet);
player.addAnimation("stand", new Animation(new string[] { "drop_move_1" }, TimeSpan.FromMilliseconds(100)));
player.addAnimation("moving", new Animation(new string[] { "drop_move_1", "drop_move_2", "drop_move_3" }, TimeSpan.FromMilliseconds(100)));
player.addAnimation("falling", new Animation(new string[] { "drop_fall_1", "drop_fall_2" }, TimeSpan.FromMilliseconds(100)));
player.addAnimation("splat", new Animation(new string[] { "drop_splat_1", "drop_splat_2", "drop_splat_3", "drop_splat_4", "drop_splat_5", "drop_splat_6"}, TimeSpan.FromMilliseconds(100)));
ground.addAnimation("grass", new Animation(new string[] { "grass" }, TimeSpan.FromMilliseconds(50)));
drops.addAnimation("drop_1", new Animation(new string[] { "droplet_1" }, TimeSpan.FromMilliseconds(50)));
background.addAnimation("background1", new Animation(new string[] { "background-01" }, TimeSpan.FromMilliseconds(50)));
clouds.addAnimation("cloud1", new Animation(new string[] { "cloud1" }, TimeSpan.FromMilliseconds(50)));
clouds.addAnimation("cloud2", new Animation(new string[] { "cloud2" }, TimeSpan.FromMilliseconds(50)));
clouds.addAnimation("cloud3", new Animation(new string[] { "cloud3" }, TimeSpan.FromMilliseconds(50)));
acid.addAnimation("acid", new Animation(new string[] { "acid" }, TimeSpan.FromMilliseconds(50)));
buildings.addAnimation("smokestack", new Animation(new string[] { "smokestack" }, TimeSpan.FromMilliseconds(100)));
buildings.addAnimation("powerplant", new Animation(new string[] { "powerplant" }, TimeSpan.FromMilliseconds(100)));
buildings.addAnimation("building1", new Animation(new string[] { "building-02" }, TimeSpan.FromMilliseconds(100)));
buildings.addAnimation("building2", new Animation(new string[] { "building-03" }, TimeSpan.FromMilliseconds(100)));
animationTables.Add("player", player);
animationTables.Add("ground", ground);
animationTables.Add("drops", drops);
animationTables.Add("background", background);
animationTables.Add("clouds", clouds);
animationTables.Add("acid", acid);
animationTables.Add("buildings", buildings);
}
protected void updateCamera(GameTime gameTime)
{
camera.Position = (new Vector2(player.Position.X - 360, player.Position.Y - 240));
camera.update(gameTime);
Console.Out.WriteLine(camera.Position);
}
protected void handleControls(GameTime gameTime)
{
}
}
}