-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
162 lines (131 loc) · 4.88 KB
/
Program.cs
File metadata and controls
162 lines (131 loc) · 4.88 KB
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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Networking;
using LiteNetLib;
using FontStashSharp;
class VindexGame : Game {
[STAThread]
static void Main(string[] args) {
if (args.Length > 0) {
Console.WriteLine($"Host address: {args[0]}");
Globals.hostAddress = args[0];
} else {
Console.WriteLine("No arguments were passed.");
}
using VindexGame g = new();
g.Run();
}
private VindexGame() {
GraphicsDeviceManager gdm = new GraphicsDeviceManager(this);
gdm.PreferredBackBufferWidth = 1280;
gdm.PreferredBackBufferHeight = 720;
gdm.IsFullScreen = false;
gdm.SynchronizeWithVerticalRetrace = true;
IsMouseVisible = true;
Window.IsBorderlessEXT = false;
Content.RootDirectory = "Content";
}
protected override void Initialize() {
base.Initialize();
}
SpriteBatch? spriteBatch;
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
GameAssets.Initialize(Content, GraphicsDevice, spriteBatch);
Globals.window = Window;
base.LoadContent();
}
protected override void UnloadContent() {
spriteBatch?.Dispose();
client?.Stop();
server?.Stop();
GameAssets.Unload();
base.UnloadContent();
}
bool selected = false;
Server? server;
Client? client;
protected override void Update(GameTime gameTime) {
Globals.Update();
Globals.gameTime = gameTime;
if (selected && client?.netHandle != null) {
server?.Update(gameTime);
client.Update();
}
if (!selected) ChooseClient_Server();
base.Update(gameTime);
}
public void ChooseClient_Server() {
// Server & Client
if (Globals.keyboardState.IsKeyDown(Keys.S)) {
selected = true;
server = new Server();
server.Initialize();
client = new Client();
}
// Client
else if (Globals.keyboardState.IsKeyDown(Keys.C)) {
selected = true;
client = new Client();
}
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(selected ? Color.DarkCyan : Color.CornflowerBlue);
Matrix pv_mat = Matrix.Identity;
if (client != null && client.player.camera != null) {
pv_mat = client.player.camera.getTransform();
}
if (client != null && client.netHandle.GetPeerHandle() != null) {
spriteBatch?.Begin();
DrawUI();
DrawDebugText(spriteBatch);
client.player.DrawUI(Vector2.Zero);
spriteBatch?.End();
}
if (spriteBatch != null){
spriteBatch.Begin(SpriteSortMode.Texture, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone, null, pv_mat);
DrawWorld();
spriteBatch.End();
}
base.Draw(gameTime);
}
public void DrawUI() {
float offset_x = 0f;
float offset_x_delta = GameAssets.font.TextBounds("0", Vector2.Zero).X2 - GameAssets.font.TextBounds("0", Vector2.Zero).X;
for (int i = 0; i < Server.MAX_PLAYERS; i++) {
if (client.scores[i] >= 0)
GameAssets.font.DrawText(GameAssets.spriteBatch, client.scores[i].ToString(), new Vector2(offset_x, Globals.window.ClientBounds.Height - 50), Server.PLAYER_COLORS[i]);
offset_x += offset_x_delta * 3f;
}
}
public void DrawWorld() {
for(int i = 0; i < Server.MAX_PLAYERS; i++) {
if (i == client?.player.id) {
client.player.Draw(client.player.color);
} else {
client?.players[i]?.DrawUI(client.players[i].position - new Vector2(0, 27));
client?.players[i]?.Draw(client.players[i].color);
}
}
for (int i = 0; i < client?.bulletCount; i++) {
if (client.bullets[i] != null) {
if (client.bullets[i].safe_id == client.player.id)
client.bullets[i].Draw(Color.White);
else
client.bullets[i].Draw(Color.Red);
}
}
}
public void DrawDebugText(SpriteBatch spriteBatch) {
Bounds bounds = GameAssets.font.TextBounds("Hosting server", Vector2.Zero);
float y_offset = 27.0f;
if (server != null) {
GameAssets.font.DrawText(spriteBatch, "Hosting server", new Vector2(0.0f, y_offset), Color.White);
y_offset += bounds.Y2 - bounds.Y;
}
GameAssets.font.DrawText(spriteBatch, "Peer: " + client.netHandle.GetPeerHandle().GetID(), new Vector2(0.0f, y_offset), Color.White);
}
}