forked from RazakLore/ClaimTheCastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
256 lines (206 loc) · 9.82 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace ClaimTheCastle
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D temp, back;
private Vector2 player;
private Cam2D cam;
int currentLevel = 1;
private List<Tilemap> arenas;
private TextureAtlas _textureAtlas;
private Player player1, player2, player3, player4;
private List<Bomb> bombs;
int bombcount;
Effect wizardRed, wizardGreen, wizardYellow;
KeyboardState kb, kbOld;
public static SpriteFont debug;
public static Random RNG = new Random();
float recordFPS = 60; bool isRecord = true;
public static GameConsole GConsole;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
_graphics.PreferredBackBufferHeight = 720;
_graphics.PreferredBackBufferWidth = 1080;
}
protected override void Initialize()
{
// Create a console (only causes the constructor to be called.)
GConsole = new GameConsole(this, new Point(256, 512), ConsoleLocation.BottomRight)
{
FadeAfterEvent = false,
ShowTimeStamps = true,
LogLogging = false,
};
// Register it as a component so that its own Initialise, Update and Draw will be called when appropriate.
Components.Add(GConsole);
base.Initialize();
cam.Position = new Vector2(46, 20);
player = new Vector2 (50, 50);
arenas = new List<Tilemap>();
bombs = new List<Bomb>();
bombcount = 0;
for (int i = 0; i < 2; i++)
{
arenas.Add(new Tilemap($"Content/Levels/level{i}", Vector2.Zero, 64, 16));
}
_textureAtlas = new TextureAtlas(Content.Load<Texture2D>("TextureAtlas"), 8, 6, 16, 16);
#region Load Players
player1 = new Player(new Vector2(2 * 16, 1 * 16), Content.Load<Texture2D>("Actors/WizardSpriteSheet"), 4, 8, true);
player2 = new Player(new Vector2(14 * 16, 1 * 16), Content.Load<Texture2D>("Actors/WizardSpriteSheet"), 4, 8, false);
player3 = new Player(new Vector2(2 * 16, 11 * 16), Content.Load<Texture2D>("Actors/WizardSpriteSheet"), 4, 8, false);
player4 = new Player(new Vector2(14 * 16, 11 * 16), Content.Load<Texture2D>("Actors/WizardSpriteSheet"), 4, 8, false);
#endregion
debug = Content.Load<SpriteFont>("Debug");
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
wizardRed = Content.Load<Effect>("WizardRed"); wizardGreen = Content.Load<Effect>("WizardGreen");
wizardYellow = Content.Load<Effect>("WizardYellow");
temp = Content.Load<Texture2D>("Wizard"); back = Content.Load<Texture2D>("TitleScreen");
}
protected override void Update(GameTime gameTime)
{
kb = Keyboard.GetState();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (kb.IsKeyDown(Keys.Space) && kbOld.IsKeyUp(Keys.Space) && player1.BombsPlaced <= player1.MaxBombs /*&& arenas[currentLevel].testing(new Vector2(player1.Position.X, player1.Position.Y))*/)
{
bombs.Add(new Bomb(new Point(player1.Position.ToPoint().X / 16, player1.Position.ToPoint().Y / 16), 3, 5, arenas[currentLevel], Content.Load<Texture2D>("TestBomb"), 1));
player1.BombsPlaced++;
}
#region AI Bombing
if (player2.isDeciding && player2.BombsPlaced <= player2.MaxBombs)
{
bombs.Add(new Bomb(new Point(player2.Position.ToPoint().X / 16, player2.Position.ToPoint().Y / 16), 3, 5, arenas[currentLevel], Content.Load<Texture2D>("TestBomb"), 2));
player2.BombsPlaced++;
}
if (player3.isDeciding && player3.BombsPlaced <= player3.MaxBombs)
{
bombs.Add(new Bomb(new Point(player3.Position.ToPoint().X / 16, player3.Position.ToPoint().Y / 16), 3, 5, arenas[currentLevel], Content.Load<Texture2D>("TestBomb"), 3));
player3.BombsPlaced++;
}
if (player4.isDeciding && player4.BombsPlaced <= player4.MaxBombs)
{
bombs.Add(new Bomb(new Point(player4.Position.ToPoint().X / 16, player4.Position.ToPoint().Y / 16), 3, 5, arenas[currentLevel], Content.Load<Texture2D>("TestBomb"), 4));
player4.BombsPlaced++;
}
#endregion
for (int i = 0; i < bombs.Count; i++)
{
bombs[i].Update(gameTime);
if (arenas[currentLevel].animateExplosion)
{
arenas[currentLevel].TileExplosionAnimate(bombs[i].x, bombs[i].y, gameTime);
}
if (bombs[i].TimeToDie == true && arenas[currentLevel].animateExplosion == false) //The bomb must persist while the explosion occurs, otherwise the location variables are lost
{
if (bombs[i].PlayerOwner == 1)
player1.BombsPlaced--;
else if (bombs[i].PlayerOwner == 2)
player2.BombsPlaced--;
else if (bombs[i].PlayerOwner == 3)
player3.BombsPlaced--;
else if (bombs[i].PlayerOwner == 4)
player4.BombsPlaced--;
bombs.RemoveAt(i);
bombcount -= 1;
}
}
player1.Update(gameTime, arenas[currentLevel], kb, kbOld);
player2.Update(gameTime, arenas[currentLevel], kb, kbOld);
player3.Update(gameTime, arenas[currentLevel], kb, kbOld);
player4.Update(gameTime, arenas[currentLevel], kb, kbOld);
base.Update(gameTime);
kbOld = kb;
}
protected override void Draw(GameTime gameTime)
{
float fps = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, cam.getCam()); // Original spritebatch
_spriteBatch.Draw(back, Vector2.Zero, Color.White);
arenas[currentLevel].Draw(_spriteBatch, _textureAtlas);
for (int i = 0; i < bombs.Count; i++)
{
bombs[i].Draw(_spriteBatch);
}
player1.Draw(_spriteBatch, gameTime, 16, 16);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, wizardRed, cam.getCam()); //Red wizard
player2.Draw(_spriteBatch, gameTime, 16, 16);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, wizardGreen, cam.getCam()); //Green Wizard
player3.Draw(_spriteBatch, gameTime, 16, 16);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, wizardYellow, cam.getCam()); //Yellow wizard
player4.Draw(_spriteBatch, gameTime, 16, 16);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, cam.getCam()); //Debug
//_spriteBatch.DrawString(debug, bombs.Count.ToString(), Vector2.Zero, Color.Orange);
//_spriteBatch.DrawString(debug, player1.Position.ToString(), player1.Position, Color.Orange);
_spriteBatch.DrawString(debug, fps.ToString() + "Frames when no explode", Vector2.Zero, Color.Black);
if (fps < 58 && isRecord && gameTime.TotalGameTime.TotalSeconds > 4 && bombs.Count > 2)
{
recordFPS = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;
//isRecord = false;
}
_spriteBatch.DrawString(debug, recordFPS.ToString() + "Frames when explode", new Vector2(20, 20), Color.Black);
_spriteBatch.End();
base.Draw(gameTime);
}
struct Cam2D
{
public Vector2 Position; // Camera's Position
public Matrix getCam() // Function to get the camera's values
{
Matrix temp;
temp = Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, 0));
temp *= Matrix.CreateScale(3); // Zooms in by 2x
return temp;
}
}
}
}
//Checklist
/*
* Player Functionality
* Tilemap
* Cauldron Spawning
* Destructible Tiles - Indestructible Tiles
* Tile shadow over floor from indestructibles
* Throwable Cauldrons
* Powerups
* Title Screen
* Menu
* Game Over Screen
*
*
*/
// Notes
/*
* Cauldrons start with 3 tile max reach
* Cauldrons go in 4 directions only
* Cauldrons have 3 second timer
* 2 Cauldrons max to start with
* Cauldrons immediately explode if hit by explosion
* Potion Powerup will increase bomb reach
* Potion Cauldron + will increase max cauldron cap
* Potion Feather will increase player speed
* Start with 3 shields
* Explosions remove a shield and teleport player to a safe location
* Brick walls should explode when bombed, but block the blast
*
*
* https://community.monogame.net/t/how-to-randomly-generate-maps/14228/7
*/