-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cs
299 lines (243 loc) · 10.7 KB
/
GameManager.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using TiledCS;
using GrapplingGame.GameObjectsComponentsLevels.GameObjects;
using GrapplingGame.GameObjectsComponentsLevels.Levels;
using GrapplingGame.GameObjectsComponentsLevels.Components;
using GrapplingGame.GameObjectsComponentsLevels.Helpers;
using MonoGame.Extended;
using MonoGame.Extended.ViewportAdapters;
using System.Diagnostics;
namespace GrapplingGame;
public class GameManager : Game
{
readonly GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
// Content
public Texture2D playerSprite;
public Texture2D grapplingGunSprite;
public Texture2D targetActiveSprite;
public Texture2D cursorImage;
public SpriteFont pixelify;
public Texture2D CurtainSheet;
Texture2D target;
// Levels
Level currentLevel;
public Level CurrentLevel
{
get { return currentLevel; }
set
{
currentLevel = value;
currentLevel.Initialize();
}
}
public List<Level> levels = new();
// Singleton stuff
public static GameManager Instance;
public Camera Camera;
// Camera
public OrthographicCamera OrthographicCamera;
public float HalfScreenWidth;
public float HalfScreenHeight;
public Color SpriteTint = Color.White;
public GameManager()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
// Singleton stuff
Instance ??= this;
Camera = new();
}
protected override void Initialize()
{
base.Initialize();
_graphics.PreferredBackBufferWidth = 1600;
_graphics.PreferredBackBufferHeight = 900;
_graphics.ApplyChanges();
var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 1600, 900);
OrthographicCamera = new OrthographicCamera(viewportAdapter);
HalfScreenWidth = 800;
HalfScreenHeight = 450;
Level1 level1 = new(this, false);
CurrentLevel = level1;
//IsMouseVisible = false;
// Create Target
GameObject newTile = new(target, new Point((int)HalfScreenWidth, 100), new Point(1, 1), "target", currentLevel);
newTile.AddComponent("TargetComponent");
newTile.SetComponentVariable("TargetComponent", "TargetType", TARGETTYPE.swing);
currentLevel.Targets.Add(newTile);
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
playerSprite = this.Content.Load<Texture2D>("Player");
grapplingGunSprite = Content.Load<Texture2D>("grappling_gun");
targetActiveSprite = Content.Load<Texture2D>("TargetActive");
cursorImage = Content.Load<Texture2D>("Cursor");
pixelify = Content.Load<SpriteFont>("Pixelify");
CurtainSheet = Content.Load<Texture2D>("curtain-sheet");
target = Content.Load<Texture2D>("Target");
// Add it to the desktop
/*_desktop = new Desktop();
_desktop.Root = grid;*/
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
Camera.Instance.Update(gameTime);
foreach (GameObject obj in currentLevel.GameObjects.ToArray())
{
// Set the rect of the GameObject
obj.rect.X = obj.position.X;
obj.rect.Y = obj.position.Y;
// Run the update functions of every Component of every GameObject
if (obj.attributes != null)
{
foreach (Component a in obj.attributes)
{
a.Update(gameTime);
}
}
}
currentLevel.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
var transformMatrix = OrthographicCamera.GetViewMatrix();
_spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix : transformMatrix);
_spriteBatch.Draw(cursorImage, new Rectangle(currentLevel.MousePosition, new Point(32, 32)), SpriteTint);
// Render Gameobjects
foreach (GameObject obj in currentLevel.GameObjects)
{
if (obj.Visible)
{
if (obj.sprite != null && !obj.dontRender)
{
if (obj.cropped == false)
{
_spriteBatch.Draw(obj.sprite, new Vector2(obj.position.X, obj.position.Y), null, SpriteTint, obj.Rotation, obj.origin, new Vector2(obj.sizeMultiplier.X, obj.sizeMultiplier.Y), SpriteEffects.None, 1);
}
else
{
_spriteBatch.Draw(obj.sprite, new Rectangle(obj.rect.X, obj.rect.Y, obj.width * obj.sizeMultiplier.X, obj.height * obj.sizeMultiplier.Y), obj.cropRect, SpriteTint, obj.Rotation, obj.origin, SpriteEffects.None, 1);
}
}
}
if (obj.type == "target")
{
if ((bool)obj.GetComponentVariable("TargetComponent", "Active"))
{
Point startPos = (Point)CurrentLevel.GrappleGun.GetComponentVariable("GrappleGunComponent", "TipOfGun");
Point endPos = new(obj.position.X + 16, obj.position.Y + 16);
Functions.DrawLineBetween(_spriteBatch, startPos, endPos, 3, SpriteTint);
}
}
if (currentLevel.Player != null)
{
Point currentPlayerPosition = new(currentLevel.Player.rect.X, currentLevel.Player.rect.Y);
_spriteBatch.DrawString(pixelify, $"{currentPlayerPosition.X}, {currentPlayerPosition.Y}", OrthographicCamera.ScreenToWorld(new Vector2(0, 0)), SpriteTint);
}
}
_spriteBatch.End();
// UI
//_desktop.Render();
base.Draw(gameTime);
}
/*public void CreateMap(string tiledLevel)
{
// Remove all current tiles
foreach (GameObject obj in tiles)
{
obj.Remove();
}
string path = "";
FileStream specialFileStream;
FileStream fileStream;
try
{
path = Path.GetDirectoryName(AppContext.BaseDirectory);
_map = new TiledMap(path + "\\" + Content.RootDirectory + "\\tilemaps\\" + tiledLevel);
_tileset = new TiledTileset(path + "\\" + Content.RootDirectory + "\\tilemaps\\Tileset.tsx");
specialFileStream = new FileStream(path + "\\" + Content.RootDirectory + "\\tilemaps\\special_tileset.png", FileMode.Open);
fileStream = new FileStream(path + "\\" + Content.RootDirectory + "\\tilemaps\\tileset.png", FileMode.Open);
} catch
{
path = Path.GetDirectoryName(AppContext.BaseDirectory).Replace("\\win-x64", "");
path = Path.GetDirectoryName(AppContext.BaseDirectory).Replace("\\osx-x64", "");
_map = new TiledMap(path + "\\" + Content.RootDirectory + "\\tilemaps\\" + tiledLevel);
_tileset = new TiledTileset(path + "\\" + Content.RootDirectory + "\\tilemaps\\Tileset.tsx");
specialFileStream = new FileStream(path + "\\" + Content.RootDirectory + "\\tilemaps\\special_tileset.png", FileMode.Open);
fileStream = new FileStream(path + "\\" + Content.RootDirectory + "\\tilemaps\\tileset.png", FileMode.Open);
}
Texture2D specialTilesetTexture = Texture2D.FromStream(GraphicsDevice, specialFileStream);
specialFileStream.Dispose();
Texture2D tilesetTexture = Texture2D.FromStream(GraphicsDevice, fileStream);
fileStream.Dispose();
_tileWidth = _tileset.TileWidth;
_tileHeight = _tileset.TileHeight;
_tilesetTilesWide = _tileset.Columns;
_tilesetTilesHeight = _tileset.TileCount / _tileset.Columns;
for (var i = 0; i < _map.Layers[1].data.Length; i++)
{
int gid = _map.Layers[1].data[i];
if (gid != 0)
{
int tileFrame = gid - 1;
var tile = _map.GetTiledTile(_map.Tilesets[0], _tileset, gid);
int column = tileFrame % _tilesetTilesWide;
int row = (int)Math.Floor((double)tileFrame / (double)_tilesetTilesWide);
int x = (i % _map.Width) * _map.TileWidth;
int y = (i / _map.Width) * _map.TileHeight;
Rectangle tilesetRec = new(_tileWidth * column, _tileHeight * row, _tileWidth, _tileHeight);
GameObject newTile = new(tilesetTexture, tilesetRec, new Point((int)x, (int)y), new Point(1, 1), "tile", currentLevel);
}
}
// Now, we loop through the special tiles layer.
// For some reason, the gids in this tileset start at 1600
for (var i = 0; i < _map.Layers[2].data.Length; i++)
{
int gid = _map.Layers[2].data[i];
if (gid != 0)
{
int tileFrame = gid - 1601;
var tile = _map.GetTiledTile(_map.Tilesets[1], _tileset, gid - 1600);
int column = tileFrame % _tilesetTilesWide;
int row = (int)Math.Floor((double)tileFrame / (double)_tilesetTilesWide);
int x = (i % _map.Width) * _map.TileWidth;
int y = (i / _map.Width) * _map.TileHeight;
Rectangle tilesetRec = new(_tileWidth * column, _tileHeight * row, _tileWidth, _tileHeight);
switch (gid - 1600)
{
// Add special tile code here
case 1:
newTile.Remove();
currentLevel.PlayerSpawn = new Point(x, y);
break;
case 2:
newTile.type = "target";
newTile.AddComponent("TargetComponent");
newTile.SetComponentVariable("TargetComponent", "TargetType", TARGETTYPE.swing);
currentLevel.Targets.Add(newTile);
break;
case 3:
newTile.type = "target";
newTile.AddComponent("TargetComponent");
newTile.SetComponentVariable("TargetComponent", "TargetType", TARGETTYPE.pull);
currentLevel.Targets.Add(newTile);
break;
}
}
}
}*/
}