/
Game1.cs
329 lines (257 loc) · 11.8 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using Boatanator.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Boatanator
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
RenderTarget2D reflectionMap;
//Boat boat = new Boat();
Heli heli = new Heli();
Sky sky;
Boolean autoHover = true;
Camera freeCam = new Camera();
Camera camBoat = new Camera();
Camera cam;
Water water = new Water();
Islands island = new Islands();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] buffer = new byte[64 * 1024];
TimeSpan lastSend = TimeSpan.Zero;
IPEndPoint broadcast = new IPEndPoint(IPAddress.Broadcast, 12345);
Dictionary<string, Heli> helis = new Dictionary<string, Heli>();
IPAddress[] localAdresses;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
sky = new Sky();
cam = camBoat;
}
/// <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()
{
Window.AllowUserResizing = true;
Mouse.SetPosition(100, 100);
Mouse.SetPosition(100, 100);
socket.EnableBroadcast = true;
localAdresses = Dns.GetHostAddresses(Dns.GetHostName());
//socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
socket.Bind(new IPEndPoint(IPAddress.Any, 12345));
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
heli.Load(Content);
sky.Load(GraphicsDevice, Content);
water.Load(GraphicsDevice, Content);
island.Load(Content, GraphicsDevice);
font = Content.Load<SpriteFont>("font");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
var ks = Keyboard.GetState();
bool w = false, a = false, s = false, d = false, q = false, e = false, o = false, l = false;
if (autoHover)
{
if (ks.IsKeyDown(Keys.Space))
autoHover = false;
}
else
{
if (ks.IsKeyDown(Keys.Space))
autoHover = true;
}
if(cam == freeCam)
{
if (ks.IsKeyDown(Keys.Tab))
cam = camBoat;
if (ks.IsKeyDown(Keys.W))
cam.Position += cam.Direction;
if (ks.IsKeyDown(Keys.S))
cam.Position -= cam.Direction;
if (ks.IsKeyDown(Keys.D))
cam.Position += cam.Right;
if (ks.IsKeyDown(Keys.A))
cam.Position -= cam.Right;
}
else
{
if (ks.IsKeyDown(Keys.Tab))
cam = freeCam;
w = ks.IsKeyDown(Keys.W);
a = ks.IsKeyDown(Keys.A);
s = ks.IsKeyDown(Keys.S);
d = ks.IsKeyDown(Keys.D);
q = ks.IsKeyDown(Keys.Q);
e = ks.IsKeyDown(Keys.E);
o = ks.IsKeyDown(Keys.O);
l = ks.IsKeyDown(Keys.L);
}
heli.Step(w,s,a,d,q,e,o,l, autoHover);
camBoat.Position = heli.Position - new Vector3(heli.Direction.X, 0, heli.Direction.Z)*10+new Vector3(0,4,0);
camBoat.Target = heli.Position + new Vector3(0, 2, 0);
//camBoat.Direction = boat.Position;
var ms = Mouse.GetState();
var pos = ms.Position;
try { Mouse.SetPosition(100, 100); }
catch { }
var delta = pos - new Point(100, 100);
var m = Matrix.CreateRotationY(-delta.X/200f)*Matrix.CreateFromAxisAngle(cam.Right, -delta.Y / 200f);
cam.Direction = Vector3.Transform(cam.Direction, m);
foreach (var b in helis.Values)
b.Step(false, false, false, false, false, false, false, false, false);
if (socket.Available > 0)
{
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
int len = socket.ReceiveFrom(buffer, ref ep);
if (!Array.Exists(localAdresses, x => x.Equals( ((IPEndPoint)ep).Address)))
{
string key = ep.ToString();
Heli b;
if (!helis.TryGetValue(key, out b))
helis[key] = b = new Heli();
int p = 1 + b.Deserialize(buffer, 1);
}
//byte cmd
// n byte boat
// m byte control
//string name
// color
}
if (lastSend + TimeSpan.FromMilliseconds(1000) < gameTime.TotalGameTime)
{
int packetLength = 0;
buffer[0] = 0;
packetLength = 1 + heli.Serialize(buffer, 1);
buffer[packetLength++] = (byte)( w ? 1 : 0);
buffer[packetLength++] = (byte)(a ? 1 : 0);
buffer[packetLength++] = (byte)(s ? 1 : 0);
buffer[packetLength++] = (byte)(d ? 1 : 0);
int len = socket.SendTo(buffer, packetLength, SocketFlags.None, broadcast);
lastSend = gameTime.TotalGameTime;
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
if(Window.ClientBounds.Width != GraphicsDevice.Viewport.Width
||
Window.ClientBounds.Height != GraphicsDevice.Viewport.Height)
{
graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
graphics.ApplyChanges();
}
if(reflectionMap == null)
{
reflectionMap = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, false, SurfaceFormat.Color, DepthFormat.Depth24);
}
cam.AspectRatio = GraphicsDevice.Viewport.AspectRatio;
//GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SetRenderTarget(reflectionMap);
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Red, 1, 0);
var rCam = cam.CreateReflectionCam();
var depthState = new DepthStencilState();
depthState.DepthBufferEnable = false;
sky.Draw(rCam);
depthState.DepthBufferEnable = true;
heli.Draw(rCam);
foreach (var b in helis.Values)
b.Draw(rCam);
island.Draw(rCam);
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Red, 1, 0);
heli.Draw(cam);
foreach (var b in helis.Values)
b.Draw(cam);
island.Draw(cam);
sky.Draw(cam);
water.Draw(cam, gameTime, reflectionMap);
spriteBatch.Begin();
var pos = GraphicsDevice.Viewport.Project(heli.Position, cam.Projection, cam.View, Matrix.Identity);
spriteBatch.DrawString(font, heli.nickname, new Vector2(pos.X, pos.Y - 100), heli.color);
foreach (var b in helis)
{
var posb = GraphicsDevice.Viewport.Project(b.Value.Position, cam.Projection, cam.View, Matrix.Identity);
spriteBatch.DrawString(font, b.Value.nickname, new Vector2(posb.X, posb.Y - 100), b.Value.color);
spriteBatch.DrawString(font, b.Key, new Vector2(posb.X, posb.Y - 80), b.Value.color);
}
spriteBatch.DrawString(font, "Collective: " + heli.collective.ToString(), new Vector2(10, 1), heli.color);
spriteBatch.DrawString(font, "Roll: " + heli.roll.ToString(), new Vector2(10,25), heli.color);
spriteBatch.DrawString(font, "Pitch: " + heli.pitch.ToString(), new Vector2(10, 50), heli.color);
spriteBatch.DrawString(font, "Autohover: " + autoHover, new Vector2(10, 75), heli.color);
spriteBatch.DrawString(font, heli.Position.X.ToString(), new Vector2(10, 100), heli.color);
spriteBatch.DrawString(font, (Math.Cos(heli.pitch)*Math.Cos(heli.roll)).ToString(), new Vector2(10, 125), heli.color);
if (heli.Position.X > 500 || heli.Position.X < -500 || heli.Position.Y > 500 || heli.Position.Y < -500)
spriteBatch.DrawString(font, "Unauthorized territory! Get back to the islands now!", new Vector2(200, 300), Color.Red);
if (heli.vertices[5].Pos.Y >= 200)
{
spriteBatch.DrawString(font, "Maximum Altitude", new Vector2(400, 300), Color.Red);
foreach (var v in heli.vertices)
{
v.A = Vector3.Zero;
v.Pos.Y = 200;
}
}
if (heli.vertices[5].Pos.Y < 0)
{
spriteBatch.Draw(reflectionMap, new Rectangle(0, 0, 10000, 10000), Color.Black);
spriteBatch.DrawString(font, "Game Over", new Vector2(400, 300), Color.Red);
foreach (var v in heli.vertices)
{
v.Pos.Y = -100;
}
}
spriteBatch.End();
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
base.Draw(gameTime);
}
public float dir { get; set; }
}
}